diff options
Diffstat (limited to 'src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java')
-rw-r--r-- | src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java | 171 |
1 files changed, 76 insertions, 95 deletions
diff --git a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java index 8fce71eeb..0902e5504 100644 --- a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java +++ b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java @@ -18,6 +18,7 @@ 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; @@ -32,7 +33,6 @@ import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; -import javax.xml.xpath.XPathVariableResolver; import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.BuildException; @@ -71,6 +71,15 @@ import org.apache.tools.ant.util.StringUtils; */ 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; @@ -93,7 +102,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { private String fileDirParameter = null; /** additional parameters to be passed to the stylesheets */ - private final List<Param> params = new ArrayList<Param>(); + private final List<Param> params = new ArrayList<>(); /** Input XML document to be used */ private File inFile = null; @@ -119,14 +128,11 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { private boolean force = false; /** XSL output properties to be used */ - private final Vector outputProperties = new Vector(); + private final List<OutputProperty> outputProperties = new Vector<>(); /** for resolving entities such as dtds */ private final 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. * @@ -180,12 +186,6 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { 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 @@ -243,12 +243,6 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { 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. * @@ -303,8 +297,8 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { */ public void addConfiguredStyle(final Resources rc) { if (rc.size() != 1) { - handleError("The style element must be specified with exactly one" - + " nested resource."); + handleError( + "The style element must be specified with exactly one nested resource."); } else { setXslResource(rc.iterator().next()); } @@ -345,13 +339,8 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { } final File savedBaseDir = baseDir; - DirectoryScanner scanner; - String[] list; - String[] dirs; - final String baseMessage = - "specify the stylesheet either as a filename in style attribute " - + "or as a nested resource"; + "specify the stylesheet either as a filename in style attribute or as a nested resource"; if (xslResource == null && xslFile == null) { handleError(baseMessage); @@ -395,8 +384,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * the wrong version has been used. */ if (alternative.exists()) { - log("DEPRECATED - the 'style' attribute should be " - + "relative to the project's"); + log("DEPRECATED - the 'style' attribute should be relative to the project's"); log(" basedir, not the tasks's basedir."); stylesheet = alternative; } @@ -427,33 +415,30 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { //-- make sure destination directory exists... checkDest(); + if (useImplicitFileset) { - scanner = getDirectoryScanner(baseDir); + DirectoryScanner 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); + for (String element : scanner.getIncludedFiles()) { + process(baseDir, element, 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, + for (String dir : scanner.getIncludedDirectories()) { + for (String element : new File(baseDir, dir).list()) { + process(baseDir, dir + File.separator + element, destDir, styleResource); } } } - } else { // only resource collections, there better be some - if (resources.size() == 0) { - if (failOnNoResources) { - handleError("no resources specified"); - } - return; + } else if (resources.isEmpty()) { + // only resource collections, there better be some + if (failOnNoResources) { + handleError("no resources specified"); } + return; } processResources(styleResource); } finally { @@ -678,8 +663,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { */ public TraceConfiguration createTrace() { if (traceConfiguration != null) { - throw new BuildException("can't have more than one trace" - + " configuration"); + throw new BuildException("can't have more than one trace configuration"); } traceConfiguration = new TraceConfiguration(); return traceConfiguration; @@ -703,12 +687,12 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * @exception Exception if the processor cannot be loaded. */ private void resolveProcessor(final String proc) throws Exception { - if (proc.equals(PROCESSOR_TRAX)) { + if (PROCESSOR_TRAX.equals(proc)) { liaison = new org.apache.tools.ant.taskdefs.optional.TraXLiaison(); } else { //anything else is a classname - final Class clazz = loadClass(proc); - liaison = (XSLTLiaison) clazz.newInstance(); + final Class<? extends XSLTLiaison> clazz = loadClass(proc).asSubclass(XSLTLiaison.class); + liaison = clazz.newInstance(); } } @@ -721,7 +705,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * @return the requested class. * @exception Exception if the class could not be loaded. */ - private Class loadClass(final String classname) throws ClassNotFoundException { + private Class<?> loadClass(final String classname) throws ClassNotFoundException { setupLoader(); if (loader == null) { return Class.forName(classname); @@ -811,29 +795,25 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { throws BuildException { File outF = null; - File inF = null; try { final long styleSheetLastModified = stylesheet.getLastModified(); - inF = new File(baseDir, xmlFile); + File 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(); - } + FileNameMapper mapper = mapperElement == null ? new StyleMapper() + : mapperElement.getImplementation(); final 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.length > 1) { - log("Skipping " + inFile + " its mapping is ambiguos.", Project.MSG_VERBOSE); + } + if (outFileName.length > 1) { + log("Skipping " + inFile + " its mapping is ambiguous.", Project.MSG_VERBOSE); return; } outF = new File(destDir, outFileName[0]); @@ -931,8 +911,8 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * Get an enumeration on the outputproperties. * @return the outputproperties */ - public Enumeration getOutputProperties() { - return outputProperties.elements(); + public Enumeration<OutputProperty> getOutputProperties() { + return Collections.enumeration(outputProperties); } /** @@ -1175,7 +1155,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { */ public OutputProperty createOutputProperty() { final OutputProperty p = new OutputProperty(); - outputProperties.addElement(p); + outputProperties.add(p); return p; } @@ -1236,11 +1216,8 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { xpathFactory = XPathFactory.newInstance(); xpath = xpathFactory.newXPath(); - xpath.setXPathVariableResolver(new XPathVariableResolver() { - public Object resolveVariable(final QName variableName) { - return getProject().getProperty(variableName.toString()); - } - }); + xpath.setXPathVariableResolver( + variableName -> getProject().getProperty(variableName.toString())); } /** @@ -1300,16 +1277,15 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { if (p.shouldUse()) { final Object evaluatedParam = evaluateParam(p); if (liaison instanceof XSLTLiaison4) { - ((XSLTLiaison4)liaison).addParam(p.getName(), evaluatedParam); + ((XSLTLiaison4) liaison).addParam(p.getName(), + evaluatedParam); + } else if (evaluatedParam == null || evaluatedParam instanceof String) { + liaison.addParam(p.getName(), (String)evaluatedParam); } else { - if (evaluatedParam == null || evaluatedParam instanceof String) { - liaison.addParam(p.getName(), (String)evaluatedParam); - } else { - log("XSLTLiaison '" + liaison.getClass().getName() - + "' supports only String parameters. Converting parameter '" + p.getName() - + "' to its String value '" + evaluatedParam, Project.MSG_WARN); - liaison.addParam(p.getName(), String.valueOf(evaluatedParam)); - } + log("XSLTLiaison '" + liaison.getClass().getName() + + "' supports only String parameters. Converting parameter '" + p.getName() + + "' to its String value '" + evaluatedParam, Project.MSG_WARN); + liaison.addParam(p.getName(), String.valueOf(evaluatedParam)); } } } @@ -1336,7 +1312,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { ParamType type; - if (typeName == null || "".equals(typeName)) { + if (typeName == null || typeName.isEmpty()) { type = ParamType.STRING; // String is default } else { try { @@ -1361,11 +1337,10 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { final QName xpathType = ParamType.XPATH_TYPES.get(type); if (xpathType == null) { throw new IllegalArgumentException("Invalid XSLT parameter type: " + typeName); - } else { - final XPathExpression xpe = xpath.compile(expression); - // null = evaluate XPath on empty XML document - return xpe.evaluate((Object) null, xpathType); } + final XPathExpression xpe = xpath.compile(expression); + // null = evaluate XPath on empty XML document + return xpe.evaluate((Object) null, xpathType); } } @@ -1432,9 +1407,8 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { protected void handleError(final Throwable ex) { if (failOnError) { throw new BuildException(ex); - } else { - log("Caught an exception: " + ex, Project.MSG_WARN); } + log("Caught an exception: " + ex, Project.MSG_WARN); } /** @@ -1447,10 +1421,9 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { protected void handleTransformationError(final Exception ex) { if (failOnError && failOnTransformationError) { throw new BuildException(ex); - } else { - log("Caught an error during transformation: " + ex, - Project.MSG_WARN); } + log("Caught an error during transformation: " + ex, + Project.MSG_WARN); } /** @@ -1465,12 +1438,12 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { /** * the list of factory attributes to use for TraXLiaison */ - private final List<Attribute> attributes = new ArrayList<Attribute>(); + private final List<Attribute> attributes = new ArrayList<>(); /** * the list of factory features to use for TraXLiaison */ - private final List<Feature> features = new ArrayList<Feature>(); + private final List<Feature> features = new ArrayList<>(); /** * @return the name of the factory. @@ -1499,7 +1472,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * return the attribute elements. * @return the enumeration of attributes */ - public Enumeration getAttributes() { + public Enumeration<Attribute> getAttributes() { return Collections.enumeration(attributes); } @@ -1528,8 +1501,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * <li>http://xml.apache.org/xalan/features/incremental (true|false) </li> * </ul> */ - public static class Attribute - extends ProjectComponent + public static class Attribute extends ProjectComponent implements DynamicConfigurator { /** attribute name, mostly processor specific */ @@ -1558,6 +1530,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * @return null * @throws BuildException never */ + @Override public Object createDynamicElement(final String name) throws BuildException { return null; } @@ -1569,6 +1542,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * @param value the value of the attribute * @throws BuildException on error */ + @Override public void setDynamicAttribute(final String name, final String value) throws BuildException { // only 'name' and 'value' exist. if ("name".equalsIgnoreCase(name)) { @@ -1582,7 +1556,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { this.value = Boolean.FALSE; } else { try { - this.value = new Integer(value); + this.value = Integer.valueOf(value); } catch (final NumberFormatException e) { this.value = value; } @@ -1595,7 +1569,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { new Reference(getProject(), value)); } else { - throw new BuildException("Unsupported attribute: " + name); + throw new BuildException("Unsupported attribute: %s", name); } } } // -- class Attribute @@ -1608,7 +1582,9 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { private String name; private boolean value; - public Feature() { } + public Feature() { + } + public Feature(String name, boolean value) { this.name = name; this.value = value; @@ -1654,10 +1630,15 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { * @since Ant 1.6.2 */ private class StyleMapper implements FileNameMapper { + @Override public void setFrom(final String from) { } + + @Override public void setTo(final String to) { } + + @Override public String[] mapFileName(String xmlFile) { final int dotPos = xmlFile.lastIndexOf('.'); if (dotPos > 0) { @@ -1758,7 +1739,7 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger { /** * The stream to write traces to. */ - public java.io.OutputStream getOutputStream() { + public OutputStream getOutputStream() { return new LogOutputStream(XSLTProcess.this); } } |