saximpl.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,896 行 · 第 1/4 页
JAVA
1,896 行
/* * Copyright 2001-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: SAXImpl.java,v 1.19 2004/02/24 04:21:50 zongaro Exp $ */package com.sun.org.apache.xalan.internal.xsltc.dom;import java.net.URL;import java.net.MalformedURLException;import java.util.Enumeration;import javax.xml.transform.Source;import javax.xml.transform.dom.DOMSource;import com.sun.org.apache.xalan.internal.xsltc.DOM;import com.sun.org.apache.xalan.internal.xsltc.DOMEnhancedForDTM;import com.sun.org.apache.xalan.internal.xsltc.StripFilter;import com.sun.org.apache.xalan.internal.xsltc.TransletException;import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;import com.sun.org.apache.xalan.internal.xsltc.runtime.Hashtable;import com.sun.org.apache.xml.internal.dtm.DTM;import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;import com.sun.org.apache.xml.internal.dtm.DTMManager;import com.sun.org.apache.xml.internal.dtm.DTMWSFilter;import com.sun.org.apache.xml.internal.dtm.ref.DTMAxisIterNodeList;import com.sun.org.apache.xml.internal.dtm.ref.DTMDefaultBase;import com.sun.org.apache.xml.internal.dtm.ref.EmptyIterator;import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeProxy;import com.sun.org.apache.xml.internal.dtm.ref.sax2dtm.SAX2DTM2;import com.sun.org.apache.xml.internal.serializer.SerializationHandler;import com.sun.org.apache.xml.internal.serializer.ToXMLSAXHandler;import com.sun.org.apache.xml.internal.utils.XMLStringFactory;import com.sun.org.apache.xml.internal.utils.SystemIDResolver;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Entity;import org.xml.sax.Attributes;import org.xml.sax.SAXException;/** * SAXImpl is the core model for SAX input source. SAXImpl objects are * usually created from an XSLTCDTMManager. * * <p>DOMSource inputs are handled using DOM2SAX + SAXImpl. SAXImpl has a * few specific fields (e.g. _node2Ids, _document) to keep DOM-related * information. They are used when the processing behavior between DOM and * SAX has to be different. Examples of these include id function and * unparsed entity. * * <p>SAXImpl extends SAX2DTM2 instead of SAX2DTM for better performance. * @author Jacek Ambroziak * @author Santiago Pericas-Geertsen * @author Morten Jorgensen * @author Douglas Sellers <douglasjsellers@hotmail.com> */public final class SAXImpl extends SAX2DTM2 implements DOMEnhancedForDTM, DOMBuilder{ /* ------------------------------------------------------------------- */ /* DOMBuilder fields BEGIN */ /* ------------------------------------------------------------------- */ // Namespace prefix-to-uri mapping stuff private int _uriCount = 0; private int _prefixCount = 0; // Stack used to keep track of what whitespace text nodes are protected // by xml:space="preserve" attributes and which nodes that are not. private int[] _xmlSpaceStack; private int _idx = 1; private boolean _preserve = false; private static final String XML_STRING = "xml:"; private static final String XML_PREFIX = "xml"; private static final String XMLSPACE_STRING = "xml:space"; private static final String PRESERVE_STRING = "preserve"; private static final String XMLNS_PREFIX = "xmlns"; private static final String XML_URI = "http://www.w3.org/XML/1998/namespace"; private boolean _escaping = true; private boolean _disableEscaping = false; private int _textNodeToProcess = DTM.NULL; /* ------------------------------------------------------------------- */ /* DOMBuilder fields END */ /* ------------------------------------------------------------------- */ // empty String for null attribute values private final static String EMPTYSTRING = ""; // empty iterator to be returned when there are no children private final static DTMAxisIterator EMPTYITERATOR = EmptyIterator.getInstance(); // The number of expanded names private int _namesSize = -1; // Namespace related stuff private Hashtable _nsIndex = new Hashtable(); // The initial size of the text buffer private int _size = 0; // Tracks which textnodes are not escaped private BitArray _dontEscape = null; // The URI to this document private String _documentURI = null; static private int _documentURIIndex = 0; // The owner Document when the input source is DOMSource. private Document _document; // The hashtable for org.w3c.dom.Node to node id mapping. // This is only used when the input is a DOMSource and the // buildIdIndex flag is true. private Hashtable _node2Ids = null; // True if the input source is a DOMSource. private boolean _hasDOMSource = false; // The DTMManager private XSLTCDTMManager _dtmManager; // Support for access/navigation through org.w3c.dom API private Node[] _nodes; private NodeList[] _nodeLists; private final static String XML_LANG_ATTRIBUTE = "http://www.w3.org/XML/1998/namespace:@lang"; /** * Define the origin of the document from which the tree was built */ public void setDocumentURI(String uri) { if (uri != null) { setDocumentBaseURI(SystemIDResolver.getAbsoluteURI(uri)); } } /** * Returns the origin of the document from which the tree was built */ public String getDocumentURI() { String baseURI = getDocumentBaseURI(); return (baseURI != null) ? baseURI : "rtf" + _documentURIIndex++; } public String getDocumentURI(int node) { return getDocumentURI(); } public void setupMapping(String[] names, String[] urisArray, int[] typesArray, String[] namespaces) { // This method only has a function in DOM adapters } /** * Lookup a namespace URI from a prefix starting at node. This method * is used in the execution of xsl:element when the prefix is not known * at compile time. */ public String lookupNamespace(int node, String prefix) throws TransletException { int anode, nsnode; final AncestorIterator ancestors = new AncestorIterator(); if (isElement(node)) { ancestors.includeSelf(); } ancestors.setStartNode(node); while ((anode = ancestors.next()) != DTM.NULL) { final NamespaceIterator namespaces = new NamespaceIterator(); namespaces.setStartNode(anode); while ((nsnode = namespaces.next()) != DTM.NULL) { if (getLocalName(nsnode).equals(prefix)) { return getNodeValue(nsnode); } } } BasisLibrary.runTimeError(BasisLibrary.NAMESPACE_PREFIX_ERR, prefix); return null; } /** * Returns 'true' if a specific node is an element (of any type) */ public boolean isElement(final int node) { return getNodeType(node) == DTM.ELEMENT_NODE; } /** * Returns 'true' if a specific node is an attribute (of any type) */ public boolean isAttribute(final int node) { return getNodeType(node) == DTM.ATTRIBUTE_NODE; } /** * Returns the number of nodes in the tree (used for indexing) */ public int getSize() { return getNumberOfNodes(); } /** * Part of the DOM interface - no function here. */ public void setFilter(StripFilter filter) { } /** * Returns true if node1 comes before node2 in document order */ public boolean lessThan(int node1, int node2) { if (node1 == DTM.NULL) { return false; } if (node2 == DTM.NULL) { return true; } return (node1 < node2); } /** * Create an org.w3c.dom.Node from a node in the tree */ public Node makeNode(int index) { if (_nodes == null) { _nodes = new Node[_namesSize]; } int nodeID = makeNodeIdentity(index); if (nodeID < 0) { return null; } else if (nodeID < _nodes.length) { return (_nodes[nodeID] != null) ? _nodes[nodeID] : (_nodes[nodeID] = new DTMNodeProxy((DTM)this, index)); } else { return new DTMNodeProxy((DTM)this, index); } } /** * Create an org.w3c.dom.Node from a node in an iterator * The iterator most be started before this method is called */ public Node makeNode(DTMAxisIterator iter) { return makeNode(iter.next()); } /** * Create an org.w3c.dom.NodeList from a node in the tree */ public NodeList makeNodeList(int index) { if (_nodeLists == null) { _nodeLists = new NodeList[_namesSize]; } int nodeID = makeNodeIdentity(index); if (nodeID < 0) { return null; } else if (nodeID < _nodeLists.length) { return (_nodeLists[nodeID] != null) ? _nodeLists[nodeID] : (_nodeLists[nodeID] = new DTMAxisIterNodeList(this, new SingletonIterator(index))); } else { return new DTMAxisIterNodeList(this, new SingletonIterator(index)); } } /** * Create an org.w3c.dom.NodeList from a node iterator * The iterator most be started before this method is called */ public NodeList makeNodeList(DTMAxisIterator iter) { return new DTMAxisIterNodeList(this, iter); } /** * Iterator that returns the namespace nodes as defined by the XPath data * model for a given node, filtered by extended type ID. */ public class TypedNamespaceIterator extends NamespaceIterator { private String _nsPrefix; /** * Constructor TypedChildrenIterator * * * @param nodeType The extended type ID being requested. */ public TypedNamespaceIterator(int nodeType) { super(); if(m_expandedNameTable != null){ _nsPrefix = m_expandedNameTable.getLocalName(nodeType); } } /** * Get the next node in the iteration. * * @return The next node handle in the iteration, or END. */ public int next() { if ((_nsPrefix == null) ||(_nsPrefix.length() == 0) ){ return (END); } int node = END; for (node = super.next(); node != END; node = super.next()) { if (_nsPrefix.compareTo(getLocalName(node))== 0) { return returnNode(node); } } return (END); } } // end of TypedNamespaceIterator /************************************************************** * This is a specialised iterator for predicates comparing node or * attribute values to variable or parameter values. */ private final class NodeValueIterator extends InternalAxisIteratorBase { private DTMAxisIterator _source; private String _value; private boolean _op; private final boolean _isReverse; private int _returnType = RETURN_PARENT; public NodeValueIterator(DTMAxisIterator source, int returnType, String value, boolean op) { _source = source; _returnType = returnType; _value = value; _op = op; _isReverse = source.isReverse(); } public boolean isReverse() { return _isReverse; } public DTMAxisIterator cloneIterator() { try { NodeValueIterator clone = (NodeValueIterator)super.clone(); clone._isRestartable = false; clone._source = _source.cloneIterator(); clone._value = _value; clone._op = _op; return clone.reset(); } catch (CloneNotSupportedException e) { BasisLibrary.runTimeError(BasisLibrary.ITERATOR_CLONE_ERR, e.toString()); return null; } } public void setRestartable(boolean isRestartable) { _isRestartable = isRestartable; _source.setRestartable(isRestartable); } public DTMAxisIterator reset() { _source.reset(); return resetPosition(); } public int next() { int node; while ((node = _source.next()) != END) { String val = getStringValueX(node); if (_value.equals(val) == _op) { if (_returnType == RETURN_CURRENT) { return returnNode(node); } else { return returnNode(getParent(node)); } } } return END; } public DTMAxisIterator setStartNode(int node) { if (_isRestartable) { _source.setStartNode(_startNode = node); return resetPosition(); } return this; } public void setMark() { _source.setMark(); } public void gotoMark() { _source.gotoMark(); } } // end NodeValueIterator public DTMAxisIterator getNodeValueIterator(DTMAxisIterator iterator, int type, String value, boolean op) { return(DTMAxisIterator)(new NodeValueIterator(iterator, type, value, op)); } /** * Encapsulates an iterator in an OrderedIterator to ensure node order */ public DTMAxisIterator orderNodes(DTMAxisIterator source, int node) { return new DupFilterIterator(source); } /** * Returns singleton iterator containg the document root * Works for them main document (mark == 0) */ public DTMAxisIterator getIterator() { return new SingletonIterator(getDocument()); } /** * Get mapping from DOM namespace types to external namespace types */ public int getNSType(int node) { String s = getNamespaceURI(node); if (s == null) { return 0;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?