- Timestamp:
- 06/04/13 16:15:35 (11 years ago)
- Location:
- SRUClient/trunk/src/main/java/eu/clarin/sru/client
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRecordData.java
r2956 r2986 258 258 } // class DatabaseInfo 259 259 260 public static final class IndexInfo { 261 public static class Set { 262 private final String identifier; 263 private final String name; 264 private final List<LocalizedString> title; 265 266 267 Set(String identifier, String name, List<LocalizedString> title) { 268 this.identifier = identifier; 269 this.name = name; 270 if ((title != null) && !title.isEmpty()) { 271 this.title = Collections.unmodifiableList(title); 272 } else { 273 this.title = null; 274 } 275 } 276 277 278 public String getIdentifier() { 279 return identifier; 280 } 281 282 283 public String getName() { 284 return name; 285 } 286 287 288 public List<LocalizedString> getTitle() { 289 return title; 290 } 291 } // class IndexInfo.Set 292 293 public static class Index { 294 public static class Map { 295 private final boolean primary; 296 private final String set; 297 private final String name; 298 299 300 Map(boolean primary, String set, String name) { 301 this.primary = primary; 302 this.set = set; 303 this.name = name; 304 } 305 306 307 public boolean isPrimary() { 308 return primary; 309 } 310 311 312 public String getSet() { 313 return set; 314 } 315 316 317 public String getName() { 318 return name; 319 } 320 } // class IndexInfo.Index.Map 321 322 private final String id; 323 private final List<LocalizedString> title; 324 private final boolean can_search; 325 private final boolean can_scan; 326 private final boolean can_sort; 327 private final List<Index.Map> maps; 328 329 330 Index(String id, List<LocalizedString> title, boolean can_search, 331 boolean can_scan, boolean can_sort, List<Map> maps) { 332 this.id = id; 333 if ((title != null) && !title.isEmpty()) { 334 this.title = Collections.unmodifiableList(title); 335 } else { 336 this.title = null; 337 } 338 this.can_search = can_search; 339 this.can_scan = can_scan; 340 this.can_sort = can_sort; 341 this.maps = maps; 342 } 343 344 345 public String getId() { 346 return id; 347 } 348 349 350 public List<LocalizedString> getTitle() { 351 return title; 352 } 353 354 355 public boolean canSearch() { 356 return can_search; 357 } 358 359 360 public boolean canScan() { 361 return can_scan; 362 } 363 364 365 public boolean canSort() { 366 return can_sort; 367 } 368 369 370 public List<Index.Map> getMaps() { 371 return maps; 372 } 373 374 } // class Index 375 376 private final List<IndexInfo.Set> sets; 377 private final List<IndexInfo.Index> indexes; 378 379 380 IndexInfo(List<IndexInfo.Set> sets, List<IndexInfo.Index> indexes) { 381 if ((sets != null) && !sets.isEmpty()) { 382 this.sets = Collections.unmodifiableList(sets); 383 } else { 384 this.sets = null; 385 } 386 if ((indexes != null) && !indexes.isEmpty()) { 387 this.indexes = Collections.unmodifiableList(indexes); 388 } else { 389 this.indexes = null; 390 } 391 } 392 393 394 public List<IndexInfo.Set> getSets() { 395 return sets; 396 } 397 398 399 public List<IndexInfo.Index> getIndexes() { 400 return indexes; 401 } 402 } // class IndexInfo 403 260 404 private final ServerInfo serverInfo; 261 405 private final DatabaseInfo databaseInfo; 262 263 264 SRUExplainRecordData(ServerInfo serverInfo, DatabaseInfo databaseInfo) { 406 private final IndexInfo indexInfo; 407 408 409 SRUExplainRecordData(ServerInfo serverInfo, DatabaseInfo databaseInfo, 410 IndexInfo indexInfo) { 265 411 this.serverInfo = serverInfo; 266 412 this.databaseInfo = databaseInfo; 413 this.indexInfo = indexInfo; 267 414 } 268 415 … … 289 436 } 290 437 438 439 public IndexInfo getIndexInfo() { 440 return indexInfo; 441 } 442 291 443 } // class SRUExplainRecordData -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRecordDataParser.java
r2956 r2986 30 30 31 31 import eu.clarin.sru.client.SRUExplainRecordData.DatabaseInfo; 32 import eu.clarin.sru.client.SRUExplainRecordData.IndexInfo; 32 33 import eu.clarin.sru.client.SRUExplainRecordData.LocalizedString; 33 34 import eu.clarin.sru.client.SRUExplainRecordData.ServerInfo; … … 49 50 private static final String TRANSPORT_HTTP = "http"; 50 51 private static final String TRANSPORT_HTTPS = "https"; 51 private static final String PRIMARY_TRUE= "true";52 private static final String STRING_TRUE = "true"; 52 53 private static final String PRIMARY_FALSE = "false"; 53 54 private static final Logger logger = … … 70 71 // explain 71 72 if (XmlStreamReaderUtils.peekStart(reader, ZEEREX_NS, "explain")) { 72 if (strict) {73 logger.warn("namespace '{}' is not defined by ZeeRex, " +74 "enabling quirks mode (consider using namespace '{}'" +75 " which is defined)", ZEEREX_NS_QUIRK, ZEEREX_NS);76 } else {77 logger.info("namespace '{}' is not defined by ZeeRex, " +78 "enabling quirks mode (consider using namespace '{}'" +79 " which is defined)", ZEEREX_NS_QUIRK, ZEEREX_NS);80 }81 73 return parseExplain(reader, version, strict, ZEEREX_NS); 82 74 } else if (XmlStreamReaderUtils.peekStart(reader, … … 132 124 133 125 // explain/indexInfo (optional) 134 while (XmlStreamReaderUtils.readStart(reader, ns, "indexInfo", false)) { 135 parseIndexInfo(reader, strict, ns); 126 IndexInfo indexInfo = null; 127 if (XmlStreamReaderUtils.readStart(reader, ns, "indexInfo", false)) { 128 indexInfo = parseIndexInfo(reader, strict, ns); 136 129 XmlStreamReaderUtils.readEnd(reader, ns, "indexInfo"); 137 } // while130 } // if 138 131 139 132 // explain/recordInfo or explain/schemaInfo … … 161 154 162 155 XmlStreamReaderUtils.readEnd(reader, ns, "explain", true); 163 return new SRUExplainRecordData(serverInfo, databaseInfo );156 return new SRUExplainRecordData(serverInfo, databaseInfo, indexInfo); 164 157 } 165 158 … … 252 245 */ 253 246 254 // make sure to remove any whitespace255 XmlStreamReaderUtils.consumeWhitespace(reader);256 257 247 List<LocalizedString> title = null; 258 248 List<LocalizedString> description = null; … … 268 258 269 259 byte mode = 0; /* 1 = title, 2 = description, 3 = others */ 270 while (reader.isStartElement()) { 260 for (;;) { 261 /* 262 * make sure to remove any whitespace and break, if we do not point 263 * to a start tag 264 */ 265 XmlStreamReaderUtils.consumeWhitespace(reader); 266 if (!reader.isStartElement()) { 267 break; 268 } 269 271 270 if (XmlStreamReaderUtils.peekStart(reader, ns, "title")) { 272 271 if ((mode != 0) && (mode != 1)) { … … 292 291 } 293 292 } 294 LocalizedString s = parseStringI18N(reader, strict, ns, "title"); 293 LocalizedString s = 294 parseStringI18N(reader, strict, ns, "title", true); 295 295 if (title == null) { 296 296 title = new ArrayList<LocalizedString>(); … … 322 322 } 323 323 LocalizedString s = 324 parseStringI18N(reader, strict, ns, "description" );324 parseStringI18N(reader, strict, ns, "description", true); 325 325 if (description == null) { 326 326 description = new ArrayList<LocalizedString>(); … … 330 330 } else if (XmlStreamReaderUtils.peekStart(reader, ns, "author")) { 331 331 LocalizedString s = 332 parseStringI18N(reader, strict, ns, "author" );332 parseStringI18N(reader, strict, ns, "author", true); 333 333 if (author == null) { 334 334 author = new ArrayList<LocalizedString>(); … … 338 338 } else if (XmlStreamReaderUtils.peekStart(reader, ns, "contact")) { 339 339 LocalizedString s = 340 parseStringI18N(reader, strict, ns, "contact" );340 parseStringI18N(reader, strict, ns, "contact", true); 341 341 if (contact == null) { 342 342 contact = new ArrayList<LocalizedString>(); … … 346 346 } else if (XmlStreamReaderUtils.peekStart(reader, ns, "extent")) { 347 347 LocalizedString s = 348 parseStringI18N(reader, strict, ns, "extent" );348 parseStringI18N(reader, strict, ns, "extent", true); 349 349 if (extent == null) { 350 350 extent = new ArrayList<LocalizedString>(); … … 354 354 } else if (XmlStreamReaderUtils.peekStart(reader, ns, "history")) { 355 355 LocalizedString s = 356 parseStringI18N(reader, strict, ns, "history" );356 parseStringI18N(reader, strict, ns, "history", true); 357 357 if (history == null) { 358 358 history = new ArrayList<LocalizedString>(); … … 363 363 ns, "restrictions")) { 364 364 LocalizedString s = 365 parseStringI18N(reader, strict, ns, "restrictions" );365 parseStringI18N(reader, strict, ns, "restrictions", true); 366 366 if (restrictions == null) { 367 367 restrictions = new ArrayList<LocalizedString>(); … … 371 371 } else if (XmlStreamReaderUtils.peekStart(reader, ns, "subjects")) { 372 372 LocalizedString s = 373 parseStringI18N(reader, strict, ns, "subjects" );373 parseStringI18N(reader, strict, ns, "subjects", true); 374 374 if (subjects == null) { 375 375 subjects = new ArrayList<LocalizedString>(); … … 379 379 } else if (XmlStreamReaderUtils.peekStart(reader, ns, "links")) { 380 380 LocalizedString s = 381 parseStringI18N(reader, strict, ns, "links" );381 parseStringI18N(reader, strict, ns, "links", true); 382 382 if (links == null) { 383 383 links = new ArrayList<LocalizedString>(); … … 388 388 ns, "implementation")) { 389 389 LocalizedString s = 390 parseStringI18N(reader, strict, ns, "implementation" );390 parseStringI18N(reader, strict, ns, "implementation", true); 391 391 if (implementation == null) { 392 392 implementation = new ArrayList<LocalizedString>(); … … 401 401 break; 402 402 } 403 404 // make sure to remove any whitespace 405 XmlStreamReaderUtils.consumeWhitespace(reader); 406 } // while 403 } // for 407 404 408 405 return new DatabaseInfo(title, description, author, contact, … … 412 409 413 410 414 private static ObjectparseIndexInfo(XMLStreamReader reader,411 private static IndexInfo parseIndexInfo(XMLStreamReader reader, 415 412 boolean strict, String ns) throws XMLStreamException, 416 413 SRUClientException { … … 418 415 * indexInfo ((set | index | sortKeywords)+) 419 416 */ 420 logger.debug("indexInfo");421 417 boolean found = false; 418 List<IndexInfo.Set> sets = null; 419 List<IndexInfo.Index> indexes = null; 422 420 for (;;) { 423 421 if (XmlStreamReaderUtils.readStart(reader, ns, "set", false, true)) { 424 logger.debug("-> set"); 425 // FIXME: read attributes 422 final String identifier = 423 XmlStreamReaderUtils.readAttributeValue(reader, 424 null, "identifier"); 425 final String name = 426 XmlStreamReaderUtils.readAttributeValue(reader, 427 null, "name"); 426 428 XmlStreamReaderUtils.consumeStart(reader); 427 429 428 XmlStreamReaderUtils.readEnd(reader, ns, "set", true); 430 // indexInfo/set/title 431 List<LocalizedString> titles = null; 432 for (;;) { 433 LocalizedString title = 434 parseStringI18N(reader, strict, ns, "title", false); 435 if (title == null) { 436 break; 437 } 438 if (titles == null) { 439 titles = new ArrayList<LocalizedString>(); 440 } 441 titles.add(title); 442 } // for 443 XmlStreamReaderUtils.readEnd(reader, ns, "set"); 444 445 if (sets == null) { 446 sets = new ArrayList<IndexInfo.Set>(); 447 } 448 sets.add(new IndexInfo.Set(identifier, name, titles)); 429 449 found = true; 430 } else if (XmlStreamReaderUtils.readStart(reader, ns, "index", false, true)) { 431 logger.debug("-> index"); 432 // FIXME: read attributes 450 } else if (XmlStreamReaderUtils.readStart(reader, ns, 451 "index", false, true)) { 452 final String id = 453 XmlStreamReaderUtils.readAttributeValue(reader, null, "id"); 454 final boolean can_search = 455 parseBooleanAttribute(reader, strict,null, "search"); 456 final boolean can_scan = 457 parseBooleanAttribute(reader, strict,null, "search"); 458 final boolean can_sort = 459 parseBooleanAttribute(reader, strict,null, "search"); 433 460 XmlStreamReaderUtils.consumeStart(reader); 434 XmlStreamReaderUtils.readEnd(reader, ns, "index", true); 461 462 // indexInfo/index/title 463 List<LocalizedString> titles = null; 464 for (;;) { 465 LocalizedString title = 466 parseStringI18N(reader, strict, ns, "title", false); 467 if (title == null) { 468 break; 469 } 470 if (titles == null) { 471 titles = new ArrayList<LocalizedString>(); 472 } 473 titles.add(title); 474 } // for 475 476 // indexInfo/index/map ((attr+)|name) 477 List<IndexInfo.Index.Map> maps = null; 478 boolean first_map = true; 479 while (XmlStreamReaderUtils.readStart(reader, ns, "map", 480 first_map, true)) { 481 final boolean primary = parseBooleanAttribute(reader, 482 strict, null, "primary"); 483 XmlStreamReaderUtils.consumeStart(reader); 484 485 if (XmlStreamReaderUtils.peekStart(reader, ns, "attr")) { 486 /* 487 * skip "attr" elements, because they are not supported 488 */ 489 while (XmlStreamReaderUtils.readStart(reader, 490 ns, "attr", false)) { 491 logger.debug("skipping 'attr'"); 492 XmlStreamReaderUtils.readEnd(reader, ns, "attr"); 493 } // while (attr) 494 } else if (XmlStreamReaderUtils.peekStart(reader, 495 ns, "name")) { 496 XmlStreamReaderUtils.readStart(reader, ns, "name", true, true); 497 final String set = 498 XmlStreamReaderUtils.readAttributeValue(reader, 499 null, "set"); 500 // FIXME: check 'set' 501 XmlStreamReaderUtils.consumeStart(reader); 502 final String name = 503 XmlStreamReaderUtils.readString(reader, false); 504 // FIXME: check 'name' 505 XmlStreamReaderUtils.readEnd(reader, ns, "name"); 506 if ((set != null) && (name != null)) { 507 if (maps == null) { 508 maps = new ArrayList<IndexInfo.Index.Map>(); 509 } 510 maps.add(new IndexInfo.Index.Map(primary, set, name)); 511 } 512 } else { 513 // FIXME: error message 514 throw new XMLStreamException( 515 "expected 'attr' or 'name'", 516 reader.getLocation()); 517 } 518 519 XmlStreamReaderUtils.readEnd(reader, ns, "map"); 520 first_map = false; 521 } // while (map) 522 523 // indexInfo/index/configInfo (optional) 524 if (XmlStreamReaderUtils.readStart(reader, ns, "configInfo", false)) { 525 logger.debug("skipping 'configInfo' within 'indexInfo/index'"); 526 XmlStreamReaderUtils.readEnd(reader, ns, "configInfo", true); 527 } 528 529 XmlStreamReaderUtils.readEnd(reader, ns, "index"); 530 if (indexes == null) { 531 indexes = new ArrayList<IndexInfo.Index>(); 532 } 533 indexes.add(new IndexInfo.Index(id, titles, can_search, 534 can_scan, can_sort, maps)); 435 535 found = true; 436 536 } else if (XmlStreamReaderUtils.readStart(reader, ns, "sortKeyword", false)) { 437 logger.debug(" -> sortKeywords");537 logger.debug("skipping 'sortKeywords'"); 438 538 XmlStreamReaderUtils.readEnd(reader, ns, "sortKeyword", true); 439 539 found = true; … … 451 551 reader.getLocation()); 452 552 } 453 return null; 553 return ((sets != null) || (indexes != null)) 554 ? new IndexInfo(sets, indexes) 555 : null; 454 556 } 455 557 … … 504 606 505 607 private static LocalizedString parseStringI18N(XMLStreamReader reader, 506 boolean strict, String ns, String localName )608 boolean strict, String ns, String localName, boolean required) 507 609 throws XMLStreamException { 508 XmlStreamReaderUtils.readStart(reader, ns, localName, true, true); 610 if (!XmlStreamReaderUtils.readStart(reader, ns, localName, required, 611 true)) { 612 return null; 613 } 509 614 final String lang = XmlStreamReaderUtils.readAttributeValue(reader, 510 615 null, "lang", false); … … 515 620 } 516 621 } 622 final boolean primary = 623 parseBooleanAttribute(reader, strict, null, "primary"); 624 XmlStreamReaderUtils.consumeStart(reader); 625 String value = XmlStreamReaderUtils.readString(reader, false); 626 XmlStreamReaderUtils.readEnd(reader, ns, localName); 627 return new LocalizedString(value, lang, primary); 628 } 629 630 631 private static boolean parseBooleanAttribute(XMLStreamReader reader, 632 boolean strict, String ns, String localName) 633 throws XMLStreamException { 634 boolean result = false; 517 635 final String s = XmlStreamReaderUtils.readAttributeValue(reader, 518 null, "primary", false); 519 boolean primary = false; 636 ns, localName, false); 520 637 if (s != null) { 521 if ( PRIMARY_TRUE.equalsIgnoreCase(s)) {522 if (! PRIMARY_TRUE.equals(s)) {638 if (STRING_TRUE.equalsIgnoreCase(s)) { 639 if (!STRING_TRUE.equals(s)) { 523 640 if (strict) { 524 641 throw new XMLStreamException("capitalization"); … … 528 645 } 529 646 } 530 primary= true;647 result = true; 531 648 } else if (PRIMARY_FALSE.equalsIgnoreCase(s)) { 532 649 if (!PRIMARY_FALSE.equals(s)) { … … 538 655 } 539 656 } 540 primary= false;657 result = false; 541 658 } else { 542 659 // FIXME: message 543 throw new XMLStreamException("value of @primary invalid"); 544 } 545 } 546 XmlStreamReaderUtils.consumeStart(reader); 547 String value = XmlStreamReaderUtils.readString(reader, false); 548 XmlStreamReaderUtils.readEnd(reader, ns, localName); 549 logger.debug("databaseInfo/{}='{}' (primary={}, lang={})", localName, 550 value, primary, lang); 551 return new LocalizedString(value, lang, primary); 660 throw new XMLStreamException("value of @" + localName + " invalid"); 661 } 662 } 663 return result; 552 664 } 553 665
Note: See TracChangeset
for help on using the changeset viewer.