ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r720858 - in /ant/core/trunk: WHATSNEW docs/manual/CoreTasks/style.html src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java src/tests/antunit/taskdefs/xslt-test.xml
Date Wed, 26 Nov 2008 14:08:20 GMT
Author: bodewig
Date: Wed Nov 26 06:08:20 2008
New Revision: 720858

URL: http://svn.apache.org/viewvc?rev=720858&view=rev
Log:
Add failOnError to xslt.  PR 36260.

Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/style.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
    ant/core/trunk/src/tests/antunit/taskdefs/xslt-test.xml

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=720858&r1=720857&r2=720858&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Wed Nov 26 06:08:20 2008
@@ -560,6 +560,11 @@
    the "trax" processor included with Ant does support it.
    Bugzilla Report 18897.
 
+ * <xslt> has two new attributes failOnError and
+   failOnTransformationError that can be used to not make the build
+   process proceed if an error occurs.
+   Bugzilla Report 36260.
+
 Changes from Ant 1.7.0 TO Ant 1.7.1
 =============================================
 

Modified: ant/core/trunk/docs/manual/CoreTasks/style.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/style.html?rev=720858&r1=720857&r2=720858&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/style.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/style.html Wed Nov 26 06:08:20 2008
@@ -230,6 +230,23 @@
     <em>Since Ant 1.8.0</em>.</td>
     <td valign="top" align="center">No, default is false.</td>
   </tr>
+  <tr>
+    <td valign="top">failOnError</td>
+    <td valign="top">Whether the build should fail if any error
+    occurs.  Note that transformation errors can still be surpressed by
+    setting failOnTransformationError to false even if this attribute
+    is true.
+    <em>Since Ant 1.8.0</em>.</td>
+    <td valign="top" align="center">No, default is true.</td>
+  </tr>
+  <tr>
+    <td valign="top">failOnTransformationError</td>
+    <td valign="top">Whether the build should fail if an error occurs
+    while transforming the document.  Note that this attribute has no
+    effect of <code>failOnError</code> is false.
+    <em>Since Ant 1.8.0</em>.</td>
+    <td valign="top" align="center">No, default is true.</td>
+  </tr>
 </table>
 <h3>Parameters specified as nested elements</h3>
 

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java?rev=720858&r1=720857&r2=720858&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/XSLTProcess.java Wed Nov 26 06:08:20
2008
@@ -177,6 +177,20 @@
     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;
+
+    /**
      * Creates a new XSLTProcess Task.
      */
     public XSLTProcess() {
@@ -213,9 +227,10 @@
      */
     public void addMapper(Mapper mapper) {
         if (mapperElement != null) {
-            throw new BuildException("Cannot define more than one mapper", getLocation());
+            handleError("Cannot define more than one mapper");
+        } else {
+            mapperElement = mapper;
         }
-        mapperElement = mapper;
     }
 
     /**
@@ -236,10 +251,11 @@
      */
     public void addConfiguredStyle(Resources rc) {
         if (rc.size() != 1) {
-            throw new BuildException(
-                    "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((Resource) rc.iterator().next());
         }
-        setXslResource((Resource) rc.iterator().next());
     }
 
     /**
@@ -285,13 +301,16 @@
             + "or as a nested resource";
 
         if (xslResource == null && xslFile == null) {
-            throw new BuildException(baseMessage, getLocation());
+            handleError(baseMessage);
+            return;
         }
         if (xslResource != null && xslFile != null) {
-            throw new BuildException(baseMessage + " but not as both", getLocation());
+            handleError(baseMessage + " but not as both");
+            return;
         }
         if (inFile != null && !inFile.exists()) {
-            throw new BuildException("input file " + inFile + " does not exist", getLocation());
+            handleError("input file " + inFile + " does not exist");
+            return;
         }
         try {
             Resource styleResource;
@@ -331,8 +350,8 @@
             }
 
             if (!styleResource.isExists()) {
-                throw new BuildException("stylesheet " + styleResource
-                                         + " doesn't exist.");
+                handleError("stylesheet " + styleResource + " doesn't exist.");
+                return;
             }
 
             // if we have an in file and out then process them
@@ -370,7 +389,8 @@
                 }
             } else { // only resource collections, there better be some
                 if (resources.size() == 0) {
-                    throw new BuildException("no resources specified");
+                    handleError("no resources specified");
+                    return;
                 }
             }
             processResources(styleResource);
@@ -539,6 +559,24 @@
     }    
 
     /**
+     * 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;
+    }
+
+    /**
      * Load processor here instead of in setProcessor - this will be
      * called from within execute, so we have access to the latest
      * classpath.
@@ -603,8 +641,7 @@
      */
     private void checkDest() {
         if (destDir == null) {
-            String msg = "destdir attributes must be set!";
-            throw new BuildException(msg);
+            handleError("destdir attributes must be set!");
         }
     }
 
@@ -691,8 +728,7 @@
             if (outF != null) {
                 outF.delete();
             }
-
-            throw new BuildException(ex);
+            handleTransformationError(ex);
         }
 
     } //-- processXML
@@ -727,7 +763,7 @@
             if (outFile != null) {
                 outFile.delete();
             }
-            throw new BuildException(ex);
+            handleTransformationError(ex);
         }
     }
 
@@ -741,8 +777,8 @@
         File directory = targetFile.getParentFile();
         if (!directory.exists()) {
             if (!directory.mkdirs()) {
-                throw new BuildException("Unable to create directory: "
-                        + directory.getAbsolutePath());
+                handleError("Unable to create directory: "
+                            + directory.getAbsolutePath());
             }
         }
     }
@@ -787,14 +823,14 @@
                 try {
                     resolveProcessor(processor);
                 } catch (Exception e) {
-                    throw new BuildException(e);
+                    handleError(e);
                 }
             } else {
                 try {
                     resolveProcessor(PROCESSOR_TRAX);
                 } catch (Throwable e1) {
                     e1.printStackTrace();
-                    throw new BuildException(e1);
+                    handleError(e1);
                 }
             }
         }
@@ -1026,8 +1062,9 @@
                 if (fp != null) {
                     liaison.setStylesheet(fp.getFile());
                 } else {
-                    throw new BuildException(liaison.getClass().toString()
-                            + " accepts the stylesheet only as a file", getLocation());
+                    handleError(liaison.getClass().toString()
+                                + " accepts the stylesheet only as a file");
+                    return;
                 }
             }
             for (Enumeration e = params.elements(); e.hasMoreElements();) {
@@ -1038,7 +1075,7 @@
             }
         } catch (Exception ex) {
             log("Failed to transform using stylesheet " + stylesheet, Project.MSG_INFO);
-            throw new BuildException(ex);
+            handleTransformationError(ex);
         }
     }
 
@@ -1074,13 +1111,59 @@
      */
     public Factory createFactory() throws BuildException {
         if (factory != null) {
-            throw new BuildException("'factory' element must be unique");
+            handleError("'factory' element must be unique");
+        } else {
+            factory = new Factory();
         }
-        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
      */

Modified: ant/core/trunk/src/tests/antunit/taskdefs/xslt-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/xslt-test.xml?rev=720858&r1=720857&r2=720858&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/xslt-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/xslt-test.xml Wed Nov 26 06:08:20 2008
@@ -73,4 +73,47 @@
             style="i-m-not-there.xslt"/>
     </au:expectfailure>
   </target>
+
+  <target name="testStyleDoesntExistNoError" depends="setUp">
+    <xslt in="${legacy.dir}/data.xml"
+          out="${output}/out.xml"
+          style="i-m-not-there.xslt"
+          failOnError="false"/>
+    <au:assertFileDoesntExist file="${output}/out.xml"/>
+  </target>
+
+  <target name="testStyleDoesntExistNoTransformationError" depends="setUp">
+    <au:expectfailure expectedmessage="i-m-not-there.xslt doesn't exist.">
+      <xslt in="${legacy.dir}/data.xml"
+            out="${output}/out.xml"
+            style="i-m-not-there.xslt"
+            failOnTransformationError="false"/>
+    </au:expectfailure>
+  </target>
+
+  <target name="testTransformationError">
+    <au:expectfailure expectedmessage="Fatal error during transformation">
+      <xslt in="${legacy.dir}/../input.stdin"
+            out="${output}/out.xml"
+            style="${legacy.dir}/printParams.xsl"
+            />
+    </au:expectfailure>
+  </target>
+
+  <target name="testTransformationErrorNoFail">
+    <xslt in="${legacy.dir}/../input.stdin"
+          out="${output}/out.xml"
+          style="${legacy.dir}/printParams.xsl"
+          failOnError="false"/>
+    <au:assertFileDoesntExist file="${output}/out.xml"/>
+  </target>
+
+  <target name="testTransformationErrorNoFailOnTransformation">
+    <xslt in="${legacy.dir}/../input.stdin"
+          out="${output}/out.xml"
+          style="${legacy.dir}/printParams.xsl"
+          failOnTransformationError="false"/>
+    <au:assertFileDoesntExist file="${output}/out.xml"/>
+  </target>
+
 </project>



Mime
View raw message