sax2dtm2.java

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

JAVA
2,403
字号
      return m_exptype_map0[identity];    else      return m_exptype_map[identity>>>m_SHIFT][identity&m_MASK];  }  /**   * The optimized version of DTMDefaultBase._nextsib().   *   * @param identity A node identity, which <em>must not</em> be equal to   *        <code>DTM.NULL</code>   */  public final int _nextsib2(int identity)  {    //return m_nextsib.elementAt(identity);    if (identity < m_blocksize)      return m_nextsib_map0[identity];    else      return m_nextsib_map[identity>>>m_SHIFT][identity&m_MASK];  }  /**   * The optimized version of DTMDefaultBase._firstch().   *   * @param identity A node identity, which <em>must not</em> be equal to   *        <code>DTM.NULL</code>   */  public final int _firstch2(int identity)  {    //return m_firstch.elementAt(identity);    if (identity < m_blocksize)      return m_firstch_map0[identity];    else      return m_firstch_map[identity>>>m_SHIFT][identity&m_MASK];  }  /**   * The optimized version of DTMDefaultBase._parent().   *   * @param identity A node identity, which <em>must not</em> be equal to   *        <code>DTM.NULL</code>   */  public final int _parent2(int identity)  {    //return m_parent.elementAt(identity);    if (identity < m_blocksize)      return m_parent_map0[identity];    else      return m_parent_map[identity>>>m_SHIFT][identity&m_MASK];  }  /**   * The optimized version of DTMDefaultBase._type().   *   * @param identity A node identity, which <em>must not</em> be equal to   *        <code>DTM.NULL</code>   */  public final int _type2(int identity)  {    //int eType = _exptype2(identity);    int eType;    if (identity < m_blocksize)      eType = m_exptype_map0[identity];    else      eType = m_exptype_map[identity>>>m_SHIFT][identity&m_MASK];    if (NULL != eType)      return m_extendedTypes[eType].getNodeType();    else      return NULL;  }  /**   * The optimized version of DTMDefaultBase.getExpandedTypeID(int).   *   * <p>This one is only used by DOMAdapter.getExpandedTypeID(int), which   * is mostly called from the compiled translets.   */  public final int getExpandedTypeID2(int nodeHandle)  {    int nodeID = makeNodeIdentity(nodeHandle);    //return (nodeID != NULL) ? _exptype2(nodeID) : NULL;    if (nodeID != NULL) {      if (nodeID < m_blocksize)        return m_exptype_map0[nodeID];      else        return m_exptype_map[nodeID>>>m_SHIFT][nodeID&m_MASK];    }    else      return NULL;  }  /*************************************************************************   *                 END of DTM base accessor interfaces   *************************************************************************/  /**   * Return the node type from the expanded type   */  public final int _exptype2Type(int exptype)  {    if (NULL != exptype)      return m_extendedTypes[exptype].getNodeType();    else      return NULL;  }  /**   * Get a prefix either from the uri mapping, or just make   * one up!   *   * @param uri The namespace URI, which may be null.   *   * @return The prefix if there is one, or null.   */  public int getIdForNamespace(String uri)  {     int index = m_values.indexOf(uri);     if (index < 0)     {       m_values.addElement(uri);       return m_valueIndex++;     }     else       return index;  }  /**   * Override SAX2DTM.startElement()   *   * <p>Receive notification of the start of an element.   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions at the start of   * each element (such as allocating a new tree node or writing   * output to a file).</p>   *   * @param uri The Namespace URI, or the empty string if the   *        element has no Namespace URI or if Namespace   *        processing is not being performed.   * @param localName The local name (without prefix), or the   *        empty string if Namespace processing is not being   *        performed.   * @param qName The qualified name (with prefix), or the   *        empty string if qualified names are not available.   * @param attributes The specified or defaulted attributes.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#startElement   */  public void startElement(String uri, String localName, String qName, Attributes attributes)      throws SAXException  {    charactersFlush();    int exName = m_expandedNameTable.getExpandedTypeID(uri, localName, DTM.ELEMENT_NODE);    int prefixIndex = (qName.length() != localName.length())                      ? m_valuesOrPrefixes.stringToIndex(qName) : 0;    int elemNode = addNode(DTM.ELEMENT_NODE, exName,                           m_parents.peek(), m_previous, prefixIndex, true);    if(m_indexing)      indexNode(exName, elemNode);    m_parents.push(elemNode);    int startDecls = m_contextIndexes.peek();    int nDecls = m_prefixMappings.size();    String prefix;    if(!m_pastFirstElement)    {      // SPECIAL CASE: Implied declaration at root element      prefix="xml";      String declURL = "http://www.w3.org/XML/1998/namespace";      exName = m_expandedNameTable.getExpandedTypeID(null, prefix, DTM.NAMESPACE_NODE);      m_values.addElement(declURL);      int val = m_valueIndex++;      addNode(DTM.NAMESPACE_NODE, exName, elemNode,                     DTM.NULL, val, false);      m_pastFirstElement=true;    }    for (int i = startDecls; i < nDecls; i += 2)    {      prefix = (String) m_prefixMappings.elementAt(i);      if (prefix == null)        continue;      String declURL = (String) m_prefixMappings.elementAt(i + 1);      exName = m_expandedNameTable.getExpandedTypeID(null, prefix, DTM.NAMESPACE_NODE);      m_values.addElement(declURL);      int val = m_valueIndex++;      addNode(DTM.NAMESPACE_NODE, exName, elemNode, DTM.NULL, val, false);    }    int n = attributes.getLength();    for (int i = 0; i < n; i++)    {      String attrUri = attributes.getURI(i);      String attrQName = attributes.getQName(i);      String valString = attributes.getValue(i);      int nodeType;      String attrLocalName = attributes.getLocalName(i);      if ((null != attrQName)              && (attrQName.equals("xmlns")                  || attrQName.startsWith("xmlns:")))      {        prefix = getPrefix(attrQName, attrUri);        if (declAlreadyDeclared(prefix))          continue;  // go to the next attribute.        nodeType = DTM.NAMESPACE_NODE;      }      else      {        nodeType = DTM.ATTRIBUTE_NODE;        if (m_buildIdIndex && attributes.getType(i).equalsIgnoreCase("ID"))          setIDAttribute(valString, elemNode);      }      // Bit of a hack... if somehow valString is null, stringToIndex will      // return -1, which will make things very unhappy.      if(null == valString)        valString = "";      m_values.addElement(valString);      int val = m_valueIndex++;      if (attrLocalName.length() != attrQName.length())      {        prefixIndex = m_valuesOrPrefixes.stringToIndex(attrQName);        int dataIndex = m_data.size();        m_data.addElement(prefixIndex);        m_data.addElement(val);        val = -dataIndex;      }      exName = m_expandedNameTable.getExpandedTypeID(attrUri, attrLocalName, nodeType);      addNode(nodeType, exName, elemNode, DTM.NULL, val,                     false);    }    if (null != m_wsfilter)    {      short wsv = m_wsfilter.getShouldStripSpace(makeNodeHandle(elemNode), this);      boolean shouldStrip = (DTMWSFilter.INHERIT == wsv)                            ? getShouldStripWhitespace()                            : (DTMWSFilter.STRIP == wsv);      pushShouldStripWhitespace(shouldStrip);    }    m_previous = DTM.NULL;    m_contextIndexes.push(m_prefixMappings.size());  // for the children.  }  /**   * Receive notification of the end of an element.   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions at the end of   * each element (such as finalising a tree node or writing   * output to a file).</p>   *   * @param uri The Namespace URI, or the empty string if the   *        element has no Namespace URI or if Namespace   *        processing is not being performed.   * @param localName The local name (without prefix), or the   *        empty string if Namespace processing is not being   *        performed.   * @param qName The qualified XML 1.0 name (with prefix), or the   *        empty string if qualified names are not available.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#endElement   */  public void endElement(String uri, String localName, String qName)          throws SAXException  {    charactersFlush();    // If no one noticed, startPrefixMapping is a drag.    // Pop the context for the last child (the one pushed by startElement)    m_contextIndexes.quickPop(1);    // Do it again for this one (the one pushed by the last endElement).    int topContextIndex = m_contextIndexes.peek();    if (topContextIndex != m_prefixMappings.size()) {      m_prefixMappings.setSize(topContextIndex);    }    m_previous = m_parents.pop();    popShouldStripWhitespace();  }  /**   * Report an XML comment anywhere in the document.   *   * <p>This callback will be used for comments inside or outside the   * document element, including comments in the external DTD   * subset (if read).</p>   *   * @param ch An array holding the characters in the comment.   * @param start The starting position in the array.   * @param length The number of characters to use from the array.   * @throws SAXException The application may raise an exception.   */  public void comment(char ch[], int start, int length) throws SAXException  {    if (m_insideDTD)      // ignore comments if we're inside the DTD      return;    charactersFlush();    // %OPT% Saving the comment string in a Vector has a lower cost than    // saving it in DTMStringPool.    m_values.addElement(new String(ch, start, length));    int dataIndex = m_valueIndex++;    m_previous = addNode(DTM.COMMENT_NODE, DTM.COMMENT_NODE,                         m_parents.peek(), m_previous, dataIndex, false);  }  /**   * Receive notification of the beginning of the document.   *   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#startDocument   */  public void startDocument() throws SAXException  {    int doc = addNode(DTM.DOCUMENT_NODE,                      DTM.DOCUMENT_NODE,                      DTM.NULL, DTM.NULL, 0, true);    m_parents.push(doc);    m_previous = DTM.NULL;    m_contextIndexes.push(m_prefixMappings.size());  // for the next element.  }  /**   * Receive notification of the end of the document.   *   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#endDocument   */  public void endDocument() throws SAXException  {    super.endDocument();    // Add a NULL entry to the end of the node arrays as    // the end indication.    m_exptype.addElement(NULL);    m_parent.addElement(NULL);    m_nextsib.addElement(NULL);    m_firstch.addElement(NULL);    // Set the cached references after the document is built.    m_extendedTypes = m_expandedNameTable.getExtendedTypes();    m_exptype_map = m_exptype.getMap();    m_nextsib_map = m_nextsib.getMap();    m_firstch_map = m_firstch.getMap();    m_parent_map  = m_parent.getMap();  }  /**   * Construct the node map from the node.   *   * @param type raw type ID, one of DTM.XXX_NODE.   * @param expandedTypeID The expended type ID.   * @param parentIndex The current parent index.   * @param previousSibling The previous sibling index.   * @param dataOrPrefix index into m_data table, or string handle.   * @param canHaveFirstChild true if the node can have a first child, false   *                          if it is atomic.   *   * @return The index identity of the node that was added.   */  protected final int addNode(int type, int expandedTypeID,                        int parentIndex, int previousSibling,                        int dataOrPrefix, boolean canHaveFirstChild)  {    // Common to all nodes:    int nodeIndex = m_size++;    // Have we overflowed a DTM Identity's addressing range?    //if(m_dtmIdent.size() == (nodeIndex>>>DTMManager.IDENT_DTM_NODE_BITS))    if (nodeIndex == m_maxNodeIndex)    {      addNewDTMID(nodeIndex);      m_maxNodeIndex += (1 << DTMManager.IDENT_DTM_NODE_BITS);    }    m_firstch.addElement(DTM.NULL);    m_nextsib.addElement(DTM.NULL);    m_parent.addElement(parentIndex);    m_exptype.addElement(expandedTypeID);    m_dataOrQName.addElement(dataOrPrefix);    if (m_prevsib != null) {      m_prevsib.addElement(previousSibling);    }    if (m_locator != null && m_useSourceLocationProperty) {      setSourceLocation();    }    // Note that nextSibling is not processed until charactersFlush()    // is called, to handle successive characters() events.    // Special handling by type: Declare namespaces, attach first child    switch(type)    {    case DTM.NAMESPACE_NODE:      declareNamespaceInContext(parentIndex,nodeIndex);      break;    case DTM.ATTRIBUTE_NODE:      break;    default:      if (DTM.NULL != previousSibling) {        m_nextsib.setElementAt(nodeIndex,previousSibling);      }      else if (DTM.NULL != parentIndex) {        m_firstch.setElementAt(nodeIndex,parentIndex);      }      break;    }    return nodeIndex;  }  /**   * Check whether accumulated text should be stripped; if not,   * append the appropriate flavor of text/cdata node.   */  protected final void charactersFlush()  {    if (m_textPendingStart >= 0)  // -1 indicates no-text-in-progress    {      int length = m_chars.size() - m_textPendingStart;      boolean doStrip = false;      if (getShouldStripWhitespace())      {        doStrip = m_chars.isWhitespace(m_textPendingStart, length);      }      if (doStrip) {        m_chars.setLength(m_textPendingStart);  // Discard accumulated

⌨️ 快捷键说明

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