cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sylv...@apache.org
Subject svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml
Date Mon, 01 Aug 2005 16:52:55 GMT
Author: sylvain
Date: Mon Aug  1 09:52:50 2005
New Revision: 226838

URL: http://svn.apache.org/viewcvs?rev=226838&view=rev
Log:
Yeah! Real exceptions with Xalan rather than a useless RuntimeException!

Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/TraxTransformer.java
    cocoon/branches/BRANCH_2_1_X/status.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/TraxTransformer.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/TraxTransformer.java?rev=226838&r1=226837&r2=226838&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/TraxTransformer.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/TraxTransformer.java
Mon Aug  1 09:52:50 2005
@@ -26,6 +26,8 @@
 import java.util.Set;
 import java.util.Map.Entry;
 
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.TransformerException;
 import javax.xml.transform.sax.SAXResult;
 import javax.xml.transform.sax.TransformerHandler;
 
@@ -47,6 +49,10 @@
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.environment.Session;
 import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.util.ExceptionUtils;
+import org.apache.cocoon.util.TraxErrorHandler;
+import org.apache.cocoon.util.location.LocatedRuntimeException;
+import org.apache.cocoon.util.location.Location;
 import org.apache.cocoon.xml.XMLConsumer;
 import org.apache.commons.lang.BooleanUtils;
 
@@ -201,6 +207,32 @@
 
     /** Exception that might occur during setConsumer */
     private SAXException exceptionDuringSetConsumer;
+    
+    private TransformerException transformerException;
+    
+    private ErrorListener errorListener = new ErrorListener() {
+
+        public void warning(TransformerException ex) throws TransformerException {
+            if (getLogger().isWarnEnabled()) {
+                Location loc = ExceptionUtils.getLocation(ex);
+                getLogger().warn("Warning at " + loc == null ? inputSource.getURI() : loc.toString(),
ex);
+            }
+        }
+
+        public void error(TransformerException ex) throws TransformerException {
+            if (getLogger().isWarnEnabled()) {
+                Location loc = ExceptionUtils.getLocation(ex);
+                getLogger().error("Error at " + loc == null ? inputSource.getURI() : loc.toString(),
ex);
+            }
+        }
+
+        public void fatalError(TransformerException ex) throws TransformerException {
+            // Keep it for later use
+            transformerException = ex;
+            // and rethrow it
+            throw ex;
+        }
+    };
 
     /**
      * Configure this transformer.
@@ -413,6 +445,8 @@
         final SAXResult result = new SAXResult(consumer);
         result.setLexicalHandler(consumer);
         this.transformerHandler.setResult(result);
+        
+        this.transformerHandler.getTransformer().setErrorListener(this.errorListener);
     }
 
     /**
@@ -567,6 +601,7 @@
         this.transformerHandler = null;
         this.transformerValidity = null;
         this.exceptionDuringSetConsumer = null;
+        this.transformerException = null;
         super.recycle();
     }
 
@@ -575,7 +610,30 @@
      */
     public void endDocument()
     throws SAXException {
-        super.endDocument();
+        try {
+            super.endDocument();
+        } catch(SAXException se) {
+            // Rethrow
+            throw se;
+        } catch(Exception e) {
+            if (transformerException != null) {
+                // Ignore the fake RuntimeException sent by Xalan
+                Location loc = ExceptionUtils.getLocation(transformerException);
+                if (loc == null) {
+                    // No location: if it's just a wrapper, consider only the wrapped exception.
+                    Throwable realEx = transformerException.getCause();
+                    if (realEx == null) realEx = transformerException;
+                    
+                    // Now throw an exception locating the current stylesheet
+                    throw new LocatedRuntimeException("Error during transformation", realEx,
new Location(this.inputSource.getURI()));
+                } else {
+                    throw new SAXException(transformerException);
+                }
+            } else {
+                // It's not a fake exception
+                throw new SAXException(e);
+            }
+        }
         this.finishedDocument = true;
     }
     

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/status.xml?rev=226838&r1=226837&r2=226838&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Mon Aug  1 09:52:50 2005
@@ -196,6 +196,11 @@
 
   <changes>
   <release version="@version@" date="@date@">
+    <action dev="SW" type="fix">
+      Add a specific ErrorListener in TraxTransformer to track exceptions raised during XSLT
transformations.
+      This allows this transformer to throw the exception that actually occured, rather than
a useless RuntimeException
+      as reported by Xalan.
+    </action>
     <action dev="AN" type="add">
       XSP block: Extend {#expr} interpolation parser to understand {}, "}", '}'
       as not being the closing brace.



Mime
View raw message