dtmdocument.java

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

JAVA
1,630
字号
/** * @(#) SQLDocument.java * * The Apache Software License, Version 1.1 * * * Copyright (c) 1999 The Apache Software Foundation.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xalan" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, Lotus * Development Corporation., http://www.lotus.com.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */package org.apache.xalan.lib.sql;import org.apache.xml.dtm.DTMManager;import org.apache.xml.dtm.DTMWSFilter;import org.apache.xml.dtm.ref.DTMDefaultBaseIterators;import org.apache.xml.dtm.DTM;import org.apache.xml.dtm.DTMAxisTraverser;import org.apache.xml.dtm.DTMAxisIterator;import org.apache.xml.utils.XMLString;import org.apache.xml.utils.XMLStringFactory;import org.apache.xml.utils.SuballocatedIntVector;import org.w3c.dom.Node;import org.xml.sax.ext.DeclHandler;import org.xml.sax.ErrorHandler;import org.xml.sax.DTDHandler;import org.xml.sax.EntityResolver;import org.xml.sax.ext.LexicalHandler;import org.xml.sax.ContentHandler;import java.io.IOException;import java.io.File;import java.io.PrintStream;import java.io.FileOutputStream;import org.xml.sax.*;import javax.xml.transform.SourceLocator;import org.apache.xml.utils.*;import org.apache.xml.dtm.*;import org.xml.sax.ext.*;/** * The SQL Document is the main controlling class the executesa SQL Query */public class DTMDocument extends DTMDefaultBaseIterators{  /**   */  public interface CharacterNodeHandler  {    /**     * @param node     * @return     * @throws org.xml.sax.SAXException     */    public void characters( Node node )throws org.xml.sax.SAXException ;  }  /**   */  private boolean DEBUG = false;  /**   */  protected static final String S_NAMESPACE = "http://xml.apache.org/xalan/SQLExtension";  /**   */  protected static final String S_ATTRIB_NOT_SUPPORTED = "Not Supported";  /**   */  protected static final String S_ISTRUE = "true";  /**   */  protected static final String S_ISFALSE = "false";  /**   */  protected static final String S_DOCUMENT = "#root";  /**   */  protected static final String S_TEXT_NODE = "#text";  /**   */  protected static final String S_ELEMENT_NODE = "#element";  /**   */  protected int m_Document_TypeID = 0;  /**   */  protected int m_TextNode_TypeID = 0;  /**   * Store the SQL Data in this growable array   */  protected ObjectArray m_ObjectArray = new ObjectArray();  /**   * For each element node, there can be zero or more attributes. If Attributes   * are assigned, the first attribute for that element will be use here.   * Subsequent elements will use the m_nextsib, m_prevsib array. The sibling   * arrays are not meeant to hold indexes to attribute information but as   * long as there is not direct connection back into the main DTM tree   * we should be OK.   */  protected SuballocatedIntVector m_attribute;  /**   * The Document Index will most likely be 0, but we will reference it   * by variable in case that paradigm falls through.   */  protected int m_DocumentIdx;  /**   * @param mgr   * @param ident   */  public DTMDocument( DTMManager mgr, int ident )  {    super(mgr, null, ident,      null, mgr.getXMLStringFactory(), true);    m_attribute = new SuballocatedIntVector(m_initialblocksize);  }  /**   * A common routine that allocates an Object from the Object Array.   * One of the common bugs in this code was to allocate an Object and   * not incerment m_size, using this method will assure that function.   * @param o   * @return   */  private int allocateNodeObject( Object o )  {    // Need to keep this counter going even if we don't use it.    m_size++;    return m_ObjectArray.append(o);  }  /**   * @param o   * @param level   * @param extendedType   * @param parent   * @param prevsib   * @return   */  protected int addElementWithData( Object o, int level, int extendedType, int parent, int prevsib )  {    int elementIdx = addElement(level,extendedType,parent,prevsib);    int data = allocateNodeObject(o);    m_firstch.setElementAt(data,elementIdx);    m_exptype.setElementAt(m_TextNode_TypeID, data);    // m_level.setElementAt((byte)(level), data);    m_parent.setElementAt(elementIdx, data);    m_prevsib.setElementAt(DTM.NULL, data);    m_nextsib.setElementAt(DTM.NULL, data);    m_attribute.setElementAt(DTM.NULL, data);    m_firstch.setElementAt(DTM.NULL, data);    return elementIdx;  }  /**   * @param level   * @param extendedType   * @param parent   * @param prevsib   * @return   */  protected int addElement( int level, int extendedType, int parent, int prevsib )  {    int node = DTM.NULL;    try    {      // Add the Node and adjust its Extended Type      node = allocateNodeObject(S_ELEMENT_NODE);      m_exptype.setElementAt(extendedType, node);      m_nextsib.setElementAt(DTM.NULL, node);      m_prevsib.setElementAt(prevsib, node);      m_parent.setElementAt(parent, node);      m_firstch.setElementAt(DTM.NULL, node);      // m_level.setElementAt((byte)level, node);      m_attribute.setElementAt(DTM.NULL, node);      if (prevsib != DTM.NULL)      {        // If the previous sibling is already assigned, then we are        // inserting a value into the chain.        if (m_nextsib.elementAt(prevsib) != DTM.NULL)          m_nextsib.setElementAt(m_nextsib.elementAt(prevsib), node);        // Tell the proevious sibling that they have a new bother/sister.        m_nextsib.setElementAt(node, prevsib);      }       // So if we have a valid parent and the new node ended up being first      // in the list, i.e. no prevsib, then set the new node up as the      // first child of the parent. Since we chained the node in the list,      // there should be no reason to worry about the current first child      // of the parent node.      if ((parent != DTM.NULL) && (m_prevsib.elementAt(node) == DTM.NULL))      {        m_firstch.setElementAt(node, parent);      }    }    catch(Exception e)    {      error("Error in addElement: "+e.getMessage());    }    return node;  }  /**   * Link an attribute to a node, if the node already has one or more   * attributes assigned, then just link this one to the attribute list.   * The first attribute is attached to the Parent Node (pnode) through the   * m_attribute array, subsequent attributes are linked through the   * m_prevsib, m_nextsib arrays.   * @param o   * @param extendedType   * @param pnode   * @return   */  protected int addAttributeToNode( Object o, int extendedType, int pnode )  {    int attrib = DTM.NULL;    int prevsib = DTM.NULL;    int lastattrib = DTM.NULL;    int value = DTM.NULL;    try    {      // Add the Node and adjust its Extended Type      attrib = allocateNodeObject(o);      m_attribute.setElementAt(DTM.NULL, attrib);      m_exptype.setElementAt(extendedType, attrib);      // m_level.setElementAt((byte)0, attrib);      // Clear the sibling references      m_nextsib.setElementAt(DTM.NULL, attrib);      m_prevsib.setElementAt(DTM.NULL,attrib);      // Set the parent, although the was we are using attributes      // in the SQL extension this reference will more than likly      // be wrong      m_parent.setElementAt(pnode, attrib);      m_firstch.setElementAt(DTM.NULL, attrib);      if (m_attribute.elementAt(pnode) != DTM.NULL)      {        // OK, we already have an attribute assigned to this        // Node, Insert us into the head of the list.        lastattrib = m_attribute.elementAt(pnode);        m_nextsib.setElementAt(lastattrib, attrib);        m_prevsib.setElementAt(attrib, lastattrib);      }      // Okay set the new attribute up as the first attribute      // for the node.      m_attribute.setElementAt(attrib, pnode);    }    catch(Exception e)    {      error("Error in addAttributeToNode: "+e.getMessage());    }    return attrib;  }  /**   * Allow two nodes to share the same set of attributes. There may be some   * problems because the parent of any attribute will be the original node   * they were assigned to. Need to see how the attribute walker works, then   * we should be able to fake it out.   * @param toNode   * @param fromNode   * @return   */  protected void cloneAttributeFromNode( int toNode, int fromNode )  {   try    {      if (m_attribute.elementAt(toNode) != DTM.NULL)      {        error("Cloneing Attributes, where from Node already had addtibures assigned");      }      m_attribute.setElementAt(m_attribute.elementAt(fromNode), toNode);    }    catch(Exception e)    {      error("Cloning attributes");    }  }  /**   * @param parm1   * @return   */  public int getFirstAttribute( int parm1 )  {    if (DEBUG) System.out.println("getFirstAttribute("+ parm1+")");    int nodeIdx = makeNodeIdentity(parm1);    if (nodeIdx != DTM.NULL)    {      int attribIdx =  m_attribute.elementAt(nodeIdx);      return makeNodeHandle(attribIdx);    }    else return DTM.NULL;  } /**   * @param parm1   * @return   */  public String getNodeValue( int parm1 )  {    if (DEBUG) System.out.println("getNodeValue(" + parm1 + ")");    try    {      Object o = m_ObjectArray.getAt(makeNodeIdentity(parm1));      if (o != null)      {        return o.toString();      }      else      {        return "";      }    }    catch(Exception e)    {      error("Getting String Value");      return null;    }  }  /**   * @param parm1   * @return   */  public XMLString getStringValue( int parm1 )  {    int nodeIdx = makeNodeIdentity(parm1);    if (DEBUG) System.out.println("getStringValue(" + nodeIdx + ")");    try    {      Object o = m_ObjectArray.getAt(nodeIdx);      if (o != null)      {        return m_xstrf.newstr(o.toString());      }      else      {        return m_xstrf.emptystr();      }    }    catch(Exception e)    {      error("Getting String Value");      return null;    }  }  /**   * @param parm1   * @return   */  public int getNextAttribute( int parm1 )  {    int nodeIdx = makeNodeIdentity(parm1);    if (DEBUG) System.out.println("getNextAttribute(" + nodeIdx + ")");    if (nodeIdx != DTM.NULL) return makeNodeHandle(m_nextsib.elementAt(nodeIdx));    else return DTM.NULL;  }  /**   * @return   */  protected int getNumberOfNodes( )  {    if (DEBUG) System.out.println("getNumberOfNodes()");    return m_size;  }  /**   * @return   */  protected boolean nextNode( )  {    if (DEBUG) System.out.println("nextNode()");    return false;  }  /**   * The Expanded Name table holds all of our Node names. The Base class   * will add the common element types, need to call this function from   * the derived class.   * @return   */  protected void createExpandedNameTable( )  {    m_Document_TypeID =      m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_DOCUMENT, DTM.DOCUMENT_NODE);    m_TextNode_TypeID =      m_expandedNameTable.getExpandedTypeID(S_NAMESPACE, S_TEXT_NODE, DTM.TEXT_NODE);  }  /**   * @return   */  public void dumpDTM( )  {    try    {//      File f = new File("DTMDump"+((Object)this).hashCode()+".txt");      File f = new File("DTMDump.txt");      System.err.println("Dumping... "+f.getAbsolutePath());      PrintStream ps = new PrintStream(new FileOutputStream(f));      while (nextNode()){}      int nRecords = m_size;      ps.println("Total nodes: " + nRecords);      for (int i = 0; i < nRecords; i++)      {        ps.println("=========== " + i + " ===========");        ps.println("NodeName: " + getNodeName(makeNodeHandle(i)));        ps.println("NodeNameX: " + getNodeNameX(makeNodeHandle(i)));        ps.println("LocalName: " + getLocalName(makeNodeHandle(i)));        ps.println("NamespaceURI: " + getNamespaceURI(makeNodeHandle(i)));        ps.println("Prefix: " + getPrefix(makeNodeHandle(i)));        int exTypeID = getExpandedTypeID(makeNodeHandle(i));        ps.println("Expanded Type ID: "                           + Integer.toHexString(exTypeID));        int type = getNodeType(makeNodeHandle(i));        String typestring;        switch (type)        {        case DTM.ATTRIBUTE_NODE :          typestring = "ATTRIBUTE_NODE";          break;        case DTM.CDATA_SECTION_NODE :          typestring = "CDATA_SECTION_NODE";          break;        case DTM.COMMENT_NODE :          typestring = "COMMENT_NODE";          break;        case DTM.DOCUMENT_FRAGMENT_NODE :          typestring = "DOCUMENT_FRAGMENT_NODE";          break;        case DTM.DOCUMENT_NODE :          typestring = "DOCUMENT_NODE";          break;        case DTM.DOCUMENT_TYPE_NODE :          typestring = "DOCUMENT_NODE";          break;        case DTM.ELEMENT_NODE :          typestring = "ELEMENT_NODE";          break;        case DTM.ENTITY_NODE :          typestring = "ENTITY_NODE";          break;        case DTM.ENTITY_REFERENCE_NODE :          typestring = "ENTITY_REFERENCE_NODE";          break;        case DTM.NAMESPACE_NODE :          typestring = "NAMESPACE_NODE";          break;        case DTM.NOTATION_NODE :          typestring = "NOTATION_NODE";          break;        case DTM.NULL :

⌨️ 快捷键说明

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