sax2dtm.java

来自「JAVA 所有包」· Java 代码 · 共 2,129 行 · 第 1/5 页

JAVA
2,129
字号
  }  /**   * Return this DTM's EntityResolver.   *   * @return null if this model doesn't respond to SAX entity ref events.   */  public EntityResolver getEntityResolver()  {    return this;  }  /**   * Return this DTM's DTDHandler.   *   * @return null if this model doesn't respond to SAX dtd events.   */  public DTDHandler getDTDHandler()  {    return this;  }  /**   * Return this DTM's ErrorHandler.   *   * @return null if this model doesn't respond to SAX error events.   */  public ErrorHandler getErrorHandler()  {    return this;  }  /**   * Return this DTM's DeclHandler.   *   * @return null if this model doesn't respond to SAX Decl events.   */  public DeclHandler getDeclHandler()  {    return this;  }  /**   * @return true iff we're building this model incrementally (eg   * we're partnered with a IncrementalSAXSource) and thus require that the   * transformation and the parse run simultaneously. Guidance to the   * DTMManager.   */  public boolean needsTwoThreads()  {    return null != m_incrementalSAXSource;  }  /**   * Directly call the   * characters method on the passed ContentHandler for the   * string-value of the given node (see http://www.w3.org/TR/xpath#data-model   * for the definition of a node's string-value). Multiple calls to the   * ContentHandler's characters methods may well occur for a single call to   * this method.   *   * @param nodeHandle The node ID.   * @param ch A non-null reference to a ContentHandler.   * @param normalize true if the content should be normalized according to   * the rules for the XPath   * <a href="http://www.w3.org/TR/xpath#function-normalize-space">normalize-space</a>   * function.   *   * @throws SAXException   */  public void dispatchCharactersEvents(int nodeHandle, ContentHandler ch,                                       boolean normalize)          throws SAXException  {    int identity = makeNodeIdentity(nodeHandle);        if (identity == DTM.NULL)      return;        int type = _type(identity);    if (isTextType(type))    {      int dataIndex = m_dataOrQName.elementAt(identity);      int offset = m_data.elementAt(dataIndex);      int length = m_data.elementAt(dataIndex + 1);      if(normalize)        m_chars.sendNormalizedSAXcharacters(ch, offset, length);      else        m_chars.sendSAXcharacters(ch, offset, length);    }    else    {      int firstChild = _firstch(identity);      if (DTM.NULL != firstChild)      {        int offset = -1;        int length = 0;        int startNode = identity;        identity = firstChild;        do {          type = _type(identity);          if (isTextType(type))          {            int dataIndex = _dataOrQName(identity);            if (-1 == offset)            {              offset = m_data.elementAt(dataIndex);            }            length += m_data.elementAt(dataIndex + 1);          }          identity = getNextNodeIdentity(identity);        } while (DTM.NULL != identity && (_parent(identity) >= startNode));        if (length > 0)        {          if(normalize)            m_chars.sendNormalizedSAXcharacters(ch, offset, length);          else            m_chars.sendSAXcharacters(ch, offset, length);        }      }      else if(type != DTM.ELEMENT_NODE)      {        int dataIndex = _dataOrQName(identity);        if (dataIndex < 0)        {          dataIndex = -dataIndex;          dataIndex = m_data.elementAt(dataIndex + 1);        }        String str = m_valuesOrPrefixes.indexToString(dataIndex);          if(normalize)            FastStringBuffer.sendNormalizedSAXcharacters(str.toCharArray(),                                                         0, str.length(), ch);          else            ch.characters(str.toCharArray(), 0, str.length());      }    }  }  /**   * 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)  {    int expandedTypeID = getExpandedTypeID(nodeHandle);    // If just testing nonzero, no need to shift...    int namespaceID = m_expandedNameTable.getNamespaceID(expandedTypeID);                         if (0 == namespaceID)    {      // Don't retrieve name until/unless needed      // String name = m_expandedNameTable.getLocalName(expandedTypeID);      int type = getNodeType(nodeHandle);      if (type == DTM.NAMESPACE_NODE)      {        if (null == m_expandedNameTable.getLocalName(expandedTypeID))          return "xmlns";        else          return "xmlns:" + m_expandedNameTable.getLocalName(expandedTypeID);      }      else if (0 == m_expandedNameTable.getLocalNameID(expandedTypeID))      {        return m_fixednames[type];      }      else        return m_expandedNameTable.getLocalName(expandedTypeID);    }    else    {      int qnameIndex = m_dataOrQName.elementAt(makeNodeIdentity(nodeHandle));      if (qnameIndex < 0)      {        qnameIndex = -qnameIndex;        qnameIndex = m_data.elementAt(qnameIndex);      }      return m_valuesOrPrefixes.indexToString(qnameIndex);    }  }  /**   * 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)  {    int expandedTypeID = getExpandedTypeID(nodeHandle);        int namespaceID = m_expandedNameTable.getNamespaceID(expandedTypeID);                          if (0 == namespaceID)    {      String name = m_expandedNameTable.getLocalName(expandedTypeID);      if (name == null)        return "";      else        return name;    }    else    {      int qnameIndex = m_dataOrQName.elementAt(makeNodeIdentity(nodeHandle));      if (qnameIndex < 0)      {        qnameIndex = -qnameIndex;        qnameIndex = m_data.elementAt(qnameIndex);      }      return m_valuesOrPrefixes.indexToString(qnameIndex);    }  }  /**   *     5. [specified] A flag indicating whether this attribute was actually   *        specified in the start-tag of its element, or was defaulted from the   *        DTD.   *   * @param attributeHandle Must be a valid handle to an attribute node.   * @return <code>true</code> if the attribute was specified;   *         <code>false</code> if it was defaulted.   */  public boolean isAttributeSpecified(int attributeHandle)  {    // I'm not sure if I want to do anything with this...    return true;  // ??  }  /**   *   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()  {    /** @todo: implement this com.sun.org.apache.xml.internal.dtm.DTMDefaultBase abstract method */    error(XMLMessages.createXMLMessage(XMLErrorResources.ER_METHOD_NOT_SUPPORTED, null));//"Not yet supported!");    return null;  }  /**   * Get the next node identity value in the list, and call the iterator   * if it hasn't been added yet.   *   * @param identity The node identity (index).   * @return identity+1, or DTM.NULL.   */  protected int getNextNodeIdentity(int identity)  {    identity += 1;    while (identity >= m_size)    {      if (null == m_incrementalSAXSource)        return DTM.NULL;      nextNode();    }    return identity;  }  /**   * Directly create SAX parser events from a subtree.   *   * @param nodeHandle The node ID.   * @param ch A non-null reference to a ContentHandler.   *   * @throws org.xml.sax.SAXException   */  public void dispatchToEvents(int nodeHandle, org.xml.sax.ContentHandler ch)          throws org.xml.sax.SAXException  {    DTMTreeWalker treeWalker = m_walker;    ContentHandler prevCH = treeWalker.getcontentHandler();    if (null != prevCH)    {      treeWalker = new DTMTreeWalker();    }    treeWalker.setcontentHandler(ch);    treeWalker.setDTM(this);    try    {      treeWalker.traverse(nodeHandle);    }    finally    {      treeWalker.setcontentHandler(null);    }  }  /**   * Get the number of nodes that have been added.   *   * @return The number of that are currently in the tree.   */  public int getNumberOfNodes()  {    return m_size;  }  /**   * This method should try and build one or more nodes in the table.   *   * @return The true if a next node is found or false if   *         there are no more nodes.   */  protected boolean nextNode()  {    if (null == m_incrementalSAXSource)      return false;    if (m_endDocumentOccured)    {      clearCoRoutine();      return false;    }    Object gotMore = m_incrementalSAXSource.deliverMoreNodes(true);    // gotMore may be a Boolean (TRUE if still parsing, FALSE if    // EOF) or an exception if IncrementalSAXSource malfunctioned    // (code error rather than user error).    //    // %REVIEW% Currently the ErrorHandlers sketched herein are    // no-ops, so I'm going to initially leave this also as a    // no-op.    if (!(gotMore instanceof Boolean))    {      if(gotMore instanceof RuntimeException)      {        throw (RuntimeException)gotMore;      }      else if(gotMore instanceof Exception)      {        throw new WrappedRuntimeException((Exception)gotMore);      }      // for now...      clearCoRoutine();      return false;      // %TBD%    }    if (gotMore != Boolean.TRUE)    {      // EOF reached without satisfying the request      clearCoRoutine();  // Drop connection, stop trying      // %TBD% deregister as its listener?    }    return true;  }  /**   * Bottleneck determination of text type.   *   * @param type oneof DTM.XXX_NODE.   *   * @return true if this is a text or cdata section.   */  private final boolean isTextType(int type)  {    return (DTM.TEXT_NODE == type || DTM.CDATA_SECTION_NODE == type);  }//    /**//     * Ensure that the size of the information arrays can hold another entry//     * at the given index.//     *//     * @param on exit from this function, the information arrays sizes must be//     * at least index+1.//     *//     * NEEDSDOC @param index//     *///    protected void ensureSize(int index)//    {//          // dataOrQName is an SuballocatedIntVector and hence self-sizing.//          // But DTMDefaultBase may need fixup.//        super.ensureSize(index);//    }  /**

⌨️ 快捷键说明

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