📄 xsltprocess.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;import java.io.File;import java.util.Enumeration;import java.util.Iterator;import java.util.Vector;import org.apache.tools.ant.AntClassLoader;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.DirectoryScanner;import org.apache.tools.ant.DynamicConfigurator;import org.apache.tools.ant.Project;import org.apache.tools.ant.types.Mapper;import org.apache.tools.ant.types.Path;import org.apache.tools.ant.types.Reference;import org.apache.tools.ant.types.Resource;import org.apache.tools.ant.types.ResourceCollection;import org.apache.tools.ant.types.XMLCatalog;import org.apache.tools.ant.types.resources.FileResource;import org.apache.tools.ant.types.resources.Resources;import org.apache.tools.ant.types.resources.Union;import org.apache.tools.ant.util.FileNameMapper;import org.apache.tools.ant.util.FileUtils;/** * Processes a set of XML documents via XSLT. This is * useful for building views of XML based documentation. * * * @since Ant 1.1 * * @ant.task name="xslt" category="xml" */public class XSLTProcess extends MatchingTask implements XSLTLogger { /** destination directory */ private File destDir = null; /** where to find the source XML file, default is the project's basedir */ private File baseDir = null; /** XSL stylesheet as a filename */ private String xslFile = null; /** XSL stylesheet as a {@link org.apache.tools.ant.types.Resource} */ private Resource xslResource = null; /** extension of the files produced by XSL processing */ private String targetExtension = ".html"; /** name for XSL parameter containing the filename */ private String fileNameParameter = null; /** name for XSL parameter containing the file directory */ private String fileDirParameter = null; /** additional parameters to be passed to the stylesheets */ private Vector params = new Vector(); /** Input XML document to be used */ private File inFile = null; /** Output file */ private File outFile = null; /** The name of the XSL processor to use */ private String processor; /** Classpath to use when trying to load the XSL processor */ private Path classpath = null; /** The Liason implementation to use to communicate with the XSL * processor */ private XSLTLiaison liaison; /** Flag which indicates if the stylesheet has been loaded into * the processor */ private boolean stylesheetLoaded = false; /** force output of target files even if they already exist */ private boolean force = false; /** XSL output properties to be used */ private Vector outputProperties = new Vector(); /** for resolving entities such as dtds */ private XMLCatalog xmlCatalog = new XMLCatalog(); /** Name of the TRAX Liaison class */ private static final String TRAX_LIAISON_CLASS = "org.apache.tools.ant.taskdefs.optional.TraXLiaison"; /** Utilities used for file operations */ private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** * Whether to style all files in the included directories as well. * * @since Ant 1.5 */ private boolean performDirectoryScan = true; /** * factory element for TraX processors only * @since Ant 1.6 */ private Factory factory = null; /** * whether to reuse Transformer if transforming multiple files. * @since 1.5.2 */ private boolean reuseLoadedStylesheet = true; /** * AntClassLoader for the nested <classpath> - if set. * * <p>We keep this here in order to reset the context classloader * in execute. We can't use liaison.getClass().getClassLoader() * since the actual liaison class may have been loaded by a loader * higher up (system classloader, for example).</p> * * @since Ant 1.6.2 */ private AntClassLoader loader = null; /** * Mapper to use when a set of files gets processed. * * @since Ant 1.6.2 */ private Mapper mapperElement = null; /** * Additional resource collections to process. * * @since Ant 1.7 */ private Union resources = new Union(); /** * Whether to use the implicit fileset. * * @since Ant 1.7 */ private boolean useImplicitFileset = true; /** * The default processor is trax * @since Ant 1.7 */ public static final String PROCESSOR_TRAX = "trax"; /** * Creates a new XSLTProcess Task. */ public XSLTProcess() { } //-- XSLTProcess /** * Whether to style all files in the included directories as well; * optional, default is true. * * @param b true if files in included directories are processed. * @since Ant 1.5 */ public void setScanIncludedDirectories(boolean b) { performDirectoryScan = b; } /** * Controls whether the stylesheet is reloaded for every transform. * * <p>Setting this to true may get around a bug in certain * Xalan-J versions, default is false.</p> * @param b a <code>boolean</code> value * @since Ant 1.5.2 */ public void setReloadStylesheet(boolean b) { reuseLoadedStylesheet = !b; } /** * Defines the mapper to map source to destination files. * @param mapper the mapper to use * @exception BuildException if more than one mapper is defined * @since Ant 1.6.2 */ public void addMapper(Mapper mapper) { if (mapperElement != null) { throw new BuildException("Cannot define more than one mapper", getLocation()); } mapperElement = mapper; } /** * Adds a collection of resources to style in addition to the * given file or the implicit fileset. * * @param rc the collection of resources to style * @since Ant 1.7 */ public void add(ResourceCollection rc) { resources.add(rc); } /** * Add a nested <style> element. * @param rc the configured Resources object represented as <style>. * @since Ant 1.7 */ public void addConfiguredStyle(Resources rc) { if (rc.size() != 1) { throw new BuildException("The style element must be specified" + " with exactly one nested resource."); } setXslResource((Resource) rc.iterator().next()); } /** * API method to set the XSL Resource. * @param xslResource Resource to set as the stylesheet. * @since Ant 1.7 */ public void setXslResource(Resource xslResource) { this.xslResource = xslResource; } /** * Adds a nested filenamemapper. * @param fileNameMapper the mapper to add * @exception BuildException if more than one mapper is defined * @since Ant 1.7.0 */ public void add(FileNameMapper fileNameMapper) throws BuildException { Mapper mapper = new Mapper(getProject()); mapper.add(fileNameMapper); addMapper(mapper); } /** * Executes the task. * * @exception BuildException if there is an execution problem. * @todo validate that if either in or our is defined, then both are */ public void execute() throws BuildException { if ("style".equals(getTaskType())) { log("Warning: the task name <style> is deprecated. Use <xslt> instead.", Project.MSG_WARN); } File savedBaseDir = baseDir; DirectoryScanner scanner; String[] list; String[] dirs; if (xslResource == null && xslFile == null) { throw new BuildException("specify the " + "stylesheet either as a filename in style " + "attribute or as a nested resource", getLocation()); } if (xslResource != null && xslFile != null) { throw new BuildException("specify the " + "stylesheet either as a filename in style " + "attribute or as a nested resource but not " + "as both", getLocation()); } if (inFile != null && !inFile.exists()) { throw new BuildException( "input file " + inFile.toString() + " does not exist", getLocation()); } try { if (baseDir == null) { baseDir = getProject().resolveFile("."); } liaison = getLiaison(); // check if liaison wants to log errors using us as logger if (liaison instanceof XSLTLoggerAware) { ((XSLTLoggerAware) liaison).setLogger(this); } log("Using " + liaison.getClass().toString(), Project.MSG_VERBOSE); if (xslFile != null) { // If we enter here, it means that the stylesheet is supplied // via style attribute File stylesheet = getProject().resolveFile(xslFile); if (!stylesheet.exists()) { stylesheet = FILE_UTILS.resolveFile(baseDir, xslFile); /* * shouldn't throw out deprecation warnings before we know, * the wrong version has been used. */ if (stylesheet.exists()) { log("DEPRECATED - the 'style' attribute should be relative " + "to the project's"); log(" basedir, not the tasks's basedir."); } } FileResource fr = new FileResource(); fr.setProject(getProject()); fr.setFile(stylesheet); xslResource = fr; } // if we have an in file and out then process them if (inFile != null && outFile != null) { process(inFile, outFile, xslResource); return; } /* * if we get here, in and out have not been specified, we are * in batch processing mode. */ //-- make sure destination directory exists... checkDest(); if (useImplicitFileset) { scanner = getDirectoryScanner(baseDir); log("Transforming into " + destDir, Project.MSG_INFO); // Process all the files marked for styling list = scanner.getIncludedFiles(); for (int i = 0; i < list.length; ++i) { process(baseDir, list[i], destDir, xslResource); } if (performDirectoryScan) { // Process all the directories marked for styling dirs = scanner.getIncludedDirectories(); for (int j = 0; j < dirs.length; ++j) { list = new File(baseDir, dirs[j]).list(); for (int i = 0; i < list.length; ++i) { process(baseDir, dirs[j] + File.separator + list[i], destDir, xslResource); } } } } else { // only resource collections, there better be some if (resources.size() == 0) { throw new BuildException("no resources specified"); } } processResources(xslResource); } finally { if (loader != null) { loader.resetThreadContextLoader(); loader.cleanup(); loader = null; } liaison = null; stylesheetLoaded = false; baseDir = savedBaseDir; } } /** * Set whether to check dependencies, or always generate; * optional, default is false. * * @param force true if always generate. */ public void setForce(boolean force) { this.force = force; } /** * Set the base directory; * optional, default is the project's basedir. * * @param dir the base directory **/ public void setBasedir(File dir) { baseDir = dir; } /** * Set the destination directory into which the XSL result * files should be copied to; * required, unless <tt>in</tt> and <tt>out</tt> are * specified. * @param dir the name of the destination directory
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -