You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2005/08/01 18:52:55 UTC

svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml

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.



Re: svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml

Posted by Thorsten Scherler <th...@apache.org>.
On Tue, 2005-08-02 at 08:28 +0200, Bertrand Delacretaz wrote:
> Le 2 août 05, à 08:12, Sylvain Wallez a écrit :
> 
> > Thorsten Scherler wrote:
> >
> >> Will you commit to trunk as well?
> >>
> >
> > Not yet. I still have a bit of cleanup to do and will then commit to 
> > trunk, with the whole Cocoon stacktrace stuff.
> 
> Note that I also have a change to the TraxTransformer that's in the 
> branch only (the default-src option), if you're around feel free to 
> port this change as well. Otherwise I'll do it eventually, it's on my 
> list.
> 

Cheers for the information. :)

Good on ya.

salu2

> -Bertrand
-- 
thorsten

"Together we stand, divided we fall!" 
Hey you (Pink Floyd)


Re: svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml

Posted by Bertrand Delacretaz <bd...@apache.org>.
Le 2 août 05, à 08:12, Sylvain Wallez a écrit :

> Thorsten Scherler wrote:
>
>> Will you commit to trunk as well?
>>
>
> Not yet. I still have a bit of cleanup to do and will then commit to 
> trunk, with the whole Cocoon stacktrace stuff.

Note that I also have a change to the TraxTransformer that's in the 
branch only (the default-src option), if you're around feel free to 
port this change as well. Otherwise I'll do it eventually, it's on my 
list.

-Bertrand

Re: svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml

Posted by Sylvain Wallez <sy...@apache.org>.
Thorsten Scherler wrote:

>Will you commit to trunk as well?
>  
>

Not yet. I still have a bit of cleanup to do and will then commit to 
trunk, with the whole Cocoon stacktrace stuff.

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://people.apache.org/~sylvain     http://www.anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml

Posted by Thorsten Scherler <th...@apache.org>.
Will you commit to trunk as well?

salu2

On Mon, 2005-08-01 at 16:52 +0000, sylvain@apache.org wrote:
> 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.
> 
> 
-- 
thorsten

"Together we stand, divided we fall!" 
Hey you (Pink Floyd)


Re: svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml

Posted by Sylvain Wallez <sy...@apache.org>.
Vadim Gritsenko wrote:

> sylvain@apache.org wrote:
>
>> Author: sylvain
>> Date: Mon Aug  1 09:52:50 2005
>> New Revision: 226838
>>
>> 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
>> @@ -575,7 +610,30 @@
>>       */
>>      public void endDocument()
>>      throws SAXException {
>> -        super.endDocument();
>> +        try {
>> +            super.endDocument();
>> +        } catch(SAXException se) {
>> +            // Rethrow
>> +            throw se;
>
>
> Nitpicking... Shouldn't this be:
>
>
>> +        } catch(Exception e) {
>
>
>   +        } catch(RuntimeException 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);
>
>
>   +                throw e;
>
> It's better to preserve original (non xalan) RuntimeException, than to 
> wrap it. WDYT?


Hmm... Actually, we should even remove the initial SAXException, i.e. 
only having catch(Exception), as any TransformerException that would 
have been given to the ErrorListener should prevail on the actual 
exception that was caught.

And then yes, once we've unwrapped the exception, we could rethrow 
verbatim SAXException and RuntimeException without wrapping them.

Sylvain

-- 
Sylvain Wallez                        Anyware Technologies
http://people.apache.org/~sylvain     http://www.anyware-tech.com
Apache Software Foundation Member     Research & Technology Director


Re: svn commit: r226838 - in /cocoon/branches/BRANCH_2_1_X: src/java/org/apache/cocoon/transformation/TraxTransformer.java status.xml

Posted by Vadim Gritsenko <va...@reverycodes.com>.
sylvain@apache.org wrote:
> Author: sylvain
> Date: Mon Aug  1 09:52:50 2005
> New Revision: 226838
> 
> 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
> @@ -575,7 +610,30 @@
>       */
>      public void endDocument()
>      throws SAXException {
> -        super.endDocument();
> +        try {
> +            super.endDocument();
> +        } catch(SAXException se) {
> +            // Rethrow
> +            throw se;

Nitpicking... Shouldn't this be:


> +        } catch(Exception e) {

   +        } catch(RuntimeException 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);

   +                throw e;

It's better to preserve original (non xalan) RuntimeException, than to wrap it. 
WDYT?

Vadim


> +            }
> +        }
>          this.finishedDocument = true;
>      }