📄 xmlpullparser.java
字号:
int pos = input.find("-->", openBracketIndex + 1); if (pos == -1) { throw new ParseException("Unclosed comment beginning at line:" + input.getLineNumber() + " column:" + input.getColumnNumber(), openBracketIndex); } pos += 3; lastText = input.getSubstring(openBracketIndex, pos); lastType = COMMENT; // Conditional comment? <!--[if ...]>..<![endif]--> if (tagText.startsWith("!--[if ") && tagText.endsWith("]") && lastText.toString().endsWith("<![endif]-->")) { // Actually it is no longer a comment. It is now // up to the browser to select the section appropriate. input.setPosition(closeBracketIndex + 1); } else { input.setPosition(pos); } return; } // The closing tag of a conditional comment <!--[if IE]>...<![endif]--> if (tagText.equals("![endif]--")) { lastType = COMMENT; input.setPosition(closeBracketIndex + 1); return; } // CDATA sections might contain "<" which is not part of an XML tag. // Make sure escaped "<" are treated right if (tagText.startsWith("![")) { final String startText = (tagText.length() <= 8 ? tagText : tagText.substring(0, 8)); if (startText.toUpperCase().equals("![CDATA[")) { int pos1 = openBracketIndex; do { // Get index of closing tag and advance past the tag closeBracketIndex = findChar('>', pos1); if (closeBracketIndex == -1) { throw new ParseException("No matching close bracket at line:" + input.getLineNumber() + " column:" + input.getColumnNumber(), input .getPosition()); } // Get the tagtext between open and close brackets tagText = input.getSubstring(openBracketIndex + 1, closeBracketIndex) .toString(); pos1 = closeBracketIndex + 1; } while (tagText.endsWith("]]") == false); // Move to position after the tag input.setPosition(closeBracketIndex + 1); lastText = tagText; lastType = CDATA; return; } } if (tagText.charAt(0) == '?') { lastType = PROCESSING_INSTRUCTION; // Move to position after the tag input.setPosition(closeBracketIndex + 1); return; } // Move to position after the tag lastType = SPECIAL_TAG; input.setPosition(closeBracketIndex + 1); } /** * Gets the next tag from the input string. * * @return The extracted tag (will always be of type XmlTag). * @throws ParseException */ public final MarkupElement nextTag() throws ParseException { while (next()) { switch (lastType) { case TAG : return lastTag; case BODY : break; case COMMENT : break; case CDATA : break; case PROCESSING_INSTRUCTION : break; case SPECIAL_TAG : break; } } return null; } /** * Find the char but ignore any text within ".." and '..' * * @param ch * The character to search * @param startIndex * Start index * @return -1 if not found, else the index */ private int findChar(final char ch, int startIndex) { char quote = 0; for (; startIndex < input.size(); startIndex++) { final char charAt = input.charAt(startIndex); if (quote != 0) { if (quote == charAt) { quote = 0; } } else if ((charAt == '"') || (charAt == '\'')) { quote = charAt; } else if (charAt == ch) { return startIndex; } } return -1; } /** * Parse the given string. * <p> * Note: xml character encoding is NOT applied. It is assumed the input provided does have the * correct encoding already. * * @param string * The input string * @throws IOException * Error while reading the resource * @throws ResourceStreamNotFoundException * Resource not found */ public void parse(final CharSequence string) throws IOException, ResourceStreamNotFoundException { parse(new ByteArrayInputStream(string.toString().getBytes()), null); } /** * Reads and parses markup from an input stream, using UTF-8 encoding by default when not * specified in XML declaration. * * @param in * The input stream to read and parse * @throws IOException * @throws ResourceStreamNotFoundException */ public void parse(final InputStream in) throws IOException, ResourceStreamNotFoundException { // When XML declaration does not specify encoding, it defaults to UTF-8 parse(in, "UTF-8"); } /** * Reads and parses markup from an input stream * * @param inputStream * The input stream to read and parse * @param encoding * The default character encoding of the input * @throws IOException * @throws ResourceStreamNotFoundException */ public void parse(final InputStream inputStream, final String encoding) throws IOException, ResourceStreamNotFoundException { try { xmlReader = new XmlReader(new BufferedInputStream(inputStream, 4000), encoding); input = new FullyBufferedReader(xmlReader); } finally { inputStream.close(); if (xmlReader != null) { xmlReader.close(); } } } /** * * @see org.apache.wicket.markup.parser.IXmlPullParser#setPositionMarker() */ public final void setPositionMarker() { input.setPositionMarker(input.getPosition()); } /** * * @see org.apache.wicket.markup.parser.IXmlPullParser#setPositionMarker(int) */ public final void setPositionMarker(final int pos) { input.setPositionMarker(pos); } /** * * @see java.lang.Object#toString() */ public String toString() { return input.toString(); } /** * Parses the text between tags. For example, "a href=foo.html". * * @param tagText * The text between tags * @return A new Tag object or null if the tag is invalid * @throws ParseException */ private XmlTag parseTagText(final String tagText) throws ParseException { // Get the length of the tagtext final int tagTextLength = tagText.length(); // If we match tagname pattern final TagNameParser tagnameParser = new TagNameParser(tagText); if (tagnameParser.matcher().lookingAt()) { final XmlTag tag = new XmlTag(); // Extract the tag from the pattern matcher tag.name = tagnameParser.getName(); tag.namespace = tagnameParser.getNamespace(); // Are we at the end? Then there are no attributes, so we just // return the tag int pos = tagnameParser.matcher().end(0); if (pos == tagTextLength) { return tag; } // Extract attributes final VariableAssignmentParser attributeParser = new VariableAssignmentParser(tagText); while (attributeParser.matcher().find(pos)) { // Get key and value using attribute pattern String value = attributeParser.getValue(); // In case like <html xmlns:wicket> will the value be null if (value == null) { value = ""; } // Set new position to end of attribute pos = attributeParser.matcher().end(0); // Chop off double quotes or single quotes if (value.startsWith("\"") || value.startsWith("\'")) { value = value.substring(1, value.length() - 1); } // Trim trailing whitespace value = value.trim(); // Get key final String key = attributeParser.getKey(); // Put the attribute in the attributes hash if (null != tag.put(key, value)) { throw new ParseException("Same attribute found twice: " + key, input .getPosition()); } // The input has to match exactly (no left over junk after // attributes) if (pos == tagTextLength) { return tag; } } return tag; } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -