📄 traxliaison.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. * */package org.apache.tools.ant.taskdefs.optional;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Hashtable;import java.util.Vector;import java.util.Enumeration;import java.net.URL;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParserFactory;import javax.xml.transform.ErrorListener;import javax.xml.transform.Source;import javax.xml.transform.SourceLocator;import javax.xml.transform.Templates;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.URIResolver;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;import javax.xml.transform.TransformerConfigurationException;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.taskdefs.XSLTLiaison3;import org.apache.tools.ant.taskdefs.XSLTLogger;import org.apache.tools.ant.taskdefs.XSLTLoggerAware;import org.apache.tools.ant.taskdefs.XSLTProcess;import org.apache.tools.ant.types.XMLCatalog;import org.apache.tools.ant.types.Resource;import org.apache.tools.ant.types.resources.FileResource;import org.apache.tools.ant.types.resources.URLResource;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.JAXPUtils;import org.xml.sax.EntityResolver;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.XMLReader;/** * Concrete liaison for XSLT processor implementing TraX. (ie JAXP 1.1) * * @since Ant 1.3 */public class TraXLiaison implements XSLTLiaison3, ErrorListener, XSLTLoggerAware { /** * Helper for transforming filenames to URIs. * * @since Ant 1.7 */ private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** * The current <code>Project</code> */ private Project project; /** * the name of the factory implementation class to use * or null for default JAXP lookup. */ private String factoryName = null; /** The trax TransformerFactory */ private TransformerFactory tfactory = null; /** stylesheet to use for transformation */ private Resource stylesheet; private XSLTLogger logger; /** possible resolver for publicIds */ private EntityResolver entityResolver; /** transformer to use for processing files */ private Transformer transformer; /** The In memory version of the stylesheet */ private Templates templates; /** * The modification time of the stylesheet from which the templates * are read */ private long templatesModTime; /** possible resolver for URIs */ private URIResolver uriResolver; /** transformer output properties */ private Vector outputProperties = new Vector(); /** stylesheet parameters */ private Hashtable params = new Hashtable(); /** factory attributes */ private Vector attributes = new Vector(); /** * Constructor for TraXLiaison. * @throws Exception never */ public TraXLiaison() throws Exception { } /** * Set the stylesheet file. * @param stylesheet a <code>File</code> value * @throws Exception on error */ public void setStylesheet(File stylesheet) throws Exception { FileResource fr = new FileResource(); fr.setProject(project); fr.setFile(stylesheet); setStylesheet(fr); } /** * Set the stylesheet file. * @param stylesheet a {@link org.apache.tools.ant.types.Resource} value * @throws Exception on error */ public void setStylesheet(Resource stylesheet) throws Exception { if (this.stylesheet != null) { // resetting the stylesheet - reset transformer transformer = null; // do we need to reset templates as well if (!this.stylesheet.equals(stylesheet) || (stylesheet.getLastModified() != templatesModTime)) { templates = null; } } this.stylesheet = stylesheet; } /** * Transform an input file. * @param infile the file to transform * @param outfile the result file * @throws Exception on error */ public void transform(File infile, File outfile) throws Exception { if (transformer == null) { createTransformer(); } InputStream fis = null; OutputStream fos = null; try { fis = new BufferedInputStream(new FileInputStream(infile)); fos = new BufferedOutputStream(new FileOutputStream(outfile)); StreamResult res = new StreamResult(fos); // not sure what could be the need of this... res.setSystemId(JAXPUtils.getSystemId(outfile)); Source src = getSource(fis, infile); // set parameters on each transformation, maybe something has changed //(e.g. value of file name parameter) setTransformationParameters(); transformer.transform(src, res); } finally { // make sure to close all handles, otherwise the garbage // collector will close them...whenever possible and // Windows may complain about not being able to delete files. try { if (fis != null) { fis.close(); } } catch (IOException ignored) { // ignore } try { if (fos != null) { fos.close(); } } catch (IOException ignored) { // ignore } } } /** * Get the source instance from the stream and id of the file. * @param is the stream containing the stylesheet data. * @param infile the file that will be used for the systemid. * @return the configured source instance matching the stylesheet. * @throws ParserConfigurationException if a parser cannot be created which * satisfies the requested configuration. * @throws SAXException in case of problem detected by the SAX parser. */ private Source getSource(InputStream is, File infile) throws ParserConfigurationException, SAXException { // todo: is this comment still relevant ?? // FIXME: need to use a SAXSource as the source for the transform // so we can plug in our own entity resolver Source src = null; if (entityResolver != null) { if (getFactory().getFeature(SAXSource.FEATURE)) { SAXParserFactory spFactory = SAXParserFactory.newInstance(); spFactory.setNamespaceAware(true); XMLReader reader = spFactory.newSAXParser().getXMLReader(); reader.setEntityResolver(entityResolver); src = new SAXSource(reader, new InputSource(is)); } else { throw new IllegalStateException("xcatalog specified, but " + "parser doesn't support SAX"); } } else { // WARN: Don't use the StreamSource(File) ctor. It won't work with // xalan prior to 2.2 because of systemid bugs. src = new StreamSource(is); } src.setSystemId(JAXPUtils.getSystemId(infile)); return src; } private Source getSource(InputStream is, Resource resource) throws ParserConfigurationException, SAXException { // todo: is this comment still relevant ?? // FIXME: need to use a SAXSource as the source for the transform // so we can plug in our own entity resolver Source src = null; if (entityResolver != null) { if (getFactory().getFeature(SAXSource.FEATURE)) { SAXParserFactory spFactory = SAXParserFactory.newInstance(); spFactory.setNamespaceAware(true); XMLReader reader = spFactory.newSAXParser().getXMLReader(); reader.setEntityResolver(entityResolver); src = new SAXSource(reader, new InputSource(is)); } else { throw new IllegalStateException("xcatalog specified, but " + "parser doesn't support SAX"); } } else { // WARN: Don't use the StreamSource(File) ctor. It won't work with // xalan prior to 2.2 because of systemid bugs. src = new StreamSource(is); } // The line below is a hack: the system id must an URI, but it is not // cleat to get the URI of an resource, so just set the name of the // resource as a system id src.setSystemId(resourceToURI(resource)); return src; } private String resourceToURI(Resource resource) { if (resource instanceof FileResource) { File f = ((FileResource) resource).getFile(); return FILE_UTILS.toURI(f.getAbsolutePath()); } if (resource instanceof URLResource) { URL u = ((URLResource) resource).getURL(); return String.valueOf(u); } else { return resource.getName(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -