saximpl.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,896 行 · 第 1/4 页

JAVA
1,896
字号
    public void processingInstruction(String target, String data)        throws SAXException    {        super.processingInstruction(target, data);        handleTextEscaping();    }    /**     * SAX2: Receive notification of ignorable whitespace in element     * content. Similar to characters(char[], int, int).     */    public void ignorableWhitespace(char[] ch, int start, int length)        throws SAXException    {        super.ignorableWhitespace(ch, start, length);        _textNodeToProcess = getNumberOfNodes();    }    /**     * SAX2: Begin the scope of a prefix-URI Namespace mapping.     */    public void startPrefixMapping(String prefix, String uri)        throws SAXException    {        super.startPrefixMapping(prefix, uri);        handleTextEscaping();        definePrefixAndUri(prefix, uri);    }    private void definePrefixAndUri(String prefix, String uri)         throws SAXException     {        // Check if the URI already exists before pushing on stack        Integer eType = new Integer(getIdForNamespace(uri));        if ((Integer)_nsIndex.get(eType) == null) {            _nsIndex.put(eType, new Integer(_uriCount++));        }    }     /**     * SAX2: Report an XML comment anywhere in the document.     */    public void comment(char[] ch, int start, int length)        throws SAXException    {        super.comment(ch, start, length);        handleTextEscaping();    }    public boolean setEscaping(boolean value) {        final boolean temp = _escaping;        _escaping = value;         return temp;    }      /*---------------------------------------------------------------------------*/   /* DOMBuilder methods end                                                    */   /*---------------------------------------------------------------------------*/    /**     * Prints the whole tree to standard output     */    public void print(int node, int level)    {        switch(getNodeType(node))        {	    case DTM.ROOT_NODE:	    case DTM.DOCUMENT_NODE:	        print(getFirstChild(node), level);	        break;	    case DTM.TEXT_NODE:	    case DTM.COMMENT_NODE:	    case DTM.PROCESSING_INSTRUCTION_NODE:	        System.out.print(getStringValueX(node));	        break;	    default:	        final String name = getNodeName(node);	        System.out.print("<" + name);	        for (int a = getFirstAttribute(node); a != DTM.NULL; a = getNextAttribute(a))                {		    System.out.print("\n" + getNodeName(a) + "=\"" + getStringValueX(a) + "\"");	        }	        System.out.print('>');	        for (int child = getFirstChild(node); child != DTM.NULL;		    child = getNextSibling(child)) {		    print(child, level + 1);	        }	        System.out.println("</" + name + '>');	        break;	}    }    /**     * Returns the name of a node (attribute or element).     */    public String getNodeName(final int node)    {	// Get the node type and make sure that it is within limits	int nodeh = node;	final short type = getNodeType(nodeh);	switch(type)        {	    case DTM.ROOT_NODE:	    case DTM.DOCUMENT_NODE:	    case DTM.TEXT_NODE:	    case DTM.COMMENT_NODE:	        return EMPTYSTRING;	    case DTM.NAMESPACE_NODE:		return this.getLocalName(nodeh);	    default:	        return super.getNodeName(nodeh);	}    }        /**     * Returns the namespace URI to which a node belongs     */    public String getNamespaceName(final int node)    {    	if (node == DTM.NULL) {    	    return "";    	}    	        String s;        return (s = getNamespaceURI(node)) == null ? EMPTYSTRING : s;    }     /**     * Returns the attribute node of a given type (if any) for an element     */    public int getAttributeNode(final int type, final int element)    {        for (int attr = getFirstAttribute(element);           attr != DTM.NULL;           attr = getNextAttribute(attr))        {            if (getExpandedTypeID(attr) == type) return attr;        }        return DTM.NULL;    }    /**     * Returns the value of a given attribute type of a given element     */    public String getAttributeValue(final int type, final int element)    {        final int attr = getAttributeNode(type, element);        return (attr != DTM.NULL) ? getStringValueX(attr) : EMPTYSTRING;    }    /**     * This method is for testing/debugging only     */    public String getAttributeValue(final String name, final int element)    {        return getAttributeValue(getGeneralizedType(name), element);    }    /**     * Returns an iterator with all the children of a given node     */    public DTMAxisIterator getChildren(final int node)    {        return (new ChildrenIterator()).setStartNode(node);    }    /**     * Returns an iterator with all children of a specific type     * for a given node (element)     */    public DTMAxisIterator getTypedChildren(final int type)    {        return(new TypedChildrenIterator(type));    }    /**     * This is a shortcut to the iterators that implement the     * supported XPath axes (only namespace::) is not supported.     * Returns a bare-bones iterator that must be initialized     * with a start node (using iterator.setStartNode()).     */    public DTMAxisIterator getAxisIterator(final int axis)    {        switch (axis)        {            case Axis.SELF:                return new SingletonIterator();            case Axis.CHILD:                return new ChildrenIterator();            case Axis.PARENT:                return new ParentIterator();            case Axis.ANCESTOR:                return new AncestorIterator();            case Axis.ANCESTORORSELF:                return (new AncestorIterator()).includeSelf();            case Axis.ATTRIBUTE:                return new AttributeIterator();            case Axis.DESCENDANT:                return new DescendantIterator();            case Axis.DESCENDANTORSELF:                return (new DescendantIterator()).includeSelf();            case Axis.FOLLOWING:                return new FollowingIterator();            case Axis.PRECEDING:                return new PrecedingIterator();            case Axis.FOLLOWINGSIBLING:                return new FollowingSiblingIterator();            case Axis.PRECEDINGSIBLING:                return new PrecedingSiblingIterator();            case Axis.NAMESPACE:                return new NamespaceIterator();            default:                BasisLibrary.runTimeError(BasisLibrary.AXIS_SUPPORT_ERR, Axis.names[axis]);        }        return null;    }    /**     * Similar to getAxisIterator, but this one returns an iterator     * containing nodes of a typed axis (ex.: child::foo)     */    public DTMAxisIterator getTypedAxisIterator(int axis, int type)    {        // Most common case handled first        if (axis == Axis.CHILD) {            return new TypedChildrenIterator(type);        }        if (type == NO_TYPE) {            return(EMPTYITERATOR);        }        switch (axis)        {            case Axis.SELF:                return new TypedSingletonIterator(type);            case Axis.CHILD:                return new TypedChildrenIterator(type);            case Axis.PARENT:                return new ParentIterator().setNodeType(type);            case Axis.ANCESTOR:                return new TypedAncestorIterator(type);            case Axis.ANCESTORORSELF:                return (new TypedAncestorIterator(type)).includeSelf();            case Axis.ATTRIBUTE:                return new TypedAttributeIterator(type);            case Axis.DESCENDANT:                return new TypedDescendantIterator(type);            case Axis.DESCENDANTORSELF:                return (new TypedDescendantIterator(type)).includeSelf();            case Axis.FOLLOWING:                return new TypedFollowingIterator(type);            case Axis.PRECEDING:                return new TypedPrecedingIterator(type);            case Axis.FOLLOWINGSIBLING:                return new TypedFollowingSiblingIterator(type);            case Axis.PRECEDINGSIBLING:                return new TypedPrecedingSiblingIterator(type);            case Axis.NAMESPACE:                return  new TypedNamespaceIterator(type);            default:                BasisLibrary.runTimeError(BasisLibrary.TYPED_AXIS_SUPPORT_ERR, Axis.names[axis]);        }        return null;    }    /**     * Do not think that this returns an iterator for the namespace axis.     * It returns an iterator with nodes that belong in a certain namespace,     * such as with <xsl:apply-templates select="blob/foo:*"/>     * The 'axis' specifies the axis for the base iterator from which the     * nodes are taken, while 'ns' specifies the namespace URI type.     */    public DTMAxisIterator getNamespaceAxisIterator(int axis, int ns)    {        DTMAxisIterator iterator = null;        if (ns == NO_TYPE) {            return EMPTYITERATOR;        }        else {            switch (axis) {                case Axis.CHILD:                    return new NamespaceChildrenIterator(ns);                case Axis.ATTRIBUTE:                    return new NamespaceAttributeIterator(ns);                default:                    return new NamespaceWildcardIterator(axis, ns);            }        }    }    /**     * Iterator that handles node tests that test for a namespace, but have     * a wild card for the local name of the node, i.e., node tests of the     * form <axis>::<prefix>:*     */    public final class NamespaceWildcardIterator        extends InternalAxisIteratorBase    {        /**         * The namespace type index.         */        protected int m_nsType;        /**         * A nested typed axis iterator that retrieves nodes of the principal         * node kind for that axis.         */        protected DTMAxisIterator m_baseIterator;        /**         * Constructor NamespaceWildcard         *         * @param axis The axis that this iterator will traverse         * @param nsType The namespace type index         */        public NamespaceWildcardIterator(int axis, int nsType) {            m_nsType = nsType;            // Create a nested iterator that will select nodes of            // the principal node kind for the selected axis.            switch (axis) {                case Axis.ATTRIBUTE: {                    // For "attribute::p:*", the principal node kind is                    // attribute                    m_baseIterator = getAxisIterator(axis);                }                case Axis.NAMESPACE: {                    // This covers "namespace::p:*".  It is syntactically                    // correct, though it doesn't make much sense.                    m_baseIterator = getAxisIterator(axis);                }                default: {                    // In all other cases, the principal node kind is                    // element                    m_baseIterator = getTypedAxisIterator(axis,                                                          DTM.ELEMENT_NODE);                }            }        }        /**         * Set start to END should 'close' the iterator,         * i.e. subsequent call to next() should return END.         *         * @param node Sets the root of the iteration.         *         * @return A DTMAxisIterator set to the start of the iteration.         */        public DTMAxisIterator setStartNode(int node) {            if (_isRestartable) {                _startNode = node;                m_baseIterator.setStartNode(node);                resetPosition();            }            return this;        }        /**         * Get the next node in the iteration.         *         * @return The next node handle in the iteration, or END.         */        public int next() {            int node;            while ((node = m_baseIterator.next()) != END) {                // Return only nodes that are in the selected namespace                if (getNSType(node) == m_nsType) {                    return returnNode(node);                }            }            return END;        }        /**         * Returns a deep copy of this iterator.  The cloned iterator is not         * reset.         *         * @return a deep copy of this iterator.         */        public DTMAxisIterator cloneIterator() {            try {                DTMAxisIterator nestedClone = m_baseIterator.cloneIterator();                NamespaceWildcardIterator clone =                    (NamespaceWildcardIterator) super.clone();                clone.m_baseIterator = nestedClone;                clone.m_nsType = m_nsType;                clone._isRestartable = false;                return clone;            } catch (CloneNotSupportedException e) {                BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR,                                          e.toString());                return null;            }        }        /**         * True if this iterator has a reversed axis.         *         * @return <code>true</code> if this iterator is a reversed axis.         */        public boolean isReverse() {            return m_baseIterator.isReverse();        }        public void setMark() {            m_baseIterator.setMark();        }        public void gotoMark() {            m_baseIterator.gotoMark();        }    }    /**     * Iterator that returns children within a given namespace for a     * given node. The functionality chould be achieved by putting a     * filter on top of a basic child iterator, but a specialised     * iterator is used for efficiency (both speed and size of translet).     */    public final class NamespaceChildrenIterator        extends InternalAxisIteratorBase    {        /** The extended type ID being requested. */        private final int _nsType;        /**         * Constructor NamespaceChildrenIterator         *         *         * @param type The extended type ID being requested.         */        public NamespaceChildrenIterator(final int type) {            _nsType = type;        }        /**         * Set start to END should 'close' the iterator,         * i.e. subsequent call to next() should return END.         *         * @param node Sets the root of the iteration.         *         * @return A DTMAxisIterator set to the start of the iteration.         */        public DTMAxisIterator setStartNode(int node) {            //%HZ%: Added reference to DTMDefaultBase.ROOTNODE back in, temporarily            if (node == DTMDefaultBase.ROOTNODE) {                node = getDocument();            }            if (_isRestartable) {                _startNode = node;                _currentNode = (node == DTM.NULL) ? DTM.NULL : NOTPROCESSED;                return resetPosition();            }            return this;        }        /**         * Get the next node in the iteration.         *         * @return The next node handle in the iteration, or END.         */

⌨️ 快捷键说明

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