📄 xmlparser.java
字号:
} public int nextTag() throws XMLStreamException { do { switch (next()) { case XMLStreamConstants.START_ELEMENT: case XMLStreamConstants.END_ELEMENT: case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: break; default: throw new XMLStreamException("Unexpected event type: " + event); } } while (event != XMLStreamConstants.START_ELEMENT && event != XMLStreamConstants.END_ELEMENT); return event; } public void require(int type, String namespaceURI, String localName) throws XMLStreamException { if (event != type) throw new XMLStreamException("Current event type is " + event); if (event == XMLStreamConstants.START_ELEMENT || event == XMLStreamConstants.END_ELEMENT) { String ln = getLocalName(); if (!ln.equals(localName)) throw new XMLStreamException("Current local-name is " + ln); String uri = getNamespaceURI(); if ((uri == null && namespaceURI != null) || (uri != null && !uri.equals(namespaceURI))) throw new XMLStreamException("Current namespace URI is " + uri); } } public boolean standaloneSet() { return (xmlStandalone != null); } public boolean hasNext() throws XMLStreamException { if (event == XMLStreamConstants.END_DOCUMENT) return false; if (!lookahead) { next(); lookahead = true; } return event != -1; } public int next() throws XMLStreamException { if (lookahead) { lookahead = false; return event; } if (event == XMLStreamConstants.END_ELEMENT) { // Pop namespace context if (namespaceAware && !namespaces.isEmpty()) namespaces.removeFirst(); // Pop base context if (baseAware && !bases.isEmpty()) bases.removeFirst(); } if (!startEntityStack.isEmpty()) { String entityName = (String) startEntityStack.removeFirst(); buf.setLength(0); buf.append(entityName); event = START_ENTITY; return extendedEventTypes ? event : next(); } else if (!endEntityStack.isEmpty()) { String entityName = (String) endEntityStack.removeFirst(); buf.setLength(0); buf.append(entityName); event = END_ENTITY; return extendedEventTypes ? event : next(); } try { if (!input.initialized) input.init(); switch (state) { case CONTENT: if (tryRead(TEST_END_ELEMENT)) { readEndElement(); if (stack.isEmpty()) state = MISC; event = XMLStreamConstants.END_ELEMENT; } else if (tryRead(TEST_COMMENT)) { readComment(false); event = XMLStreamConstants.COMMENT; } else if (tryRead(TEST_PI)) { readPI(false); event = XMLStreamConstants.PROCESSING_INSTRUCTION; } else if (tryRead(TEST_CDATA)) { readCDSect(); event = XMLStreamConstants.CDATA; } else if (tryRead(TEST_START_ELEMENT)) { state = readStartElement(); event = XMLStreamConstants.START_ELEMENT; } else { // Check for character reference or predefined entity mark(8); int c = readCh(); if (c == 0x26) // '&' { c = readCh(); if (c == 0x23) // '#' { reset(); event = readCharData(null); } else { // entity reference reset(); readCh(); // & readReference(); String ref = buf.toString(); String text = (String) PREDEFINED_ENTITIES.get(ref); if (text != null) { event = readCharData(text); } else if (replaceERefs && !isUnparsedEntity(ref)) { // this will report a start-entity event boolean external = false; if (doctype != null) { Object entity = doctype.getEntity(ref); if (entity instanceof ExternalIds) external = true; } expandEntity(ref, false, external); event = next(); } else { event = XMLStreamConstants.ENTITY_REFERENCE; } } } else { reset(); event = readCharData(null); if (validating && doctype != null) validatePCData(buf.toString()); } } break; case EMPTY_ELEMENT: String elementName = (String) stack.removeLast(); buf.setLength(0); buf.append(elementName); state = stack.isEmpty() ? MISC : CONTENT; event = XMLStreamConstants.END_ELEMENT; if (validating && doctype != null) endElementValidationHook(); break; case INIT: // XMLDecl? if (tryRead(TEST_XML_DECL)) readXMLDecl(); input.finalizeEncoding(); event = XMLStreamConstants.START_DOCUMENT; state = PROLOG; break; case PROLOG: // Misc* (doctypedecl Misc*)? skipWhitespace(); if (doctype == null && tryRead(TEST_DOCTYPE_DECL)) { readDoctypeDecl(); event = XMLStreamConstants.DTD; } else if (tryRead(TEST_COMMENT)) { readComment(false); event = XMLStreamConstants.COMMENT; } else if (tryRead(TEST_PI)) { readPI(false); event = XMLStreamConstants.PROCESSING_INSTRUCTION; } else if (tryRead(TEST_START_ELEMENT)) { state = readStartElement(); event = XMLStreamConstants.START_ELEMENT; } else { int c = readCh(); error("no root element: U+" + Integer.toHexString(c)); } break; case MISC: // Comment | PI | S skipWhitespace(); if (tryRead(TEST_COMMENT)) { readComment(false); event = XMLStreamConstants.COMMENT; } else if (tryRead(TEST_PI)) { readPI(false); event = XMLStreamConstants.PROCESSING_INSTRUCTION; } else { if (event == XMLStreamConstants.END_DOCUMENT) throw new NoSuchElementException(); int c = readCh(); if (c != -1) error("Only comments and PIs may appear after " + "the root element"); event = XMLStreamConstants.END_DOCUMENT; } break; default: event = -1; } return event; } catch (IOException e) { XMLStreamException e2 = new XMLStreamException(); e2.initCause(e); throw e2; } } // package private /** * Returns the current element name. */ String getCurrentElement() { return (String) stack.getLast(); } // private private void mark(int limit) throws IOException { input.mark(limit); } private void reset() throws IOException { input.reset(); } private int read() throws IOException { return input.read(); } private int read(int[] b, int off, int len) throws IOException { return input.read(b, off, len); } /** * Parsed character read. */ private int readCh() throws IOException, XMLStreamException { int c = read(); if (expandPE && c == 0x25) // '%' { if (peIsError) error("PE reference within decl in internal subset."); expandPEReference(); return readCh(); } return c; } /** * Reads the next character, ensuring it is the character specified. * @param delim the character to match * @exception XMLStreamException if the next character is not the * specified one */ private void require(char delim) throws IOException, XMLStreamException { mark(1); int c = readCh(); if (delim != c) { reset(); error("required character (got U+" + Integer.toHexString(c) + ")", new Character(delim)); } } /** * Reads the next few characters, ensuring they match the string specified. * @param delim the string to match * @exception XMLStreamException if the next characters do not match the * specified string */ private void require(String delim) throws IOException, XMLStreamException { char[] chars = delim.toCharArray(); int len = chars.length; mark(len); int off = 0; do { int l2 = read(tmpBuf, off, len - off); if (l2 == -1) { reset(); error("EOF before required string", delim); } off += l2; } while (off < len); for (int i = 0; i < chars.length; i++) { if (chars[i] != tmpBuf[i]) { reset(); error("required string", delim); } } } /** * Try to read a single character. On failure, reset the stream. * @param delim the character to test * @return true if the character matched delim, false otherwise. */ private boolean tryRead(char delim) throws IOException, XMLStreamException { mark(1); int c = readCh(); if (delim != c) { reset(); return false; } return true; } /** * Tries to read the specified characters. * If successful, the stream is positioned after the last character, * otherwise it is reset. * @param test the string to test * @return true if the characters matched the test string, false otherwise. */ private boolean tryRead(String test) throws IOException { char[] chars = test.toCharArray(); int len = chars.length; mark(len); int count = 0; int l2 = read(tmpBuf, 0, len); if (l2 == -1) { reset(); return false; } count += l2; while (count < len) { // force read int c = read(); if (c == -1) { reset(); return false; } tmpBuf[count++] = (char) c; } for (int i = 0; i < len; i++) { if (chars[i] != tmpBuf[i]) { reset(); return false; } } return true; } /** * Reads characters until the specified test string is encountered. * @param delim the string delimiting the end of the characters */ private void readUntil(String delim) throws IOException, XMLStreamException { int startLine = input.line; try { while (!tryRead(delim)) { int c = readCh(); if (c == -1) throw new EOFException(); else if (input.xml11) { if (!isXML11Char(c) || isXML11RestrictedChar(c)) error("illegal XML 1.1 character", "U+" + Integer.toHexString(c)); } else if (!isChar(c)) error("illegal XML character", "U+" + Integer.toHexString(c)); buf.append(Character.toChars(c)); } } catch (EOFException e) { error("end of input while looking for delimiter "+ "(started on line " + startLine + ')', delim); } } /** * Reads any whitespace characters.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -