Changeset 2986 for SRUClient


Ignore:
Timestamp:
06/04/13 16:15:35 (11 years ago)
Author:
oschonef
Message:
  • add support for parsing indexInfo within ZeeRex? (explain) record data
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  
    258258    } // class DatabaseInfo
    259259
     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
    260404    private final ServerInfo serverInfo;
    261405    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) {
    265411        this.serverInfo   = serverInfo;
    266412        this.databaseInfo = databaseInfo;
     413        this.indexInfo    = indexInfo;
    267414    }
    268415
     
    289436    }
    290437
     438
     439    public IndexInfo getIndexInfo() {
     440        return indexInfo;
     441    }
     442
    291443} // class SRUExplainRecordData
  • SRUClient/trunk/src/main/java/eu/clarin/sru/client/SRUExplainRecordDataParser.java

    r2956 r2986  
    3030
    3131import eu.clarin.sru.client.SRUExplainRecordData.DatabaseInfo;
     32import eu.clarin.sru.client.SRUExplainRecordData.IndexInfo;
    3233import eu.clarin.sru.client.SRUExplainRecordData.LocalizedString;
    3334import eu.clarin.sru.client.SRUExplainRecordData.ServerInfo;
     
    4950    private static final String TRANSPORT_HTTP  = "http";
    5051    private static final String TRANSPORT_HTTPS = "https";
    51     private static final String PRIMARY_TRUE    = "true";
     52    private static final String STRING_TRUE     = "true";
    5253    private static final String PRIMARY_FALSE   = "false";
    5354    private static final Logger logger =
     
    7071        // explain
    7172        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             }
    8173            return parseExplain(reader, version, strict, ZEEREX_NS);
    8274        } else if (XmlStreamReaderUtils.peekStart(reader,
     
    132124
    133125        // 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);
    136129            XmlStreamReaderUtils.readEnd(reader, ns, "indexInfo");
    137         } // while
     130        } // if
    138131
    139132        // explain/recordInfo or explain/schemaInfo
     
    161154
    162155        XmlStreamReaderUtils.readEnd(reader, ns, "explain", true);
    163         return new SRUExplainRecordData(serverInfo, databaseInfo);
     156        return new SRUExplainRecordData(serverInfo, databaseInfo, indexInfo);
    164157    }
    165158
     
    252245         */
    253246
    254         // make sure to remove any whitespace
    255         XmlStreamReaderUtils.consumeWhitespace(reader);
    256 
    257247        List<LocalizedString> title = null;
    258248        List<LocalizedString> description = null;
     
    268258
    269259        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
    271270            if (XmlStreamReaderUtils.peekStart(reader, ns, "title")) {
    272271                if ((mode != 0) && (mode != 1)) {
     
    292291                    }
    293292                }
    294                 LocalizedString s = parseStringI18N(reader, strict, ns, "title");
     293                LocalizedString s =
     294                        parseStringI18N(reader, strict, ns, "title", true);
    295295                if (title == null) {
    296296                    title = new ArrayList<LocalizedString>();
     
    322322                }
    323323                LocalizedString s =
    324                         parseStringI18N(reader, strict, ns, "description");
     324                        parseStringI18N(reader, strict, ns, "description", true);
    325325                if (description == null) {
    326326                    description = new ArrayList<LocalizedString>();
     
    330330            } else if (XmlStreamReaderUtils.peekStart(reader, ns, "author")) {
    331331                LocalizedString s =
    332                         parseStringI18N(reader, strict, ns, "author");
     332                        parseStringI18N(reader, strict, ns, "author", true);
    333333                if (author == null) {
    334334                    author = new ArrayList<LocalizedString>();
     
    338338            } else if (XmlStreamReaderUtils.peekStart(reader, ns, "contact")) {
    339339                LocalizedString s =
    340                         parseStringI18N(reader, strict, ns, "contact");
     340                        parseStringI18N(reader, strict, ns, "contact", true);
    341341                if (contact == null) {
    342342                    contact = new ArrayList<LocalizedString>();
     
    346346            } else if (XmlStreamReaderUtils.peekStart(reader, ns, "extent")) {
    347347                LocalizedString s =
    348                         parseStringI18N(reader, strict, ns, "extent");
     348                        parseStringI18N(reader, strict, ns, "extent", true);
    349349                if (extent == null) {
    350350                    extent = new ArrayList<LocalizedString>();
     
    354354            } else if (XmlStreamReaderUtils.peekStart(reader, ns, "history")) {
    355355                LocalizedString s =
    356                         parseStringI18N(reader, strict, ns, "history");
     356                        parseStringI18N(reader, strict, ns, "history", true);
    357357                if (history == null) {
    358358                    history = new ArrayList<LocalizedString>();
     
    363363                    ns, "restrictions")) {
    364364                LocalizedString s =
    365                         parseStringI18N(reader, strict, ns, "restrictions");
     365                        parseStringI18N(reader, strict, ns, "restrictions", true);
    366366                if (restrictions == null) {
    367367                    restrictions = new ArrayList<LocalizedString>();
     
    371371            } else if (XmlStreamReaderUtils.peekStart(reader, ns, "subjects")) {
    372372                LocalizedString s =
    373                         parseStringI18N(reader, strict, ns, "subjects");
     373                        parseStringI18N(reader, strict, ns, "subjects", true);
    374374                if (subjects == null) {
    375375                    subjects = new ArrayList<LocalizedString>();
     
    379379            } else if (XmlStreamReaderUtils.peekStart(reader, ns, "links")) {
    380380                LocalizedString s =
    381                         parseStringI18N(reader, strict, ns, "links");
     381                        parseStringI18N(reader, strict, ns, "links", true);
    382382                if (links == null) {
    383383                    links = new ArrayList<LocalizedString>();
     
    388388                    ns, "implementation")) {
    389389                LocalizedString s =
    390                         parseStringI18N(reader, strict, ns, "implementation");
     390                        parseStringI18N(reader, strict, ns, "implementation", true);
    391391                if (implementation == null) {
    392392                    implementation = new ArrayList<LocalizedString>();
     
    401401                break;
    402402            }
    403 
    404             // make sure to remove any whitespace
    405             XmlStreamReaderUtils.consumeWhitespace(reader);
    406         } // while
     403        } // for
    407404
    408405        return new DatabaseInfo(title, description, author, contact,
     
    412409
    413410
    414     private static Object parseIndexInfo(XMLStreamReader reader,
     411    private static IndexInfo parseIndexInfo(XMLStreamReader reader,
    415412            boolean strict, String ns) throws XMLStreamException,
    416413            SRUClientException {
     
    418415         * indexInfo ((set | index | sortKeywords)+)
    419416         */
    420         logger.debug("indexInfo");
    421417        boolean found = false;
     418        List<IndexInfo.Set> sets = null;
     419        List<IndexInfo.Index> indexes = null;
    422420        for (;;) {
    423421            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");
    426428                XmlStreamReaderUtils.consumeStart(reader);
    427429
    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));
    429449                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");
    433460                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));
    435535                found = true;
    436536            } else if (XmlStreamReaderUtils.readStart(reader, ns, "sortKeyword", false)) {
    437                 logger.debug("-> sortKeywords");
     537                logger.debug("skipping 'sortKeywords'");
    438538                XmlStreamReaderUtils.readEnd(reader, ns, "sortKeyword", true);
    439539                found = true;
     
    451551                    reader.getLocation());
    452552        }
    453         return null;
     553        return ((sets != null) || (indexes != null))
     554                ? new IndexInfo(sets, indexes)
     555                : null;
    454556    }
    455557
     
    504606
    505607    private static LocalizedString parseStringI18N(XMLStreamReader reader,
    506             boolean strict, String ns, String localName)
     608            boolean strict, String ns, String localName, boolean required)
    507609            throws XMLStreamException {
    508         XmlStreamReaderUtils.readStart(reader, ns, localName, true, true);
     610        if (!XmlStreamReaderUtils.readStart(reader, ns, localName, required,
     611                true)) {
     612            return null;
     613        }
    509614        final String lang = XmlStreamReaderUtils.readAttributeValue(reader,
    510615                null, "lang", false);
     
    515620            }
    516621        }
     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;
    517635        final String s = XmlStreamReaderUtils.readAttributeValue(reader,
    518                 null, "primary", false);
    519         boolean primary = false;
     636                ns, localName, false);
    520637        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)) {
    523640                    if (strict) {
    524641                        throw new XMLStreamException("capitalization");
     
    528645                    }
    529646                }
    530                 primary = true;
     647                result = true;
    531648            } else if (PRIMARY_FALSE.equalsIgnoreCase(s)) {
    532649                if (!PRIMARY_FALSE.equals(s)) {
     
    538655                    }
    539656                }
    540                 primary = false;
     657                result = false;
    541658            } else {
    542659                // 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;
    552664    }
    553665
Note: See TracChangeset for help on using the changeset viewer.