Changeset 2021
- Timestamp:
- 07/10/12 13:06:52 (12 years ago)
- Location:
- SRUClient/trunk/src/main/java/eu/clarin/sru/client
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClient.java
r1985 r2021 319 319 version, this.defaultVersion); 320 320 321 // check if a fatal error occurred 321 // explainResponse/record 322 reader.readStart(SRU_NS, "record", true); 323 324 String schema = reader.readContent(SRU_NS, "recordSchema", true); 325 326 SRURecordPacking packing = parseRecordPacking(reader); 327 328 logger.debug("schema = {}, packing = {}", schema, packing); 329 330 // explainResponse/record/recordData 331 reader.readStart(SRU_NS, "recordData", true); 332 reader.readEnd(SRU_NS, "recordData", true); 333 334 // explainResponse/record/recordPosition 335 if (reader.readStart(SRU_NS, "recordPosition", false)) { 336 reader.readEnd(SRU_NS, "recordPosition", true); 337 } 338 339 // explainResponse/record/extraRecordData 340 if (reader.readStart(SRU_NS, "extraRecordData", false)) { 341 reader.readEnd(SRU_NS, "extraRecordData", true); 342 } 343 344 reader.readEnd(SRU_NS, "record"); 345 346 // explainResponse/echoedExplainRequest 347 if (reader.readStart(SRU_NS, "echoedExplainRequest", false)) { 348 reader.readEnd(SRU_NS, "echoedExplainRequest", true); 349 } 350 351 // explainResponse/diagnostics 322 352 final List<SRUDiagnostic> diagnostics = parseDiagnostics(reader); 323 353 if (diagnostics != null) { 324 handler.onFatalError(diagnostics); 325 } else { 326 // explainResponse/record 327 reader.readStart(SRU_NS, "record", true); 328 329 String schema = 330 reader.readContent(SRU_NS, "recordSchema", true); 331 332 SRURecordPacking packing = parseRecordPacking(reader); 333 334 logger.debug("schema = {}, packing = {}", schema, packing); 335 336 // explainResponse/record/recordData 337 reader.readStart(SRU_NS, "recordData", true); 338 reader.readEnd(SRU_NS, "recordData", true); 339 340 // explainResponse/record/recordPosition 341 if (reader.readStart(SRU_NS, "recordPosition", false)) { 342 reader.readEnd(SRU_NS, "recordPosition", true); 343 } 344 345 // explainResponse/record/extraRecordData 346 if (reader.readStart(SRU_NS, "extraRecordData", false)) { 347 reader.readEnd(SRU_NS, "extraRecordData", true); 348 } 349 350 reader.readEnd(SRU_NS, "record"); 351 352 // explainResponse/echoedExplainRequest 353 if (reader.readStart(SRU_NS, "echoedExplainRequest", false)) { 354 reader.readEnd(SRU_NS, "echoedExplainRequest", true); 355 } 356 357 // explainResponse/diagnostics 358 parseDiagnostics(reader); 359 360 // explainResponse/extraResponseData 361 if (reader.readStart(SRU_NS, "extraResponseData", false)) { 362 reader.consumeWhitespace(); 363 proxy.reset(reader); 364 try { 365 handler.onExtraResponseData(proxy); 366 } catch (XMLStreamException e) { 367 throw new SRUClientException("handler triggered " + 368 "error while parsing 'extraResponseData'", e); 369 } 370 reader.consumeWhitespace(); 371 reader.readEnd(SRU_NS, "extraResponseData", true); 372 } 373 374 reader.readEnd(SRU_NS, "explainResponse"); 375 } 354 handler.onDiagnostics(diagnostics); 355 } 356 357 // explainResponse/extraResponseData 358 if (reader.readStart(SRU_NS, "extraResponseData", false)) { 359 reader.consumeWhitespace(); 360 proxy.reset(reader); 361 try { 362 handler.onExtraResponseData(proxy); 363 } catch (XMLStreamException e) { 364 throw new SRUClientException("handler triggered " 365 + "error while parsing 'extraResponseData'", e); 366 } 367 reader.consumeWhitespace(); 368 reader.readEnd(SRU_NS, "extraResponseData", true); 369 } 370 371 reader.readEnd(SRU_NS, "explainResponse"); 376 372 } catch (XMLStreamException e) { 377 373 throw new SRUClientException(e.getMessage(), e); … … 393 389 version, this.defaultVersion); 394 390 395 // check if a fatal error occurred 396 final List<SRUDiagnostic> diagnostics = parseDiagnostics(reader); 397 if (diagnostics != null) { 398 handler.onFatalError(diagnostics); 399 } else { 400 // scanResponse/terms 401 if (reader.readStart(SRU_NS, "terms", false)) { 402 boolean first = true; 403 while (reader.readStart(SRU_NS, "term", first)) { 404 if (first) { 405 first = false; 406 handler.onStartTerms(); 391 // scanResponse/terms 392 if (reader.readStart(SRU_NS, "terms", false)) { 393 boolean first = true; 394 while (reader.readStart(SRU_NS, "term", first)) { 395 if (first) { 396 first = false; 397 handler.onStartTerms(); 398 } 399 400 // scanResponse/terms/value 401 String value = reader.readContent(SRU_NS, "value", true); 402 403 // scanResponse/terms/numberOfRecords 404 int numberOfRecords = reader.readContent(SRU_NS, 405 "numberOfRecords", false, -1); 406 407 // scanResponse/terms/displayTerm 408 String displayTerm = reader.readContent(SRU_NS, 409 "displayTerm", false); 410 411 // scanResponse/terms/whereInList 412 String s = reader.readContent(SRU_NS, "whereInList", false); 413 WhereInList whereInList = null; 414 if (s != null) { 415 if ("first".equals(s)) { 416 whereInList = WhereInList.FIRST; 417 } else if ("last".equals(s)) { 418 whereInList = WhereInList.LAST; 419 } else if ("only".equals(s)) { 420 whereInList = WhereInList.ONLY; 421 } else if ("inner".equals(s)) { 422 whereInList = WhereInList.INNER; 423 } else { 424 throw new SRUClientException( 425 "invalid value for 'whereInList': " + s); 407 426 } 408 409 // scanResponse/terms/value 410 String value = reader.readContent(SRU_NS, 411 "value", true); 412 413 // scanResponse/terms/numberOfRecords 414 int numberOfRecords = reader.readContent(SRU_NS, 415 "numberOfRecords", false, -1); 416 417 // scanResponse/terms/displayTerm 418 String displayTerm = reader.readContent(SRU_NS, 419 "displayTerm", false); 420 421 // scanResponse/terms/whereInList 422 String s = reader.readContent(SRU_NS, 423 "whereInList", false); 424 WhereInList whereInList = null; 425 if (s != null) { 426 if ("first".equals(s)) { 427 whereInList = WhereInList.FIRST; 428 } else if ("last".equals(s)) { 429 whereInList = WhereInList.LAST; 430 } else if ("only".equals(s)) { 431 whereInList = WhereInList.ONLY; 432 } else if ("inner".equals(s)) { 433 whereInList = WhereInList.INNER; 434 } else { 435 throw new SRUClientException( 436 "invalid value for 'whereInList': " + s); 437 } 427 } 428 logger.debug("value = {}, numberOfRecords = {}, " 429 + "displayTerm = {}, whereInList = {}", 430 new Object[] { value, numberOfRecords, displayTerm, 431 whereInList }); 432 handler.onTerm(value, numberOfRecords, 433 displayTerm, whereInList); 434 435 // scanResponse/terms/extraTermData 436 if (reader.readStart(SRU_NS, "extraTermData", first)) { 437 reader.consumeWhitespace(); 438 proxy.reset(reader); 439 try { 440 handler.onExtraTermData(value, proxy); 441 } catch (XMLStreamException e) { 442 throw new SRUClientException("handler " 443 + "triggered error while parsing " 444 + "'extraTermData'", e); 438 445 } 439 logger.debug("value = {}, numberOfRecords = {}, " + 440 "displayTerm = {}, whereInList = {}", 441 new Object[] { value, numberOfRecords, 442 displayTerm, whereInList }); 443 handler.onTerm(value, numberOfRecords, displayTerm, whereInList); 444 445 // scanResponse/terms/extraTermData 446 if (reader.readStart(SRU_NS, "extraTermData", first)) { 447 reader.consumeWhitespace(); 448 proxy.reset(reader); 449 try { 450 handler.onExtraTermData(value, proxy); 451 } catch (XMLStreamException e) { 452 throw new SRUClientException("handler " + 453 "triggered error while parsing " + 454 "'extraTermData'", e); 455 } 456 reader.consumeWhitespace(); 457 reader.readEnd(SRU_NS, "extraTermData", true); 458 } 459 reader.readEnd(SRU_NS, "term", true); 460 461 } // while 462 reader.readEnd(SRU_NS, "terms"); 463 handler.onFinishTerms(); 464 } 446 reader.consumeWhitespace(); 447 reader.readEnd(SRU_NS, "extraTermData", true); 448 } 449 reader.readEnd(SRU_NS, "term", true); 450 451 } // while 452 reader.readEnd(SRU_NS, "terms"); 453 handler.onFinishTerms(); 465 454 466 455 // scanResponse/echoedScanRequest … … 470 459 471 460 // scanResponse/diagnostics 472 parseDiagnostics(reader); 461 final List<SRUDiagnostic> diagnostics = 462 parseDiagnostics(reader); 463 if (diagnostics != null) { 464 handler.onDiagnostics(diagnostics); 465 } 473 466 474 467 // scanResponse/extraResponseData … … 507 500 version, this.defaultVersion); 508 501 509 // check if a fatal error occurred 510 final List<SRUDiagnostic> diagnostics = parseDiagnostics(reader); 511 if (diagnostics != null) { 512 handler.onFatalError(diagnostics); 513 } else { 514 515 // searchRetrieveResponse/numberOfRecords 516 int numberOfRecords = reader.readContent(SRU_NS, 517 "numberOfRecords", true, -1); 518 519 // searchRetrieveResponse/resultSetId 520 int resultSetId = reader.readContent(SRU_NS, "resultSetId", 521 false, -1); 522 523 // searchRetrieveResponse/resultSetIdleTime 524 int resultSetIdleTime = reader.readContent(SRU_NS, 525 "resultSetIdleTime", false, -1); 526 527 logger.debug("numberOfRecords = {}, resultSetId = {}, " + 528 "resultSetIdleTime = {}", 529 new Object[] { numberOfRecords, resultSetId, 530 resultSetIdleTime }); 531 532 // searchRetrieveResponse/results 533 if (numberOfRecords > 0) { 534 reader.readStart(SRU_NS, "records", true); 535 536 // searchRetrieveResponse/records/record 537 boolean first = true; 538 while (reader.readStart(SRU_NS, "record", first)) { 539 if (first) { 540 first = false; 541 handler.onStartRecords(numberOfRecords, 542 resultSetId, resultSetIdleTime); 502 // searchRetrieveResponse/numberOfRecords 503 int numberOfRecords = reader.readContent(SRU_NS, 504 "numberOfRecords", true, -1); 505 506 // searchRetrieveResponse/resultSetId 507 int resultSetId = reader.readContent(SRU_NS, 508 "resultSetId", false, -1); 509 510 // searchRetrieveResponse/resultSetIdleTime 511 int resultSetIdleTime = reader.readContent(SRU_NS, 512 "resultSetIdleTime", false, -1); 513 514 logger.debug("numberOfRecords = {}, resultSetId = {}, " 515 + "resultSetIdleTime = {}", new Object[] { 516 numberOfRecords, 517 resultSetId, 518 resultSetIdleTime 519 }); 520 521 // searchRetrieveResponse/results 522 if (numberOfRecords > 0) { 523 reader.readStart(SRU_NS, "records", true); 524 525 // searchRetrieveResponse/records/record 526 boolean first = true; 527 while (reader.readStart(SRU_NS, "record", first)) { 528 if (first) { 529 first = false; 530 handler.onStartRecords(numberOfRecords, 531 resultSetId, resultSetIdleTime); 532 } 533 534 String schema = reader.readContent(SRU_NS, 535 "recordSchema", true); 536 537 SRURecordPacking packing = parseRecordPacking(reader); 538 539 logger.debug("schema = {}, packing = {}, requested packing = {}", 540 new Object[] { schema, packing, 541 request.getRecordPacking() }); 542 543 if ((request.getRecordPacking() != null) && 544 (packing != request.getRecordPacking())) { 545 logger.warn("requested '{}' record packing, but server responded with '{}' record packing", 546 request.getRecordPacking().toProtocolString(), 547 packing.toProtocolString()); 548 // XXX: only throw if client is pedantic? 549 throw new SRUClientException( 550 "requested '" + 551 request.getRecordPacking() 552 .toProtocolString() + 553 "' record packing, but server responded with '" + 554 packing.toProtocolString() + 555 "' record packing"); 556 } 557 558 // searchRetrieveResponse/record/recordData 559 reader.readStart(SRU_NS, "recordData", true); 560 reader.consumeWhitespace(); 561 562 SRURecordData recordData = null; 563 SRUDiagnostic surrogate = null; 564 SRUXMLStreamReader recordReader = null; 565 566 if (packing == SRURecordPacking.STRING) { 567 /* 568 * read content into temporary buffer and then use a new 569 * XML reader to parse record data 570 */ 571 final String data = reader.readString(true); 572 InputStream in = new ByteArrayInputStream( 573 data.getBytes()); 574 // FIXME: namespace context? 575 recordReader = createReader(in, false); 576 } else { 577 recordReader = reader; 578 } 579 580 if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) { 581 surrogate = parseDiagnostic(recordReader, true); 582 } else { 583 SRURecordDataParser parser = parsers.get(schema); 584 if (parser != null) { 585 try { 586 proxy.reset(recordReader); 587 recordData = parser.parse(proxy); 588 } catch (XMLStreamException e) { 589 throw new SRUClientException( 590 "error parsing record", e); 591 } 592 } else { 593 // FIXME: handle this better? 594 logger.debug( 595 "no record parser found for schema '{}'", 596 schema); 543 597 } 544 545 String schema = reader.readContent(SRU_NS, 546 "recordSchema", true); 547 548 SRURecordPacking packing = parseRecordPacking(reader); 549 550 logger.debug("schema = {}, packing = {}, requested packing = {}", 551 new Object[] { schema, packing, 552 request.getRecordPacking() }); 553 554 if ((request.getRecordPacking() != null) && 555 (packing != request.getRecordPacking())) { 556 logger.warn("requested '{}' record packing, but server responded with '{}' record packing", 557 request.getRecordPacking().toProtocolString(), 558 packing.toProtocolString()); 559 // XXX: only throw if client is pedantic? 560 throw new SRUClientException("requested '" + 561 request.getRecordPacking().toProtocolString() + 562 "' record packing, but server responded with '" + 563 packing.toProtocolString() + "' record packing"); 598 } 599 600 if (packing == SRURecordPacking.STRING) { 601 recordReader.closeCompletly(); 602 } 603 604 reader.consumeWhitespace(); 605 reader.readEnd(SRU_NS, "recordData", true); 606 607 String identifier = null; 608 if (version == SRUVersion.VERSION_1_2) { 609 identifier = reader.readContent(SRU_NS, 610 "recordIdentifier", false); 611 } 612 613 int position = reader.readContent(SRU_NS, "recordPosition", 614 false, -1); 615 616 logger.debug("recordIdentifier = {}, recordPosition = {}", 617 identifier, position); 618 619 // notify handler 620 if (surrogate != null) { 621 handler.onSurrogateRecord(identifier, position, 622 surrogate); 623 } else { 624 if (recordData != null) { 625 handler.onRecord(identifier, position, recordData); 564 626 } 565 566 // searchRetrieveResponse/record/recordData 567 reader.readStart(SRU_NS, "recordData", true);627 } 628 629 if (reader.readStart(SRU_NS, "extraRecordData", false)) { 568 630 reader.consumeWhitespace(); 569 570 SRURecordData recordData = null; 571 SRUDiagnostic surrogate = null; 572 SRUXMLStreamReader recordReader = null; 573 574 if (packing == SRURecordPacking.STRING) { 575 /* 576 * read content into temporary buffer and then use 577 * a new XML reader to parse record data 578 */ 579 final String data = reader.readString(true); 580 InputStream in = new ByteArrayInputStream( 581 data.getBytes()); 582 // FIXME: namespace context? 583 recordReader = createReader(in, false); 584 } else { 585 recordReader = reader; 631 proxy.reset(reader); 632 try { 633 handler.onExtraRecordData(identifier, position, 634 proxy); 635 } catch (XMLStreamException e) { 636 throw new SRUClientException("handler " 637 + "triggered error while parsing " 638 + "'extraRecordData'", e); 586 639 } 587 588 if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) {589 surrogate = parseDiagnostic(recordReader, true);590 } else {591 SRURecordDataParser parser = parsers.get(schema);592 if (parser != null) {593 try {594 proxy.reset(recordReader);595 recordData = parser.parse(proxy);596 } catch (XMLStreamException e) {597 throw new SRUClientException(598 "error parsing record", e);599 }600 } else {601 // FIXME: handle this better?602 logger.debug(603 "no record parser found for schema '{}'",604 schema);605 }606 }607 608 if (packing == SRURecordPacking.STRING) {609 recordReader.closeCompletly();610 }611 612 640 reader.consumeWhitespace(); 613 reader.readEnd(SRU_NS, "recordData", true); 614 615 String identifier = null; 616 if (version == SRUVersion.VERSION_1_2) { 617 identifier = reader.readContent(SRU_NS, 618 "recordIdentifier", false); 619 } 620 621 int position = reader.readContent(SRU_NS, 622 "recordPosition", false, -1); 623 624 logger.debug("recordIdentifier = {}, recordPosition = {}", 625 identifier, position); 626 627 // notify handler 628 if (surrogate != null) { 629 handler.onSurrogateRecord(identifier, 630 position, surrogate); 631 } else { 632 if (recordData != null) { 633 handler.onRecord(identifier, 634 position, recordData); 635 } 636 } 637 638 if (reader.readStart(SRU_NS, "extraRecordData", false)) { 639 reader.consumeWhitespace(); 640 proxy.reset(reader); 641 try { 642 handler.onExtraRecordData(identifier, 643 position, proxy); 644 } catch (XMLStreamException e) { 645 throw new SRUClientException("handler " + 646 "triggered error while parsing " + 647 "'extraRecordData'", e); 648 } 649 reader.consumeWhitespace(); 650 reader.readEnd(SRU_NS, "extraRecordData", true); 651 } 652 653 reader.readEnd(SRU_NS, "record"); 654 } // while 655 656 reader.readEnd(SRU_NS, "records"); 657 int nextRecordPosition = reader.readContent(SRU_NS, 658 "nextRecordPosition", false, -1); 659 logger.debug("nextRecordPosition = {}", nextRecordPosition); 660 handler.onFinishRecords(nextRecordPosition); 661 } else { 662 handler.onStartRecords(numberOfRecords, 663 resultSetId, resultSetIdleTime); 664 handler.onFinishRecords(-1); 665 } 641 reader.readEnd(SRU_NS, "extraRecordData", true); 642 } 643 644 reader.readEnd(SRU_NS, "record"); 645 } // while 646 647 reader.readEnd(SRU_NS, "records"); 648 int nextRecordPosition = reader.readContent(SRU_NS, 649 "nextRecordPosition", false, -1); 650 logger.debug("nextRecordPosition = {}", nextRecordPosition); 651 handler.onFinishRecords(nextRecordPosition); 666 652 667 653 // searchRetrieveResponse/echoedSearchRetrieveResponse … … 670 656 } 671 657 672 // explainResponse/diagnostics 673 parseDiagnostics(reader); 658 // searchRetrieveResponse/diagnostics 659 final List<SRUDiagnostic> diagnostics = 660 parseDiagnostics(reader); 661 if (diagnostics != null) { 662 handler.onDiagnostics(diagnostics); 663 } 674 664 675 665 // explainResponse/extraResponseData … … 714 704 SRUClientException { 715 705 if (reader.readStart(SRU_NS, "diagnostics", false)) { 716 List<SRUDiagnostic> diag ostics = null;706 List<SRUDiagnostic> diagnostics = null; 717 707 718 708 SRUDiagnostic diagnostic = null; 719 709 while ((diagnostic = parseDiagnostic(reader, 720 (diag ostics == null))) != null) {721 if (diag ostics == null) {722 diag ostics = new ArrayList<SRUDiagnostic>();723 } 724 diag ostics.add(diagnostic);710 (diagnostics == null))) != null) { 711 if (diagnostics == null) { 712 diagnostics = new ArrayList<SRUDiagnostic>(); 713 } 714 diagnostics.add(diagnostic); 725 715 } // while 726 716 reader.readEnd(SRU_NS, "diagnostics"); 727 return diag ostics;717 return diagnostics; 728 718 } else { 729 719 return null; -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUDefaultHandlerAdapter.java
r1972 r2021 7 7 8 8 public class SRUDefaultHandlerAdapter implements SRUDefaultHandler { 9 9 10 10 @Override 11 public void on FatalError(List<SRUDiagnostic> diagnistics)11 public void onDiagnostics(List<SRUDiagnostic> diagnostics) 12 12 throws SRUClientException { 13 13 } 14 15 14 16 15 @Override -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainHandler.java
r1972 r2021 8 8 public interface SRUExplainHandler { 9 9 10 public void on FatalError(List<SRUDiagnostic> diagnistics)10 public void onDiagnostics(List<SRUDiagnostic> diagnostics) 11 11 throws SRUClientException; 12 12 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUScanHandler.java
r1972 r2021 11 11 } 12 12 13 public void on FatalError(List<SRUDiagnostic> diagnistics)13 public void onDiagnostics(List<SRUDiagnostic> diagnostics) 14 14 throws SRUClientException; 15 15 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSearchRetrieveHandler.java
r1972 r2021 8 8 public interface SRUSearchRetrieveHandler { 9 9 10 public void on FatalError(List<SRUDiagnostic> diagnistics)10 public void onDiagnostics(List<SRUDiagnostic> diagnostics) 11 11 throws SRUClientException; 12 12 -
SRUClient/trunk/src/main/java/eu/clarin/sru/client/TestClient.java
r1985 r2021 30 30 SRUDefaultHandlerAdapter handler = new SRUDefaultHandlerAdapter() { 31 31 @Override 32 public void on FatalError(List<SRUDiagnostic> diagnostics)32 public void onDiagnostics(List<SRUDiagnostic> diagnostics) 33 33 throws SRUClientException { 34 34 for (SRUDiagnostic diagnostic : diagnostics) { 35 logger.info("on FatalError: uri={}, detail={}, message={}",35 logger.info("onDiagnostics: uri={}, detail={}, message={}", 36 36 new Object[] { diagnostic.getURI(), 37 37 diagnostic.getDetails(),
Note: See TracChangeset
for help on using the changeset viewer.