transformerfactoryimpl.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,073 行 · 第 1/3 页
JAVA
1,073 行
/* * 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.processor;import org.xml.sax.InputSource;import org.xml.sax.helpers.XMLReaderFactory;import org.xml.sax.XMLReader;import javax.xml.transform.TransformerException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.XMLFilter;import org.w3c.dom.Node;import org.apache.xml.utils.TreeWalker;import org.apache.xml.utils.SystemIDResolver;import org.apache.xml.utils.DefaultErrorHandler;import org.apache.xalan.transformer.TransformerImpl;import org.apache.xalan.transformer.TransformerIdentityImpl;import org.apache.xalan.transformer.TrAXFilter;import org.apache.xalan.res.XSLMessages;import org.apache.xalan.res.XSLTErrorResources;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.Source;import javax.xml.transform.URIResolver;import javax.xml.transform.Templates;import javax.xml.transform.sax.TemplatesHandler;import javax.xml.transform.sax.TransformerHandler;import javax.xml.transform.sax.SAXTransformerFactory;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.dom.DOMResult;import javax.xml.transform.stream.StreamSource;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.ErrorListener;import java.io.IOException;import java.io.InputStream;import java.io.BufferedInputStream;import java.io.StringWriter;import java.io.PrintWriter;import java.io.StringReader;import java.util.Properties;import java.util.Enumeration;import org.apache.xalan.transformer.XalanProperties;/** * The TransformerFactoryImpl, which implements the TRaX TransformerFactory * interface, processes XSLT stylesheets into a Templates object * (a StylesheetRoot). */public class TransformerFactoryImpl extends SAXTransformerFactory{ /** * The path/filename of the property file: XSLTInfo.properties * Maintenance note: see also org.apache.xpath.functions.FuncSystemProperty.XSLT_PROPERTIES */ public static String XSLT_PROPERTIES = "org/apache/xalan/res/XSLTInfo.properties"; /** * Constructor TransformerFactoryImpl * */ public TransformerFactoryImpl() { } /** a zero length Class array used in loadPropertyFileToSystem() */ private static final Class[] NO_CLASSES = new Class[0]; /** a zero length Object array used in loadPropertyFileToSystem() */ private static final Object[] NO_OBJS = new Object[0]; /** Static string to be used for incremental feature */ public static final String FEATURE_INCREMENTAL = "http://xml.apache.org/xalan/features/incremental"; /** Static string to be used for optimize feature */ public static final String FEATURE_OPTIMIZE = "http://xml.apache.org/xalan/features/optimize"; /** Static string to be used for source_location feature */ public static final String FEATURE_SOURCE_LOCATION = XalanProperties.SOURCE_LOCATION; /** * Retrieve a propery bundle from XSLT_PROPERTIES and load it * int the System properties. */ static { try { InputStream is = null; try { Properties props = new Properties(); try { java.lang.reflect.Method getCCL = Thread.class.getMethod("getContextClassLoader", NO_CLASSES); if (getCCL != null) { ClassLoader contextClassLoader = (ClassLoader) getCCL.invoke(Thread.currentThread(), NO_OBJS); is = contextClassLoader.getResourceAsStream(XSLT_PROPERTIES); // file should be already fully specified } } catch (Exception e) {} if (is == null) { // NOTE! For the below getResourceAsStream in Sun JDK 1.1.8M // we apparently must add the leading slash character - I // don't know why, but if it's not there, we throw an NPE from the below loading is = TransformerFactoryImpl.class.getResourceAsStream("/" + XSLT_PROPERTIES); // file should be already fully specified } // get a buffered version BufferedInputStream bis = new BufferedInputStream(is); props.load(bis); // and load up the property bag from this bis.close(); // close out after reading // OK, now we only want to set system properties that // are not already set. Properties systemProps = System.getProperties(); Enumeration propEnum = props.propertyNames(); while (propEnum.hasMoreElements()) { String prop = (String) propEnum.nextElement(); if (!systemProps.containsKey(prop)) systemProps.put(prop, props.getProperty(prop)); } System.setProperties(systemProps); } catch (Exception ex){} } catch (SecurityException se) { // In this case the caller is required to have // the needed attributes already defined. } }public javax.xml.transform.Templates processFromNode(Node node) throws TransformerConfigurationException { try { TemplatesHandler builder = newTemplatesHandler(); TreeWalker walker = new TreeWalker(builder, new org.apache.xpath.DOM2Helper(), builder.getSystemId()); walker.traverse(node); return builder.getTemplates(); } catch (org.xml.sax.SAXException se) { if (m_errorListener != null) { try { m_errorListener.fatalError(new TransformerException(se)); } catch (TransformerException ex) { throw new TransformerConfigurationException(ex); } return null; } else // Should remove this later... but right now diagnostics from // TransformerConfigurationException are not good. // se.printStackTrace(); throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se); //"processFromNode failed", //se); } catch (TransformerConfigurationException tce) { // Assume it's already been reported to the error listener. throw tce; } /* catch (TransformerException tce) { // Assume it's already been reported to the error listener. throw new TransformerConfigurationException(tce.getMessage(), tce); }*/ catch (Exception e) { if (m_errorListener != null) { try { m_errorListener.fatalError(new TransformerException(e)); } catch (TransformerException ex) { throw new TransformerConfigurationException(ex); } return null; } else // Should remove this later... but right now diagnostics from // TransformerConfigurationException are not good. // se.printStackTrace(); throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e); //"processFromNode failed", //e); } } /** * The systemID that was specified in * processFromNode(Node node, String systemID). */ private String m_DOMsystemID = null; /** * The systemID that was specified in * processFromNode(Node node, String systemID). * * @return The systemID, or null. */ String getDOMsystemID() { return m_DOMsystemID; } /** * Process the stylesheet from a DOM tree, if the * processor supports the "http://xml.org/trax/features/dom/input" * feature. * * @param node A DOM tree which must contain * valid transform instructions that this processor understands. * @param systemID The systemID from where xsl:includes and xsl:imports * should be resolved from. * * @return A Templates object capable of being used for transformation purposes. * * @throws TransformerConfigurationException */ javax.xml.transform.Templates processFromNode(Node node, String systemID) throws TransformerConfigurationException { m_DOMsystemID = systemID; return processFromNode(node); } /** * Get InputSource specification(s) that are associated with the * given document specified in the source param, * via the xml-stylesheet processing instruction * (see http://www.w3.org/TR/xml-stylesheet/), and that matches * the given criteria. Note that it is possible to return several stylesheets * that match the criteria, in which case they are applied as if they were * a list of imports or cascades. * * <p>Note that DOM2 has it's own mechanism for discovering stylesheets. * Therefore, there isn't a DOM version of this method.</p> * * * @param source The XML source that is to be searched. * @param media The media attribute to be matched. May be null, in which * case the prefered templates will be used (i.e. alternate = no). * @param title The value of the title attribute to match. May be null. * @param charset The value of the charset attribute to match. May be null. * * @return A Source object capable of being used to create a Templates object. * * @throws TransformerConfigurationException */ public Source getAssociatedStylesheet( Source source, String media, String title, String charset) throws TransformerConfigurationException { String baseID; InputSource isource = null; Node node = null; XMLReader reader = null; if (source instanceof DOMSource) { DOMSource dsource = (DOMSource) source; node = dsource.getNode(); baseID = dsource.getSystemId();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?