sax2dtm.java

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

JAVA
2,075
字号
  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 name The element type name.   *   * @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;      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 name The element type name.   * @param attributes The specified or defaulted attributes.   *   * @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_prefixMappings.setSize(m_contextIndexes.pop());    // Do it again for this one (the one pushed by the last endElement).    m_prefixMappings.setSize(m_contextIndexes.pop());    m_contextIndexes.push(m_prefixMappings.size());  // for the next element.    int lastNode = m_previous;    m_previous = m_parents.pop();    if (NOTPROCESSED == m_firstch.elementAt(m_previous))      m_firstch.setElementAt(DTM.NULL,m_previous);    else if (DTM.NULL != lastNode)      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;    }    m_chars.append(ch, start, length);    // 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.    if (m_textType == DTM.TEXT_NODE)      m_coalescedTextType = DTM.TEXT_NODE;  }  /**   * 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.println("processingInstruction: target: " + target +", data: "+data);    charactersFlush();    int exName = m_expandedNameTable.getExpandedTypeID(null, target,                                         DTM.PROCESSING_INSTRUCTION_NODE);    int dataIndex = m_valuesOrPrefixes.stringToIndex(data);    m_previous = addNode(DTM.PROCESSING_INSTRUCTION_NODE, exName,                         m_parents.peek(), m_previous,                         dataIndex, false);  }  /**   * Receive notification of a skipped entity.   *   * <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 name The name of the skipped entity.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ContentHandler#processingInstruction   */  public void skippedEntity(String name) throws SAXException  {    // %REVIEW% What should be done here?    // no op  }  ////////////////////////////////////////////////////////////////////  // Implementation of the ErrorHandler interface.  ////////////////////////////////////////////////////////////////////  /**   * Receive notification of a parser warning.   *   * <p>The default implementation does nothing.  Application writers   * may override this method in a subclass to take specific actions   * for each warning, such as inserting the message in a log file or   * printing it to the console.</p>   *   * @param e The warning information encoded as an exception.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ErrorHandler#warning   * @see org.xml.sax.SAXParseException   */  public void warning(SAXParseException e) throws SAXException  {    // %REVIEW% Is there anyway to get the JAXP error listener here?    System.err.println(e.getMessage());  }  /**   * Receive notification of a recoverable parser error.   *   * <p>The default implementation does nothing.  Application writers   * may override this method in a subclass to take specific actions   * for each error, such as inserting the message in a log file or   * printing it to the console.</p>   *   * @param e The warning information encoded as an exception.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ErrorHandler#warning   * @see org.xml.sax.SAXParseException   */  public void error(SAXParseException e) throws SAXException  {    throw e;  }  /**   * Report a fatal XML parsing error.   *   * <p>The default implementation throws a SAXParseException.   * Application writers may override this method in a subclass if   * they need to take specific actions for each fatal error (such as   * collecting all of the errors into a single report): in any case,   * the application must stop all regular processing when this   * method is invoked, since the document is no longer reliable, and   * the parser may no longer report parsing events.</p>   *   * @param e The error information encoded as an exception.   * @throws SAXException Any SAX exception, possibly   *            wrapping another exception.   * @see org.xml.sax.ErrorHandler#fatalError   * @see org.xml.sax.SAXParseException   */  public void fatalError(SAXParse

⌨️ 快捷键说明

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