dombuilder.java

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

JAVA
787
字号
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Id: DOMBuilder.java,v 1.2.4.1 2005/09/15 08:15:39 suresh_emailid Exp $ */package com.sun.org.apache.xml.internal.utils;import java.util.Stack;import com.sun.org.apache.xml.internal.res.XMLErrorResources;import com.sun.org.apache.xml.internal.res.XMLMessages;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.Text;import org.w3c.dom.CDATASection;import org.xml.sax.Attributes;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.ext.LexicalHandler;/** * This class takes SAX events (in addition to some extra events * that SAX doesn't handle yet) and adds the result to a document * or document fragment. * @xsl.usage general */public class DOMBuilder        implements ContentHandler, LexicalHandler{  /** Root document          */  public Document m_doc;  /** Current node           */  protected Node m_currentNode = null;    /** The root node          */  protected Node m_root = null;    /** The next sibling node  */  protected Node m_nextSibling = null;  /** First node of document fragment or null if not a DocumentFragment     */  public DocumentFragment m_docFrag = null;  /** Vector of element nodes          */  protected Stack m_elemStack = new Stack();  /**   * DOMBuilder instance constructor... it will add the DOM nodes   * to the document fragment.   *   * @param doc Root document   * @param node Current node   */  public DOMBuilder(Document doc, Node node)  {    m_doc = doc;    m_currentNode = m_root = node;        if (node instanceof Element)      m_elemStack.push(node);  }  /**   * DOMBuilder instance constructor... it will add the DOM nodes   * to the document fragment.   *   * @param doc Root document   * @param docFrag Document fragment   */  public DOMBuilder(Document doc, DocumentFragment docFrag)  {    m_doc = doc;    m_docFrag = docFrag;  }  /**   * DOMBuilder instance constructor... it will add the DOM nodes   * to the document.   *   * @param doc Root document   */  public DOMBuilder(Document doc)  {    m_doc = doc;  }  /**   * Get the root document or DocumentFragment of the DOM being created.   *   * @return The root document or document fragment if not null   */  public Node getRootDocument()  {    return (null != m_docFrag) ? (Node) m_docFrag : (Node) m_doc;  }    /**   * Get the root node of the DOM tree.   */  public Node getRootNode()  {    return m_root;  }    /**   * Get the node currently being processed.   *   * @return the current node being processed   */  public Node getCurrentNode()  {    return m_currentNode;  }    /**   * Set the next sibling node, which is where the result nodes    * should be inserted before.   *    * @param nextSibling the next sibling node.   */  public void setNextSibling(Node nextSibling)  {    m_nextSibling = nextSibling;  }    /**   * Return the next sibling node.   *    * @return the next sibling node.   */  public Node getNextSibling()  {    return m_nextSibling;  }  /**   * Return null since there is no Writer for this class.   *   * @return null   */  public java.io.Writer getWriter()  {    return null;  }  /**   * Append a node to the current container.   *   * @param newNode New node to append   */  protected void append(Node newNode) throws org.xml.sax.SAXException  {    Node currentNode = m_currentNode;    if (null != currentNode)    {      if (currentNode == m_root && m_nextSibling != null)        currentNode.insertBefore(newNode, m_nextSibling);      else        currentNode.appendChild(newNode);      // System.out.println(newNode.getNodeName());    }    else if (null != m_docFrag)    {      if (m_nextSibling != null)        m_docFrag.insertBefore(newNode, m_nextSibling);      else        m_docFrag.appendChild(newNode);    }    else    {      boolean ok = true;      short type = newNode.getNodeType();      if (type == Node.TEXT_NODE)      {        String data = newNode.getNodeValue();        if ((null != data) && (data.trim().length() > 0))        {          throw new org.xml.sax.SAXException(            XMLMessages.createXMLMessage(              XMLErrorResources.ER_CANT_OUTPUT_TEXT_BEFORE_DOC, null));  //"Warning: can't output text before document element!  Ignoring...");        }        ok = false;      }      else if (type == Node.ELEMENT_NODE)      {        if (m_doc.getDocumentElement() != null)        {          ok = false;                    throw new org.xml.sax.SAXException(            XMLMessages.createXMLMessage(              XMLErrorResources.ER_CANT_HAVE_MORE_THAN_ONE_ROOT, null));  //"Can't have more than one root on a DOM!");        }      }      if (ok)      {        if (m_nextSibling != null)          m_doc.insertBefore(newNode, m_nextSibling);        else          m_doc.appendChild(newNode);      }    }  }  /**   * Receive an object for locating the origin of SAX document events.   *   * <p>SAX parsers are strongly encouraged (though not absolutely   * required) to supply a locator: if it does so, it must supply   * the locator to the application by invoking this method before   * invoking any of the other methods in the ContentHandler   * interface.</p>   *   * <p>The locator allows the application to determine the end   * position of any document-related event, even if the parser is   * not reporting an error.  Typically, the application will   * use this information for reporting its own errors (such as   * character content that does not match an application's   * business rules).  The information returned by the locator   * is probably not sufficient for use with a search engine.</p>   *   * <p>Note that the locator will return correct information only   * during the invocation of the events in this interface.  The   * application should not attempt to use it at any other time.</p>   *   * @param locator An object that can return the location of   *                any SAX document event.   * @see org.xml.sax.Locator   */  public void setDocumentLocator(Locator locator)  {    // No action for the moment.  }  /**   * Receive notification of the beginning of a document.   *   * <p>The SAX parser will invoke this method only once, before any   * other methods in this interface or in DTDHandler (except for   * setDocumentLocator).</p>   */  public void startDocument() throws org.xml.sax.SAXException  {    // No action for the moment.  }  /**   * Receive notification of the end of a document.   *   * <p>The SAX parser will invoke this method only once, and it will   * be the last method invoked during the parse.  The parser shall   * not invoke this method until it has either abandoned parsing   * (because of an unrecoverable error) or reached the end of   * input.</p>   */  public void endDocument() throws org.xml.sax.SAXException  {    // No action for the moment.  }  /**   * Receive notification of the beginning of an element.   *   * <p>The Parser will invoke this method at the beginning of every   * element in the XML document; there will be a corresponding   * endElement() event for every startElement() event (even when the   * element is empty). All of the element's content will be   * reported, in order, before the corresponding endElement()   * event.</p>   *   * <p>If the element name has a namespace prefix, the prefix will   * still be attached.  Note that the attribute list provided will   * contain only attributes with explicit values (specified or   * defaulted): #IMPLIED attributes will be omitted.</p>   *   *   * @param ns The namespace of the node   * @param localName The local part of the qualified name   * @param name The element name.   * @param atts The attributes attached to the element, if any.   * @see #endElement   * @see org.xml.sax.Attributes   */  public void startElement(          String ns, String localName, String name, Attributes atts)            throws org.xml.sax.SAXException  {    Element elem;	// Note that the namespace-aware call must be used to correctly	// construct a Level 2 DOM, even for non-namespaced nodes.    if ((null == ns) || (ns.length() == 0))      elem = m_doc.createElementNS(null,name);    else      elem = m_doc.createElementNS(ns, name);    append(elem);    try    {      int nAtts = atts.getLength();      if (0 != nAtts)      {        for (int i = 0; i < nAtts; i++)        {          //System.out.println("type " + atts.getType(i) + " name " + atts.getLocalName(i) );          // First handle a possible ID attribute          if (atts.getType(i).equalsIgnoreCase("ID"))            setIDAttribute(atts.getValue(i), elem);          String attrNS = atts.getURI(i);          if("".equals(attrNS))            attrNS = null; // DOM represents no-namespace as null          // System.out.println("attrNS: "+attrNS+", localName: "+atts.getQName(i)          //                   +", qname: "+atts.getQName(i)+", value: "+atts.getValue(i));          // Crimson won't let us set an xmlns: attribute on the DOM.          String attrQName = atts.getQName(i);          // In SAX, xmlns[:] attributes have an empty namespace, while in DOM they           // should have the xmlns namespace          if (attrQName.startsWith("xmlns:") || attrQName.equals("xmlns")) {            attrNS = "http://www.w3.org/2000/xmlns/";          }          // ALWAYS use the DOM Level 2 call!          elem.setAttributeNS(attrNS,attrQName, atts.getValue(i));        }      }      // append(elem);      m_elemStack.push(elem);      m_currentNode = elem;      // append(elem);    }    catch(java.lang.Exception de)    {      // de.printStackTrace();      throw new org.xml.sax.SAXException(de);    }  }  /**   * Receive notification of the end of an element.   *   * <p>The SAX parser will invoke this method at the end of every   * element in the XML document; there will be a corresponding   * startElement() event for every endElement() event (even when the   * element is empty).</p>   *   * <p>If the element name has a namespace prefix, the prefix will   * still be attached to the name.</p>   *   *   * @param ns the namespace of the element

⌨️ 快捷键说明

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