📄 saximpl.java
字号:
/* * 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.5 2005/09/28 13:48:37 pvedula 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.Axis;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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -