transformerimpl.java
来自「java jdk 1.4的源码」· Java 代码 · 共 2,145 行 · 第 1/5 页
JAVA
2,145 行
/* * 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.transformer;// Java importsimport java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.StringWriter;import java.io.UnsupportedEncodingException;import java.util.Enumeration;import java.util.NoSuchElementException;import java.util.Properties;import java.util.Stack;import java.util.StringTokenizer;import java.util.Vector;import javax.xml.parsers.DocumentBuilder;import javax.xml.transform.ErrorListener;import javax.xml.transform.OutputKeys;import javax.xml.transform.Result;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.URIResolver;import javax.xml.transform.dom.DOMResult;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.stream.StreamResult;import org.apache.xalan.processor.TransformerFactoryImpl;import org.apache.xalan.res.XSLMessages;import org.apache.xalan.res.XSLTErrorResources;import org.apache.xalan.serialize.Method;import org.apache.xalan.serialize.Serializer;import org.apache.xalan.serialize.SerializerFactory;import org.apache.xalan.templates.AVT;import org.apache.xalan.templates.Constants;import org.apache.xalan.templates.ElemAttributeSet;import org.apache.xalan.templates.ElemForEach;import org.apache.xalan.templates.ElemSort;import org.apache.xalan.templates.ElemTemplate;import org.apache.xalan.templates.ElemTemplateElement;import org.apache.xalan.templates.ElemTextLiteral;import org.apache.xalan.templates.ElemVariable;import org.apache.xalan.templates.OutputProperties;import org.apache.xalan.templates.Stylesheet;import org.apache.xalan.templates.StylesheetComposed;import org.apache.xalan.templates.StylesheetRoot;import org.apache.xalan.templates.WhiteSpaceInfo;import org.apache.xalan.templates.XUnresolvedVariable;import org.apache.xalan.trace.TraceManager;import org.apache.xml.dtm.DTM;import org.apache.xml.dtm.DTMIterator;import org.apache.xml.dtm.DTMManager;import org.apache.xml.dtm.DTMWSFilter;import org.apache.xml.utils.BoolStack;import org.apache.xml.utils.DOMBuilder;import org.apache.xml.utils.NodeVector;import org.apache.xml.utils.ObjectPool;import org.apache.xml.utils.ObjectStack;import org.apache.xml.utils.QName;import org.apache.xml.utils.SAXSourceLocator;import org.apache.xml.utils.WrappedRuntimeException;import org.apache.xpath.Arg;import org.apache.xpath.DOMHelper;import org.apache.xpath.VariableStack;import org.apache.xpath.XPathContext;import org.apache.xpath.objects.XObject;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.Node;import org.w3c.dom.Text;import org.xml.sax.ContentHandler;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.SAXParseException;import org.xml.sax.ext.DeclHandler;import org.xml.sax.ext.LexicalHandler;//dmlimport org.apache.xpath.ExtensionsProvider;import org.apache.xalan.extensions.ExtensionsTable;/** * <meta name="usage" content="advanced"/> * This class implements the * {@link javax.xml.transform.Transformer} interface, and is the core * representation of the transformation execution.</p> */public class TransformerImpl extends Transformer implements Runnable, DTMWSFilter, ExtensionsProvider{ // Synch object to gaurd against setting values from the TrAX interface // or reentry while the transform is going on. /** NEEDSDOC Field m_reentryGuard */ private Boolean m_reentryGuard = new Boolean(true); /** * This is null unless we own the stream. */ private java.io.FileOutputStream m_outputStream = null; /** * True if the parser events should be on the main thread, * false if not. Experemental. Can not be set right now. */ private boolean m_parserEventsOnMain = true; /** The thread that the transformer is running on. */ private Thread m_transformThread; /** The base URL of the source tree. */ private String m_urlOfSource = null; /** The Result object at the start of the transform, if any. */ private Result m_outputTarget = null; /** * The output format object set by the user. May be null. */ private OutputProperties m_outputFormat; /** The output serializer */ private Serializer m_serializer; /** * The content handler for the source input tree. */ ContentHandler m_inputContentHandler; /** * The content handler for the result tree. */ private ContentHandler m_outputContentHandler = null; // /* // * Use member variable to store param variables as they're // * being created, use member variable so we don't // * have to create a new vector every time. // */ // private Vector m_newVars = new Vector(); /** The JAXP Document Builder, mainly to create Result Tree Fragments. */ DocumentBuilder m_docBuilder = null; /** * A pool of ResultTreeHandlers, for serialization of a subtree to text. * Please note that each of these also holds onto a Text Serializer. */ private ObjectPool m_textResultHandlerObjectPool = new ObjectPool("org.apache.xalan.transformer.ResultTreeHandler"); /** * Related to m_textResultHandlerObjectPool, this is a pool of * StringWriters, which are passed to the Text Serializers. * (I'm not sure if this is really needed any more. -sb) */ private ObjectPool m_stringWriterObjectPool = new ObjectPool("java.io.StringWriter"); /** * A static text format object, which can be used over and * over to create the text serializers. */ private OutputProperties m_textformat = new OutputProperties(Method.Text); // Commenteded out in response to problem reported by // Nicola Brown <Nicola.Brown@jacobsrimell.com> // /** // * Flag to let us know if an exception should be reported inside the // * postExceptionFromThread method. This is needed if the transform is // * being generated from SAX events, and thus there is no central place // * to report the exception from. (An exception is usually picked up in // * the main thread from the transform thread in {@link #transform(Source source)} // * from {@link #getExceptionThrown()}. ) // */ // private boolean m_reportInPostExceptionFromThread = false; /** * A node vector used as a stack to track the current * ElemTemplateElement. Needed for the * org.apache.xalan.transformer.TransformState interface, * so a tool can discover the calling template. Note the use of an array * for this limits the recursion depth to 4K. */ ObjectStack m_currentTemplateElements = new ObjectStack(XPathContext.RECURSIONLIMIT); /** The top of the currentTemplateElements stack. */ //int m_currentTemplateElementsTop = 0; /** * A node vector used as a stack to track the current * ElemTemplate that was matched. * Needed for the * org.apache.xalan.transformer.TransformState interface, * so a tool can discover the matched template */ Stack m_currentMatchTemplates = new Stack(); /** * A node vector used as a stack to track the current * node that was matched. * Needed for the * org.apache.xalan.transformer.TransformState interface, * so a tool can discover the matched * node. */ NodeVector m_currentMatchedNodes = new NodeVector(); /** * The root of a linked set of stylesheets. */ private StylesheetRoot m_stylesheetRoot = null; /** * If this is set to true, do not warn about pattern * match conflicts. */ private boolean m_quietConflictWarnings = true; /** * The liason to the XML parser, so the XSL processor * can handle included files, and the like, and do the * initial parse of the XSL document. */ private XPathContext m_xcontext; /** * Object to guard agains infinite recursion when * doing queries. */ private StackGuard m_stackGuard; /** * Output handler to bottleneck SAX events. */ private ResultTreeHandler m_resultTreeHandler; /** The key manager, which manages xsl:keys. */ private KeyManager m_keyManager = new KeyManager(); /** * Stack for the purposes of flagging infinite recursion with * attribute sets. */ Stack m_attrSetStack = null; /** * The table of counters for xsl:number support. * @see ElemNumber */ CountersTable m_countersTable = null; /** * Is > 0 when we're processing a for-each. */ BoolStack m_currentTemplateRuleIsNull = new BoolStack(); /** * The message manager, which manages error messages, warning * messages, and other types of message events. */ private MsgMgr m_msgMgr; /** * This is a compile-time flag to turn off calling * of trace listeners. Set this to false for optimization purposes. */ public static boolean S_DEBUG = false; /** * The SAX error handler, where errors and warnings are sent. */ private ErrorListener m_errorHandler = new org.apache.xml.utils.DefaultErrorHandler(); /** * The trace manager. */ private TraceManager m_traceManager = new TraceManager(this); /** * If the transform thread throws an exception, the exception needs to * be stashed away so that the main thread can pass it on to the * client. */ private Exception m_exceptionThrown = null; /** * The InputSource for the source tree, which is needed if the * parse thread is not the main thread, in order for the parse * thread's run method to get to the input source. * (Delete this if reversing threads is outlawed. -sb) */ private Source m_xmlSource; /** * This is needed for support of setSourceTreeDocForThread(Node doc), * which must be called in order for the transform thread's run * method to obtain the root of the source tree to be transformed. */ private int m_doc; /** * If the the transform is on the secondary thread, we * need to know when it is done, so we can return. */ private boolean m_isTransformDone = false; /** Flag to to tell if the tranformer needs to be reset. */ private boolean m_hasBeenReset = false; /** NEEDSDOC Field m_shouldReset */ private boolean m_shouldReset = true; /** * NEEDSDOC Method setShouldReset * * * NEEDSDOC @param shouldReset */ public void setShouldReset(boolean shouldReset) { m_shouldReset = shouldReset; } /** * A stack of current template modes. */ private Stack m_modes = new Stack(); //========================================================== // SECTION: Constructor //========================================================== /** * Construct a TransformerImpl. * * @param stylesheet The root of the stylesheet tree. */ public TransformerImpl(StylesheetRoot stylesheet) // throws javax.xml.transform.TransformerException { setStylesheet(stylesheet); setXPathContext(new XPathContext(this)); getXPathContext().setNamespaceContext(stylesheet); m_stackGuard = new StackGuard(this); } // ================ ExtensionsTable =================== /** * The table of ExtensionHandlers. */ private ExtensionsTable m_extensionsTable = null; /** * Get the extensions table object. * * @return The extensions table. */ public ExtensionsTable getExtensionsTable() { return m_extensionsTable; } /** * If the stylesheet contains extensions, set the extensions table object. * * * @param sroot The stylesheet. * @throws javax.xml.transform.TransformerException
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?