org.apache.tools.ant.taskdefs.XSLTProcess Maven / Gradle / Ivy
/*
* 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.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
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.ProjectComponent;
import org.apache.tools.ant.PropertyHelper;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.PropertySet;
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.FileProvider;
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.ClasspathUtils;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.ResourceUtils;
import org.apache.tools.ant.util.StringUtils;
/**
* 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 {
/**
* The default processor is trax
* @since Ant 1.7
*/
public static final String PROCESSOR_TRAX = "trax";
/** Utilities used for file operations */
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
/** 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 final List params = new ArrayList<>();
/** 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 Liaison 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 final List outputProperties = new Vector<>();
/** for resolving entities such as dtds */
private final XMLCatalog xmlCatalog = new XMLCatalog();
/**
* 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.
*
* 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).
*
* @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 final Union resources = new Union();
/**
* Whether to use the implicit fileset.
*
* @since Ant 1.7
*/
private boolean useImplicitFileset = true;
/**
* whether to suppress warnings.
*
* @since Ant 1.8.0
*/
private boolean suppressWarnings = false;
/**
* whether to fail the build if an error occurs during transformation.
*
* @since Ant 1.8.0
*/
private boolean failOnTransformationError = true;
/**
* whether to fail the build if an error occurs.
*
* @since Ant 1.8.0
*/
private boolean failOnError = true;
/**
* Whether the build should fail if the nested resource collection
* is empty.
*
* @since Ant 1.8.0
*/
private boolean failOnNoResources = true;
/**
* For evaluating template params
*
* @since Ant 1.9.3
*/
private XPathFactory xpathFactory;
/**
* For evaluating template params
*
* @since Ant 1.9.3
*/
private XPath xpath;
/**
* System properties to set during transformation.
*
* @since Ant 1.8.0
*/
private final CommandlineJava.SysProperties sysProperties =
new CommandlineJava.SysProperties();
/**
* Trace configuration for Xalan2.
*
* @since Ant 1.8.0
*/
private TraceConfiguration traceConfiguration;
/**
* 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(final boolean b) {
performDirectoryScan = b;
}
/**
* Controls whether the stylesheet is reloaded for every transform.
*
* Setting this to true may get around a bug in certain
* Xalan-J versions, default is false.
* @param b a boolean
value
* @since Ant 1.5.2
*/
public void setReloadStylesheet(final 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(final Mapper mapper) {
if (mapperElement != null) {
handleError("Cannot define more than one mapper");
} else {
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(final 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(final Resources rc) {
if (rc.size() != 1) {
handleError(
"The style element must be specified with exactly one nested resource.");
} else {
setXslResource(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(final 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(final FileNameMapper fileNameMapper) throws BuildException {
final 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 out is defined, then both are
*/
@Override
public void execute() throws BuildException {
if ("style".equals(getTaskType())) {
log("Warning: the task name