Changeset 13732 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2018-05-12T14:17:45+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/imagery/GetCapabilitiesParseHelper.java
r12279 r13732 6 6 import java.net.URL; 7 7 import java.util.Locale; 8 import java.util.function.BiFunction; 8 9 9 10 import javax.xml.namespace.QName; … … 112 113 113 114 /** 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 /** 114 149 * Moves reader to first occurrence of the structure equivalent of Xpath tags[0]/tags[1]../tags[n]. If fails to find 115 150 * moves the reader to the closing tag of current tag … … 121 156 */ 122 157 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 { 123 173 QName stopTag = reader.getName(); 124 174 int currentLevel = 0; … … 128 178 129 179 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())); 131 181 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())) { 133 183 skipTag = null; 134 184 } 135 185 if (skipTag == null) { 136 186 if (event == XMLStreamReader.START_ELEMENT) { 137 if ( searchTag.equals(reader.getName())) {187 if (equalsFunc.apply(searchTag, reader.getName())) { 138 188 currentLevel += 1; 139 189 if (currentLevel >= tags.length) { … … 147 197 } 148 198 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())) { 150 200 currentLevel -= 1; 151 201 searchTag = parentTag; … … 209 259 return crsIdentifier; 210 260 } 211 212 261 }
Note:
See TracChangeset
for help on using the changeset viewer.