dtmdocumentimpl.java

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

JAVA
1,655
字号
/* * 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.xml.dtm.ref;import org.apache.xml.dtm.*;import java.util.Hashtable;//import java.util.Stack;import java.util.Vector;import javax.xml.transform.SourceLocator;import org.apache.xml.dtm.ref.ChunkedIntArray;import org.apache.xml.utils.FastStringBuffer;import org.xml.sax.ContentHandler;import org.xml.sax.Locator;import org.xml.sax.Attributes;import org.xml.sax.ext.LexicalHandler;import org.apache.xml.utils.XMLString;import org.apache.xml.utils.XMLStringFactory;/** * This is the implementation of the DTM document interface.  It receives * requests from an XML content handler similar to that of an XML DOM or SAX parser * to store information from the xml document in an array based * dtm table structure.  This informtion is used later for document navigation, * query, and SAX event dispatch functions. The DTM can also be used directly as a * document composition model for an application.  The requests received are: * <ul> * <li>initiating DTM to set the doc handle</li> * <li>resetting DTM for data structure reuse</li> * <li>hinting the end of document to adjust the end of data structure pointers</li> * <li>createnodes (element, comment, text, attribute, ....)</li> * <li>hinting the end of an element to patch parent and siblings<li> * <li>setting application provided symbol name stringpool data structures</li> * </ul> * <p>State: In progress!!</p> * * %REVIEW% I _think_ the SAX convention is that "no namespace" is expressed * as "" rather than as null (which is the DOM's convention). What should * DTM expect? What should it do with the other? * * <p>Origin: the implemention is a composite logic based on the DTM of XalanJ1 and *     DocImpl, DocumentImpl, ElementImpl, TextImpl, etc. of XalanJ2</p> */public class DTMDocumentImplimplements DTM, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler{        // Number of lower bits used to represent node index.        protected static final byte DOCHANDLE_SHIFT = 22;        // Masks the lower order of node handle.        // Same as {@link DTMConstructor.IDENT_NODE_DEFAULT}        protected static final int NODEHANDLE_MASK = (1 << (DOCHANDLE_SHIFT + 1)) - 1;        // Masks the higher order Document handle        // Same as {@link DTMConstructor.IDENT_DOC_DEFAULT}        protected static final int DOCHANDLE_MASK = -1 - NODEHANDLE_MASK;        int m_docHandle = NULL;		 // masked document handle for this dtm document        int m_docElement = NULL;	 // nodeHandle to the root of the actual dtm doc content        // Context for parse-and-append operations        int currentParent = 0;			// current parent - default is document root        int previousSibling = 0;		// previous sibling - no previous sibling        protected int m_currentNode = -1;		// current node        // The tree under construction can itself be used as        // the element stack, so m_elemStack isn't needed.        //protected Stack m_elemStack = new Stack();	 // element stack        private boolean previousSiblingWasParent = false;        // Local cache for record-at-a-time fetch        int gotslot[] = new int[4];        // endDocument recieved?        private boolean done = false;        boolean m_isError = false;        private final boolean DEBUG = false;        /** The document base URI. */        protected String m_documentBaseURI;  /** If we're building the model incrementally on demand, we need to   * be able to tell the source when to send us more data.   *   * Note that if this has not been set, and you attempt to read ahead   * of the current build point, we'll probably throw a null-pointer   * exception. We could try to wait-and-retry instead, as a very poor   * fallback, but that has all the known problems with multithreading   * on multiprocessors and we Don't Want to Go There.   *   * @see setIncrementalSAXSource   */  private IncrementalSAXSource m_incrSAXSource=null;        // ========= DTM data structure declarations. ==============        // nodes array: integer array blocks to hold the first level reference of the nodes,        // each reference slot is addressed by a nodeHandle index value.        // Assumes indices are not larger than {@link NODEHANDLE_MASK}        // ({@link DOCHANDLE_SHIFT} bits).        ChunkedIntArray nodes = new ChunkedIntArray(4);        // text/comment table: string buffer to hold the text string values of the document,        // each of which is addressed by the absolute offset and length in the buffer        private FastStringBuffer m_char = new FastStringBuffer();        // Start of string currently being accumulated into m_char;        // needed because the string may be appended in several chunks.        private int m_char_current_start=0;        // %TBD% INITIALIZATION/STARTUP ISSUES        // -- Should we really be creating these, or should they be        // passed in from outside? Scott want to be able to share        // pools across multiple documents, so setting them here is        // probably not the right default.        private DTMStringPool m_localNames = new DTMStringPool();        private DTMStringPool m_nsNames = new DTMStringPool();        private DTMStringPool m_prefixNames = new DTMStringPool();        // %TBD% If we use the current ExpandedNameTable mapper, it        // needs to be bound to the NS and local name pools. Which        // means it needs to attach to them AFTER we've resolved their        // startup. Or it needs to attach to this document and        // retrieve them each time. Or this needs to be        // an interface _implemented_ by this class... which might be simplest!        private ExpandedNameTable m_expandedNames=                new ExpandedNameTable(m_localNames,m_nsNames);        private XMLStringFactory m_xsf;        /**         * Construct a DTM.         *         * @param documentNumber the ID number assigned to this document.         * It will be shifted up into the high bits and returned as part of         * all node ID numbers, so those IDs indicate which document they         * came from as well as a location within the document. It is the         * DTMManager's responsibility to assign a unique number to each         * document.         */        public DTMDocumentImpl(DTMManager mgr, int documentNumber,                               DTMWSFilter whiteSpaceFilter,                               XMLStringFactory xstringfactory){                initDocument(documentNumber);	 // clear nodes and document handle                m_xsf = xstringfactory;        }  /** Bind a IncrementalSAXSource to this DTM. If we discover we need nodes   * that have not yet been built, we will ask this object to send us more   * events, and it will manage interactions with its data sources.   *   * Note that we do not actually build the IncrementalSAXSource, since we don't   * know what source it's reading from, what thread that source will run in,   * or when it will run.   *   * @param source The IncrementalSAXSource that we want to recieve events from   * on demand.   */  public void setIncrementalSAXSource(IncrementalSAXSource source)  {    m_incrSAXSource=source;    // Establish SAX-stream link so we can receive the requested data    source.setContentHandler(this);    source.setLexicalHandler(this);    // Are the following really needed? IncrementalSAXSource doesn't yet    // support them, and they're mostly no-ops here...    //source.setErrorHandler(this);    //source.setDTDHandler(this);    //source.setDeclHandler(this);  }        /**         * Wrapper for ChunkedIntArray.append, to automatically update the         * previous sibling's "next" reference (if necessary) and periodically         * wake a reader who may have encountered incomplete data and entered         * a wait state.         * @param w0 int As in ChunkedIntArray.append         * @param w1 int As in ChunkedIntArray.append         * @param w2 int As in ChunkedIntArray.append         * @param w3 int As in ChunkedIntArray.append         * @return int As in ChunkedIntArray.append         * @see ChunkedIntArray.append         */        private final int appendNode(int w0, int w1, int w2, int w3)        {                // A decent compiler may inline this.                int slotnumber = nodes.appendSlot(w0, w1, w2, w3);                if (DEBUG) System.out.println(slotnumber+": "+w0+" "+w1+" "+w2+" "+w3);                if (previousSiblingWasParent)                        nodes.writeEntry(previousSibling,2,slotnumber);                previousSiblingWasParent = false;	// Set the default; endElement overrides                return slotnumber;        }        // ========= DTM Implementation Control Functions. ==============        /**         * Set an implementation dependent feature.         * <p>         * %REVIEW% Do we really expect to set features on DTMs?         *         * @param featureId A feature URL.         * @param state true if this feature should be on, false otherwise.         */        public void setFeature(String featureId, boolean state) {};        /**         * Set a reference pointer to the element name symbol table.         * %REVIEW% Should this really be Public? Changing it while         * DTM is in use would be a disaster.         *         * @param poolRef DTMStringPool reference to an instance of table.         */        public void setLocalNameTable(DTMStringPool poolRef) {                m_localNames = poolRef;        }        /**         * Get a reference pointer to the element name symbol table.         *         * @return DTMStringPool reference to an instance of table.         */        public DTMStringPool getLocalNameTable() {                 return m_localNames;         }        /**         * Set a reference pointer to the namespace URI symbol table.         * %REVIEW% Should this really be Public? Changing it while         * DTM is in use would be a disaster.         *         * @param poolRef DTMStringPool reference to an instance of table.         */        public void setNsNameTable(DTMStringPool poolRef) {                m_nsNames = poolRef;        }        /**         * Get a reference pointer to the namespace URI symbol table.         *         * @return DTMStringPool reference to an instance of table.         */        public DTMStringPool getNsNameTable() {                 return m_nsNames;         }        /**         * Set a reference pointer to the prefix name symbol table.         * %REVIEW% Should this really be Public? Changing it while         * DTM is in use would be a disaster.         *         * @param poolRef DTMStringPool reference to an instance of table.         */        public void setPrefixNameTable(DTMStringPool poolRef) {                m_prefixNames = poolRef;        }        /**         * Get a reference pointer to the prefix name symbol table.         *         * @return DTMStringPool reference to an instance of table.         */        public DTMStringPool getPrefixNameTable() {                return m_prefixNames;        }         /**

⌨️ 快捷键说明

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