treewalker.java

来自「java jdk 1.4的源码」· Java 代码 · 共 524 行 · 第 1/2 页

JAVA
524
字号
          {            nextNode = null;            break;          }        }      }      pos = nextNode;    }    this.m_contentHandler.endDocument();  }  /** Flag indicating whether following text to be processed is raw text          */  boolean nextIsRaw = false;    /**   * Optimized dispatch of characters.   */  private final void dispatachChars(Node node)     throws org.xml.sax.SAXException  {    if(m_contentHandler instanceof org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.CharacterNodeHandler)    {      ((org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.CharacterNodeHandler)m_contentHandler).characters(node);    }    else    {      String data = ((Text) node).getData();      this.m_contentHandler.characters(data.toCharArray(), 0, data.length());    }  }  /**   * Start processing given node   *   *   * @param node Node to process   *   * @throws org.xml.sax.SAXException   */  protected void startNode(Node node) throws org.xml.sax.SAXException  {    if (m_contentHandler instanceof NodeConsumer)    {      ((NodeConsumer) m_contentHandler).setOriginatingNode(node);    }                                if (node instanceof Locator)                {                        Locator loc = (Locator)node;                        m_locator.setColumnNumber(loc.getColumnNumber());                        m_locator.setLineNumber(loc.getLineNumber());                        m_locator.setPublicId(loc.getPublicId());                        m_locator.setSystemId(loc.getSystemId());                }                else                {                        m_locator.setColumnNumber(0);      m_locator.setLineNumber(0);                }    switch (node.getNodeType())    {    case Node.COMMENT_NODE :    {      String data = ((Comment) node).getData();      if (m_contentHandler instanceof LexicalHandler)      {        LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);        lh.comment(data.toCharArray(), 0, data.length());      }    }    break;    case Node.DOCUMENT_FRAGMENT_NODE :      // ??;      break;    case Node.DOCUMENT_NODE :          break;    case Node.ELEMENT_NODE :      NamedNodeMap atts = ((Element) node).getAttributes();      int nAttrs = atts.getLength();      // System.out.println("TreeWalker#startNode: "+node.getNodeName());      for (int i = 0; i < nAttrs; i++)      {        Node attr = atts.item(i);        String attrName = attr.getNodeName();        // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());        if (attrName.equals("xmlns") || attrName.startsWith("xmlns:"))        {          // System.out.println("TreeWalker#startNode: attr["+i+"] = "+attrName+", "+attr.getNodeValue());          int index;          // Use "" instead of null, as Xerces likes "" for the           // name of the default namespace.  Fix attributed           // to "Steven Murray" <smurray@ebt.com>.          String prefix = (index = attrName.indexOf(":")) < 0                          ? "" : attrName.substring(index + 1);          this.m_contentHandler.startPrefixMapping(prefix,                                                   attr.getNodeValue());        }              }      // System.out.println("m_dh.getNamespaceOfNode(node): "+m_dh.getNamespaceOfNode(node));      // System.out.println("m_dh.getLocalNameOfNode(node): "+m_dh.getLocalNameOfNode(node));      String ns = m_dh.getNamespaceOfNode(node);      if(null == ns)        ns = "";      this.m_contentHandler.startElement(ns,                                         m_dh.getLocalNameOfNode(node),                                         node.getNodeName(),                                         new AttList(atts, m_dh));      break;    case Node.PROCESSING_INSTRUCTION_NODE :    {      ProcessingInstruction pi = (ProcessingInstruction) node;      String name = pi.getNodeName();      // String data = pi.getData();      if (name.equals("xslt-next-is-raw"))      {        nextIsRaw = true;      }      else      {        this.m_contentHandler.processingInstruction(pi.getNodeName(),                                                    pi.getData());      }    }    break;    case Node.CDATA_SECTION_NODE :    {      boolean isLexH = (m_contentHandler instanceof LexicalHandler);      LexicalHandler lh = isLexH                          ? ((LexicalHandler) this.m_contentHandler) : null;      if (isLexH)      {        lh.startCDATA();      }            dispatachChars(node);      {        if (isLexH)        {          lh.endCDATA();        }      }    }    break;    case Node.TEXT_NODE :    {      //String data = ((Text) node).getData();      if (nextIsRaw)      {        nextIsRaw = false;        m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_DISABLE_OUTPUT_ESCAPING, "");        dispatachChars(node);        m_contentHandler.processingInstruction(javax.xml.transform.Result.PI_ENABLE_OUTPUT_ESCAPING, "");      }      else      {        dispatachChars(node);      }    }    break;    case Node.ENTITY_REFERENCE_NODE :    {      EntityReference eref = (EntityReference) node;      if (m_contentHandler instanceof LexicalHandler)      {        ((LexicalHandler) this.m_contentHandler).startEntity(          eref.getNodeName());      }      else      {        // warning("Can not output entity to a pure SAX ContentHandler");      }    }    break;    default :    }  }  /**   * End processing of given node    *   *   * @param node Node we just finished processing   *   * @throws org.xml.sax.SAXException   */  protected void endNode(Node node) throws org.xml.sax.SAXException  {    switch (node.getNodeType())    {    case Node.DOCUMENT_NODE :      break;          case Node.ELEMENT_NODE :      String ns = m_dh.getNamespaceOfNode(node);      if(null == ns)        ns = "";      this.m_contentHandler.endElement(ns,                                         m_dh.getLocalNameOfNode(node),                                         node.getNodeName());      NamedNodeMap atts = ((Element) node).getAttributes();      int nAttrs = atts.getLength();      for (int i = 0; i < nAttrs; i++)      {        Node attr = atts.item(i);        String attrName = attr.getNodeName();        if (attrName.equals("xmlns") || attrName.startsWith("xmlns:"))        {          int index;          // Use "" instead of null, as Xerces likes "" for the           // name of the default namespace.  Fix attributed           // to "Steven Murray" <smurray@ebt.com>.          String prefix = (index = attrName.indexOf(":")) < 0                          ? "" : attrName.substring(index + 1);          this.m_contentHandler.endPrefixMapping(prefix);        }      }      break;    case Node.CDATA_SECTION_NODE :      break;    case Node.ENTITY_REFERENCE_NODE :    {      EntityReference eref = (EntityReference) node;      if (m_contentHandler instanceof LexicalHandler)      {        LexicalHandler lh = ((LexicalHandler) this.m_contentHandler);        lh.endEntity(eref.getNodeName());      }    }    break;    default :    }  }}  //TreeWalker

⌨️ 快捷键说明

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