|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502 |
- /*
- * 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.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.FileResource;
- import org.apache.tools.ant.types.resources.Resources;
- import org.apache.tools.ant.types.resources.Union;
- import org.apache.tools.ant.types.resources.FileProvider;
- import org.apache.tools.ant.util.FileNameMapper;
- import org.apache.tools.ant.util.FileUtils;
- import org.apache.tools.ant.util.ResourceUtils;
-
- /**
- * 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();
-
- /** 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";
-
- /**
- * 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;
-
- /**
- * System properties to set during transformation.
- *
- * @since Ant 1.8.0
- */
- private CommandlineJava.SysProperties sysProperties =
- new CommandlineJava.SysProperties();
-
- /**
- * Trace configuration for Xalan2.
- *
- * @since Ant 1.8.0
- */
- private TraceConfiguration traceConfiguration;
-
- /**
- * 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) {
- 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(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) {
- handleError("The style element must be specified with exactly one"
- + " nested resource.");
- } else {
- 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;
-
- String baseMessage =
- "specify the stylesheet either as a filename in style attribute "
- + "or as a nested resource";
-
- if (xslResource == null && xslFile == null) {
- handleError(baseMessage);
- return;
- }
- if (xslResource != null && xslFile != null) {
- handleError(baseMessage + " but not as both");
- return;
- }
- if (inFile != null && !inFile.exists()) {
- handleError("input file " + inFile + " does not exist");
- return;
- }
- try {
- if (sysProperties.size() > 0) {
- sysProperties.setSystem();
- }
-
- Resource styleResource;
- if (baseDir == null) {
- baseDir = getProject().getBaseDir();
- }
- 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);
- styleResource = fr;
- } else {
- styleResource = xslResource;
- }
-
- if (!styleResource.isExists()) {
- handleError("stylesheet " + styleResource + " doesn't exist.");
- return;
- }
-
- // if we have an in file and out then process them
- if (inFile != null && outFile != null) {
- process(inFile, outFile, styleResource);
- 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, styleResource);
- }
- 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,
- styleResource);
- }
- }
- }
- } else { // only resource collections, there better be some
- if (resources.size() == 0) {
- if (failOnNoResources) {
- handleError("no resources specified");
- }
- return;
- }
- }
- processResources(styleResource);
- } finally {
- if (loader != null) {
- loader.resetThreadContextLoader();
- loader.cleanup();
- loader = null;
- }
- if (sysProperties.size() > 0) {
- sysProperties.restoreSystem();
- }
- 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
- **/
- public void setDestdir(File dir) {
- destDir = dir;
- }
-
- /**
- * Set the desired file extension to be used for the target;
- * optional, default is html.
- * @param name the extension to use
- **/
- public void setExtension(String name) {
- targetExtension = name;
- }
-
- /**
- * Name of the stylesheet to use - given either relative
- * to the project's basedir or as an absolute path; required.
- *
- * @param xslFile the stylesheet to use
- */
- public void setStyle(String xslFile) {
- this.xslFile = xslFile;
- }
-
- /**
- * Set the optional classpath to the XSL processor
- *
- * @param classpath the classpath to use when loading the XSL processor
- */
- public void setClasspath(Path classpath) {
- createClasspath().append(classpath);
- }
-
- /**
- * Set the optional classpath to the XSL processor
- *
- * @return a path instance to be configured by the Ant core.
- */
- public Path createClasspath() {
- if (classpath == null) {
- classpath = new Path(getProject());
- }
- return classpath.createPath();
- }
-
- /**
- * Set the reference to an optional classpath to the XSL processor
- *
- * @param r the id of the Ant path instance to act as the classpath
- * for loading the XSL processor
- */
- public void setClasspathRef(Reference r) {
- createClasspath().setRefid(r);
- }
-
- /**
- * Set the name of the XSL processor to use; optional, default trax.
- *
- * @param processor the name of the XSL processor
- */
- public void setProcessor(String processor) {
- this.processor = processor;
- }
-
- /**
- * Whether to use the implicit fileset.
- *
- * <p>Set this to false if you want explicit control with nested
- * resource collections.</p>
- * @param useimplicitfileset set to true if you want to use implicit fileset
- * @since Ant 1.7
- */
- public void setUseImplicitFileset(boolean useimplicitfileset) {
- useImplicitFileset = useimplicitfileset;
- }
-
- /**
- * Add the catalog to our internal catalog
- *
- * @param xmlCatalog the XMLCatalog instance to use to look up DTDs
- */
- public void addConfiguredXMLCatalog(XMLCatalog xmlCatalog) {
- this.xmlCatalog.addConfiguredXMLCatalog(xmlCatalog);
- }
-
- /**
- * Pass the filename of the current processed file as a xsl parameter
- * to the transformation. This value sets the name of that xsl parameter.
- *
- * @param fileNameParameter name of the xsl parameter retrieving the
- * current file name
- */
- public void setFileNameParameter(String fileNameParameter) {
- this.fileNameParameter = fileNameParameter;
- }
-
- /**
- * Pass the directory name of the current processed file as a xsl parameter
- * to the transformation. This value sets the name of that xsl parameter.
- *
- * @param fileDirParameter name of the xsl parameter retrieving the
- * current file directory
- */
- public void setFileDirParameter(String fileDirParameter) {
- this.fileDirParameter = fileDirParameter;
- }
-
- /**
- * Whether to suppress warning messages of the processor.
- *
- * @since Ant 1.8.0
- */
- public void setSuppressWarnings(boolean b) {
- suppressWarnings = b;
- }
-
- /**
- * Whether to suppress warning messages of the processor.
- *
- * @since Ant 1.8.0
- */
- public boolean getSuppressWarnings() {
- return suppressWarnings;
- }
-
- /**
- * Whether transformation errors should make the build fail.
- *
- * @since Ant 1.8.0
- */
- public void setFailOnTransformationError(boolean b) {
- failOnTransformationError = b;
- }
-
- /**
- * Whether any errors should make the build fail.
- *
- * @since Ant 1.8.0
- */
- public void setFailOnError(boolean b) {
- failOnError = b;
- }
-
- /**
- * Whether the build should fail if the nested resource collection is empty.
- *
- * @since Ant 1.8.0
- */
- public void setFailOnNoResources(boolean b) {
- failOnNoResources = b;
- }
-
- /**
- * A system property to set during transformation.
- *
- * @since Ant 1.8.0
- */
- public void addSysproperty(Environment.Variable sysp) {
- sysProperties.addVariable(sysp);
- }
-
- /**
- * A set of system properties to set during transformation.
- *
- * @since Ant 1.8.0
- */
- public void addSyspropertyset(PropertySet sysp) {
- sysProperties.addSyspropertyset(sysp);
- }
-
- /**
- * Enables Xalan2 traces and uses the given configuration.
- *
- * <p>Note that this element doesn't have any effect with a
- * processor other than trax or if the Transformer is not Xalan2's
- * transformer implementation.</p>
- *
- * @since Ant 1.8.0
- */
- public TraceConfiguration createTrace() {
- if (traceConfiguration != null) {
- throw new BuildException("can't have more than one trace"
- + " configuration");
- }
- traceConfiguration = new TraceConfiguration();
- return traceConfiguration;
- }
-
- /**
- * Configuration for Xalan2 traces.
- *
- * @since Ant 1.8.0
- */
- public TraceConfiguration getTraceConfiguration() {
- return traceConfiguration;
- }
-
- /**
- * Load processor here instead of in setProcessor - this will be
- * called from within execute, so we have access to the latest
- * classpath.
- *
- * @param proc the name of the processor to load.
- * @exception Exception if the processor cannot be loaded.
- */
- private void resolveProcessor(String proc) throws Exception {
- if (proc.equals(PROCESSOR_TRAX)) {
- liaison = new org.apache.tools.ant.taskdefs.optional.TraXLiaison();
- } else {
- //anything else is a classname
- Class clazz = loadClass(proc);
- liaison = (XSLTLiaison) clazz.newInstance();
- }
- }
-
- /**
- * Load named class either via the system classloader or a given
- * custom classloader.
- *
- * As a side effect, the loader is set as the thread context classloader
- * @param classname the name of the class to load.
- * @return the requested class.
- * @exception Exception if the class could not be loaded.
- */
- private Class loadClass(String classname) throws Exception {
- if (classpath == null) {
- return Class.forName(classname);
- }
- loader = getProject().createClassLoader(classpath);
- loader.setThreadContextLoader();
- return Class.forName(classname, true, loader);
- }
-
- /**
- * Specifies the output name for the styled result from the
- * <tt>in</tt> attribute; required if <tt>in</tt> is set
- *
- * @param outFile the output File instance.
- */
- public void setOut(File outFile) {
- this.outFile = outFile;
- }
-
- /**
- * specifies a single XML document to be styled. Should be used
- * with the <tt>out</tt> attribute; ; required if <tt>out</tt> is set
- *
- * @param inFile the input file
- */
- public void setIn(File inFile) {
- this.inFile = inFile;
- }
-
- /**
- * Throws a BuildException if the destination directory hasn't
- * been specified.
- * @since Ant 1.7
- */
- private void checkDest() {
- if (destDir == null) {
- handleError("destdir attributes must be set!");
- }
- }
-
- /**
- * Styles all existing resources.
- *
- * @param stylesheet style sheet to use
- * @since Ant 1.7
- */
- private void processResources(Resource stylesheet) {
- Iterator iter = resources.iterator();
- while (iter.hasNext()) {
- Resource r = (Resource) iter.next();
- if (!r.isExists()) {
- continue;
- }
- File base = baseDir;
- String name = r.getName();
- FileProvider fp = (FileProvider) r.as(FileProvider.class);
- if (fp != null) {
- FileResource f = ResourceUtils.asFileResource(fp);
- base = f.getBaseDir();
- if (base == null) {
- name = f.getFile().getAbsolutePath();
- }
- }
- process(base, name, destDir, stylesheet);
- }
- }
-
- /**
- * Processes the given input XML file and stores the result
- * in the given resultFile.
- *
- * @param baseDir the base directory for resolving files.
- * @param xmlFile the input file
- * @param destDir the destination directory
- * @param stylesheet the stylesheet to use.
- * @exception BuildException if the processing fails.
- */
- private void process(File baseDir, String xmlFile, File destDir, Resource stylesheet)
- throws BuildException {
-
- File outF = null;
- File inF = null;
-
- try {
- long styleSheetLastModified = stylesheet.getLastModified();
- inF = new File(baseDir, xmlFile);
-
- if (inF.isDirectory()) {
- log("Skipping " + inF + " it is a directory.", Project.MSG_VERBOSE);
- return;
- }
- FileNameMapper mapper = null;
- if (mapperElement != null) {
- mapper = mapperElement.getImplementation();
- } else {
- mapper = new StyleMapper();
- }
-
- String[] outFileName = mapper.mapFileName(xmlFile);
- if (outFileName == null || outFileName.length == 0) {
- log("Skipping " + inFile + " it cannot get mapped to output.", Project.MSG_VERBOSE);
- return;
- } else if (outFileName == null || outFileName.length > 1) {
- log("Skipping " + inFile + " its mapping is ambiguos.", Project.MSG_VERBOSE);
- return;
- }
- outF = new File(destDir, outFileName[0]);
-
- if (force || inF.lastModified() > outF.lastModified()
- || styleSheetLastModified > outF.lastModified()) {
- ensureDirectoryFor(outF);
- log("Processing " + inF + " to " + outF);
- configureLiaison(stylesheet);
- setLiaisonDynamicFileParameters(liaison, inF);
- liaison.transform(inF, outF);
- }
- } catch (Exception ex) {
- // If failed to process document, must delete target document,
- // or it will not attempt to process it the second time
- log("Failed to process " + inFile, Project.MSG_INFO);
- if (outF != null) {
- outF.delete();
- }
- handleTransformationError(ex);
- }
-
- } //-- processXML
-
- /**
- * Process the input file to the output file with the given stylesheet.
- *
- * @param inFile the input file to process.
- * @param outFile the destination file.
- * @param stylesheet the stylesheet to use.
- * @exception BuildException if the processing fails.
- */
- private void process(File inFile, File outFile, Resource stylesheet) throws BuildException {
- try {
- long styleSheetLastModified = stylesheet.getLastModified();
- log("In file " + inFile + " time: " + inFile.lastModified(), Project.MSG_DEBUG);
- log("Out file " + outFile + " time: " + outFile.lastModified(), Project.MSG_DEBUG);
- log("Style file " + xslFile + " time: " + styleSheetLastModified, Project.MSG_DEBUG);
- if (force || inFile.lastModified() >= outFile.lastModified()
- || styleSheetLastModified >= outFile.lastModified()) {
- ensureDirectoryFor(outFile);
- log("Processing " + inFile + " to " + outFile, Project.MSG_INFO);
- configureLiaison(stylesheet);
- setLiaisonDynamicFileParameters(liaison, inFile);
- liaison.transform(inFile, outFile);
- } else {
- log("Skipping input file " + inFile + " because it is older than output file "
- + outFile + " and so is the stylesheet " + stylesheet, Project.MSG_DEBUG);
- }
- } catch (Exception ex) {
- log("Failed to process " + inFile, Project.MSG_INFO);
- if (outFile != null) {
- outFile.delete();
- }
- handleTransformationError(ex);
- }
- }
-
- /**
- * Ensure the directory exists for a given file
- *
- * @param targetFile the file for which the directories are required.
- * @exception BuildException if the directories cannot be created.
- */
- private void ensureDirectoryFor(File targetFile) throws BuildException {
- File directory = targetFile.getParentFile();
- if (!directory.exists()) {
- if (!directory.mkdirs()) {
- handleError("Unable to create directory: "
- + directory.getAbsolutePath());
- }
- }
- }
-
- /**
- * Get the factory instance configured for this processor
- *
- * @return the factory instance in use
- */
- public Factory getFactory() {
- return factory;
- }
-
- /**
- * Get the XML catalog containing entity definitions
- *
- * @return the XML catalog for the task.
- */
- public XMLCatalog getXMLCatalog() {
- xmlCatalog.setProject(getProject());
- return xmlCatalog;
- }
-
- /**
- * Get an enumeration on the outputproperties.
- * @return the outputproperties
- */
- public Enumeration getOutputProperties() {
- return outputProperties.elements();
- }
-
- /**
- * Get the Liason implementation to use in processing.
- *
- * @return an instance of the XSLTLiason interface.
- */
- protected XSLTLiaison getLiaison() {
- // if processor wasn't specified, use TraX.
- if (liaison == null) {
- if (processor != null) {
- try {
- resolveProcessor(processor);
- } catch (Exception e) {
- handleError(e);
- }
- } else {
- try {
- resolveProcessor(PROCESSOR_TRAX);
- } catch (Exception e1) { // should not happen
- handleError(e1);
- }
- }
- }
- return liaison;
- }
-
- /**
- * Create an instance of an XSL parameter for configuration by Ant.
- *
- * @return an instance of the Param class to be configured.
- */
- public Param createParam() {
- Param p = new Param();
- params.addElement(p);
- return p;
- }
-
- /**
- * The Param inner class used to store XSL parameters
- */
- public static class Param {
- /** The parameter name */
- private String name = null;
-
- /** The parameter's value */
- private String expression = null;
-
- private Object ifCond;
- private Object unlessCond;
- private Project project;
-
- /**
- * Set the current project
- *
- * @param project the current project
- */
- public void setProject(Project project) {
- this.project = project;
- }
-
- /**
- * Set the parameter name.
- *
- * @param name the name of the parameter.
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * The parameter value
- * NOTE : was intended to be an XSL expression.
- * @param expression the parameter's value.
- */
- public void setExpression(String expression) {
- this.expression = expression;
- }
-
- /**
- * Get the parameter name
- *
- * @return the parameter name
- * @exception BuildException if the name is not set.
- */
- public String getName() throws BuildException {
- if (name == null) {
- throw new BuildException("Name attribute is missing.");
- }
- return name;
- }
-
- /**
- * Get the parameter's value
- *
- * @return the parameter value
- * @exception BuildException if the value is not set.
- */
- public String getExpression() throws BuildException {
- if (expression == null) {
- throw new BuildException("Expression attribute is missing.");
- }
- return expression;
- }
-
- /**
- * Set whether this param should be used. It will be used if
- * the expression evalutes to true or the name of a property
- * which has been set, otherwise it won't.
- * @param ifCond evaluated expression
- * @since Ant 1.8.0
- */
- public void setIf(Object ifCond) {
- this.ifCond = ifCond;
- }
-
- /**
- * Set whether this param should be used. It will be used if
- * the expression evalutes to true or the name of a property
- * which has been set, otherwise it won't.
- * @param ifProperty evaluated expression
- */
- public void setIf(String ifProperty) {
- setIf((Object) ifProperty);
- }
-
- /**
- * Set whether this param should NOT be used. It will not be
- * used if the expression evaluates to true or the name of a
- * property which has been set, otherwise it will be used.
- * @param unlessCond evaluated expression
- * @since Ant 1.8.0
- */
- public void setUnless(Object unlessCond) {
- this.unlessCond = unlessCond;
- }
-
- /**
- * Set whether this param should NOT be used. It will not be
- * used if the expression evaluates to true or the name of a
- * property which has been set, otherwise it will be used.
- * @param unlessProperty evaluated expression
- */
- public void setUnless(String unlessProperty) {
- setUnless((Object) unlessProperty);
- }
-
- /**
- * Ensures that the param passes the conditions placed
- * on it with <code>if</code> and <code>unless</code> properties.
- * @return true if the task passes the "if" and "unless" parameters
- */
- public boolean shouldUse() {
- PropertyHelper ph = PropertyHelper.getPropertyHelper(project);
- return ph.testIfCondition(ifCond)
- && ph.testUnlessCondition(unlessCond);
- }
- } // Param
-
- /**
- * Create an instance of an output property to be configured.
- * @return the newly created output property.
- * @since Ant 1.5
- */
- public OutputProperty createOutputProperty() {
- OutputProperty p = new OutputProperty();
- outputProperties.addElement(p);
- return p;
- }
-
- /**
- * Specify how the result tree should be output as specified
- * in the <a href="http://www.w3.org/TR/xslt#output">
- * specification</a>.
- * @since Ant 1.5
- */
- public static class OutputProperty {
- /** output property name */
- private String name;
-
- /** output property value */
- private String value;
-
- /**
- * @return the output property name.
- */
- public String getName() {
- return name;
- }
-
- /**
- * set the name for this property
- * @param name A non-null String that specifies an
- * output property name, which may be namespace qualified.
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * @return the output property value.
- */
- public String getValue() {
- return value;
- }
-
- /**
- * set the value for this property
- * @param value The non-null string value of the output property.
- */
- public void setValue(String value) {
- this.value = value;
- }
- }
-
- /**
- * Initialize internal instance of XMLCatalog
- * @throws BuildException on error
- */
- public void init() throws BuildException {
- super.init();
- xmlCatalog.setProject(getProject());
- }
-
- /**
- * Loads the stylesheet and set xsl:param parameters.
- *
- * @param stylesheet the file from which to load the stylesheet.
- * @exception BuildException if the stylesheet cannot be loaded.
- * @deprecated since Ant 1.7
- */
- protected void configureLiaison(File stylesheet) throws BuildException {
- FileResource fr = new FileResource();
- fr.setProject(getProject());
- fr.setFile(stylesheet);
- configureLiaison(fr);
- }
-
- /**
- * Loads the stylesheet and set xsl:param parameters.
- *
- * @param stylesheet the resource from which to load the stylesheet.
- * @exception BuildException if the stylesheet cannot be loaded.
- * @since Ant 1.7
- */
- protected void configureLiaison(Resource stylesheet) throws BuildException {
- if (stylesheetLoaded && reuseLoadedStylesheet) {
- return;
- }
- stylesheetLoaded = true;
-
- try {
- log("Loading stylesheet " + stylesheet, Project.MSG_INFO);
- // We call liason.configure() and then liaison.setStylesheet()
- // so that the internal variables of liaison can be set up
- if (liaison instanceof XSLTLiaison2) {
- ((XSLTLiaison2) liaison).configure(this);
- }
- if (liaison instanceof XSLTLiaison3) {
- // If we are here we can set the stylesheet as a
- // resource
- ((XSLTLiaison3) liaison).setStylesheet(stylesheet);
- } else {
- // If we are here we cannot set the stylesheet as
- // a resource, but we can set it as a file. So,
- // we make an attempt to get it as a file
- FileProvider fp =
- (FileProvider) stylesheet.as(FileProvider.class);
- if (fp != null) {
- liaison.setStylesheet(fp.getFile());
- } else {
- handleError(liaison.getClass().toString()
- + " accepts the stylesheet only as a file");
- return;
- }
- }
- for (Enumeration e = params.elements(); e.hasMoreElements();) {
- Param p = (Param) e.nextElement();
- if (p.shouldUse()) {
- liaison.addParam(p.getName(), p.getExpression());
- }
- }
- } catch (Exception ex) {
- log("Failed to transform using stylesheet " + stylesheet, Project.MSG_INFO);
- handleTransformationError(ex);
- }
- }
-
- /**
- * Sets file parameter(s) for directory and filename if the attribute
- * 'filenameparameter' or 'filedirparameter' are set in the task.
- *
- * @param liaison to change parameters for
- * @param inFile to get the additional file information from
- * @throws Exception if an exception occurs on filename lookup
- *
- * @since Ant 1.7
- */
- private void setLiaisonDynamicFileParameters(
- XSLTLiaison liaison, File inFile) throws Exception {
- if (fileNameParameter != null) {
- liaison.addParam(fileNameParameter, inFile.getName());
- }
- if (fileDirParameter != null) {
- String fileName = FileUtils.getRelativePath(baseDir, inFile);
- File file = new File(fileName);
- // Give always a slash as file separator, so the stylesheet could be sure about that
- // Use '.' so a dir+"/"+name would not result in an absolute path
- liaison.addParam(fileDirParameter, file.getParent() != null ? file.getParent().replace(
- '\\', '/') : ".");
- }
- }
-
- /**
- * Create the factory element to configure a trax liaison.
- * @return the newly created factory element.
- * @throws BuildException if the element is created more than one time.
- */
- public Factory createFactory() throws BuildException {
- if (factory != null) {
- handleError("'factory' element must be unique");
- } else {
- factory = new Factory();
- }
- return factory;
- }
-
- /**
- * Throws an exception with the given message if failOnError is
- * true, otherwise logs the message using the WARN level.
- *
- * @since Ant 1.8.0
- */
- protected void handleError(String msg) {
- if (failOnError) {
- throw new BuildException(msg, getLocation());
- }
- log(msg, Project.MSG_WARN);
- }
-
-
- /**
- * Throws an exception with the given nested exception if
- * failOnError is true, otherwise logs the message using the WARN
- * level.
- *
- * @since Ant 1.8.0
- */
- protected void handleError(Throwable ex) {
- if (failOnError) {
- throw new BuildException(ex);
- } else {
- log("Caught an exception: " + ex, Project.MSG_WARN);
- }
- }
-
- /**
- * Throws an exception with the given nested exception if
- * failOnError and failOnTransformationError are true, otherwise
- * logs the message using the WARN level.
- *
- * @since Ant 1.8.0
- */
- protected void handleTransformationError(Exception ex) {
- if (failOnError && failOnTransformationError) {
- throw new BuildException(ex);
- } else {
- log("Caught an error during transformation: " + ex,
- Project.MSG_WARN);
- }
- }
-
- /**
- * The factory element to configure a transformer factory
- * @since Ant 1.6
- */
- public static class Factory {
-
- /** the factory class name to use for TraXLiaison */
- private String name;
-
- /**
- * the list of factory attributes to use for TraXLiaison
- */
- private Vector attributes = new Vector();
-
- /**
- * @return the name of the factory.
- */
- public String getName() {
- return name;
- }
-
- /**
- * Set the name of the factory
- * @param name the name of the factory.
- */
- public void setName(String name) {
- this.name = name;
- }
-
- /**
- * Create an instance of a factory attribute.
- * @param attr the newly created factory attribute
- */
- public void addAttribute(Attribute attr) {
- attributes.addElement(attr);
- }
-
- /**
- * return the attribute elements.
- * @return the enumeration of attributes
- */
- public Enumeration getAttributes() {
- return attributes.elements();
- }
-
- /**
- * A JAXP factory attribute. This is mostly processor specific, for
- * example for Xalan 2.3+, the following attributes could be set:
- * <ul>
- * <li>http://xml.apache.org/xalan/features/optimize (true|false) </li>
- * <li>http://xml.apache.org/xalan/features/incremental (true|false) </li>
- * </ul>
- */
- public static class Attribute implements DynamicConfigurator {
-
- /** attribute name, mostly processor specific */
- private String name;
-
- /** attribute value, often a boolean string */
- private Object value;
-
- /**
- * @return the attribute name.
- */
- public String getName() {
- return name;
- }
-
- /**
- * @return the output property value.
- */
- public Object getValue() {
- return value;
- }
-
- /**
- * Not used.
- * @param name not used
- * @return null
- * @throws BuildException never
- */
- public Object createDynamicElement(String name) throws BuildException {
- return null;
- }
-
- /**
- * Set an attribute.
- * Only "name" and "value" are supported as names.
- * @param name the name of the attribute
- * @param value the value of the attribute
- * @throws BuildException on error
- */
- public void setDynamicAttribute(String name, String value) throws BuildException {
- // only 'name' and 'value' exist.
- if ("name".equalsIgnoreCase(name)) {
- this.name = value;
- } else if ("value".equalsIgnoreCase(name)) {
- // a value must be of a given type
- // say boolean|integer|string that are mostly used.
- if ("true".equalsIgnoreCase(value)) {
- this.value = Boolean.TRUE;
- } else if ("false".equalsIgnoreCase(value)) {
- this.value = Boolean.FALSE;
- } else {
- try {
- this.value = new Integer(value);
- } catch (NumberFormatException e) {
- this.value = value;
- }
- }
- } else {
- throw new BuildException("Unsupported attribute: " + name);
- }
- }
- } // -- class Attribute
- } // -- class Factory
-
- /**
- * Mapper implementation of the "traditional" way <xslt>
- * mapped filenames.
- *
- * <p>If the file has an extension, chop it off. Append whatever
- * the user has specified as extension or ".html".</p>
- *
- * @since Ant 1.6.2
- */
- private class StyleMapper implements FileNameMapper {
- public void setFrom(String from) {
- }
- public void setTo(String to) {
- }
- public String[] mapFileName(String xmlFile) {
- int dotPos = xmlFile.lastIndexOf('.');
- if (dotPos > 0) {
- xmlFile = xmlFile.substring(0, dotPos);
- }
- return new String[] {xmlFile + targetExtension};
- }
- }
-
- /**
- * Configuration for Xalan2 traces.
- *
- * @since Ant 1.8.0
- */
- public final class TraceConfiguration {
- private boolean elements, extension, generation, selection, templates;
-
- /**
- * Set to true if the listener is to print events that occur
- * as each node is 'executed' in the stylesheet.
- */
- public void setElements(boolean b) {
- elements = b;
- }
-
- /**
- * True if the listener is to print events that occur as each
- * node is 'executed' in the stylesheet.
- */
- public boolean getElements() {
- return elements;
- }
-
- /**
- * Set to true if the listener is to print information after
- * each extension event.
- */
- public void setExtension(boolean b) {
- extension = b;
- }
-
- /**
- * True if the listener is to print information after each
- * extension event.
- */
- public boolean getExtension() {
- return extension;
- }
-
- /**
- * Set to true if the listener is to print information after
- * each result-tree generation event.
- */
- public void setGeneration(boolean b) {
- generation = b;
- }
-
- /**
- * True if the listener is to print information after each
- * result-tree generation event.
- */
- public boolean getGeneration() {
- return generation;
- }
-
- /**
- * Set to true if the listener is to print information after
- * each selection event.
- */
- public void setSelection(boolean b) {
- selection = b;
- }
-
- /**
- * True if the listener is to print information after each
- * selection event.
- */
- public boolean getSelection() {
- return selection;
- }
-
- /**
- * Set to true if the listener is to print an event whenever a
- * template is invoked.
- */
- public void setTemplates(boolean b) {
- templates = b;
- }
-
- /**
- * True if the listener is to print an event whenever a
- * template is invoked.
- */
- public boolean getTemplates() {
- return templates;
- }
-
- /**
- * The stream to write traces to.
- */
- public java.io.OutputStream getOutputStream() {
- return new LogOutputStream(XSLTProcess.this);
- }
- }
-
- }
|