xmlparser.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,651 行 · 第 1/5 页
JAVA
2,651 行
return false; } if (_switchToXml && _activeNode == DOC_NAME && ! _inDtd) { _policy = new XmlPolicy(); } ch = parseAttributes(ch, false); if (ch != '?') throw error(L.l("expected `?' at {0}. Processing instructions end with `?>' like <?foo ... ?>", badChar(ch))); if ((ch = _reader.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.getName(), value); if (name.getName().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; } private int parsePI() throws IOException, SAXException { int ch; appendText(); ch = _reader.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")) return parsePITail(piName, ch); else if (_switchToXml && _activeNode == DOC_NAME && ! _inDtd) { _policy = new XmlPolicy(); return parsePITail(piName, ch); } else { throw error(L.l("<?xml ... ?> occurs after content. The <?xml ... ?> prolog must be at the document start.")); } } private int parsePITail(String piName, int ch) throws IOException, SAXException { ch = skipWhitespace(ch); _text.clear(); while (ch != -1) { if (ch == '?') { if ((ch = _reader.read()) == '>') break; else _text.append('?'); } else { _text.append((char) ch); ch = _reader.read(); } } if (_inDtd) { QProcessingInstruction pi; pi = new QProcessingInstruction(piName, _text.toString()); pi._owner = _dtd._owner; _dtd.appendChild(pi); } else _contentHandler.processingInstruction(piName, _text.toString()); return _reader.read(); } /** * Parses a comment. The "<!--" has already been read. */ private void parseComment() throws IOException, SAXException { if (! _skipComments) appendText(); int ch = _reader.read(); if (ch != '-') throw error(L.l("expected comment at {0}", badChar(ch))); ch = _reader.read(); if (! _skipComments) _buf.clear(); comment: while (ch != -1) { if (ch == '-') { ch = _reader.read(); while (ch == '-') { if ((ch = _reader.read()) == '>') break comment; else if (_strictComments) throw error(L.l("XML forbids `--' in comments")); else if (ch == '-') { if (! _skipComments) _buf.append('-'); } else { if (! _skipComments) _buf.append("--"); break; } } _buf.append('-'); } else if (! XmlChar.isChar(ch)) { throw error(L.l("bad character {0}", hex(ch))); } else { _buf.append((char) ch); ch = _reader.read(); } } if (_inDtd) { QComment comment = new QComment(_buf.toString()); comment._owner = _dtd._owner; _dtd.appendChild(comment); } else if (_skipComments) { } else if (_contentHandler instanceof XMLWriter && ! _skipComments) { ((XMLWriter) _contentHandler).comment(_buf.toString()); _isIgnorableWhitespace = true; } else if (_lexicalHandler != null) { _lexicalHandler.comment(_buf.getBuffer(), 0, _buf.getLength()); _isIgnorableWhitespace = true; } } /** * Parses the contents of a cdata section. * * <pre> * cdata ::= <![CDATA[ ... ]]> * </pre> */ private void parseCdata() throws IOException, SAXException { int ch; if (_forgiving) { if ((ch = _reader.read()) != 'C') { appendText("<![" + (char) ch); return; } else if ((ch = _reader.read()) != 'D') { appendText("<![C" + (char) ch); return; } else if ((ch = _reader.read()) != 'A') { appendText("<![CD" + (char) ch); return; } else if ((ch = _reader.read()) != 'T') { appendText("<![CDA" + (char) ch); return; } else if ((ch = _reader.read()) != 'A') { appendText("<![CDAT" + (char) ch); return; } else if ((ch = _reader.read()) != '[') { appendText("<![CDATA" + (char) ch); return; } } else if ((ch = _reader.read()) != 'C' || (ch = _reader.read()) != 'D' || (ch = _reader.read()) != 'A' || (ch = _reader.read()) != 'T' || (ch = _reader.read()) != 'A' || (ch = _reader.read()) != '[') { throw error(L.l("expected `<![CDATA[' at {0}", badChar(ch))); } ch = _reader.read(); if (_lexicalHandler != null) { _lexicalHandler.startCDATA(); appendText(); } else if (! _isCoalescing) appendText(); cdata: while (ch != -1) { if (ch == ']') { ch = _reader.read(); while (ch == ']') { if ((ch = _reader.read()) == '>') break cdata; else if (ch == ']') addText(']'); else { addText(']'); break; } } addText(']'); } else if (_strictCharacters && ! isChar(ch)) { throw error(L.l("expected character in cdata at {0}", badChar(ch))); } else { addText((char) ch); ch = _reader.read(); } } if (_lexicalHandler != null) { appendText(); _lexicalHandler.endCDATA(); } else if (! _isCoalescing) appendText(); } /** * Ignores content to the ']]>' */ private void parseIgnore() throws IOException, SAXException { int ch = read(); while (ch >= 0) { if (ch != ']') { ch = read(); } else if ((ch = read()) != ']') { } else if ((ch = read()) == '>') return; } } private int parseContentSpec(QElementDef def, int ch) throws IOException, SAXException { ch = expandPE(ch); if (XmlChar.isNameStart(ch)) { ch = _reader.parseName(_text, ch); String name = _text.toString(); if (name.equals("EMPTY")) { def._content = "EMPTY"; return ch; } else if (name.equals("ANY")) { def._content = "ANY"; return ch; } else throw error(L.l("expected EMPTY or ANY at `{0}'", name)); } else if (ch != '(') { throw error(L.l("expected grammar definition starting with '(' at {0}. <!ELEMENT> definitions have the syntax <!ELEMENT name - - (grammar)>", badChar(ch))); } else { QContentParticle cp = new QContentParticle(); def._content = cp; return parseContentParticle(cp, true); } } /** * Parses a content-particle, i.e. a grammer particle in the DTD * regexp. */ private int parseContentParticle(QContentParticle cp, boolean isTop) throws IOException, SAXException { boolean hasCdata = false; cp._separator = 0; cp._repeat = 0; int ch; ch = expandPE(_reader.read()); for (; ch != -1; ch = expandPE(ch)) { if (ch == '(') { QContentParticle child = new QContentParticle(); cp.addChild(child); ch = parseContentParticle(child, false); } else if (XmlChar.isNameStart(ch)) { ch = _reader.parseName(_text, ch); cp.addChild(_text.toString()); } else if (ch == '#') { ch = _reader.parseName(_text, _reader.read()); String name = _text.toString(); if (_strictXml && cp._children.size() != 0) throw error(L.l("`#{0}' must occur first", name)); if (_strictXml && ! isTop) throw error(L.l("`#{0}' may only occur at top level", name)); if (name.equals("PCDATA")) cp.addChild("#PCDATA"); else throw error(L.l("illegal content particle at `#{0}'", name)); hasCdata = true; } else throw error(L.l("expected content particle at {0}", badChar(ch))); ch = expandPE(ch); if (ch == '?' || ch == '*' || ch == '+') { Object child = cp.getChild(cp.getChildSize() - 1); if (child instanceof QContentParticle) { QContentParticle cpChild = (QContentParticle) child; cpChild._repeat = ch; } else { QContentParticle cpChild = new QContentParticle(); cpChild.addChild(child); cpChild._repeat = ch; cp.setChild(cp.getChildSize() - 1, cpChild); } ch = expandPE(_reader.read()); } if (ch == ')') break; else if (cp._separator == 0) { if (ch == '|') cp._separator = ch; else if (hasCdata && _strictXml) throw error(L.l("#PCDATA must be separated by `|' at {0}", badChar(ch))); else if (ch == ',') cp._separator = ch; else if (! _strictXml && ch =='&') cp._separator = ch; else throw error(L.l("expected separator at {0}", badChar(ch))); ch = _reader.read(); } else if (ch != cp._separator) throw error(L.l("expected `{0}' at {1}", "" + (char) cp._separator, badChar(ch))); else ch = _reader.read(); } ch = expandPE(_reader.read()); if (_strictXml && hasCdata && (ch == '+' || ch == '?')) throw error(L.l("pcdata clause can not have {0}", badChar(ch))); else if (ch == '*' || ch == '+' || ch == '?') { cp._repeat = ch; return _reader.read(); } else return ch; } private int expandPE(int ch) throws IOException, SAXException { ch = skipWhitespace(ch); while (ch == '%') { parsePEReference(); ch = skipWhitespace(_reader.read()); } return ch; } /** * Parses a PE reference %foo; and inserts the macro text to the input * stream. */ private void parsePEReference() throws IOException, SAXException { int ch = _reader.parseName(_buf, _reader.read()); if (ch != ';') throw error(L.l("`%{0};' expects `;' at {1}. Parameter entities have a `%name;' syntax.", _buf, badChar(ch))); addPEReference(_text, _buf.toString()); } /** * Expands the macro value of a PE reference. */ private void addPEReference(CharBuffer value, String name) throws IOException, SAXException { QEntity entity = _dtd.getParameterEntity(name); if (entity == null && ! _dtd.isExternal()) throw error(L.l("`%{0};' is an unknown parameter entity. Parameter entities must be defined in an <!ENTITY> declaration before use.", name)); else if (entity != null && entity._value != null) { setMacro(entity._value); } else if (entity != null && entity.getSystemId() != null) { pushInclude(entity.getPublicId(), entity.getSystemId()); } else { value.append("%"); value.append(name); value.append(";"); } } /** * <!ELEMENT name contentspec> */ private void parseElementDecl(QDocumentType doctype) throws IOException, SAXException { int ch = skipWhitespace(_reader.read()); ch = _reader.parseName(_text, ch); String name = _text.toString(); ch = skipWhitespace(ch); QElementDef def = _dtd.addElement(name); def.setLocation(getSystemId(), getFilename(), getLine(), getColumn()); boolean needsStartTag = true; boolean needsEndTag = true; if (_optionalTags && (ch == 'O' || ch == '-')) { needsStartTag = ch == '-'; ch = skipWhitespace(ch); if (ch == '0') needsEndTag = false; else if (ch == '-') needsEndTag = true; else throw error(L.l("unknown short tag")); } ch = parseContentSpec(def, ch); ch = skipWhitespace(ch); if (ch != '>') throw error(L.l("`<!ELEMENT' must close with `>' at {0}", badChar(ch))); } private static String toAttrDefault(CharBuffer text) { for (int i = 0; i < text.length(); i++) { int ch = text.charAt(i); if (ch == '"') { text.delete(i, i + 1); text.insert(i, """); i--; } else if (ch == '\'') { text.delete(i, i + 1); text.insert(i, "'"); i--; } } return text.toString(); } /** * <!ATTLIST name (attr type def)*> */ private void parseAttlistDecl(QDocumentType doctype) throws IOException, SAXException { int ch = skipWhitespace(_reader.read()); ch = _reader.parseName(_text, ch); String name = _text.toString(); ch = skipWhitespace(ch); QElementDef def = _dtd.addElement(name); while (XmlChar.isNameStart((ch = expandPE(ch)))) { ch = _reader.parseName(_text, ch); String attrName = _text.toString();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?