Changeset 13732 in josm


Ignore:
Timestamp:
2018-05-12T14:17:45+02:00 (6 years ago)
Author:
wiktorn
Message:

New functions

  • moveReaderToTag - now you can specify function which checks if tags are the same
  • getElementTextWithSubtags - returns the content of tag including all tags that may exist within
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/GetCapabilitiesParseHelper.java

    r12279 r13732  
    66import java.net.URL;
    77import java.util.Locale;
     8import java.util.function.BiFunction;
    89
    910import javax.xml.namespace.QName;
     
    112113
    113114    /**
     115     * Returns whole content of the element that reader is pointing at, including other XML elements within (with their tags).
     116     *
     117     * @param reader XMLStreamReader that should point to start of element
     118     * @return content of current tag
     119     * @throws XMLStreamException
     120     */
     121    public static String getElementTextWithSubtags(XMLStreamReader reader) throws XMLStreamException {
     122        StringBuilder ret = new StringBuilder();
     123        int level = 0;
     124        QName tag = reader.getName();
     125        for (int event = reader.getEventType(); reader.hasNext(); event = reader.next()) {
     126            if (XMLStreamReader.START_ELEMENT == event) {
     127                if (level > 0) {
     128                    ret.append("<" + reader.getLocalName() +">");
     129                }
     130                level += 1;
     131            } else if (XMLStreamReader.END_ELEMENT == event) {
     132                level -= 1;
     133                if (level == 0 && tag.equals(reader.getName())) {
     134                    return ret.toString();
     135                }
     136                ret.append("</" + reader.getLocalName() +">");
     137            } else if (XMLStreamReader.CHARACTERS == event) {
     138                ret.append(reader.getText());
     139            }
     140            if (level < 0) {
     141                throw new IllegalStateException("WMTS Parser error - moveReaderToEndCurrentTag failed to find closing tag");
     142            }
     143        }
     144        throw new IllegalStateException("WMTS Parser error - moveReaderToEndCurrentTag failed to find closing tag");
     145    }
     146
     147
     148    /**
    114149     * Moves reader to first occurrence of the structure equivalent of Xpath tags[0]/tags[1]../tags[n]. If fails to find
    115150     * moves the reader to the closing tag of current tag
     
    121156     */
    122157    public static boolean moveReaderToTag(XMLStreamReader reader, QName... tags) throws XMLStreamException {
     158        return moveReaderToTag(reader, QName::equals, tags);
     159    }
     160
     161    /**
     162     * Moves reader to first occurrence of the structure equivalent of Xpath tags[0]/tags[1]../tags[n]. If fails to find
     163     * moves the reader to the closing tag of current tag
     164     *
     165     * @param tags array of tags
     166     * @param reader XMLStreamReader which should be moved
     167     * @param equalsFunc function to check equality of the tags
     168     * @return true if tag was found, false otherwise
     169     * @throws XMLStreamException See {@link XMLStreamReader}
     170     */
     171    public static boolean moveReaderToTag(XMLStreamReader reader,
     172            BiFunction<QName, QName, Boolean> equalsFunc, QName... tags) throws XMLStreamException {
    123173        QName stopTag = reader.getName();
    124174        int currentLevel = 0;
     
    128178
    129179        for (int event = 0; //skip current element, so we will not skip it as a whole
    130                 reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && stopTag.equals(reader.getName()));
     180                reader.hasNext() && !(event == XMLStreamReader.END_ELEMENT && equalsFunc.apply(stopTag, reader.getName()));
    131181                event = reader.next()) {
    132             if (event == XMLStreamReader.END_ELEMENT && skipTag != null && skipTag.equals(reader.getName())) {
     182            if (event == XMLStreamReader.END_ELEMENT && skipTag != null && equalsFunc.apply(skipTag, reader.getName())) {
    133183                skipTag = null;
    134184            }
    135185            if (skipTag == null) {
    136186                if (event == XMLStreamReader.START_ELEMENT) {
    137                     if (searchTag.equals(reader.getName())) {
     187                    if (equalsFunc.apply(searchTag, reader.getName())) {
    138188                        currentLevel += 1;
    139189                        if (currentLevel >= tags.length) {
     
    147197                }
    148198
    149                 if (event == XMLStreamReader.END_ELEMENT && parentTag != null && parentTag.equals(reader.getName())) {
     199                if (event == XMLStreamReader.END_ELEMENT && parentTag != null && equalsFunc.apply(parentTag, reader.getName())) {
    150200                    currentLevel -= 1;
    151201                    searchTag = parentTag;
     
    209259        return crsIdentifier;
    210260    }
    211 
    212261}
Note: See TracChangeset for help on using the changeset viewer.