xmlparser.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,651 行 · 第 1/5 页

JAVA
2,651
字号
      String attrType = null;      ArrayList<String> enumeration = null;      ch = expandPE(ch);      if (ch == '(') {	attrType = "#ENUM";	enumeration = new ArrayList<String>();	do {	  ch = expandPE(_reader.read());	  ch = parseNameToken(_text, ch);	  enumeration.add(_text.toString());	  ch = expandPE(ch);	} while (ch == '|');	if (ch != ')')	  throw error(L.l("expected `{0}' at {1}.  <!ATTRLIST> enumerations definitions are enclosed in '(' ... ')'.", ")", badChar(ch)));	ch = _reader.read();      }      else {	ch = _reader.parseName(_text, ch);	attrType = _text.toString();	if (attrType.equals("NOTATION")) {	  enumeration = new ArrayList<String>();	  ch = expandPE(ch);	  if (ch != '(')            throw error(L.l("expected `{0}' at {1}", "(", badChar(ch)));	  do {	    ch = expandPE(_reader.read());	    ch = _reader.parseName(_text, ch);	    enumeration.add(_text.toString());	    ch = expandPE(ch);	  } while (ch == '|');	  if (ch != ')')	    throw error(L.l("expected `{0}' at {1}", ")", badChar(ch)));	  ch = _reader.read();	}	else if (_attrTypes.get(attrType) != null) {	}        else	  throw error(L.l("expected attribute type at `{0}'", attrType));      }      ch = skipWhitespace(ch);      String qualifier = null;      String attrDefault = null;      if (ch == '#') {	ch = _reader.parseName(_text, _reader.read());	qualifier = "#" + _text.toString();	if (qualifier.equals("#IMPLIED")) {	}	else if (qualifier.equals("#REQUIRED")) {	}	else if (qualifier.equals("#FIXED")) {	  ch = skipWhitespace(ch);	  ch = parseValue(_text, ch, false);	  attrDefault = _text.toString();	} else	  throw error(L.l("expected attribute default at `{0}'",                      qualifier));      }      else if (ch != '>') {	ch = parseValue(_text, ch, false);	attrDefault = _text.toString();      }      def.addAttribute(attrName, attrType, enumeration,                        qualifier, attrDefault);      if (attrType != null && attrType.equals("ID"))	doctype.setElementId(name, attrName);      ch = skipWhitespace(ch);    }    if (ch != '>')      throw error(L.l("expected `{0}' at {1}", ">", badChar(ch)));  }  /**   * <!NOTATION name systemId publicId>   */  private void parseNotationDecl(QDocumentType doctype)    throws IOException, SAXException  {    int ch = skipWhitespace(_reader.read());    ch = _reader.parseName(_text, ch);    String name = _text.toString();    ch = skipWhitespace(ch);    ch = _reader.parseName(_text, ch);    String key = _text.toString();    ch = skipWhitespace(ch);    ch = parseValue(_text, ch, false);    String id = _text.toString();    ch = skipWhitespace(ch);    QNotation notation;    if (key.equals("PUBLIC")) {      String systemId = null;      if (ch == '"' || ch == '\'') {	ch = parseValue(_text, ch, false);	ch = skipWhitespace(ch);	systemId = _text.toString();      }      notation = new QNotation(name, id, systemId);      notation._owner = doctype._owner;      notation.setLocation(getSystemId(), getFilename(), getLine(), getColumn());    }    else if (key.equals("SYSTEM")) {      notation = new QNotation(name, null, id);      notation._owner = doctype._owner;      notation.setLocation(getSystemId(), getFilename(), getLine(), getColumn());    }    else      throw error(L.l("expected PUBLIC or SYSTEM at `{0}'", key));        doctype.addNotation(notation);    doctype.appendChild(notation);    if (ch != '>')      throw error(L.l("expected `{0}' at {1}", ">", badChar(ch)));  }  /**   * externalID ::= PUBLIC publicId systemId   *            ::= SYSTEM systemId   */  private int parseExternalID(int ch)    throws IOException, SAXException  {    ch = _reader.parseName(_text, ch);    String key = _text.toString();    ch = skipWhitespace(ch);    _extSystemId = null;    _extPublicId = null;    if (key.equals("PUBLIC") || _forgiving && key.equalsIgnoreCase("public")) {      ch = parseValue(_text, ch, false);      _extPublicId = _text.toString();      ch = skipWhitespace(ch);      if (_extPublicId.indexOf('&') > 0)	throw error(L.l("Illegal character '&' in PUBLIC identifier '{0}'",			_extPublicId));      ch = parseValue(_text, ch, false);      ch = skipWhitespace(ch);      _extSystemId = _text.toString();    }    else if (key.equals("SYSTEM") ||	     _forgiving && key.equalsIgnoreCase("system")) {      ch = parseValue(_text, ch, false);      _extSystemId = _text.toString();    }    else      throw error(L.l("expected PUBLIC or SYSTEM at `{0}'", key));    return ch;  }  /**   * <!ENTITY name systemId publicId>   */  private void parseEntityDecl(QDocumentType doctype)    throws IOException, SAXException  {    int ch = skipWhitespace(_reader.read());    boolean isPe = ch == '%';    if (isPe)      ch = skipWhitespace(_reader.read());    ch = _reader.parseName(_text, ch);    String name = _text.toString();    ch = skipWhitespace(ch);    QEntity entity;    if (ch == '"' || ch == '\'') {      ch = parseValue(_text, ch, false);            entity = new QEntity(name, _text.toString(), null, null);      entity._owner = doctype._owner;      entity.setLocation(getSystemId(), getFilename(), getLine(), getColumn());    }    else {      ch = parseExternalID(ch);      entity = new QEntity(name, null, _extPublicId, _extSystemId);      entity._owner = doctype._owner;      entity.setLocation(getSystemId(), getFilename(), getLine(), getColumn());      ch = skipWhitespace(ch);      if (! isPe && XmlChar.isNameStart(ch)) {	ch = _reader.parseName(_text, ch);	String key = _text.toString();	if (key.equals("NDATA")) {	  ch = skipWhitespace(ch);	  ch = _reader.parseName(_text, ch);	  String ndata = _text.toString();	  entity._ndata = ndata;	} else	  throw error(L.l("expected `NDATA' at `{0}'", key));      }    }          entity._isPe = isPe;    if (isPe)      doctype.addParameterEntity(entity);    else      doctype.addEntity(entity);    doctype.appendChild(entity);    ch = skipWhitespace(ch);    if (ch != '>')      throw error(L.l("expected `>' at {0}", badChar(ch)));  }  private boolean isWhitespace(int ch)  {    return ch <= 0x20 && (ch == 0x20 || ch == 0x9 || ch == 0xa || ch == 0xd);  }  private boolean isChar(int ch)  {    return (ch >= 0x20 && ch <= 0xd7ff ||	    ch == 0x9 ||	    ch == 0xa ||	    ch == 0xd ||	    ch >= 0xe000 && ch <= 0xfffd);  }  /**   * Returns the hex representation of a byte.   */  private static String hex(int value)  {    CharBuffer cb = CharBuffer.allocate();    for (int b = 3; b >= 0; b--) {      int v = (value >> (4 * b)) & 0xf;      if (v < 10)	cb.append((char) (v + '0'));      else	cb.append((char) (v - 10 + 'a'));    }    return cb.close();  }  /**   * Returns the current filename.   */  public String getFilename()  {    return _filename;  }  /**   * Returns the current line.   */  public int getLine()  {    return _line;  }    /**   * Returns the current column.   */  private int getColumn()  {    return 0;  }  /**   * Returns the opening line of the current node.   */  int getNodeLine()  {    if (_elementTop > 0)      return _elementLines[_elementTop - 1];    else      return 1;  }  /**   * Returns the current public id being read.   */  public String getPublicId()  {    if (_reader != null)      return _reader.getPublicId();    else      return _publicId;  }    /**   * Returns the current system id being read.   */  public String getSystemId()  {    if (_reader != null)      return _reader.getSystemId();    else if (_systemId != null)      return _systemId;    else      return _filename;  }  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 >= _textCapacity) {      appendText();    }    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 appendText()    throws IOException, SAXException  {    if (_textLength > 0) {      if (_activeNode == DOC_NAME) {        if (_isJspText) {          _contentHandler.characters(_textBuffer, 0, _textLength);        }        else if (_isIgnorableWhitespace) {        }        else if (_strictXml)          throw error(L.l("expected top element at `{0}'",                          new String(_textBuffer, 0, _textLength)));        else {          addChild(TEXT_NAME);          _contentHandler.characters(_textBuffer, 0, _textLength);        }      }      else if (_isJspText) {        _contentHandler.characters(_textBuffer, 0, _textLength);      }      else if (_isIgnorableWhitespace) {        if (_isHtml)          _contentHandler.characters(_textBuffer, 0, _textLength);        else          _contentHandler.ignorableWhitespace(_textBuffer, 0, _textLength);      }      else if (_strictXml && ! _isIgnorableWhitespace && _activeNode == DOC_NAME) {      }      else {        if (_isJspText) {        }        else if (_isIgnorableWhitespace)          addChild(WHITESPACE_NAME);        else          addChild(TEXT_NAME);        _contentHandler.characters(_textBuffer, 0, _textLength);      }              _textLength = 0;      _isIgnorableWhitespace = true;    }  }  private void addElement(String child, boolean isEmpty,                          QAttributes attributes,                          NamespaceMap oldNamespace)    throws IOException, SAXException  {    _text.clear();    _text.append(child);    addElement(_policy.getName(_text), isEmpty, attributes, oldNamespace);  }    /**   * Adds an element as a child of the current tree.  Some   * DTDs, like HTML, will push additional nodes to make   * the tree work, e.g. the body tag.   *   * @param child the new child to be added.   * @param isEmpty true if the tag is already closed.   */  private void addElement(QName child, boolean isEmpty,                          QAttributes attributes, NamespaceMap oldNamespace)    throws IOException, SAXException  {    if (! _doResinInclude) {    }    else if (child.getName() == "include" &&	     child.getNamespaceURI() == "http://caucho.com/ns/resin/core" ||	     child.getName() == "resin:include") {      if (! isEmpty)        throw error(L.l("resin:include must be an empty tag"));            handleResinInclude();      return;    }    else if (child.getName() == "include-directory" &&	     child.getNamespaceURI() == "http://caucho.com/ns/resin/core" ||	     child.getName() == "resin:include-directory") {      if (! isEmpty)        throw error(L.l("resin:include-directory must be an empty tag"));            handleResinIncludeDirectory();      return;    }    if (_activeNode == DOC_NAME && _hasTopElement && _strictXml)      throw error(L.l("expected a single top-level element at `{0}'",                      child.getName()));    _hasTopElement = true;        String childURI = child.getNamespaceURI();    String childLocal = child.getLocalName();    if (childURI == null) {      childURI = "";      if (_isNamespaceAware)	childLocal = child.getName();      else	childLocal = "";    }    while (true) {      int action = _policy.openAction(this, _activeNode, child);      switch (action) {      case Policy.IGNORE:	return;      case Policy.PUSH:	//if (dbg.canWrite())	//  dbg.println("<" + child.getNodeName() + ">");        if (_contentHandler instanceof DOMBuilder)          ((DOMBuilder) _contentHandler).startElement(child, attributes);	else {	  _contentHandler.startElement(childURI,				       childLocal,				       child.getName(),				       attributes);	}                if (isEmpty) {	  _contentHandler.endElement(childURI,				     childLocal,				     child.getName());          popNamespaces(oldNamespace);        }        else {          if (_elementTop == _elementNames.length) {            int len = _elementNames.length;            QName []names = new QName[2 * len];            NamespaceMap []newNamespaces = new NamespaceMap[2 * len];            int []lines = new int[2 * len];            System.arraycopy(_elementNames, 0, names, 0, len);            System.arraycopy(_elementLines, 0, lines, 0, len);            System.arraycopy(_namespaces, 0, newNamespaces, 0, len);            _elementNames = names;            _elementLines = lines;            _namespaces = newNamespaces;          }          _namespaces[_elementTop] = oldNamespace;          _elementLines[_elementTop] = getLine();          _elementNames[_elementTop] = _activeNode;          _elementTop++;	  _activeNode = child;	  _isTagStart = true;        }	return;      case Policy.PUSH_EMPT

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?