xmlprinter.java

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

JAVA
1,723
字号
                  attr.getLocalName(),                  attr.getNodeName(),                  attr.getNodeValue());      }            for (Node child = node.getFirstChild();           child != null;           child = child.getNextSibling()) {        printNode(child);      }      endElement(elt.getNamespaceURI(), elt.getLocalName(), elt.getNodeName());      break;    }        case Node.TEXT_NODE:    case Node.CDATA_SECTION_NODE:    {      CharacterData text = (CharacterData) node;      text(text.getData());      break;    }        case Node.COMMENT_NODE:    {      Comment comment = (Comment) node;      comment(comment.getData());      break;    }        case Node.PROCESSING_INSTRUCTION_NODE:    {      ProcessingInstruction pi = (ProcessingInstruction) node;      processingInstruction(pi.getNodeName(), pi.getData());      break;    }    }  }  WriteStream getStream()  {    return _os;  }  public void startDocument(Document document)    throws IOException  {    _currentDocument = document;    startDocument();  }  /**   * Callback when the document starts printing.   */  public void startDocument()    throws IOException  {    _isTop = true;  }  /**   * Callback when the document completes   */  public void endDocument()    throws IOException  {    if (_isPretty && _lastTextChar < SPACE)      println();    flush();  }  /**   * Sets the locator.   */  public void setDocumentLocator(Locator locator)  {    _locator = (ExtendedLocator) locator;  }    /**   * Sets the current location.   *   * @param filename the source filename   * @param line the source line   * @param column the source column   */  public void setLocation(String filename, int line, int column)  {    _srcFilename = filename;    _srcLine = line;  }  /**   * Called at the start of a new element.   *   * @param url the namespace url   * @param localName the local name   * @param qName the qualified name   */  public void startElement(String url, String localName, String qName)    throws IOException  {    if (_isText)      return;        if (_isAutomaticMethod) {      _isHtml = (qName.equalsIgnoreCase("html") &&                 (url == null || url.equals("")));            if (_isAutomaticPretty)        _isPretty = _isHtml;      _isAutomaticMethod = false;    }    if (_isTop)      printHeader(qName);    if (_currentElement != null)      completeOpenTag();    _attributeNames.clear();    _attributeValues.clear();    if (_isHtml && _verbatimTags.get(qName.toLowerCase()) != null)      _inVerbatim = true;    if (_isPretty && _preCount <= 0)      printPrettyStart(qName);    if (_lineMap == null) {    }    else if (_locator != null) {      _lineMap.add(_locator.getFilename(), _locator.getLineNumber(), _line);    }    else if (_srcFilename != null)      _lineMap.add(_srcFilename, _srcLine, _line);        print('<');    print(qName);    _currentElement = qName;    _lastTextChar = NULL_SPACE;  }  /**   * Prints the header, if necessary.   *   * @param top name of the top element   */  public void printHeader(String top)    throws IOException  {    if (! _isTop)      return;        _isTop = false;        String encoding = _encoding;    if (encoding != null && encoding.equalsIgnoreCase("UTF-16"))      print('\ufeff');        if (_isHtml) {      double dVersion = 4.0;            if (_version == null || _version.compareTo("4.0") >= 0) {      }      else {        dVersion = 3.2;      }      if (_systemId != null || _publicId != null)        printDoctype("html");      if (encoding == null || encoding.equalsIgnoreCase("ISO-8859-1")) 	// _entities = Latin1Entities.create(dVersion);        _entities = HtmlEntities.create(dVersion);      else if (encoding.equalsIgnoreCase("US-ASCII"))        _entities = HtmlEntities.create(dVersion);      else        _entities = OtherEntities.create(dVersion);    }    else {      if (_printDeclaration) {        String version = _version;        if (version == null)          version = "1.0";        print("<?xml version=\"");        print(version);        print("\"");        if (encoding == null ||            encoding.equals("") ||            encoding.equalsIgnoreCase("US-ASCII")) {        }        else          print(" encoding=\"" + encoding + "\"");        if (_standalone != null &&            (_standalone.equals("true") || _standalone.equals("yes")))          print(" standalone=\"yes\"");        println("?>");      }      printDoctype(top);      if (encoding == null ||          encoding.equalsIgnoreCase("US-ASCII") ||	  encoding.equalsIgnoreCase("ISO-8859-1"))        _entities = XmlLatin1Entities.create();      else        _entities = XmlEntities.create();    }        _lastTextChar = NEWLINE;  }  /**   * Prints the doctype declaration   *   * @param topElt name of the top element   */  private void printDoctype(String topElt)    throws IOException  {    if (_publicId != null && _systemId != null)      println("<!DOCTYPE " + topElt + " PUBLIC \"" + _publicId + "\" \"" +              _systemId + "\">");    else if (_publicId != null)      println("<!DOCTYPE " + topElt + " PUBLIC \"" + _publicId + "\">");    else if (_systemId != null)      println("<!DOCTYPE " + topElt + " SYSTEM \"" + _systemId + "\">");    else if (_currentDocument instanceof QDocument) {      QDocumentType dtd = (QDocumentType) _currentDocument.getDoctype();      if (dtd != null && dtd.getName() != null && dtd.getParentNode() == null) {        dtd.print(this);        println();      }    }  }  public void startPrefixMapping(String prefix, String uri)    throws IOException  {  }    public void endPrefixMapping(String prefix)    throws IOException  {  }  /**   * Pretty printing for a start tag.   *   * @param qName the name of the element   */  private void printPrettyStart(String qName)    throws IOException  {    int code = _isHtml ? _prettyMap.get(qName.toLowerCase()) : -1;    if (code == NO_PRETTY) {      if (_lastTextChar == OMITTED_NEWLINE)        println();      else if (_lastTextChar == OMITTED_SPACE)        print(' ');    }    else if (code != INLINE && _lastTextChar < WHITESPACE) {      if (_lastTextChar != NEWLINE)        println();      for (int i = 0; i < _indent; i++)        print(' ');    }    else if (code == INLINE && _lastTextChar < WHITESPACE) {      if (_lastTextChar == OMITTED_NEWLINE)        println();      else if (_lastTextChar == OMITTED_SPACE)        print(' ');    }    if (! _isHtml || code < 0) {      _indent += 2;    }    if (code == PRE) {      _preCount++;      _lastTextChar = 'a';    }    else if (code == NO_PRETTY || code == INLINE)      _lastTextChar = 'a';    else      _lastTextChar = NULL_SPACE;  }  /**   * Prints an attribute   *   * @param uri namespace uri   * @param localName localname of the attribute   * @param qName qualified name of the attribute   * @param value value of the attribute.   */  public void attribute(String uri, String localName, String qName,                        String value)    throws IOException  {    if (_isText)      return;    if (_currentElement != null) {    }    else if (qName.equals("encoding")) {      _encoding = value;      return;    }    else if (qName.startsWith("xmlns")) {    }    else      throw new IOException(L.l("attribute `{0}' outside element.", qName));        qName = qName.intern();    if (qName.startsWith("xmlns")) {      if (localName == null)        localName = "";      if (_isHtml && localName.equals("") && value.equals(""))        return;      _namespace.put(localName, value);      if (_prefixList == null)        _prefixList = new ArrayList<String>();      if (! _prefixList.contains(localName))        _prefixList.add(localName);      return;    }    else if (qName.equals("xtp:jsp-attribute")) {      _attributeNames.add("<%= " + value + "%>");      _attributeValues.add(null);      return;    }        if (_isHtml && ! _hasMetaContentType &&        _currentElement.equals("meta") &&        qName.equalsIgnoreCase("http-equiv") &&        value.equalsIgnoreCase("content-type")) {      _hasMetaContentType = true;    }    for (int i = 0; i < _attributeNames.size(); i++) {      String oldName = _attributeNames.get(i);      if (oldName == qName) {        _attributeValues.set(i, value);        return;      }    }    if (qName == null || qName.equals(""))      throw new NullPointerException();    _attributeNames.add(qName);    _attributeValues.add(value);  }  /**   * Complete printing of the attributes when the open tag completes.   */  public boolean finishAttributes()    throws IOException  {    if (_currentElement == null)      return false;    for (int i = 0; i < _attributeNames.size(); i++) {      String qName = _attributeNames.get(i);      String value = _attributeValues.get(i);            if (_isHtml &&          _booleanAttrs.get(qName.toLowerCase()) != null &&          (value == null || value.equals("") || value.equals(qName))) {        print(' ');        print(qName);      }      else {        print(' ');        print(qName);        if (value != null) {          print("=\"");          if (! _escapeText || _inVerbatim)            print(value);          /*          else if (isHtml && isURIAttribute(currentElement, qName)) {            int len = value.length();            int offset = 0;            while (len > abuf.length) {              value.getChars(offset, offset + abuf.length, abuf, 0);              entities.printURIAttr(this, abuf, 0, abuf.length);              len -= abuf.length;              offset += abuf.length;            }                        value.getChars(offset, offset + len, abuf, 0);            entities.printURIAttr(this, abuf, 0, len);          }          */          else {            int len = value.length();            int offset = 0;            while (len > _abuf.length) {              value.getChars(offset, offset + _abuf.length, _abuf, 0);              _entities.printText(this, _abuf, 0, _abuf.length, true);              len -= _abuf.length;              offset += _abuf.length;            }                        value.getChars(offset, offset + len, _abuf, 0);            _entities.printText(this, _abuf, 0, len, true);          }          print('\"');        }        else if (! _isHtml) {          print("=\"\"");        }      }    }    if (_prefixList != null && _prefixList.size() > 0) {      for (int i = 0; i < _prefixList.size(); i++) {        String prefix = _prefixList.get(i);        String url = _namespace.get(prefix);        if (prefix.equals("")) {          print(" xmlns=\"");          print(url);          print('\"');        }        else if (prefix.startsWith("xmlns")) {          print(" ");          print(prefix);          print("=\"");          print(url);          print('\"');        }        else {          print(" xmlns:");          print(prefix);          print("=\"");          print(url);          print('\"');        }      }            _prefixList.clear();      _namespace.clear();    }        _currentElement = null;    // lastTextChar = NULL_SPACE;        return true;  }  /**   * Prints the end tag of an element   *   * @param uri the namespace uri of the element   * @param localName the localname of the element tag   * @param qName qualified name of the element   */  public void endElement(String uri, String localName, String qName)    throws IOException  {    if (_isText)      return;    String normalName = _isHtml ? qName.toLowerCase() : qName;        if (_isHtml && _verbatimTags.get(normalName) != null)      _inVerbatim = false;    int prevTextChar = _lastTextChar;    boolean isEmpty = _currentElement != null;    if (_currentElement != null)      finishAttributes();          if (! _isHtml || _hasMetaContentType) {    }    else if (normalName.equals("head")) {      if (isEmpty)        print(">");      isEmpty = false;      printMetaContentType();      _currentElement = null;    }          if (isEmpty) {      if (_isHtml && _empties.get(normalName) >= 0)        print(">");      else if (prevTextChar <= OMITTED) {        print(">");        printPrettyEnd(qName);        print("</");        print(qName);        print(">");        return;      }      else if (_isHtml) {        print("></");        print(qName);        print(">");      }      else {        print("/>");      }      if (_isPretty)        closePretty(qName);    }    else if (_isHtml && _empties.get(normalName) >= 0 &&             ! normalName.equals("p")) {      if (_isPretty)        closePretty(qName);    }    else if (_isPretty) {      printPrettyEnd(qName);      print("</");      print(qName);      print(">");    }    else {      print("</");      print(qName);      print(">");    }        _currentElement = null;  }  /**   * Handle pretty printing at an end tag.   */  private void printPrettyEnd(String qName)    throws IOException  {    int code = _isHtml ? _prettyMap.get(qName.toLowerCase()) : -1;    if (code == PRE) {      _preCount--;      _lastTextChar = NULL_SPACE;      return;    }    else if (_preCount > 0) {      return;    }    else if (code == NO_PRETTY) {      if (_lastTextChar <= OMITTED)        println();      _lastTextChar = 'a';      // indent -= 2;      return;    }    else if (code == INLINE) {      _lastTextChar = NULL_SPACE;      return;    }

⌨️ 快捷键说明

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