Changeset 2936 for SRUClient


Ignore:
Timestamp:
05/27/13 15:54:47 (11 years ago)
Author:
oschonef
Message:
  • add preliminary Zeerex record parser and Zeerex record data object (used in explain, still disabled)
  • make non-strict mode more forgiving
  • (preliminary) add version attribute to parse() method of record data parsers
Location:
SRUClient/trunk/src
Files:
2 added
12 edited

Legend:

Unmodified
Added
Removed
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUClient.java

    r2466 r2936  
    5656    private List<SRUDiagnostic> diagnostics;
    5757    private DocumentFragment extraResponseData;
    58     /* explain */
    59     private SRURecord record;
    6058    /* scan */
    6159    private List<SRUTerm> terms;
     
    6462    private String resultSetId;
    6563    private int resultSetIdleTime;
     64    private int nextRecordPosition;
     65    /* explain/searchRetrieve */
    6666    private List<SRURecord> records;
    67     private int nextRecordPosition;
    6867    /* statistics */
    6968    private int totalBytesTransferred;
     
    167166    /**
    168167     * Register a record data parser.
    169      *
     168     * 
    170169     * @param parser
    171170     *            a parser instance
    172      * @throws SRUClientException
    173      *             if a parser handing the same record schema is already
    174      *             registered
    175171     * @throws NullPointerException
    176172     *             if any required argument is <code>null</code>
    177173     * @throws IllegalArgumentException
    178      *             if the supplied parser is invalid
    179      */
    180     public void registerRecordParser(SRURecordDataParser parser)
    181             throws SRUClientException {
     174     *             if the supplied parser is invalid or a parser handing the
     175     *             same record schema is already registered
     176     */
     177    public void registerRecordParser(SRURecordDataParser parser) {
    182178        client.registerRecordParser(parser);
    183179    }
     
    202198        try {
    203199            client.explain(request, handler);
     200            SRURecord record = null;
     201            if ((records != null) && !records.isEmpty()) {
     202                record = records.get(0);
     203            }
    204204            return new SRUExplainResponse(request,
    205205                    diagnostics,
     
    313313        diagnostics           = null;
    314314        extraResponseData     = null;
    315         /* explain */
    316         record                = null;
    317315        /* scan */
    318316        terms                 = null;
     
    321319        resultSetId           = null;
    322320        resultSetIdleTime     = -1;
     321        nextRecordPosition    = -1;
     322        /* explain/searchRetrieve */
    323323        records               = null;
    324         nextRecordPosition    = -1;
    325324        /* statistics */
    326325        totalBytesTransferred = -1;
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainHandler.java

    r2466 r2936  
    7676            throws XMLStreamException, SRUClientException;
    7777
     78    /**
     79     * Receive notifications of the start of the enumeration of records in the
     80     * response.
     81     *
     82     * @param numberOfRecords
     83     *            the number of records or <code>-1</code> if not available
     84     * @param resultSetId
     85     *            the result set id or <code>null</code> if not available
     86     * @param resultSetIdleTime
     87     *            the result set idle time or <code>-1</code> if not available
     88     * @throws SRUClientException
     89     *             any SRU exception, possibly wrapping another exception
     90     */
     91    public void onStartRecords(int numberOfRecords, String resultSetId,
     92            int resultSetIdleTime) throws SRUClientException;
     93
     94
     95    /**
     96     * Receive notifications of the end of the enumeration of records in the
     97     * response.
     98     *
     99     * @param nextRecordPosition
     100     *            the next record position or <code>-1</code> if not available
     101     * @throws SRUClientException
     102     *             any SRU exception, possibly wrapping another exception
     103     */
     104    public void onFinishRecords(int nextRecordPosition)
     105            throws SRUClientException;
     106
     107
     108    /**
     109     * Receive notification of a record in the result set.
     110     *
     111     * @param identifier
     112     *            identifier of the record or <code>null</code> if not available
     113     * @param position
     114     *            position of the record in the result set or <code>-1</code> if
     115     *            not available
     116     * @param data
     117     *            the parsed record data
     118     * @throws SRUClientException
     119     *             any SRU exception, possibly wrapping another exception
     120     * @see SRURecordData
     121     * @see SRURecordDataParser
     122     */
     123    public void onRecord(String identifier, int position, SRURecordData data)
     124            throws SRUClientException;
     125
     126
     127    /**
     128     * Receive notification of extra record data.
     129     *
     130     * @param identifier
     131     *            identifier of the record or <code>null</code> if not available
     132     * @param position
     133     *            position of the record in the result set or <code>-1</code> if
     134     *            not available
     135     * @param reader
     136     *            a {@link XMLStreamReader} to parse the extra term data
     137     * @throws XMLStreamException
     138     *             an error occurred while parsing the response
     139     * @throws SRUClientException
     140     *             any SRU exception, possibly wrapping another exception
     141     */
     142    public void onExtraRecordData(String identifier, int position,
     143            XMLStreamReader reader) throws XMLStreamException,
     144            SRUClientException;
     145
    78146} // interface SRUExplainHandler
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRURecordDataParser.java

    r2466 r2936  
    3939     * @param reader
    4040     *            a {@link XMLStreamReader} to parse the record data
     41     * @param version
     42     *            the {@link SRUVersion} that was used to encode the record
    4143     * @return the parsed record
    4244     * @throws XMLStreamException
     
    4648     * @see SRURecordData
    4749     */
    48     public SRURecordData parse(XMLStreamReader reader)
     50    public SRURecordData parse(XMLStreamReader reader, SRUVersion version)
    4951            throws XMLStreamException, SRUClientException;
    5052
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUSimpleClient.java

    r2879 r2936  
    8484    private final HttpClient httpClient;
    8585    private final XmlStreamReaderProxy proxy = new XmlStreamReaderProxy();
    86 
     86    private final SRUExplainRecordDataParser explainRecordParser =
     87            new SRUExplainRecordDataParser();
    8788
    8889    /**
     
    193194     * @param parser
    194195     *            a parser instance
    195      * @throws SRUClientException
    196      *             if a parser handing the same record schema is already
    197      *             registered
    198196     * @throws NullPointerException
    199197     *             if any required argument is <code>null</code>
    200198     * @throws IllegalArgumentException
    201      *             if the supplied parser is invalid
     199     *             if the supplied parser is invalid or a parser handing the
     200     *             same record schema is already registered
    202201     */
    203     public void registerRecordParser(SRURecordDataParser parser)
    204             throws SRUClientException {
     202    public void registerRecordParser(SRURecordDataParser parser) {
    205203        if (parser == null) {
    206204            throw new NullPointerException("parser == null");
     
    218216            parsers.put(recordSchema, parser);
    219217        } else {
    220             throw new SRUClientException(
     218            throw new IllegalArgumentException(
    221219                    "record data parser already registered: " + recordSchema);
    222220        }
     
    247245            throw new NullPointerException("handler == null");
    248246        }
    249         logger.debug("explain");
     247        logger.debug("performing explain request");
    250248
    251249        final long ts_start = System.nanoTime();
     
    332330            throw new NullPointerException("handler == null");
    333331        }
    334         logger.debug("searchRetrieve: scanClause = {}", request.getScanClause());
     332        logger.debug("performing scan request: scanClause = {}",
     333                request.getScanClause());
    335334
    336335        final long ts_start = System.nanoTime();
     
    417416            throw new NullPointerException("handler == null");
    418417        }
    419         logger.debug("searchRetrieve: query = {}", request.getQuery());
     418        logger.debug("performing searchRetrieve request: query = {}",
     419                request.getQuery());
    420420
    421421        final long ts_start = System.nanoTime();
     
    483483        HttpResponse response = null;
    484484        try {
    485             logger.debug("performing HTTP request: {}", uri.toString());
     485            logger.debug("submitting HTTP request: {}", uri.toString());
    486486            try {
    487487                request = new HttpGet(uri);
     
    535535
    536536    private void parseExplainResponse(final SRUXMLStreamReader reader,
    537             final SRUAbstractRequest request, final SRUExplainHandler handler)
     537            final SRUExplainRequest request, final SRUExplainHandler handler)
    538538            throws SRUClientException {
    539539        logger.debug("parsing 'explain' response");
     540// FIXME: ship explain record data for now
     541//        doParseExplainResponse(reader, request, handler, true);
     542        doParseExplainResponse(reader, request, handler, false);
     543    }
     544
     545
     546    private void doParseExplainResponse(SRUXMLStreamReader reader,
     547            SRUAbstractRequest request, SRUExplainHandler handler,
     548            boolean parseRecordData) throws SRUClientException {
    540549        try {
    541550            // explainResponse
     
    549558            // explainResponse/record
    550559            reader.readStart(SRU_NS, "record", true);
    551 
    552             String schema = reader.readContent(SRU_NS, "recordSchema", true);
    553 
    554             SRURecordPacking packing = parseRecordPacking(reader, strictMode);
    555 
    556             logger.debug("schema = {}, packing = {}", schema, packing);
    557 
    558             // explainResponse/record/recordData
    559             reader.readStart(SRU_NS, "recordData", true);
    560             reader.readEnd(SRU_NS, "recordData", true);
    561 
    562             // explainResponse/record/recordPosition
    563             if (reader.readStart(SRU_NS, "recordPosition", false)) {
    564                 reader.readEnd(SRU_NS, "recordPosition", true);
    565             }
    566 
    567             // explainResponse/record/extraRecordData
    568             if (reader.readStart(SRU_NS, "extraRecordData", false)) {
    569                 reader.readEnd(SRU_NS, "extraRecordData", true);
    570             }
    571 
    572             reader.readEnd(SRU_NS, "record");
     560            if (parseRecordData) {
     561                handler.onStartRecords(-1, null, -1);
     562
     563                SRURecordPacking packing = null;
     564                if (!strictMode && reader.peekStart(SRU_NS, "recordPacking")) {
     565                    packing = parseRecordPacking(reader, false);
     566                    if (packing != null) {
     567                        logger.error("element <recordPacking> must apperear after element <recordSchema> within element <record>");
     568                    }
     569                }
     570                String schema =
     571                        reader.readContent(SRU_NS, "recordSchema", true);
     572                if (packing == null) {
     573                    packing = parseRecordPacking(reader, strictMode);
     574                }
     575                logger.debug("schema = {}, packing = {}", schema, packing);
     576
     577                // explainResponse/record/recordData
     578                reader.readStart(SRU_NS, "recordData", true);
     579                reader.consumeWhitespace();
     580
     581                SRURecordData recordData = null;
     582                SRUXMLStreamReader recordReader = null;
     583
     584                if (packing == SRURecordPacking.STRING) {
     585                    /*
     586                     * read content into temporary buffer and then use a new XML
     587                     * reader to parse record data
     588                     */
     589                    final String data = reader.readString(true);
     590                    InputStream in = new ByteArrayInputStream(data.getBytes());
     591                    // FIXME: namespace context?
     592                    recordReader = createReader(in, false);
     593                } else {
     594                    recordReader = reader;
     595                }
     596
     597                if (!SRUExplainRecordData.RECORD_SCHEMA.equals(schema)) {
     598                    // FIXME: message
     599                    throw new SRUClientException("bad record schema");
     600                }
     601                try {
     602                    proxy.reset(recordReader);
     603                    recordData = explainRecordParser.parse(proxy, version);
     604                } catch (XMLStreamException e) {
     605                    throw new SRUClientException(
     606                            "error parsing explain record", e);
     607                }
     608                if (recordData == null) {
     609                    // FIXME: error message
     610                    throw new SRUClientException(
     611                            "error parsing explain record");
     612                }
     613                if (packing == SRURecordPacking.STRING) {
     614                    recordReader.closeCompletly();
     615                }
     616                reader.consumeWhitespace();
     617                reader.readEnd(SRU_NS, "recordData", true);
     618
     619                if (version == SRUVersion.VERSION_1_2) {
     620                    reader.readContent(SRU_NS, "recordIdentifier", false);
     621                }
     622                reader.readContent(SRU_NS, "recordPosition", false, -1);
     623
     624                // notify handler
     625                handler.onRecord(null, -1, recordData);
     626
     627                if (reader.readStart(SRU_NS, "extraRecordData", false)) {
     628                    reader.consumeWhitespace();
     629                    proxy.reset(reader);
     630                    try {
     631                        handler.onExtraRecordData(null, -1, proxy);
     632                    } catch (XMLStreamException e) {
     633                        throw new SRUClientException("handler "
     634                                + "triggered error while parsing "
     635                                + "'extraRecordData'", e);
     636                    }
     637                    reader.consumeWhitespace();
     638                    reader.readEnd(SRU_NS, "extraRecordData", true);
     639                }
     640
     641                handler.onFinishRecords(-1);
     642
     643                reader.readEnd(SRU_NS, "record");
     644            } else {
     645                /*
     646                 * do not really parse record and skip everything until <record>
     647                 * end tag
     648                 */
     649                reader.readEnd(SRU_NS, "record", true);
     650            }
    573651
    574652            // explainResponse/echoedExplainRequest
     
    625703             */
    626704            if (reader.peekStart(SRU_NS, "explainResponse")) {
    627                 parseExplainResponse(reader, request, new SRUExplainHandler() {
     705                doParseExplainResponse(reader, request, new SRUExplainHandler() {
    628706                    @Override
    629707                    public void onRequestStatistics(int bytes, long millisTotal,
     
    643721                        handler.onDiagnostics(diagnostics);
    644722                    }
    645                 });
     723
     724
     725                    @Override
     726                    public void onStartRecords(int numberOfRecords,
     727                            String resultSetId, int resultSetIdleTime)
     728                            throws SRUClientException {
     729                    }
     730
     731
     732                    @Override
     733                    public void onFinishRecords(int nextRecordPosition)
     734                            throws SRUClientException {
     735                    }
     736
     737
     738                    @Override
     739                    public void onRecord(String identifier, int position,
     740                            SRURecordData data) throws SRUClientException {
     741                    }
     742
     743
     744                    @Override
     745                    public void onExtraRecordData(String identifier,
     746                            int position, XMLStreamReader reader)
     747                            throws XMLStreamException, SRUClientException {
     748                    }
     749                }, false);
    646750            } else {
    647751                logger.debug("parsing 'scanResponse' response");
     
    665769
    666770                        // scanResponse/terms/value
    667                         String value = reader
    668                                 .readContent(SRU_NS, "value", true);
     771                        String value =
     772                                reader.readContent(SRU_NS, "value", true);
    669773
    670774                        // scanResponse/terms/numberOfRecords
     
    776880             */
    777881            if (reader.peekStart(SRU_NS, "explainResponse")) {
    778                 parseExplainResponse(reader, request, new SRUExplainHandler() {
     882                doParseExplainResponse(reader, request, new SRUExplainHandler() {
    779883                    @Override
    780884                    public void onRequestStatistics(int bytes, long millisTotal,
     
    794898                        handler.onDiagnostics(diagnostics);
    795899                    }
    796                 });
     900
     901
     902                    @Override
     903                    public void onStartRecords(int numberOfRecords,
     904                            String resultSetId, int resultSetIdleTime)
     905                            throws SRUClientException {
     906                    }
     907
     908
     909                    @Override
     910                    public void onFinishRecords(int nextRecordPosition)
     911                            throws SRUClientException {
     912                    }
     913
     914
     915                    @Override
     916                    public void onRecord(String identifier, int position,
     917                            SRURecordData data) throws SRUClientException {
     918                    }
     919
     920
     921                    @Override
     922                    public void onExtraRecordData(String identifier,
     923                            int position, XMLStreamReader reader)
     924                            throws XMLStreamException, SRUClientException {
     925                    }
     926                }, false);
    797927            } else {
    798928                logger.debug("parsing 'searchRetrieve' response");
     
    824954                // searchRetrieveResponse/results
    825955                if (numberOfRecords > 0) {
    826                     reader.readStart(SRU_NS, "records", true);
    827 
    828                     // searchRetrieveResponse/records/record
    829                     boolean first = true;
    830                     while (reader.readStart(SRU_NS, "record", first)) {
    831                         if (first) {
    832                             first = false;
    833                             handler.onStartRecords(numberOfRecords,
    834                                     resultSetId, resultSetIdleTime);
    835                         }
    836 
    837                         String schema = reader.readContent(SRU_NS,
    838                                 "recordSchema", true);
    839 
    840                         SRURecordPacking packing =
    841                                 parseRecordPacking(reader, strictMode);
    842 
    843                         logger.debug("schema = {}, packing = {}, " +
    844                                 "requested packing = {}",
    845                                 new Object[] { schema, packing,
    846                                         request.getRecordPacking() });
    847 
    848                         if ((request.getRecordPacking() != null) &&
    849                                 (packing != request.getRecordPacking())) {
    850                             final SRURecordPacking p =
    851                                     request.getRecordPacking();
    852                             logger.error("requested '{}' record packing, but " +
    853                                 "server responded with '{}' record packing",
    854                                     p.getStringValue(),
    855                                     packing.getStringValue());
    856                             if (strictMode) {
    857                                 throw new SRUClientException("requested '" +
    858                                         p.getStringValue() +
    859                                         "' record packing, but server " +
    860                                         "responded with '" +
    861                                         packing.getStringValue() +
    862                                         "' record packing");
     956                    /*
     957                     * some endpoints set numberOfRecords but do not serialize
     958                     * any records, e.g if requested record schema is not
     959                     * supported
     960                     */
     961                    int recordCount = 0;
     962                    if (reader.readStart(SRU_NS, "records", false)) {
     963                        // searchRetrieveResponse/records/record
     964                        boolean first = true;
     965                        while (reader.readStart(SRU_NS, "record", first)) {
     966                            if (first) {
     967                                first = false;
     968                                handler.onStartRecords(numberOfRecords,
     969                                        resultSetId, resultSetIdleTime);
    863970                            }
    864                         }
    865 
    866                         // searchRetrieveResponse/record/recordData
    867                         reader.readStart(SRU_NS, "recordData", true);
    868                         reader.consumeWhitespace();
    869 
    870                         SRURecordData recordData = null;
    871                         SRUDiagnostic surrogate = null;
    872                         SRUXMLStreamReader recordReader = null;
    873 
    874                         if (packing == SRURecordPacking.STRING) {
     971
    875972                            /*
    876                              * read content into temporary buffer and then use
    877                              * a new XML reader to parse record data
     973                             * common error: recordPacking before recordSchema
    878974                             */
    879                             final String data = reader.readString(true);
    880                             InputStream in =
    881                                     new ByteArrayInputStream(data.getBytes());
    882                             // FIXME: namespace context?
    883                             recordReader = createReader(in, false);
    884                         } else {
    885                             recordReader = reader;
    886                         }
    887 
    888                         if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) {
    889                             surrogate = parseDiagnostic(recordReader,
    890                                     true, strictMode);
    891                         } else {
    892                             SRURecordDataParser parser = findParser(schema);
    893                             if (parser != null) {
     975                            SRURecordPacking packing = null;
     976                            if (!strictMode &&
     977                                    reader.peekStart(SRU_NS, "recordPacking")) {
     978                                packing = parseRecordPacking(reader, false);
     979                                if (packing != null) {
     980                                    logger.error("element <recordPacking> " +
     981                                            "must appear after element " +
     982                                            "<recordSchema> within " +
     983                                            "element <record>");
     984                                }
     985                            }
     986                            String schema = reader.readContent(SRU_NS,
     987                                    "recordSchema", true);
     988                            if (packing == null) {
     989                                packing = parseRecordPacking(reader, strictMode);
     990                            }
     991
     992                            logger.debug("schema = {}, packing = {}, " +
     993                                    "requested packing = {}",
     994                                    new Object[] { schema, packing,
     995                                            request.getRecordPacking() });
     996
     997                            if ((request.getRecordPacking() != null) &&
     998                                    (packing != request.getRecordPacking())) {
     999                                final SRURecordPacking p =
     1000                                        request.getRecordPacking();
     1001                                logger.error("requested '{}' record packing, " +
     1002                                        "but server responded with '{}' " +
     1003                                        "record packing",
     1004                                        p.getStringValue(),
     1005                                        packing.getStringValue());
     1006                                if (strictMode) {
     1007                                    throw new SRUClientException("requested '" +
     1008                                            p.getStringValue() +
     1009                                            "' record packing, but server " +
     1010                                            "responded with '" +
     1011                                            packing.getStringValue() +
     1012                                            "' record packing");
     1013                                }
     1014                            }
     1015
     1016                            // searchRetrieveResponse/record/recordData
     1017                            reader.readStart(SRU_NS, "recordData", true);
     1018                            reader.consumeWhitespace();
     1019
     1020                            SRURecordData recordData = null;
     1021                            SRUDiagnostic surrogate = null;
     1022                            SRUXMLStreamReader recordReader = null;
     1023
     1024                            if (packing == SRURecordPacking.STRING) {
     1025                                /*
     1026                                 * read content into temporary buffer and then
     1027                                 * use a new XML reader to parse record data
     1028                                 */
     1029                                final String data = reader.readString(true);
     1030                                InputStream in = new ByteArrayInputStream(
     1031                                        data.getBytes());
     1032                                // FIXME: namespace context?
     1033                                recordReader = createReader(in, false);
     1034                            } else {
     1035                                recordReader = reader;
     1036                            }
     1037
     1038                            if (SRU_DIAGNOSTIC_RECORD_SCHEMA.equals(schema)) {
     1039                                surrogate = parseDiagnostic(recordReader, true,
     1040                                        strictMode);
     1041                            } else {
     1042                                SRURecordDataParser parser = findParser(schema);
     1043                                if (parser != null) {
     1044                                    try {
     1045                                        proxy.reset(recordReader);
     1046                                        recordData =
     1047                                                parser.parse(proxy, version);
     1048                                    } catch (XMLStreamException e) {
     1049                                        throw new SRUClientException(
     1050                                                "error parsing record", e);
     1051                                    }
     1052                                    if (recordData == null) {
     1053                                        logger.debug("record parser did not parse "
     1054                                                + "record correctly and returned "
     1055                                                + "null; injecting client side "
     1056                                                + "surrogate diagnostic");
     1057                                        surrogate = new SRUDiagnostic(
     1058                                                SRUClientDiagnostics.DIAG_RECORD_PARSER_NULL,
     1059                                                null,
     1060                                                "Record parser for schema '" +
     1061                                                        schema +
     1062                                                        "' did not " +
     1063                                                        "parse record correctly " +
     1064                                                        "and errornously " +
     1065                                                        "returned null.");
     1066                                    }
     1067                                } else {
     1068                                    /*
     1069                                     * no record parser found, inject a
     1070                                     * surrogate diagnostic
     1071                                     */
     1072                                    logger.debug(
     1073                                            "no record data parser found "
     1074                                                    + "for schema '{}'; injecting client "
     1075                                                    + "side surrogate diagnostic",
     1076                                            schema);
     1077                                    surrogate = new SRUDiagnostic(
     1078                                            SRUClientDiagnostics.DIAG_NO_RECORD_PARSER,
     1079                                            schema,
     1080                                            "No record data parser for schema '" +
     1081                                                    schema + "' found.");
     1082                                }
     1083                            }
     1084
     1085                            if (packing == SRURecordPacking.STRING) {
     1086                                recordReader.closeCompletly();
     1087                            }
     1088
     1089                            reader.consumeWhitespace();
     1090                            reader.readEnd(SRU_NS, "recordData", true);
     1091
     1092                            String identifier = null;
     1093                            if (version == SRUVersion.VERSION_1_2) {
     1094                                identifier = reader.readContent(SRU_NS,
     1095                                        "recordIdentifier", false);
     1096                            }
     1097
     1098                            int position = reader.readContent(SRU_NS,
     1099                                    "recordPosition", false, -1);
     1100
     1101                            logger.debug("recordIdentifier = {}, " +
     1102                                    "recordPosition = {}",
     1103                                    identifier, position);
     1104
     1105                            // notify handler
     1106                            if (surrogate != null) {
     1107                                handler.onSurrogateRecord(identifier,
     1108                                        position, surrogate);
     1109                            } else {
     1110                                if (recordData != null) {
     1111                                    handler.onRecord(identifier,
     1112                                            position, recordData);
     1113                                }
     1114                            }
     1115
     1116                            if (reader.readStart(SRU_NS,
     1117                                    "extraRecordData", false)) {
     1118                                reader.consumeWhitespace();
     1119                                proxy.reset(reader);
    8941120                                try {
    895                                     proxy.reset(recordReader);
    896                                     recordData = parser.parse(proxy);
     1121                                    handler.onExtraRecordData(identifier,
     1122                                            position, proxy);
    8971123                                } catch (XMLStreamException e) {
    898                                     throw new SRUClientException(
    899                                             "error parsing record", e);
     1124                                    throw new SRUClientException("handler " +
     1125                                            "triggered error while parsing " +
     1126                                            "'extraRecordData'", e);
    9001127                                }
    901                                 if (recordData == null) {
    902                                     logger.debug("record parser did not parse " +
    903                                             "record correctly and returned " +
    904                                             "null; injecting client side " +
    905                                             "surrogate diagnostic");
    906                                     surrogate = new SRUDiagnostic(
    907                                             SRUClientDiagnostics.DIAG_RECORD_PARSER_NULL,
    908                                             null, "Record parser for schema '" +
    909                                                     schema + "' did not " +
    910                                                     "parse record correctly " +
    911                                                     "and errornously " +
    912                                                     "returned null.");
    913                                 }
    914                             } else {
    915                                 /*
    916                                  * no record parser found, inject a
    917                                  * surrogate diagnostic
    918                                  */
    919                                 logger.debug("no record data parser found " +
    920                                         "for schema '{}'; injecting client " +
    921                                         "side surrogate diagnostic", schema);
    922                                 surrogate = new SRUDiagnostic(
    923                                         SRUClientDiagnostics.DIAG_NO_RECORD_PARSER,
    924                                         schema,
    925                                         "No record data parser for schema '" +
    926                                                 schema + "' found.");
     1128                                reader.consumeWhitespace();
     1129                                reader.readEnd(SRU_NS, "extraRecordData", true);
    9271130                            }
    928                         }
    929 
    930                         if (packing == SRURecordPacking.STRING) {
    931                             recordReader.closeCompletly();
    932                         }
    933 
    934                         reader.consumeWhitespace();
    935                         reader.readEnd(SRU_NS, "recordData", true);
    936 
    937                         String identifier = null;
    938                         if (version == SRUVersion.VERSION_1_2) {
    939                             identifier = reader.readContent(SRU_NS,
    940                                     "recordIdentifier", false);
    941                         }
    942 
    943                         int position = reader.readContent(SRU_NS,
    944                                 "recordPosition", false, -1);
    945 
    946                         logger.debug("recordIdentifier = {}, recordPosition = {}",
    947                                 identifier, position);
    948 
    949                         // notify handler
    950                         if (surrogate != null) {
    951                             handler.onSurrogateRecord(identifier,
    952                                     position, surrogate);
    953                         } else {
    954                             if (recordData != null) {
    955                                 handler.onRecord(identifier,
    956                                         position, recordData);
    957                             }
    958                         }
    959 
    960                         if (reader.readStart(SRU_NS, "extraRecordData", false)) {
    961                             reader.consumeWhitespace();
    962                             proxy.reset(reader);
    963                             try {
    964                                 handler.onExtraRecordData(identifier,
    965                                         position, proxy);
    966                             } catch (XMLStreamException e) {
    967                                 throw new SRUClientException("handler "
    968                                         + "triggered error while parsing "
    969                                         + "'extraRecordData'", e);
    970                             }
    971                             reader.consumeWhitespace();
    972                             reader.readEnd(SRU_NS, "extraRecordData", true);
    973                         }
    974 
    975                         reader.readEnd(SRU_NS, "record");
    976                     } // while
    977                     reader.readEnd(SRU_NS, "records");
     1131
     1132                            reader.readEnd(SRU_NS, "record");
     1133                            recordCount++;
     1134                        } // while
     1135                        reader.readEnd(SRU_NS, "records");
     1136                    }
     1137                    if (recordCount == 0) {
     1138                        logger.error("endpoint declared {} results, but response contained no <record> elements (behavior may violate SRU specification)", numberOfRecords);
     1139                    } else if ((request.getMaximumRecords() != -1) && (recordCount > request.getMaximumRecords())) {
     1140                        logger.error("endpoint did not honour 'maximumRecords' request parameter and responded with {} records instead of a maximum of {}", recordCount, request.getMaximumRecords());
     1141                    }
    9781142                } else {
    9791143                    /*
    980                      * provide a better error format, if
     1144                     * provide a better error format, if endpoints responds with
     1145                     * an empty <records> element
    9811146                     */
    9821147                    if (reader.readStart(SRU_NS, "records", false)) {
     
    10001165                        } else {
    10011166                            logger.error("endpoint declared 0 results, but " +
    1002                                     "response contained an " + bad +
     1167                                    "response contained " + bad +
    10031168                                    " record(s)");
    10041169                            if (strictMode) {
     
    11081273
    11091274    private static SRUDiagnostic parseDiagnostic(SRUXMLStreamReader reader,
    1110             boolean required, boolean stictMode) throws XMLStreamException,
     1275            boolean required, boolean strictMode) throws XMLStreamException,
    11111276            SRUClientException {
    11121277        if (reader.readStart(SRU_DIAGNOSIC_NS, "diagnostic", required)) {
     
    11151280            String uri = reader.readContent(SRU_DIAGNOSIC_NS, "uri", true);
    11161281
    1117             // diagnostic/details
    1118             String details = reader.readContent(SRU_DIAGNOSIC_NS, "details",
    1119                     false, stictMode);
     1282            String details = null;
     1283            String message = null;
     1284            if (strictMode) {
     1285                // diagnostic/details
     1286                details = reader.readContent(SRU_DIAGNOSIC_NS, "details",
     1287                        false, true);
     1288
     1289                // diagnostic/message
     1290                message = reader.readContent(SRU_DIAGNOSIC_NS, "message",
     1291                        false, true);
     1292            } else {
     1293                /*
     1294                 * common error: diagnostic/details and diagnostic/message may
     1295                 * appear in any order
     1296                 */
     1297                if (reader.peekStart(SRU_DIAGNOSIC_NS, "details")) {
     1298                    details = reader.readContent(SRU_DIAGNOSIC_NS, "details",
     1299                            false, false);
     1300                    message = reader.readContent(SRU_DIAGNOSIC_NS, "message",
     1301                            false, false);
     1302                } else {
     1303                    message = reader.readContent(SRU_DIAGNOSIC_NS, "message",
     1304                            false, false);
     1305                    details = reader.readContent(SRU_DIAGNOSIC_NS, "details",
     1306                            false, false);
     1307                    if ((message != null) && (details != null)) {
     1308                        logger.error("element <message> and element " +
     1309                                "<details> within element <diagnostic> " +
     1310                                "appeared in wrong order");
     1311                    }
     1312                }
     1313            }
     1314
    11201315            if ((details != null) && details.isEmpty()) {
    11211316                details = null;
    1122                 logger.warn("omitting empty element <details> " +
     1317                logger.debug("omitting empty element <details> " +
    11231318                        "within element <diagnostic>");
    11241319            }
    1125 
    1126             // diagnostic/message
    1127             String message = reader.readContent(SRU_DIAGNOSIC_NS, "message",
    1128                     false, stictMode);
    11291320            if ((message != null) && message.isEmpty()) {
    11301321                message = null;
    1131                 logger.warn("omitting empty element <message> " +
     1322                logger.debug("omitting empty element <message> " +
    11321323                        "within element <diagnostic>");
    11331324            }
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUThreadedClient.java

    r2870 r2936  
    107107    /**
    108108     * Register a record data parser.
    109      *
     109     * 
    110110     * @param parser
    111111     *            a parser instance
    112      * @throws SRUClientException
    113      *             if a parser handing the same record schema is already
    114      *             registered
    115112     * @throws NullPointerException
    116113     *             if any required argument is <code>null</code>
    117114     * @throws IllegalArgumentException
    118      *             if the supplied parser is invalid
    119      */
    120     public void registerRecordParser(SRURecordDataParser parser)
    121             throws SRUClientException {
     115     *             if the supplied parser is invalid or a parser handing the
     116     *             same record schema is already registered
     117     */
     118    public void registerRecordParser(SRURecordDataParser parser) {
    122119        if (parser == null) {
    123120            throw new NullPointerException("parser == null");
     
    133130
    134131        if (parsers.putIfAbsent(recordSchema, parser) != null) {
    135             throw new SRUClientException(
     132            throw new IllegalArgumentException(
    136133                    "record data parser already registered: " + recordSchema);
    137134
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/XmlStreamReaderUtils.java

    r2466 r2936  
    3232    }
    3333
    34     public static boolean readStart(XMLStreamReader reader, String namespaceURI, String localName, boolean required)
     34
     35    public static boolean readStart(XMLStreamReader reader,
     36            String namespaceURI, String localName, boolean required)
    3537            throws XMLStreamException {
    3638        return readStart(reader, namespaceURI, localName, required, false);
     
    3840
    3941
    40     public static boolean readStart(XMLStreamReader reader, String namespaceURI, String localName, boolean required,
     42    public static boolean readStart(XMLStreamReader reader,
     43            String namespaceURI, String localName, boolean required,
    4144            boolean attributes) throws XMLStreamException {
    4245        // System.err.println("readStart (" + localName + ", required = " +
     
    8083
    8184
    82     public static void readEnd(XMLStreamReader reader, String namespaceURI, String localName)
    83             throws XMLStreamException {
     85    public static void readEnd(XMLStreamReader reader, String namespaceURI,
     86            String localName) throws XMLStreamException {
    8487        readEnd(reader, namespaceURI, localName, false);
    8588    }
    8689
    8790
    88     public static void readEnd(XMLStreamReader reader, String namespaceURI, String localName, boolean skipContent)
    89             throws XMLStreamException {
     91    public static void readEnd(XMLStreamReader reader, String namespaceURI,
     92            String localName, boolean skipContent) throws XMLStreamException {
    9093        // System.err.println("readEnd (" + localName + ") @ " + dumpState() +
    9194        // ", skipContent = " + skipContent);
     
    135138
    136139
    137     public static String readContent(XMLStreamReader reader, String namespaceURI, String localName, boolean required)
     140    public static String readContent(XMLStreamReader reader,
     141            String namespaceURI, String localName, boolean required)
    138142            throws XMLStreamException {
    139143        String result = null;
     
    146150
    147151
    148     public static int readContent(XMLStreamReader reader, String namespaceURI, String localName, boolean required,
    149             int defaultValue) throws XMLStreamException {
     152    public static int readContent(XMLStreamReader reader, String namespaceURI,
     153            String localName, boolean required, int defaultValue)
     154            throws XMLStreamException {
    150155        if (readStart(reader, namespaceURI, localName, required)) {
    151156            String s = readString(reader, true);
     
    162167
    163168
    164     public static String readString(XMLStreamReader reader, boolean required) throws XMLStreamException {
     169    public static String readString(XMLStreamReader reader, boolean required)
     170            throws XMLStreamException {
    165171        // System.err.println("readString @ " + toReadable(reader));
    166172        String s = null;
     
    181187
    182188
    183     public static String readAttributeValue(XMLStreamReader reader, String namespaceURI, String localName)
     189    public static String readAttributeValue(XMLStreamReader reader,
     190            String namespaceURI, String localName, boolean required)
    184191            throws XMLStreamException {
    185192        if (!reader.isStartElement()) {
     
    189196        String attr = reader.getAttributeValue(namespaceURI, localName);
    190197        if (attr != null) {
    191             attr = attr.trim().intern();
     198            attr = attr.trim();
     199            if (attr.isEmpty()) {
     200                attr = null;
     201            }
     202        }
     203        if ((attr == null) && required) {
     204            throw new XMLStreamException("expected non-empty attribute '" +
     205                    new QName(namespaceURI, localName) + "' on element '" +
     206                    reader.getName() + "'", reader.getLocation());
    192207        }
    193208        return attr;
     
    195210
    196211
    197     public static String readNamespaceURI(XMLStreamReader reader) throws XMLStreamException {
     212    public static String readAttributeValue(XMLStreamReader reader,
     213            String namespaceURI, String localName) throws XMLStreamException {
     214        return readAttributeValue(reader, namespaceURI, localName, false);
     215    }
     216
     217
     218    public static String readNamespaceURI(XMLStreamReader reader)
     219            throws XMLStreamException {
    198220        if (!reader.isStartElement()) {
    199221            throw new XMLStreamException("not at a start elment event",
     
    204226
    205227
    206     public static String peekElementLocalName(XMLStreamReader reader) throws XMLStreamException {
     228    public static String peekElementLocalName(XMLStreamReader reader)
     229            throws XMLStreamException {
    207230        if (!reader.isStartElement()) {
    208231            throw new XMLStreamException("not at a start elment event",
     
    213236
    214237
    215     public static void consumeStart(XMLStreamReader reader) throws XMLStreamException {
     238    public static void consumeStart(XMLStreamReader reader)
     239            throws XMLStreamException {
    216240        if (!reader.isStartElement()) {
    217241            throw new XMLStreamException("not at a start elment event",
     
    222246
    223247
    224     public static void consumeWhitespace(XMLStreamReader reader) throws XMLStreamException {
     248    public static void consumeWhitespace(XMLStreamReader reader)
     249            throws XMLStreamException {
    225250        while (reader.isWhiteSpace() && reader.hasNext()) {
    226251            reader.next();
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/fcs/ClarinFCSRecordParser.java

    r2467 r2936  
    2727import eu.clarin.sru.client.SRURecordData;
    2828import eu.clarin.sru.client.SRURecordDataParser;
     29import eu.clarin.sru.client.SRUVersion;
    2930import eu.clarin.sru.client.XmlStreamReaderUtils;
    3031
     
    5354
    5455    @Override
    55     public SRURecordData parse(XMLStreamReader reader)
     56    public SRURecordData parse(XMLStreamReader reader, SRUVersion version)
    5657            throws XMLStreamException, SRUClientException {
    5758        logger.debug("parsing CLARIN-FCS record");
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestClient.java

    r2467 r2936  
    3333            SRUClient client = new SRUClient();
    3434
    35             try {
    36                 client.registerRecordParser(new ClarinFCSRecordParser());
    37             } catch (SRUClientException e) {
    38                 logger.error("error adding record parser", e);
    39                 System.exit(1);
    40             }
     35            // register record data parsers
     36            client.registerRecordParser(new ClarinFCSRecordParser());
    4137
    4238            // explain
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestSimpleClient.java

    r2467 r2936  
    3333        if (args.length > 0) {
    3434            logger.info("initializing client ...");
    35             SRUSimpleClient client = new SRUSimpleClient(SRUVersion.VERSION_1_2);
    36             try {
    37                 client.registerRecordParser(new ClarinFCSRecordParser());
    38             } catch (SRUClientException e) {
    39                 logger.error("error adding record parser", e);
    40                 System.exit(1);
    41             }
     35            SRUSimpleClient client = new SRUSimpleClient();
     36
     37            // register record data parsers
     38            client.registerRecordParser(new ClarinFCSRecordParser());
    4239
    4340            /*
     
    110107                                (ClarinFCSRecordData) data;
    111108                        TestUtils.dumpResource(record.getResource());
     109                    } else if (SRUExplainRecordData.RECORD_SCHEMA
     110                            .equals(data.getRecordSchema())) {
     111                        TestUtils.dumpExplainRecordData(data);
    112112                    }
    113113                }
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClient.java

    r2870 r2936  
    3535            SRUThreadedClient client = new SRUThreadedClient();
    3636
    37             try {
    38                 client.registerRecordParser(new ClarinFCSRecordParser());
    39             } catch (SRUClientException e) {
    40                 logger.error("error adding record parser", e);
    41                 System.exit(1);
    42             }
     37            // register record data parsers
     38            client.registerRecordParser(new ClarinFCSRecordParser());
    4339
    4440            try {
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestThreadedClientCallback.java

    r2467 r2936  
    3434            SRUThreadedClient client = new SRUThreadedClient();
    3535
    36             try {
    37                 client.registerRecordParser(new ClarinFCSRecordParser());
    38             } catch (SRUClientException e) {
    39                 logger.error("error adding record parser", e);
    40                 System.exit(1);
    41             }
     36            // register record data parsers
     37            client.registerRecordParser(new ClarinFCSRecordParser());
    4238
    4339            try {
  • SRUClient/trunk/src/test/java/eu/clarin/sru/client/TestUtils.java

    r2467 r2936  
    4242    public static SRUScanRequest makeScanRequest(String baseURI) {
    4343        SRUScanRequest request = new SRUScanRequest(baseURI);
    44         request.setScanClause("fcs.resource");
     44        request.setScanClause("fcs.resource = root");
    4545        request.setExtraRequestData("x-clarin-resource-info", "true");
    4646        return request;
     
    7575            SRURecord record = response.getRecord();
    7676            logger.info("schema = {}", record.getRecordSchema());
     77            if (record.isRecordSchema(SRUExplainRecordData.RECORD_SCHEMA)) {
     78                dumpExplainRecordData(record.getRecordData());
     79            }
    7780        }
    7881    }
     
    145148
    146149
     150    public static void dumpExplainRecordData(SRURecordData recordData) {
     151        if (SRUExplainRecordData.RECORD_SCHEMA.equals(recordData.getRecordSchema())) {
     152            SRUExplainRecordData data = (SRUExplainRecordData) recordData;
     153            logger.info("host={}, port={}, database={}", new Object[] {
     154                    data.getServerInfo().getHost(),
     155                    data.getServerInfo().getPort(),
     156                    data.getServerInfo().getDatabase()
     157            });
     158           
     159        }
     160    }
     161
     162
    147163    public static void dumpResource(Resource resource) {
    148164        logger.info("CLARIN-FCS: pid={}, ref={}",
Note: See TracChangeset for help on using the changeset viewer.