xmlparser.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,909 行 · 第 1/3 页
JAVA
1,909 行
public void setLine(int line) { _line = line; } public int getLineNumber() { return getLine(); } public int getColumnNumber() { return getColumn(); } /** * Adds a string to the current text buffer. */ private void addText(String s) throws IOException, SAXException { int len = s.length(); for (int i = 0; i < len; i++) addText(s.charAt(i)); } /** * Adds a character to the current text buffer. */ private void addText(char ch) throws IOException, SAXException { if (_textLength > 0 && _textBuffer[_textLength - 1] == '\r') { _textBuffer[_textLength - 1] = '\n'; if (ch == '\n') return; } if (_isIgnorableWhitespace && ! XmlChar.isWhitespace(ch)) _isIgnorableWhitespace = false; _textBuffer[_textLength++] = ch; } /** * Flushes the text buffer to the SAX callback. */ private void addText(char []buffer, int offset, int length, boolean isWhitespace) throws IOException, SAXException { if (length <= 0) return; if (_namespace.getDepth() == 1) { if (! isWhitespace) { throw error(L.l("expected top element at '{0}'", new String(buffer, offset, length))); } else { _contentHandler.ignorableWhitespace(buffer, offset, length); } } else _contentHandler.characters(buffer, offset, length); } /** * Parses a name. */ private SaxIntern.Entry parseName(int offset, boolean isAttribute) throws IOException { char []inputBuf = _inputBuffer; int inputLength = _inputLength; int inputOffset = _inputOffset; char []valueBuf = _valueBuffer; int valueLength = offset; int colon = 0; while (true) { if (inputOffset < inputLength) { char ch = inputBuf[inputOffset++]; if (XML_NAME_CHAR[ch]) { valueBuf[valueLength++] = ch; } else if (ch == ':') { if (colon <= 0) colon = valueLength; valueBuf[valueLength++] = ch; } else { _inputOffset = inputOffset - 1; return _intern.add(valueBuf, offset, valueLength - offset, colon, isAttribute); } } else if (fillBuffer()) { inputLength = _inputLength; inputOffset = 0; } else { return _intern.add(valueBuf, offset, valueLength - offset, colon, isAttribute); } } } final int skipWhitespace(int ch) throws IOException, SAXException { while (ch <= 0x20 && (ch == 0x20 || ch == 0x9 || ch == 0xa || ch == 0xd)) { ch = read(); } return ch; } public void setReader(XmlReader reader) { _reader = reader; } /** * Adds text to the macro, escaping attribute values. */ void setMacroAttr(String text) throws IOException, SAXException { if (_reader != _macro) { _macro.init(this, _reader); _reader = _macro; } int j = _macroIndex; for (int i = 0; i < text.length(); i++) { int ch = text.charAt(i); if (ch == '\'') _macro.add("'"); else if (ch == '"') _macro.add("""); else _macro.add((char) ch); } } void pushInclude(String systemId) throws IOException, SAXException { pushInclude(null, systemId); } /** * Pushes the named file as a lexical include. * * @param systemId the name of the file to include. */ void pushInclude(String publicId, String systemId) throws IOException, SAXException { InputStream stream = openStream(systemId, publicId); if (stream == null) throw new FileNotFoundException(systemId); _is = Vfs.openRead(stream); Path oldSearchPath = _searchPath; Path path = _is.getPath(); if (path != null) { _owner.addDepend(path); if (_searchPath != null) { _searchPath = path.getParent(); _reader.setSearchPath(oldSearchPath); } } _filename = systemId; /* XmlReader nextReader; if (_reader instanceof Utf8Reader) nextReader = new Utf8Reader(this, _is); else { _is.setEncoding(_reader.getReadStream().getEncoding()); nextReader = new XmlReader(this, _is); } _reader = nextReader; */ XmlReader oldReader = _reader; _reader = null; _line = 1; parseXMLDeclaration(oldReader); int ch = read(); XmlReader reader = _reader; if (reader instanceof MacroReader) reader = reader.getNext(); reader.setSystemId(systemId); reader.setFilename(systemId); reader.setPublicId(publicId); reader.setNext(oldReader); unread(ch); } private void popInclude() throws IOException, SAXException { XmlReader oldReader = _reader; _reader = _reader.getNext(); oldReader.setNext(null); _filename = _reader.getFilename(); _line = _reader.getLine(); _is = _reader.getReadStream(); if (_reader.getSearchPath() != null) _searchPath = _reader.getSearchPath(); } void setMacro(String text) throws IOException, SAXException { if (_reader == _macro) { } else if (_macro.getNext() == null) { _macro.init(this, _reader); _reader = _macro; } else { _macro = new MacroReader(); _macro.init(this, _reader); _reader = _macro; } _macro.add(text); } protected final int read() throws IOException, SAXException { int inputOffset = _inputOffset; if (inputOffset < _inputLength) { char ch = _inputBuffer[inputOffset]; _inputOffset = inputOffset + 1; return ch; } else if (fillBuffer()) { return _inputBuffer[_inputOffset++]; } else return -1; } public final void unread(int ch) { if (ch < 0 || _inputOffset <= 0) return; _inputOffset--; } protected boolean fillBuffer() throws IOException { int len = _is.read(_inputBuffer, 0, _inputBuffer.length); if (len >= 0) { _inputLength = len; _inputOffset = 0; return true; } else { _inputLength = 0; _inputOffset = 0; return false; } } private void parseXMLDeclaration(XmlReader oldReader) throws IOException, SAXException { int startOffset = _is.getOffset(); boolean isEBCDIC = false; int ch = _is.read(); XmlReader reader = null; // utf-16 starts with \xfe \xff if (ch == 0xfe) { ch = _is.read(); if (ch == 0xff) { _owner.setAttribute("encoding", "UTF-16"); _is.setEncoding("utf-16"); reader = new Utf16Reader(this, _is); ch = reader.read(); } } // utf-16 rev starts with \xff \xfe else if (ch == 0xff) { ch = _is.read(); if (ch == 0xfe) { _owner.setAttribute("encoding", "UTF-16"); _is.setEncoding("utf-16"); reader = new Utf16Reader(this, _is); ((Utf16Reader) reader).setReverse(true); ch = reader.read(); } } // utf-16 can also start with \x00 < else if (ch == 0x00) { ch = _is.read(); _owner.setAttribute("encoding", "UTF-16"); _is.setEncoding("utf-16"); reader = new Utf16Reader(this, _is); } // utf-8 BOM is \xef \xbb \xbf else if (ch == 0xef) { ch = _is.read(); if (ch == 0xbb) { ch = _is.read(); if (ch == 0xbf) { ch = _is.read(); _owner.setAttribute("encoding", "UTF-8"); _is.setEncoding("utf-8"); reader = new Utf8Reader(this, _is); } } } else if (ch == 0x4c) { // ebcdic // xml/00l1 _is.unread(); // _is.setEncoding("cp037"); _is.setEncoding("cp500"); isEBCDIC = true; reader = new XmlReader(this, _is); ch = reader.read(); } else { int ch2 = _is.read(); if (ch2 == 0x00) { _owner.setAttribute("encoding", "UTF-16LE"); _is.setEncoding("utf-16le"); reader = new Utf16Reader(this, _is); ((Utf16Reader) reader).setReverse(true); } else if (ch2 > 0) _is.unread(); } if (reader != null && reader != oldReader) { } else if (_is.getSource() instanceof ReaderWriterStream) { reader = new XmlReader(this, _is); } else { reader = new Utf8Reader(this, _is); } if (ch == '\n') reader.setLine(2); reader.setSystemId(_systemId); if (_systemId == null) reader.setSystemId(_filename); reader.setFilename(_filename); reader.setPublicId(_publicId); reader.setNext(oldReader); _reader = reader; /* XXX: this might be too strict. */ /* if (! strictXml) { for (; XmlChar.isWhitespace(ch); ch = reader.read()) { } } */ if (ch != '<') { unreadByte(ch); return; } if (parseXMLDecl(_reader) && isEBCDIC) { // EBCDIC requires a re-read _is.setOffset(startOffset); ch = read(); if (ch != '<') throw new IllegalStateException(); parseXMLDecl(_reader); } } private boolean parseXMLDecl(XmlReader reader) throws IOException, SAXException { int ch = readByte(); if (ch != '?') { unreadByte((char) ch); unreadByte('<'); return false; } ch = read(); if (! XmlChar.isNameStart(ch)) throw error(L.l("expected name after '<?' at {0}. Processing instructions expect a name like <?foo ... ?>", badChar(ch))); ch = _reader.parseName(_text, ch); String piName = _text.toString(); if (! piName.equals("xml")) { ch = parsePITail(piName, ch); unreadByte(ch); return false; } ch = parseAttributes(ch, false); if (ch != '?') throw error(L.l("expected '?' at {0}. Processing instructions end with '?>' like <?foo ... ?>", badChar(ch))); if ((ch = read()) != '>') throw error(L.l("expected '>' at {0}. Processing instructions end with '?>' like <?foo ... ?>", ">", badChar(ch))); for (int i = 0; i < _attributes.getLength(); i++) { QName name = _attributes.getName(i); String value = _attributes.getValue(i); if (_owner != null) _owner.setAttribute(name.getLocalPart(), value); if (name.getLocalPart().equals("encoding")) { // xml/00hb // && ! _inDtd) { String encoding = value; if (! _isStaticEncoding && ! encoding.equalsIgnoreCase("UTF-8") && ! encoding.equalsIgnoreCase("UTF-16") && ! (_is.getSource() instanceof ReaderWriterStream)) { _is.setEncoding(encoding); XmlReader oldReader = _reader; _reader = new XmlReader(this, _is); // _reader.setNext(oldReader); _reader.setLine(oldReader.getLine()); _reader.setSystemId(_filename); _reader.setPublicId(null); } } } return true; } protected int readByte() throws IOException { return _is.read(); } protected void unreadByte(int ch) { _is.unread(); } /** * Returns an error including the current line. * * @param text the error message text. */ XmlParseException error(String text) { if (_errorHandler != null) { SAXParseException e = new SAXParseException(text, _locator); try { _errorHandler.fatalError(e); } catch (SAXException e1) { } } return new XmlParseException(_filename + ":" + _line + ": " + text); } public void free() { } int parseName(CharBuffer cb, int ch) throws IOException, SAXException { return _reader.parseName(cb, ch); } /** * Returns a user-readable string for an error character. */ static String badChar(int ch) { if (ch < 0 || ch == 0xffff) return L.l("end of file"); else if (ch == '\n' || ch == '\r') return L.l("end of line"); else if (ch >= 0x20 && ch <= 0x7f) return "'" + (char) ch + "'"; else return "'" + (char) ch + "' (\\u" + hex(ch) + ")"; } private void printDebugNode(WriteStream s, Node node, int depth) throws IOException { if (node == null) return; for (int i = 0; i < depth; i++) s.print(' '); if (node.getFirstChild() != null) { s.println("<" + node.getNodeName() + ">"); for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { printDebugNode(s, child, depth + 2); } for (int i = 0; i < depth; i++) s.print(' '); s.println("</" + node.getNodeName() + ">"); } else s.println("<" + node.getNodeName() + "/>"); } public void close() { TempCharBuffer tempInputBuffer = _tempInputBuffer; _tempInputBuffer = null; _inputBuffer = null; if (tempInputBuffer != null) TempCharBuffer.free(tempInputBuffer); } public static class LocatorImpl implements ExtendedLocator { XmlParser _parser; LocatorImpl(XmlParser parser) { _parser = parser; } public String getSystemId() { if (_parser._reader != null && _parser._reader.getSystemId() != null) return _parser._reader.getSystemId(); else if (_parser.getSystemId() != null) return _parser.getSystemId(); else if (_parser._reader != null && _parser._reader.getFilename() != null) return _parser._reader.getFilename(); else if (_parser.getFilename() != null) return _parser.getFilename(); else return null; } public String getFilename() { if (_parser._reader != null && _parser._reader.getFilename() != null) return _parser._reader.getFilename(); else if (_parser.getFilename() != null) return _parser.getFilename(); else if (_parser._reader != null && _parser._reader.getSystemId() != null) return _parser._reader.getSystemId(); else if (_parser.getSystemId() != null) return _parser.getSystemId(); else return null; } public String getPublicId() { if (_parser._reader != null) return _parser._reader.getPublicId(); else return _parser.getPublicId(); } public int getLineNumber() { if (_parser._reader != null) return _parser._reader.getLine(); else return _parser.getLineNumber(); } public int getColumnNumber() { return _parser.getColumnNumber(); } } static { XML_NAME_CHAR = new boolean[65536]; for (int i = 0; i < 65536; i++) { XML_NAME_CHAR[i] = XmlChar.isNameChar(i) && i != ':'; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?