ASF Bugzilla – Attachment 30928 Details for
Bug 21525
[PATCH] XSLT/param@expression used as string instead of as expression as documented
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
improved patch - adds XSLTLiaison4 interface for compatibility
xslt-param-types-2.diff (text/plain), 15.10 KB, created by
František Kučera <xkucf03/>
on 2013-10-15 16:12:52 UTC
(
hide
)
Description:
improved patch - adds XSLTLiaison4 interface for compatibility
Filename:
MIME Type:
Creator:
František Kučera <xkucf03/>
Created:
2013-10-15 16:12:52 UTC
Size:
15.10 KB
patch
obsolete
>Index: src/main/org/apache/tools/ant/taskdefs/XSLTLiaison.java >=================================================================== >--- src/main/org/apache/tools/ant/taskdefs/XSLTLiaison.java (revision 1532335) >+++ src/main/org/apache/tools/ant/taskdefs/XSLTLiaison.java (working copy) >@@ -51,6 +51,7 @@ > * @param name the parameter name. > * @param expression the parameter value as an expression string. > * @throws Exception thrown if any problems happens. >+ * @see XSLTLiaison4#addParam(java.lang.String, java.lang.Object) > * @since Ant 1.3 > */ > void addParam(String name, String expression) throws Exception; >Index: src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java >=================================================================== >--- src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java (revision 1532335) >+++ src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java (working copy) >@@ -18,8 +18,20 @@ > package org.apache.tools.ant.taskdefs; > > import java.io.File; >+import java.util.ArrayList; >+import java.util.Collections; > import java.util.Enumeration; >+import java.util.HashMap; >+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 javax.xml.xpath.XPathVariableResolver; > import org.apache.tools.ant.AntClassLoader; > import org.apache.tools.ant.BuildException; > import org.apache.tools.ant.DirectoryScanner; >@@ -76,7 +88,7 @@ > private String fileDirParameter = null; > > /** additional parameters to be passed to the stylesheets */ >- private Vector params = new Vector(); >+ private List<Param> params = new ArrayList<Param>(); > > /** Input XML document to be used */ > private File inFile = null; >@@ -196,6 +208,19 @@ > * @since Ant 1.8.0 > */ > private boolean failOnNoResources = true; >+ >+ /** >+ * For evaluating template params >+ * >+ * @since Ant 1.9.2 >+ */ >+ private XPathFactory xpathFactory; >+ /** >+ * For evaluating template params >+ * >+ * @since Ant 1.9.2 >+ */ >+ private XPath xpath; > > /** > * System properties to set during transformation. >@@ -305,8 +330,9 @@ > * Executes the task. > * > * @exception BuildException if there is an execution problem. >- * @todo validate that if either in or our is defined, then both are >+ * @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 <style> is deprecated. Use <xslt> instead.", >@@ -937,7 +963,7 @@ > */ > public Param createParam() { > Param p = new Param(); >- params.addElement(p); >+ params.add(p); > return p; > } > >@@ -950,6 +976,12 @@ > > /** The parameter's value */ > private String expression = null; >+ >+ /** >+ * Type of the expression. >+ * @see ParamType >+ */ >+ private String type; > > private Object ifCond; > private Object unlessCond; >@@ -974,15 +1006,23 @@ > } > > /** >- * The parameter value >- * NOTE : was intended to be an XSL expression. >- * @param expression the parameter's value. >+ * The parameter value - can be a primitive value or XPath expression. >+ * @param expression the parameter's value/expression. >+ * @see #setType(java.lang.String) > */ > public void setExpression(String expression) { > this.expression = expression; > } > > /** >+ * @see ParamType >+ * @since Ant 1.9.2 >+ */ >+ public void setType(String type) { >+ this.type = type; >+ } >+ >+ /** > * Get the parameter name > * > * @return the parameter name >@@ -1000,6 +1040,7 @@ > * > * @return the parameter value > * @exception BuildException if the value is not set. >+ * @see #getType() > */ > public String getExpression() throws BuildException { > if (expression == null) { >@@ -1009,6 +1050,14 @@ > } > > /** >+ * @see ParamType >+ * @since Ant 1.9.2 >+ */ >+ public String getType() { >+ return type; >+ } >+ >+ /** > * Set whether this param should be used. It will be used if > * the expression evaluates to true or the name of a property > * which has been set, otherwise it won't. >@@ -1061,7 +1110,64 @@ > && ph.testUnlessCondition(unlessCond); > } > } // Param >+ >+ /** >+ * Constants for types of the parameter expression. >+ * >+ * The expression can be: >+ * <ul> >+ * <li>primitive type that will be parsed from the string value e.g. >+ * {@linkplain Integer#parseInt(java.lang.String)}</li> >+ * <li>XPath expression that will be evaluated (outside of the transformed >+ * document - on empty one) and casted to given type. Inside XPath >+ * expressions the Ant variables (properties) can be used (as XPath >+ * variables - e.g. $variable123). n.b. placeholders in form of >+ * ${variable123} will be replaced with their values before evaluating the >+ * XPath expression (so it can be used for dynamic XPath function names and >+ * other hacks).</li> >+ * </ul> >+ * The parameter will be then passed to the XSLT template. >+ * >+ * Default type (if omited) is primitive String. So if the expression is e.g >+ * "true" with no type, in XSLT it will be only a text string, not true >+ * boolean. >+ * >+ * @see Param#setType(java.lang.String) >+ * @see Param#setExpression(java.lang.String) >+ * @since Ant 1.9.2 >+ */ >+ public static class ParamType { > >+ public static final String STRING = "string"; >+ public static final String BOOLEAN = "boolean"; >+ public static final String INT = "int"; >+ public static final String LONG = "long"; >+ public static final String DOUBLE = "double"; >+ >+ >+ private static final String XPATH_PREFIX = "xpath:"; >+ >+ public static final String XPATH_STRING = "xpath:string"; >+ public static final String XPATH_BOOLEAN = "xpath:boolean"; >+ public static final String XPATH_NUMBER = "xpath:number"; >+ public static final String XPATH_NODE = "xpath:node"; >+ public static final String XPATH_NODESET = "xpath:nodeset"; >+ >+ private static final Map<String,QName> XPATH_TYPES; >+ >+ static { >+ Map<String, QName> m = new HashMap<String, QName>(); >+ m.put(XPATH_STRING, XPathConstants.STRING); >+ m.put(XPATH_BOOLEAN, XPathConstants.BOOLEAN); >+ m.put(XPATH_NUMBER, XPathConstants.NUMBER); >+ m.put(XPATH_NODE, XPathConstants.NODE); >+ m.put(XPATH_NODESET, XPathConstants.NODESET); >+ XPATH_TYPES = Collections.unmodifiableMap(m); >+ } >+ >+ private ParamType() { } >+ } >+ > /** > * Create an instance of an output property to be configured. > * @return the newly created output property. >@@ -1119,12 +1225,22 @@ > } > > /** >- * Initialize internal instance of XMLCatalog >+ * Initialize internal instance of XMLCatalog. >+ * Initialize XPath for parameter evaluation. > * @throws BuildException on error > */ >+ @Override > public void init() throws BuildException { > super.init(); > xmlCatalog.setProject(getProject()); >+ >+ xpathFactory = XPathFactory.newInstance(); >+ xpath = xpathFactory.newXPath(); >+ xpath.setXPathVariableResolver(new XPathVariableResolver() { >+ public Object resolveVariable(QName variableName) { >+ return getProject().getProperty(variableName.toString()); >+ } >+ }); > } > > /** >@@ -1179,10 +1295,21 @@ > return; > } > } >- for (Enumeration e = params.elements(); e.hasMoreElements();) { >- Param p = (Param) e.nextElement(); >+ for (Param p : params) { > if (p.shouldUse()) { >- liaison.addParam(p.getName(), p.getExpression()); >+ Object evaluatedParam = evaluateParam(p); >+ if (liaison instanceof XSLTLiaison4) { >+ ((XSLTLiaison4)liaison).addParam(p.getName(), 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)); >+ } >+ } > } > } > } catch (Exception ex) { >@@ -1190,7 +1317,46 @@ > handleTransformationError(ex); > } > } >+ >+ /** >+ * Evaluates parameter expression according to its type. >+ * >+ * @param param parameter from Ant build file >+ * @return value to be passed to XSLT as parameter >+ * @throws IllegalArgumentException if param type is unsupported >+ * @throws NumberFormatException if expression of numeric type is not >+ * desired numeric type >+ * @throws XPathExpressionException if XPath expression can not be compiled >+ * @since Ant 1.9.2 >+ */ >+ private Object evaluateParam(Param param) throws XPathExpressionException { >+ String type = param.getType(); >+ String expression = param.getExpression(); > >+ if (type == null || ParamType.STRING.equals(type)) { >+ return expression; >+ } else if (ParamType.BOOLEAN.equals(type)) { >+ return Boolean.parseBoolean(expression); >+ } else if (ParamType.DOUBLE.equals(type)) { >+ return Double.parseDouble(expression); >+ } else if (ParamType.INT.equals(type)) { >+ return Integer.parseInt(expression); >+ } else if (ParamType.LONG.equals(type)) { >+ return Long.parseLong(expression); >+ } else if (type.startsWith(ParamType.XPATH_PREFIX)) { >+ XPathExpression xpe = xpath.compile(expression); >+ QName xpathType = ParamType.XPATH_TYPES.get(type); >+ if (xpathType == null) { >+ throw new IllegalArgumentException("Invalid XSLT parameter XPath type: " + type); >+ } else { >+ // null = evaluate XPath on empty XML document >+ return xpe.evaluate((Object) null, xpathType); >+ } >+ } else { >+ throw new IllegalArgumentException("Invalid XSLT parameter type: " + type); >+ } >+ } >+ > /** > * Sets file parameter(s) for directory and filename if the attribute > * 'filenameparameter' or 'filedirparameter' are set in the task. >Index: src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java >=================================================================== >--- src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java (revision 1532335) >+++ src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java (working copy) >@@ -47,7 +47,7 @@ > import javax.xml.transform.TransformerConfigurationException; > import org.apache.tools.ant.BuildException; > import org.apache.tools.ant.Project; >-import org.apache.tools.ant.taskdefs.XSLTLiaison3; >+import org.apache.tools.ant.taskdefs.XSLTLiaison4; > import org.apache.tools.ant.taskdefs.XSLTLogger; > import org.apache.tools.ant.taskdefs.XSLTLoggerAware; > import org.apache.tools.ant.taskdefs.XSLTProcess; >@@ -68,7 +68,7 @@ > * > * @since Ant 1.3 > */ >-public class TraXLiaison implements XSLTLiaison3, ErrorListener, XSLTLoggerAware { >+public class TraXLiaison implements XSLTLiaison4, ErrorListener, XSLTLoggerAware { > > /** > * Helper for transforming filenames to URIs. >@@ -118,7 +118,7 @@ > private Vector outputProperties = new Vector(); > > /** stylesheet parameters */ >- private Hashtable params = new Hashtable(); >+ private Hashtable<String, Object> params = new Hashtable<String, Object>(); > > /** factory attributes */ > private Vector attributes = new Vector(); >@@ -369,7 +369,7 @@ > for (final Enumeration enumeration = params.keys(); > enumeration.hasMoreElements();) { > final String name = (String) enumeration.nextElement(); >- final String value = (String) params.get(name); >+ final Object value = params.get(name); > transformer.setParameter(name, value); > } > } >@@ -505,6 +505,15 @@ > public void addParam(String name, String value) { > params.put(name, value); > } >+ >+ /** >+ * Add a parameter. >+ * @param name the name of the parameter >+ * @param value the value of the parameter >+ */ >+ public void addParam(String name, Object value) { >+ params.put(name, value); >+ } > > /** > * Set a logger. >Index: src/main/org/apache/tools/ant/taskdefs/XSLTLiaison4.java >=================================================================== >--- src/main/org/apache/tools/ant/taskdefs/XSLTLiaison4.java (revision 0) >+++ src/main/org/apache/tools/ant/taskdefs/XSLTLiaison4.java (working copy) >@@ -0,0 +1,40 @@ >+/* >+ * 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; >+ >+/** >+ * Extends Proxy interface for XSLT processors: adds support for XSLT parameters >+ * of various types (not only String) >+ * >+ * >+ * @see XSLTProcess >+ * @since Ant 1.9.2 >+ */ >+public interface XSLTLiaison4 extends XSLTLiaison3 { >+ >+ /** >+ * Add a parameter to be set during the XSL transformation. >+ * >+ * @param name the parameter name. >+ * @param value the parameter value as String, Boolean, int, etc. >+ * @throws Exception thrown if any problems happens. >+ * @since Ant 1.9.2 >+ * @see Transformer#setParameter(java.lang.String, java.lang.Object) >+ */ >+ void addParam(String name, Object value) throws Exception; >+}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 21525
:
30495
|
30496
|
30928
|
30929
|
30943