sax2dtm.java

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

JAVA
2,129
字号
   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#endDocument   */  public void endDocument() throws SAXException  {    if (DEBUG)      System.out.println("endDocument");		charactersFlush();    m_nextsib.setElementAt(NULL,0);    if (m_firstch.elementAt(0) == NOTPROCESSED)      m_firstch.setElementAt(NULL,0);    if (DTM.NULL != m_previous)      m_nextsib.setElementAt(DTM.NULL,m_previous);    m_parents = null;    m_prefixMappings = null;    m_contextIndexes = null;    m_endDocumentOccured = true;        // Bugzilla 4858: throw away m_locator. we cache m_systemId    m_locator = null;  }  /**   * Receive notification of the start of a Namespace mapping.   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions at the start of   * each Namespace prefix scope (such as storing the prefix mapping).</p>   *   * @param prefix The Namespace prefix being declared.   * @param uri The Namespace URI mapped to the prefix.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#startPrefixMapping   */  public void startPrefixMapping(String prefix, String uri)          throws SAXException  {    if (DEBUG)      System.out.println("startPrefixMapping: prefix: " + prefix + ", uri: "                         + uri);    if(null == prefix)      prefix = "";    m_prefixMappings.addElement(prefix);  // JDK 1.1.x compat -sc    m_prefixMappings.addElement(uri);  // JDK 1.1.x compat -sc  }  /**   * Receive notification of the end of a Namespace mapping.   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions at the end of   * each prefix mapping.</p>   *   * @param prefix The Namespace prefix being declared.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#endPrefixMapping   */  public void endPrefixMapping(String prefix) throws SAXException  {    if (DEBUG)      System.out.println("endPrefixMapping: prefix: " + prefix);    if(null == prefix)      prefix = "";    int index = m_contextIndexes.peek() - 1;    do    {      index = m_prefixMappings.indexOf(prefix, ++index);    } while ( (index >= 0) && ((index & 0x01) == 0x01) );    if (index > -1)    {      m_prefixMappings.setElementAt("%@$#^@#", index);      m_prefixMappings.setElementAt("%@$#^@#", index + 1);    }    // no op  }  /**   * Check if a declaration has already been made for a given prefix.   *   * @param prefix non-null prefix string.   *   * @return true if the declaration has already been declared in the   *         current context.   */  protected boolean declAlreadyDeclared(String prefix)  {    int startDecls = m_contextIndexes.peek();    java.util.Vector prefixMappings = m_prefixMappings;    int nDecls = prefixMappings.size();    for (int i = startDecls; i < nDecls; i += 2)    {      String prefixDecl = (String) prefixMappings.elementAt(i);      if (prefixDecl == null)        continue;      if (prefixDecl.equals(prefix))        return true;    }    return false;  }	boolean m_pastFirstElement=false;  /**   * 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  {   if (DEBUG)	 {      System.out.println("startElement: uri: " + uri + ", localname: "												 + localName + ", qname: "+qName+", atts: " + attributes);			boolean DEBUG_ATTRS=true;			if(DEBUG_ATTRS & attributes!=null)			{				int n = attributes.getLength();				if(n==0)					System.out.println("\tempty attribute list");				else for (int i = 0; i < n; i++)					System.out.println("\t attr: uri: " + attributes.getURI(i) +														 ", localname: " + attributes.getLocalName(i) +														 ", qname: " + attributes.getQName(i) +														 ", type: " + attributes.getType(i) +														 ", value: " + attributes.getValue(i)														 );			}	 }		    charactersFlush();    int exName = m_expandedNameTable.getExpandedTypeID(uri, localName, DTM.ELEMENT_NODE);    String prefix = getPrefix(qName, uri);    int prefixIndex = (null != prefix)                      ? 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();    int prev = DTM.NULL;    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);      int val = m_valuesOrPrefixes.stringToIndex(declURL);      prev = addNode(DTM.NAMESPACE_NODE, exName, elemNode,                     prev, 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);      int val = m_valuesOrPrefixes.stringToIndex(declURL);      prev = addNode(DTM.NAMESPACE_NODE, exName, elemNode,                     prev, 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);      prefix = getPrefix(attrQName, attrUri);      int nodeType;             String attrLocalName = attributes.getLocalName(i);      if ((null != attrQName)              && (attrQName.equals("xmlns")                  || attrQName.startsWith("xmlns:")))      {        if (declAlreadyDeclared(prefix))          continue;  // go to the next attribute.        nodeType = DTM.NAMESPACE_NODE;      }      else      {        nodeType = DTM.ATTRIBUTE_NODE;        if (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 = "";      int val = m_valuesOrPrefixes.stringToIndex(valString);      //String attrLocalName = attributes.getLocalName(i);      if (null != prefix)      {        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);      prev = addNode(nodeType, exName, elemNode, prev, val,                     false);    }    if (DTM.NULL != prev)      m_nextsib.setElementAt(DTM.NULL,prev);    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  {   if (DEBUG)      System.out.println("endElement: uri: " + uri + ", localname: "												 + localName + ", qname: "+qName);    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);    }    int lastNode = m_previous;    m_previous = m_parents.pop();    // If lastNode is still DTM.NULL, this element had no children    if (DTM.NULL == lastNode)      m_firstch.setElementAt(DTM.NULL,m_previous);    else      m_nextsib.setElementAt(DTM.NULL,lastNode);    popShouldStripWhitespace();  }  /**   * Receive notification of character data inside an element.   *   * <p>By default, do nothing.  Application writers may override this   * method to take specific actions for each chunk of character data   * (such as adding the data to a node or buffer, or printing it to   * a file).</p>   *   * @param ch The characters.   * @param start The start position in the character array.   * @param length The number of characters to use from the   *               character array.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#characters   */  public void characters(char ch[], int start, int length) throws SAXException  {    if (m_textPendingStart == -1)  // First one in this block    {      m_textPendingStart = m_chars.size();      m_coalescedTextType = m_textType;    }    // Type logic: If all adjacent text is CDATASections, the    // concatentated text is treated as a single CDATASection (see    // initialization above).  If any were ordinary Text, the whole    // thing is treated as Text. This may be worth %REVIEW%ing.    else if (m_textType == DTM.TEXT_NODE)    {      m_coalescedTextType = DTM.TEXT_NODE;    }    m_chars.append(ch, start, length);  }  /**   * Receive notification of ignorable whitespace in element content.   *   * <p>By default, do nothing.  Application writers may override this   * method to take specific actions for each chunk of ignorable   * whitespace (such as adding data to a node or buffer, or printing   * it to a file).</p>   *   * @param ch The whitespace characters.   * @param start The start position in the character array.   * @param length The number of characters to use from the   *               character array.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#ignorableWhitespace   */  public void ignorableWhitespace(char ch[], int start, int length)          throws SAXException  {    // %OPT% We can probably take advantage of the fact that we know this     // is whitespace.    characters(ch, start, length);  }  /**   * Receive notification of a processing instruction.   *   * <p>By default, do nothing.  Application writers may override this   * method in a subclass to take specific actions for each   * processing instruction, such as setting status variables or   * invoking other methods.</p>   *   * @param target The processing instruction target.   * @param data The processing instruction data, or null if   *             none is supplied.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#processingInstruction   */  public void processingInstruction(String target, String data)          throws SAXException  {    if (DEBUG)		 System.out

⌨️ 快捷键说明

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