From f0565366fe94c3cdf2a35c881ce385a67bc0b88d Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 5 Dec 2013 18:49:03 +0000 Subject: [PATCH] =?UTF-8?q?Allow=20params=20of=20XSLT=20to=20be=20optional?= =?UTF-8?q?ly=20typed.=20=20PR=2021525.=20=20Submitted=20by=20Franti=C5=A1?= =?UTF-8?q?ek=20Ku=C4=8Dera?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1548237 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + WHATSNEW | 3 + contributors.xml | 4 + manual/Tasks/style.html | 88 +++++++- .../tools/ant/taskdefs/XSLTLiaison.java | 1 + .../tools/ant/taskdefs/XSLTLiaison4.java | 41 ++++ .../tools/ant/taskdefs/XSLTProcess.java | 192 +++++++++++++++++- .../ant/taskdefs/optional/TraXLiaison.java | 18 +- src/tests/antunit/taskdefs/xslt-test.xml | 40 ++++ 9 files changed, 372 insertions(+), 16 deletions(-) create mode 100644 src/main/org/apache/tools/ant/taskdefs/XSLTLiaison4.java diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 1b1d8698f..7449a1f64 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -121,6 +121,7 @@ Ernst de Haan Frank Harnack Frank Somers Frank Zeyda +Frantisek Kucera Frederic Bothamy Frederic Lavigne Gary S. Weaver diff --git a/WHATSNEW b/WHATSNEW index 45a3fe70e..8ddbccfa6 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -32,6 +32,9 @@ Other changes: * now supports a nested to specify filters. Bugzilla Report 55794. + * 's params can now be typed. + Bugzilla Report 21525. + Changes from Ant 1.9.1 TO Ant 1.9.2 =================================== diff --git a/contributors.xml b/contributors.xml index 88cd45221..68b144efe 100644 --- a/contributors.xml +++ b/contributors.xml @@ -507,6 +507,10 @@ Frank Zeyda + + František + Kučera + Frédéric Bothamy diff --git a/manual/Tasks/style.html b/manual/Tasks/style.html index 8ea94935b..e97e88ea5 100644 --- a/manual/Tasks/style.html +++ b/manual/Tasks/style.html @@ -295,10 +295,32 @@ element is used to perform Entity and URI resolution.

expression - Text value to be placed into the param.
- Was originally intended to be an XSL expression. + + The value to be placed into the param or an XPath expression + (depending on type). + Yes + + type + + Data type of the parameter. Possible values are: +
    +
  • STRING
  • +
  • BOOLEAN
  • +
  • INT
  • +
  • LONG
  • +
  • DOUBLE
  • +
  • XPATH_STRING
  • +
  • XPATH_BOOLEAN
  • +
  • XPATH_NUMBER
  • +
  • XPATH_NODE
  • +
  • XPATH_NODESET
  • +
+ since Ant 1.9.3 + + No; default is STRING + if The param will only be passed if this property is set. @@ -313,6 +335,18 @@ element is used to perform Entity and URI resolution.

+

+ The XPATH_* types says that the expression is not just a primitive-type value but an XPath expression. + This expression will be evaluated on an empty XML document and the result will be passed to the XSLT transformer as a parameter of given type. + In these expressions the declared Ant properties can be used as XPath variables e.g. $someProperty. + So you can compute something using standard XPath functions and operators. +

+

+ If you write ${someProperty} instead of $someProperty, + the value will be simply substituted by Ant before evaluating the XPath expression + (this substitution works also for primitive types). +

+

outputproperty ('trax' processors only)

Used to specify how you wish the result tree to be output as specified in the @@ -459,6 +493,7 @@ with syspropertysets.

</xslt>

Using XSL parameters

+

Simple String parameter:

 <xslt basedir="doc" destdir="build/doc"
       extension=".html" style="style/apache.xsl">
@@ -469,6 +504,55 @@ with syspropertysets.

element <xsl:param name="date"/>, the variable $date will subsequently have the value 07-01-2000.

+ +

Various data types and XPath expressions:

+ +
<property name="antProperty1" value="ANT_PROPERTY_1"/>
+<property name="antProperty2" value="ANT_PROPERTY_2"/>
+<property name="antProperty3" value="3"/>
+<property name="antProperty4" value="substring-before"/>
+
+<!--
+  ${this} is substituted by Ant itself
+  and $this is evaluated by XPath as a variable
+-->
+
+<xslt in="in.xml" out="out.xml" style="template.xsl">
+  
+  <!-- Simple String parameter: -->
+  <param name="p0" expression="some nice string" type="STRING"/>
+  
+  <!-- A value substituted by Ant -->
+  <param name="p1" expression="some string with ${antProperty1} constructed by Ant" type="STRING"/>
+  
+  <!-- XPath resulting in: and this is done in XPath: ANT_PROPERTY_2 -->
+  <param name="p2" expression="concat('and this is done in XPath: ', $antProperty2)" type="XPATH_STRING"/>
+  
+  <!-- Some XPath math, result: 42 -->
+  <param name="p3" expression="64 * 64 div 128 + 10" type="XPATH_NUMBER"/>
+  
+  <!-- Some numeric parameter: -->
+  <param name="p4" expression="123.45" type="DOUBLE"/>
+  
+  <!-- XPath expression, result: true boolean -->
+  <param name="p5" expression="$antProperty1 = 'ANT_PROPERTY_1'" type="XPATH_BOOLEAN"/>
+  
+  <!-- First one is an XPath variable, second one is a text substituted by Ant, result: true boolean -->
+  <param name="p6" expression="$antProperty2 = '${antProperty2}'" type="XPATH_BOOLEAN"/>
+  
+  <!-- Some XPath math with a variable, result: 64 -->
+  <param name="p7" expression="$antProperty3 * 4 * 5 + 4" type="XPATH_NUMBER"/>
+  
+  <!-- 
+    XPath expression with substituted function name and a variable:
+    substring-before($antProperty2, '_')
+    result: ANT
+  -->
+  <param name="p8" expression="${antProperty4}($antProperty2, '_')" type="XPATH_STRING"/>
+  
+  <!-- Without type attribute: -->
+  <param name="p9" expression="default type is String"/>
+</xslt>

Using output properties

diff --git a/src/main/org/apache/tools/ant/taskdefs/XSLTLiaison.java b/src/main/org/apache/tools/ant/taskdefs/XSLTLiaison.java
index 71e1def7e..df22cf2d3 100644
--- a/src/main/org/apache/tools/ant/taskdefs/XSLTLiaison.java
+++ b/src/main/org/apache/tools/ant/taskdefs/XSLTLiaison.java
@@ -51,6 +51,7 @@ public interface XSLTLiaison {
      * @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;
diff --git a/src/main/org/apache/tools/ant/taskdefs/XSLTLiaison4.java b/src/main/org/apache/tools/ant/taskdefs/XSLTLiaison4.java
new file mode 100644
index 000000000..5de380b70
--- /dev/null
+++ b/src/main/org/apache/tools/ant/taskdefs/XSLTLiaison4.java
@@ -0,0 +1,41 @@
+/*
+ *  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
+ * @author Frantisek Kucera (xkucf03)
+ * @since Ant 1.9.3
+ */
+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.3
+     * @see Transformer#setParameter(java.lang.String, java.lang.Object)
+     */
+    void addParam(String name, Object value) throws Exception;
+}
diff --git a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
index bde1c55dc..92f4ef1ce 100644
--- a/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
+++ b/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
@@ -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.EnumMap;
 import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
 import java.util.Vector;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+import 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 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
     private String fileDirParameter = null;
 
     /** additional parameters to be passed to the stylesheets */
-    private Vector params = new Vector();
+    private List params = new ArrayList();
 
     /** Input XML document to be used */
     private File inFile = null;
@@ -196,6 +208,19 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
      * @since Ant 1.8.0
      */
     private boolean failOnNoResources = true;
+    
+    /**
+     * For evaluating template params
+     *
+     * @since Ant 1.9.3
+     */
+    private XPathFactory xpathFactory;
+    /**
+     * For evaluating template params
+     *
+     * @since Ant 1.9.3
+     */
+    private XPath xpath;
 
     /**
      * System properties to set during transformation.
@@ -305,8 +330,9 @@ public class XSLTProcess extends MatchingTask implements XSLTLogger {
      * 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 
+    
+    
+    
+    
+