dom2dtm.java

来自「JAVA 所有包」· Java 代码 · 共 1,764 行 · 第 1/4 页

JAVA
1,764
字号
      }     boolean b = buf.isWhitespace(0, buf.length());      StringBufferPool.free(buf);     return b;    }    return false;  }    /**   * Retrieve the text content of a DOM subtree, appending it into a   * user-supplied FastStringBuffer object. Note that attributes are   * not considered part of the content of an element.   * <p>   * There are open questions regarding whitespace stripping.    * Currently we make no special effort in that regard, since the standard   * DOM doesn't yet provide DTD-based information to distinguish   * whitespace-in-element-context from genuine #PCDATA. Note that we   * should probably also consider xml:space if/when we address this.   * DOM Level 3 may solve the problem for us.   * <p>   * %REVIEW% Actually, since this method operates on the DOM side of the   * fence rather than the DTM side, it SHOULDN'T do   * any special handling. The DOM does what the DOM does; if you want   * DTM-level abstractions, use DTM-level methods.   *   * @param node Node whose subtree is to be walked, gathering the   * contents of all Text or CDATASection nodes.   * @param buf FastStringBuffer into which the contents of the text   * nodes are to be concatenated.   */  protected static void getNodeData(Node node, FastStringBuffer buf)  {    switch (node.getNodeType())    {    case Node.DOCUMENT_FRAGMENT_NODE :    case Node.DOCUMENT_NODE :    case Node.ELEMENT_NODE :    {      for (Node child = node.getFirstChild(); null != child;              child = child.getNextSibling())      {        getNodeData(child, buf);      }    }    break;    case Node.TEXT_NODE :    case Node.CDATA_SECTION_NODE :    case Node.ATTRIBUTE_NODE :	// Never a child but might be our starting node      buf.append(node.getNodeValue());      break;    case Node.PROCESSING_INSTRUCTION_NODE :      // warning(XPATHErrorResources.WG_PARSING_AND_PREPARING);              break;    default :      // ignore      break;    }  }  /**   * Given a node handle, return its DOM-style node name. This will   * include names such as #text or #document.   *   * @param nodeHandle the id of the node.   * @return String Name of this node, which may be an empty string.   * %REVIEW% Document when empty string is possible...   * %REVIEW-COMMENT% It should never be empty, should it?   */  public String getNodeName(int nodeHandle)  {    Node node = getNode(nodeHandle);    // Assume non-null.    return node.getNodeName();  }  /**   * Given a node handle, return the XPath node name.  This should be   * the name as described by the XPath data model, NOT the DOM-style   * name.   *   * @param nodeHandle the id of the node.   * @return String Name of this node, which may be an empty string.   */  public String getNodeNameX(int nodeHandle)  {    String name;    short type = getNodeType(nodeHandle);    switch (type)    {    case DTM.NAMESPACE_NODE :    {      Node node = getNode(nodeHandle);      // assume not null.      name = node.getNodeName();      if(name.startsWith("xmlns:"))      {        name = QName.getLocalPart(name);      }      else if(name.equals("xmlns"))      {        name = "";      }    }    break;    case DTM.ATTRIBUTE_NODE :    case DTM.ELEMENT_NODE :    case DTM.ENTITY_REFERENCE_NODE :    case DTM.PROCESSING_INSTRUCTION_NODE :    {      Node node = getNode(nodeHandle);      // assume not null.      name = node.getNodeName();    }    break;    default :      name = "";    }    return name;  }  /**   * Given a node handle, return its XPath-style localname.   * (As defined in Namespaces, this is the portion of the name after any   * colon character).   *   * @param nodeHandle the id of the node.   * @return String Local name of this node.   */  public String getLocalName(int nodeHandle)  {    if(JJK_NEWCODE)    {      int id=makeNodeIdentity(nodeHandle);      if(NULL==id) return null;      Node newnode=(Node)m_nodes.elementAt(id);      String newname=newnode.getLocalName();      if (null == newname)      {	// XSLT treats PIs, and possibly other things, as having QNames.	String qname = newnode.getNodeName();	if('#'==qname.charAt(0))	{	  //  Match old default for this function	  // This conversion may or may not be necessary	  newname="";	}	else	{	  int index = qname.indexOf(':');	  newname = (index < 0) ? qname : qname.substring(index + 1);	}      }      return newname;    }    else    {      String name;      short type = getNodeType(nodeHandle);      switch (type)      {      case DTM.ATTRIBUTE_NODE :      case DTM.ELEMENT_NODE :      case DTM.ENTITY_REFERENCE_NODE :      case DTM.NAMESPACE_NODE :      case DTM.PROCESSING_INSTRUCTION_NODE :	{	  Node node = getNode(nodeHandle);	  	  // assume not null.	  name = node.getLocalName();	  	  if (null == name)	  {	    String qname = node.getNodeName();	    int index = qname.indexOf(':');	    	    name = (index < 0) ? qname : qname.substring(index + 1);	  }	}	break;      default :	name = "";      }      return name;    }  }  /**   * Given a namespace handle, return the prefix that the namespace decl is   * mapping.   * Given a node handle, return the prefix used to map to the namespace.   *   * <p> %REVIEW% Are you sure you want "" for no prefix?  </p>   * <p> %REVIEW-COMMENT% I think so... not totally sure. -sb  </p>   *   * @param nodeHandle the id of the node.   * @return String prefix of this node's name, or "" if no explicit   * namespace prefix was given.   */  public String getPrefix(int nodeHandle)  {    String prefix;    short type = getNodeType(nodeHandle);    switch (type)    {    case DTM.NAMESPACE_NODE :    {      Node node = getNode(nodeHandle);      // assume not null.      String qname = node.getNodeName();      int index = qname.indexOf(':');      prefix = (index < 0) ? "" : qname.substring(index + 1);    }    break;    case DTM.ATTRIBUTE_NODE :    case DTM.ELEMENT_NODE :    {      Node node = getNode(nodeHandle);      // assume not null.      String qname = node.getNodeName();      int index = qname.indexOf(':');      prefix = (index < 0) ? "" : qname.substring(0, index);    }    break;    default :      prefix = "";    }    return prefix;  }  /**   * Given a node handle, return its DOM-style namespace URI   * (As defined in Namespaces, this is the declared URI which this node's   * prefix -- or default in lieu thereof -- was mapped to.)   *   * <p>%REVIEW% Null or ""? -sb</p>   *   * @param nodeHandle the id of the node.   * @return String URI value of this node's namespace, or null if no   * namespace was resolved.   */  public String getNamespaceURI(int nodeHandle)  {    if(JJK_NEWCODE)    {      int id=makeNodeIdentity(nodeHandle);      if(id==NULL) return null;      Node node=(Node)m_nodes.elementAt(id);      return node.getNamespaceURI();    }    else    {      String nsuri;      short type = getNodeType(nodeHandle);            switch (type)      {      case DTM.ATTRIBUTE_NODE :      case DTM.ELEMENT_NODE :      case DTM.ENTITY_REFERENCE_NODE :      case DTM.NAMESPACE_NODE :      case DTM.PROCESSING_INSTRUCTION_NODE :	{	  Node node = getNode(nodeHandle);	  	  // assume not null.	  nsuri = node.getNamespaceURI();	  	  // %TBD% Handle DOM1?	}	break;      default :	nsuri = null;      }      return nsuri;    }      }    /** Utility function: Given a DOM Text node, determine whether it is   * logically followed by another Text or CDATASection node. This may   * involve traversing into Entity References.   *    * %REVIEW% DOM Level 3 is expected to add functionality which may    * allow us to retire this.   */  private Node logicalNextDOMTextNode(Node n)  {        Node p=n.getNextSibling();        if(p==null)        {                // Walk out of any EntityReferenceNodes that ended with text                for(n=n.getParentNode();                        n!=null && ENTITY_REFERENCE_NODE == n.getNodeType();                        n=n.getParentNode())                {                        p=n.getNextSibling();                        if(p!=null)                                break;                }        }        n=p;        while(n!=null && ENTITY_REFERENCE_NODE == n.getNodeType())        {                // Walk into any EntityReferenceNodes that start with text                if(n.hasChildNodes())                        n=n.getFirstChild();                else                        n=n.getNextSibling();        }        if(n!=null)        {                // Found a logical next sibling. Is it text?                int ntype=n.getNodeType();                if(TEXT_NODE != ntype && CDATA_SECTION_NODE != ntype)                        n=null;        }        return n;  }  /**   * Given a node handle, return its node value. This is mostly   * as defined by the DOM, but may ignore some conveniences.   * <p>   *   * @param nodeHandle The node id.   * @return String Value of this node, or null if not   * meaningful for this node type.   */  public String getNodeValue(int nodeHandle)  {    // The _type(nodeHandle) call was taking the lion's share of our    // time, and was wrong anyway since it wasn't coverting handle to    // identity. Inlined it.    int type = _exptype(makeNodeIdentity(nodeHandle));    type=(NULL != type) ? getNodeType(nodeHandle) : NULL;        if(TEXT_NODE!=type && CDATA_SECTION_NODE!=type)      return getNode(nodeHandle).getNodeValue();        // If this is a DTM text node, it may be made of multiple DOM text    // nodes -- including navigating into Entity References. DOM2DTM    // records the first node in the sequence and requires that we    // pick up the others when we retrieve the DTM node's value.    //    // %REVIEW% DOM Level 3 is expected to add a "whole text"    // retrieval method which performs this function for us.    Node node = getNode(nodeHandle);    Node n=logicalNextDOMTextNode(node);    if(n==null)      return node.getNodeValue();        FastStringBuffer buf = StringBufferPool.get();        buf.append(node.getNodeValue());    while(n!=null)    {      buf.append(n.getNodeValue());      n=logicalNextDOMTextNode(n);    }    String s = (buf.length() > 0) ? buf.toString() : "";    StringBufferPool.free(buf);    return s;  }  /**   *   A document type declaration information item has the following properties:   *   *     1. [system identifier] The system identifier of the external subset, if   *        it exists. Otherwise this property has no value.   *   * @return the system identifier String object, or null if there is none.   */  public String getDocumentTypeDeclarationSystemIdentifier()  {    Document doc;    if (m_root.getNodeType() == Node.DOCUMENT_NODE)      doc = (Document) m_root;    else      doc = m_root.getOwnerDocument();    if (null != doc)    {      DocumentType dtd = doc.getDoctype();      if (null != dtd)      {        return dtd.getSystemId();      }    }    return null;  }  /**   * Return the public identifier of the external subset,   * normalized as described in 4.2.2 External Entities [XML]. If there is   * no external subset or if it has no public identifier, this property   * has no value.   *   * @return the public identifier String object, or null if there is none.   */  public String getDocumentTypeDeclarationPublicIdentifier()  {    Document doc;    if (m_root.getNodeType() == Node.DOCUMENT_NODE)      doc = (Document) m_root;    else      doc = m_root.getOwnerDocument();    if (null != doc)    {      DocumentType dtd = doc.getDoctype();      if (null != dtd)      {        return dtd.getPublicId();      }    }    return null;  }

⌨️ 快捷键说明

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