Fields Summary |
---|
private File | destDirdestination directory |
private File | baseDirwhere to find the source XML file, default is the project's basedir |
private String | xslFileXSL stylesheet as a filename |
private org.apache.tools.ant.types.Resource | xslResourceXSL stylesheet as a {@link org.apache.tools.ant.types.Resource} |
private String | targetExtensionextension of the files produced by XSL processing |
private String | fileNameParametername for XSL parameter containing the filename |
private String | fileDirParametername for XSL parameter containing the file directory |
private Vector | paramsadditional parameters to be passed to the stylesheets |
private File | inFileInput XML document to be used |
private File | outFileOutput file |
private String | processorThe name of the XSL processor to use |
private org.apache.tools.ant.types.Path | classpathClasspath to use when trying to load the XSL processor |
private XSLTLiaison | liaisonThe Liason implementation to use to communicate with the XSL
processor |
private boolean | stylesheetLoadedFlag which indicates if the stylesheet has been loaded into
the processor |
private boolean | forceforce output of target files even if they already exist |
private Vector | outputPropertiesXSL output properties to be used |
private org.apache.tools.ant.types.XMLCatalog | xmlCatalogfor resolving entities such as dtds |
private static final String | TRAX_LIAISON_CLASSName of the TRAX Liaison class |
private static final org.apache.tools.ant.util.FileUtils | FILE_UTILSUtilities used for file operations |
private boolean | performDirectoryScanWhether to style all files in the included directories as well. |
private Factory | factoryfactory element for TraX processors only |
private boolean | reuseLoadedStylesheetwhether to reuse Transformer if transforming multiple files. |
private org.apache.tools.ant.AntClassLoader | loaderAntClassLoader 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). |
private org.apache.tools.ant.types.Mapper | mapperElementMapper to use when a set of files gets processed. |
private org.apache.tools.ant.types.resources.Union | resourcesAdditional resource collections to process. |
private boolean | useImplicitFilesetWhether to use the implicit fileset. |
public static final String | PROCESSOR_TRAXThe default processor is trax |
Methods Summary |
---|
public void | add(org.apache.tools.ant.types.ResourceCollection rc)Adds a collection of resources to style in addition to the
given file or the implicit fileset.
resources.add(rc);
|
public void | add(org.apache.tools.ant.util.FileNameMapper fileNameMapper)Adds a nested filenamemapper.
Mapper mapper = new Mapper(getProject());
mapper.add(fileNameMapper);
addMapper(mapper);
|
public void | addConfiguredStyle(org.apache.tools.ant.types.resources.Resources rc)Add a nested <style> element.
if (rc.size() != 1) {
throw new BuildException("The style element must be specified"
+ " with exactly one nested resource.");
}
setXslResource((Resource) rc.iterator().next());
|
public void | addConfiguredXMLCatalog(org.apache.tools.ant.types.XMLCatalog xmlCatalog)Add the catalog to our internal catalog
this.xmlCatalog.addConfiguredXMLCatalog(xmlCatalog);
|
public void | addMapper(org.apache.tools.ant.types.Mapper mapper)Defines the mapper to map source to destination files.
if (mapperElement != null) {
throw new BuildException("Cannot define more than one mapper",
getLocation());
}
mapperElement = mapper;
|
private void | checkDest()Throws a BuildException if the destination directory hasn't
been specified.
if (destDir == null) {
String msg = "destdir attributes must be set!";
throw new BuildException(msg);
}
|
protected void | configureLiaison(java.io.File stylesheet)Loads the stylesheet and set xsl:param parameters.
FileResource fr = new FileResource();
fr.setProject(getProject());
fr.setFile(stylesheet);
configureLiaison(fr);
|
protected void | configureLiaison(org.apache.tools.ant.types.Resource stylesheet)Loads the stylesheet and set xsl:param parameters.
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
if (stylesheet instanceof FileResource) {
liaison.setStylesheet(
((FileResource) stylesheet).getFile());
} else {
throw new BuildException(liaison.getClass().toString()
+ " accepts the stylesheet only as a file",
getLocation());
}
}
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);
throw new BuildException(ex);
}
|
public org.apache.tools.ant.types.Path | createClasspath()Set the optional classpath to the XSL processor
if (classpath == null) {
classpath = new Path(getProject());
}
return classpath.createPath();
|
public org.apache.tools.ant.taskdefs.XSLTProcess$Factory | createFactory()Create the factory element to configure a trax liaison.
if (factory != null) {
throw new BuildException("'factory' element must be unique");
}
factory = new Factory();
return factory;
|
public org.apache.tools.ant.taskdefs.XSLTProcess$OutputProperty | createOutputProperty()Create an instance of an output property to be configured.
OutputProperty p = new OutputProperty();
outputProperties.addElement(p);
return p;
|
public org.apache.tools.ant.taskdefs.XSLTProcess$Param | createParam()Create an instance of an XSL parameter for configuration by Ant.
Param p = new Param();
params.addElement(p);
return p;
|
private void | ensureDirectoryFor(java.io.File targetFile)Ensure the directory exists for a given file
File directory = targetFile.getParentFile();
if (!directory.exists()) {
if (!directory.mkdirs()) {
throw new BuildException("Unable to create directory: "
+ directory.getAbsolutePath());
}
}
|
public void | execute()Executes the task.
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;
}
|
public org.apache.tools.ant.taskdefs.XSLTProcess$Factory | getFactory()Get the factory instance configured for this processor
return factory;
|
protected XSLTLiaison | getLiaison()Get the Liason implementation to use in processing.
// if processor wasn't specified, see if TraX is available. If not,
// default it to xalan, depending on which is in the classpath
if (liaison == null) {
if (processor != null) {
try {
resolveProcessor(processor);
} catch (Exception e) {
throw new BuildException(e);
}
} else {
try {
resolveProcessor(PROCESSOR_TRAX);
} catch (Throwable e1) {
e1.printStackTrace();
throw new BuildException(e1);
}
}
}
return liaison;
|
public java.util.Enumeration | getOutputProperties()Get an enumeration on the outputproperties.
return outputProperties.elements();
|
public org.apache.tools.ant.types.XMLCatalog | getXMLCatalog()Get the XML catalog containing entity definitions
xmlCatalog.setProject(getProject());
return xmlCatalog;
|
public void | init()Initialize internal instance of XMLCatalog
super.init();
xmlCatalog.setProject(getProject());
|
private java.lang.Class | loadClass(java.lang.String classname)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
if (classpath == null) {
return Class.forName(classname);
} else {
loader = getProject().createClassLoader(classpath);
loader.setThreadContextLoader();
Class c = Class.forName(classname, true, loader);
return c;
}
|
private void | process(java.io.File baseDir, java.lang.String xmlFile, java.io.File destDir, org.apache.tools.ant.types.Resource stylesheet)Processes the given input XML file and stores the result
in the given resultFile.
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();
}
throw new BuildException(ex);
}
|
private void | process(java.io.File inFile, java.io.File outFile, org.apache.tools.ant.types.Resource stylesheet)Process the input file to the output file with the given stylesheet.
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();
}
throw new BuildException(ex);
}
|
private void | processResources(org.apache.tools.ant.types.Resource stylesheet)Styles all existing resources.
Iterator iter = resources.iterator();
while (iter.hasNext()) {
Resource r = (Resource) iter.next();
if (!r.isExists()) {
continue;
}
File base = baseDir;
String name = r.getName();
if (r instanceof FileResource) {
FileResource f = (FileResource) r;
base = f.getBaseDir();
if (base == null) {
name = f.getFile().getAbsolutePath();
}
}
process(base, name, destDir, stylesheet);
}
|
private void | resolveProcessor(java.lang.String proc)Load processor here instead of in setProcessor - this will be
called from within execute, so we have access to the latest
classpath.
String classname;
if (proc.equals(PROCESSOR_TRAX)) {
classname = TRAX_LIAISON_CLASS;
} else {
//anything else is a classname
classname = proc;
}
Class clazz = loadClass(classname);
liaison = (XSLTLiaison) clazz.newInstance();
|
public void | setBasedir(java.io.File dir)Set the base directory;
optional, default is the project's basedir.
baseDir = dir;
|
public void | setClasspath(org.apache.tools.ant.types.Path classpath)Set the optional classpath to the XSL processor
createClasspath().append(classpath);
|
public void | setClasspathRef(org.apache.tools.ant.types.Reference r)Set the reference to an optional classpath to the XSL processor
createClasspath().setRefid(r);
|
public void | setDestdir(java.io.File dir)Set the destination directory into which the XSL result
files should be copied to;
required, unless in and out are
specified.
destDir = dir;
|
public void | setExtension(java.lang.String name)Set the desired file extension to be used for the target;
optional, default is html.
targetExtension = name;
|
public void | setFileDirParameter(java.lang.String fileDirParameter)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.
this.fileDirParameter = fileDirParameter;
|
public void | setFileNameParameter(java.lang.String fileNameParameter)Pass the filename of the current processed file as a xsl parameter
to the transformation. This value sets the name of that xsl parameter.
this.fileNameParameter = fileNameParameter;
|
public void | setForce(boolean force)Set whether to check dependencies, or always generate;
optional, default is false.
this.force = force;
|
public void | setIn(java.io.File inFile)specifies a single XML document to be styled. Should be used
with the out attribute; ; required if out is set
this.inFile = inFile;
|
private void | setLiaisonDynamicFileParameters(XSLTLiaison liaison, java.io.File inFile)Sets file parameter(s) for directory and filename if the attribute
'filenameparameter' or 'filedirparameter' are set in the task.
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('\\", '/") : ".");
}
|
public void | setOut(java.io.File outFile)Specifies the output name for the styled result from the
in attribute; required if in is set
this.outFile = outFile;
|
public void | setProcessor(java.lang.String processor)Set the name of the XSL processor to use; optional, default trax.
Other values are "xalan" for Xalan1
this.processor = processor;
|
public void | setReloadStylesheet(boolean 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.
reuseLoadedStylesheet = !b;
|
public void | setScanIncludedDirectories(boolean b)Whether to style all files in the included directories as well;
optional, default is true.
performDirectoryScan = b;
|
public void | setStyle(java.lang.String xslFile)Name of the stylesheet to use - given either relative
to the project's basedir or as an absolute path; required.
this.xslFile = xslFile;
|
public void | setUseImplicitFileset(boolean useimplicitfileset)Whether to use the implicit fileset.
Set this to false if you want explicit control with nested
resource collections.
useImplicitFileset = useimplicitfileset;
|
public void | setXslResource(org.apache.tools.ant.types.Resource xslResource)API method to set the XSL Resource.
this.xslResource = xslResource;
|