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/02 19:22:18 UTC

svn commit: r227047 - in /cocoon/branches/BRANCH_2_1_X/src: java/org/apache/cocoon/ java/org/apache/cocoon/components/treeprocessor/sitemap/ java/org/apache/cocoon/generation/ java/org/apache/cocoon/transformation/ java/org/apache/cocoon/util/location/...

Author: sylvain
Date: Tue Aug  2 10:22:04 2005
New Revision: 227047

URL: http://svn.apache.org/viewcvs?rev=227047&view=rev
Log:
Decouple Cocoon stacktrace from exception stacktrace by allowing exceptions to have multiple locations

Added:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/MultiLocatable.java   (with props)
Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/ProcessingException.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/CallFunctionNode.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/MountNode.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SerializeNode.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/ExceptionGenerator.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/transformation/TraxTransformer.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java
    cocoon/branches/BRANCH_2_1_X/src/webapp/stylesheets/system/exception2html.xslt

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/ProcessingException.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/ProcessingException.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/ProcessingException.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/ProcessingException.java Tue Aug  2 10:22:04 2005
@@ -15,17 +15,12 @@
  */
 package org.apache.cocoon;
 
-import java.io.PrintStream;
-import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
 
-import javax.xml.transform.SourceLocator;
-import javax.xml.transform.TransformerException;
-
-import org.apache.avalon.framework.CascadingException;
-import org.apache.cocoon.util.location.Locatable;
 import org.apache.cocoon.util.location.LocatedException;
 import org.apache.cocoon.util.location.Location;
-import org.xml.sax.SAXParseException;
+import org.apache.cocoon.util.location.MultiLocatable;
 
 /**
  * This Exception is thrown every time there is a problem in processing
@@ -35,8 +30,8 @@
  *         (Apache Software Foundation)
  * @version CVS $Id$
  */
-public class ProcessingException extends LocatedException {
-
+public class ProcessingException extends LocatedException implements MultiLocatable {
+    
     /**
      * Construct a new <code>ProcessingException</code> instance.
      */
@@ -69,6 +64,17 @@
         super(message, t, location);
     }
     
+    public static ProcessingException getLocatedException(String description, Throwable thr, Location location) {
+        if (thr instanceof ProcessingException) {
+            ProcessingException pe = (ProcessingException)thr;
+            pe.addLocation(location);
+            return pe;
+        }
+        
+        return new ProcessingException(description, thr, location);
+    }
+
+
 //    public String toString() {
 //        StringBuffer s = new StringBuffer();
 //        s.append(super.toString());

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/CallFunctionNode.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/CallFunctionNode.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/CallFunctionNode.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/CallFunctionNode.java Tue Aug  2 10:22:04 2005
@@ -124,7 +124,7 @@
             try {
                 interpreter.handleContinuation(continuation, params, redirector);
             } catch(Exception e) {
-                throw new ProcessingException("Sitemap: error calling continuation", e, Location.parse(getLocation()));
+                throw ProcessingException.getLocatedException("Sitemap: error calling continuation", e, Location.parse(getLocation()));
             }
             if (!redirector.hasRedirected()) {
                 throw new ProcessingException("Sitemap: <map:call continuation> did not send a response", Location.parse(getLocation()));
@@ -139,7 +139,7 @@
             try {
                 interpreter.callFunction(name, params, redirector);
             } catch(Exception e) {
-                throw new ProcessingException("Sitemap: error calling function '" + name + "'", e, Location.parse(getLocation()));
+                throw ProcessingException.getLocatedException("Sitemap: error calling function '" + name + "'", e, Location.parse(getLocation()));
             }
             if (!redirector.hasRedirected()) {
                 throw new ProcessingException("Sitemap: <map:call function> did not send a response", Location.parse(getLocation()));

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/MountNode.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/MountNode.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/MountNode.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/MountNode.java Tue Aug  2 10:22:04 2005
@@ -119,7 +119,7 @@
             }
         } catch(Exception e) {
             // Wrap with our location
-            throw new ProcessingException("Sitemap: error when calling sub-sitemap", e, Location.parse(getLocation()));
+            throw ProcessingException.getLocatedException("Sitemap: error when calling sub-sitemap", e, Location.parse(getLocation()));
 
         } finally {
             // We restore the context only if no pipeline was built. This allows the pipeline

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SerializeNode.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SerializeNode.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SerializeNode.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/treeprocessor/sitemap/SerializeNode.java Tue Aug  2 10:22:04 2005
@@ -123,7 +123,7 @@
                 return pipeline.process(env);
             } catch(Exception e) {
                 // Indicate error location
-                throw new ProcessingException("Sitemap: error during pipeline execution", e, Location.parse(getLocation()));
+                throw ProcessingException.getLocatedException("Sitemap: error during pipeline execution", e, Location.parse(getLocation()));
             }
 
         } else {

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/ExceptionGenerator.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/ExceptionGenerator.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/ExceptionGenerator.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/ExceptionGenerator.java Tue Aug  2 10:22:04 2005
@@ -16,6 +16,7 @@
 package org.apache.cocoon.generation;
 
 import java.io.IOException;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.avalon.framework.parameters.Parameters;
@@ -25,9 +26,9 @@
 import org.apache.cocoon.util.ExceptionUtils;
 import org.apache.cocoon.util.location.LocatableException;
 import org.apache.cocoon.util.location.Location;
+import org.apache.cocoon.util.location.MultiLocatable;
 import org.apache.cocoon.xml.AttributesImpl;
 import org.apache.commons.lang.SystemUtils;
-import org.mozilla.javascript.JavaScriptException;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
@@ -69,25 +70,49 @@
 
         // Root exception message
         attr.clear();
-        textElement("message", attr, root.getMessage(), handler);
+        simpleElement("message", attr, root.getMessage(), handler);
         
         // Locations
         handler.startElement(EXCEPTION_NS, "locations", "ex:locations", attr);
         Throwable current = thr;
         while (current != null) {
-            loc = ExceptionUtils.getLocation(current);
-            if (loc != null) {
-                attr.clear();
-                attr.addCDATAAttribute("uri", loc.getURI());
-                attr.addCDATAAttribute("line", Integer.toString(loc.getLine()));
-                attr.addCDATAAttribute("column", Integer.toString(loc.getColumn()));
-                
+            if (current instanceof MultiLocatable) {
                 // Get raw message for LocatableExceptions, message otherwise
                 String message = current instanceof LocatableException ?
                         ((LocatableException)current).getRawMessage() :
                         current.getMessage();
 
-                textElement("location", attr, message, handler);
+                attr.clear();
+                handler.startElement(EXCEPTION_NS, "location-list", "ex:location-list", attr);
+                simpleElement("description", attr, message, handler);
+                List locations = ((MultiLocatable)current).getLocations();
+                for (int i = 0; i < locations.size(); i++) {
+                    attr.clear();
+                    dumpLocation((Location)locations.get(i), attr, null, handler);
+                }
+                handler.endElement(EXCEPTION_NS, "location-list", "ex:location-list");
+
+            } else {
+                // Not a MultiLocatable
+                loc = ExceptionUtils.getLocation(current);
+                if (loc != null) {
+                    // Get raw message for LocatableExceptions, message otherwise
+                    String message = current instanceof LocatableException ?
+                            ((LocatableException)current).getRawMessage() :
+                            current.getMessage();
+    
+                    attr.clear();                
+                    dumpLocation(loc, attr, message, handler);
+                    
+                    // Dump additional locations (explains "1" below) if it's a MultiLocatable (e.g. ProcessingException)
+                    if (current instanceof MultiLocatable) {
+                        List locations = ((MultiLocatable)current).getLocations();
+                        for (int i = 1; i < locations.size(); i++) {
+                            attr.clear();
+                            dumpLocation((Location)locations.get(i), attr, null, handler);
+                        }
+                    }
+                }
             }
             
             // Dump parent location
@@ -98,7 +123,7 @@
         
         // Root exception stacktrace
         attr.clear();
-        textElement("stacktrace", attr, ExceptionUtils.getStackTrace(root), handler);
+        simpleElement("stacktrace", attr, ExceptionUtils.getStackTrace(root), handler);
         
         // Full stack trace (if exception is chained)
         if (thr != root) {
@@ -106,16 +131,26 @@
                     ExceptionUtils.getStackTrace(thr) :
                     ExceptionUtils.getFullStackTrace(thr);
 
-            textElement("full-stacktrace", attr, trace, handler);
+            simpleElement("full-stacktrace", attr, trace, handler);
         }
         
         handler.endElement(EXCEPTION_NS, "exception", "ex:exception");
         handler.endPrefixMapping("ex");
     }
+    
+    private static void dumpLocation(Location loc, AttributesImpl attr, String message, ContentHandler handler) throws SAXException {
+        attr.addCDATAAttribute("uri", loc.getURI());
+        attr.addCDATAAttribute("line", Integer.toString(loc.getLine()));
+        attr.addCDATAAttribute("column", Integer.toString(loc.getColumn()));
+        
+        simpleElement("location", attr, message, handler);
+    }
 
-    private static void textElement(String name, Attributes attr, String value, ContentHandler handler) throws SAXException {
+    private static void simpleElement(String name, Attributes attr, String value, ContentHandler handler) throws SAXException {
         handler.startElement(EXCEPTION_NS, name, "ex:" + name, attr);
-        handler.characters(value.toCharArray(), 0, value.length());
+        if (value != null && value.length() > 0) {
+            handler.characters(value.toCharArray(), 0, value.length());
+        }
         handler.endElement(EXCEPTION_NS, name, "ex:" + name);
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/generation/JXTemplateGenerator.java Tue Aug  2 10:22:04 2005
@@ -54,6 +54,8 @@
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.environment.SourceResolver;
 import org.apache.cocoon.transformation.ServiceableTransformer;
+import org.apache.cocoon.util.location.LocatedRuntimeException;
+import org.apache.cocoon.util.location.LocationAttributes;
 import org.apache.cocoon.xml.IncludeXMLConsumer;
 import org.apache.cocoon.xml.RedundantNamespacesFilter;
 import org.apache.cocoon.xml.XMLConsumer;
@@ -99,7 +101,6 @@
 import org.xml.sax.ContentHandler;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
 import org.xml.sax.ext.LexicalHandler;
 import org.xml.sax.helpers.AttributesImpl;
 import org.xml.sax.helpers.LocatorImpl;
@@ -119,6 +120,17 @@
  * @version CVS $Id$
  */
 public class JXTemplateGenerator extends ServiceableGenerator implements CacheableProcessingComponent {
+    
+    // Quick'n dirty hack to replace all SAXParseException by a located runtime exception
+    private static final class JXTException extends LocatedRuntimeException {
+        JXTException(String message, Locator loc, Throwable thr) {
+            super(message, thr, LocationAttributes.getLocation(loc));
+        }
+        
+        static LocatedRuntimeException get(String message, Locator loc, Throwable thr) {
+            return LocatedRuntimeException.getLocatedException(message, thr, LocationAttributes.getLocation(loc));
+        }
+    }
 
     private static final JXPathContextFactory jxpathContextFactory = JXPathContextFactory.newInstance();
 
@@ -678,13 +690,11 @@
      * (contained in #{})
      */
 
-    private static JXTExpression compileExpr(String expr, String errorPrefix, Locator location) throws SAXParseException {
+    private static JXTExpression compileExpr(String expr, String errorPrefix, Locator location) throws JXTException {
         try {
             return compileExpr(expr);
         } catch (Exception exc) {
-            throw new SAXParseException(errorPrefix + exc.getMessage(), location, exc);
-        } catch (Error err) {
-            throw new SAXParseException(errorPrefix + err.getMessage(), location, new ErrorHolder(err));
+            throw JXTException.get(errorPrefix + exc.getMessage(), location, exc);
         }
     }
 
@@ -947,10 +957,7 @@
                                         compiledExpression = ExpressionFactory.createExpression(str);
                                     }
                                 } catch (Exception exc) {
-                                    throw new SAXParseException(exc.getMessage(), this.location, exc);
-                                } catch (Error err) {
-                                    throw new SAXParseException(err.getMessage(), this.location, new ErrorHolder(err));
-
+                                    throw JXTException.get(exc.getMessage(), this.location, exc);
                                 }
                                 substitutions.add(new JXTExpression(str, compiledExpression));
                                 buf.setLength(0);
@@ -1151,9 +1158,7 @@
                                     try {
                                         compiledExpression = compile(str, xpath);
                                     } catch (Exception exc) {
-                                        throw new SAXParseException(exc.getMessage(), location, exc);
-                                    } catch (Error err) {
-                                        throw new SAXParseException(err.getMessage(), location, new ErrorHolder(err));
+                                        throw JXTException.get(exc.getMessage(), location, exc);
                                     }
                                     substEvents.add(compiledExpression);
                                     buf.setLength(0);
@@ -1189,7 +1194,7 @@
                 if (inExpr) {
                     // unclosed #{} or ${}
                     String msg = "Unterminated " + (xpath ? "#" : "$") + "{";
-                    throw new SAXParseException(msg, location, null);
+                    throw new JXTException(msg, location, null);
                 }
                 if (buf.length() > 0) {
                     if (substEvents.size() == 0) {
@@ -1404,12 +1409,12 @@
                 if (e instanceof StartParameter) {
                     StartParameter startParam = (StartParameter)e;
                     if (!params) {
-                        throw new SAXParseException("<parameter> not allowed here: \"" + startParam.name + "\"",
+                        throw new JXTException("<parameter> not allowed here: \"" + startParam.name + "\"",
                                 startParam.location, null);
                     }
                     Object prev = parameters.put(startParam.name, startParam);
                     if (prev != null) {
-                        throw new SAXParseException("duplicate parameter: \"" + startParam.name + "\"", location, null);
+                        throw new JXTException("duplicate parameter: \"" + startParam.name + "\"", location, null);
                     }
                     e = startParam.endInstruction;
                 } else if (e instanceof IgnorableWhitespace) {
@@ -2014,10 +2019,10 @@
                     JXTExpression varStatus = compileExpr(attrs.getValue("varStatus"), null, locator);
                     if (items == null) {
                         if (select == null && (begin == null || end == null)) {
-                            throw new SAXParseException("forEach: \"select\", \"items\", or both \"begin\" and \"end\" must be specified", locator, null);
+                            throw new JXTException("forEach: \"select\", \"items\", or both \"begin\" and \"end\" must be specified", locator, null);
                         }
                     } else if (select != null) {
-                        throw new SAXParseException("forEach: only one of \"select\" or \"items\" may be specified", locator, null);
+                        throw new JXTException("forEach: only one of \"select\" or \"items\" may be specified", locator, null);
                     }
                     JXTExpression expr = compileExpr(items == null ? select : items, null, locator);
                     String lenientValue = attrs.getValue("lenient");
@@ -2077,7 +2082,7 @@
                     newEvent = startChoose;
                 } else if (localName.equals(WHEN)) {
                     if (stack.size() == 0 || !(stack.peek() instanceof StartChoose)) {
-                        throw new SAXParseException("<when> must be within <choose>", locator, null);
+                        throw new JXTException("<when> must be within <choose>", locator, null);
                     }
                     String test = attrs.getValue("test");
                     if (test != null) {
@@ -2085,7 +2090,7 @@
                         StartWhen startWhen = new StartWhen(startElement, expr);
                         newEvent = startWhen;
                     } else {
-                        throw new SAXParseException("when: \"test\" is required", locator, null);
+                        throw new JXTException("when: \"test\" is required", locator, null);
                     }
                 } else if (localName.equals(OUT)) {
                     String value = attrs.getValue("value");
@@ -2095,14 +2100,14 @@
                         Boolean lenient = lenientValue == null ? null : Boolean.valueOf(lenientValue);
                         newEvent = new StartOut(startElement, expr, lenient);
                     } else {
-                        throw new SAXParseException("out: \"value\" is required", locator, null);
+                        throw new JXTException("out: \"value\" is required", locator, null);
                     }
                 } else if (localName.equals(OTHERWISE)) {
                     if (stack.size() != 0 && (stack.peek() instanceof StartChoose)) {
                         StartOtherwise startOtherwise = new StartOtherwise(startElement);
                         newEvent = startOtherwise;
                     } else {
-                        throw new SAXParseException( "<otherwise> must be within <choose>", locator, null);
+                        throw new JXTException( "<otherwise> must be within <choose>", locator, null);
                     }
                 } else if (localName.equals(IF)) {
                     String test = attrs.getValue("test");
@@ -2111,7 +2116,7 @@
                         StartIf startIf = new StartIf(startElement, expr);
                         newEvent = startIf;
                     } else {
-                        throw new SAXParseException("if: \"test\" is required", locator, null);
+                        throw new JXTException("if: \"test\" is required", locator, null);
                     }
                 } else if (localName.equals(MACRO)) {
                     // <macro name="myTag" targetNamespace="myNamespace">
@@ -2124,11 +2129,11 @@
                         StartDefine startDefine = new StartDefine(startElement, namespace, name);
                         newEvent = startDefine;
                     } else {
-                        throw new SAXParseException("macro: \"name\" is required", locator, null);
+                        throw new JXTException("macro: \"name\" is required", locator, null);
                     }
                 } else if (localName.equals(PARAMETER)) {
                     if (stack.size() == 0 || !(stack.peek() instanceof StartDefine)) {
-                        throw new SAXParseException("<parameter> not allowed here", locator, null);
+                        throw new JXTException("<parameter> not allowed here", locator, null);
                     } else {
                         String name = attrs.getValue("name");
                         String optional = attrs.getValue("optional");
@@ -2137,7 +2142,7 @@
                             StartParameter startParameter = new StartParameter(startElement, name, optional, default_);
                             newEvent = startParameter;
                         } else {
-                            throw new SAXParseException("parameter: \"name\" is required", locator, null);
+                            throw new JXTException("parameter: \"name\" is required", locator, null);
                         }
                     }
                 } else if (localName.equals(EVALBODY)) {
@@ -2181,7 +2186,7 @@
                         StartImport startImport = new StartImport(startElement, uri, expr);
                         newEvent = startImport;
                     } else {
-                        throw new SAXParseException("import: \"uri\" is required", locator, null);
+                        throw new JXTException("import: \"uri\" is required", locator, null);
                     }
                 } else if (localName.equals(TEMPLATE)) {
                     StartTemplate startTemplate = new StartTemplate(startElement);
@@ -2191,7 +2196,7 @@
                     StartComment startJXComment = new StartComment(startElement);
                     newEvent = startJXComment;
                 } else {
-                    throw new SAXParseException("unrecognized tag: " + localName, locator, null);
+                    throw new JXTException("unrecognized tag: " + localName, locator, null);
                 }
             } else {
                 newEvent = startElement;
@@ -2506,9 +2511,7 @@
                     Object val = getValue(expr, jexlContext, jxpathContext);
                     chars = val != null ? val.toString().toCharArray() : ArrayUtils.EMPTY_CHAR_ARRAY;
                 } catch (Exception e) {
-                    throw new SAXParseException(e.getMessage(), event.location, e);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), event.location, new ErrorHolder(err));
+                    throw JXTException.get(e.getMessage(), event.location, e);
                 }
             }
             handler.characters(chars, 0, chars.length);
@@ -2527,8 +2530,8 @@
             JXPathContext jxpathContext, Event startEvent, Event endEvent) throws SAXException {
         try {
             execute(consumer, jexlContext, jxpathContext, macroCall, startEvent, endEvent);
-        } catch (SAXParseException exc) {
-            throw new SAXParseException(macroCall.localName + ": " + exc.getMessage(), location, exc);
+        } catch (Exception exc) {
+            throw JXTException.get(macroCall.localName + ": " + exc.getMessage(), location, exc);
         }
     }
 
@@ -2612,9 +2615,7 @@
                             }
                             chars = val != null ? val.toString().toCharArray() : ArrayUtils.EMPTY_CHAR_ARRAY;
                         } catch (Exception e) {
-                            throw new SAXParseException(e.getMessage(), ev.location, e);
-                        } catch (Error err) {
-                            throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                            throw JXTException.get(e.getMessage(), ev.location, e);
                         }
                     }
                     consumer.characters(chars, 0, chars.length);
@@ -2642,9 +2643,7 @@
                 try {
                     val = getValue(startIf.test, jexlContext, jxpathContext, Boolean.TRUE);
                 } catch (Exception e) {
-                    throw new SAXParseException(e.getMessage(), ev.location, e);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                    throw JXTException.get(e.getMessage(), ev.location, e);
                 }
                 boolean result = false;
                 if (val instanceof Boolean) {
@@ -2714,9 +2713,7 @@
                     var = getStringValue(startForEach.var, jexlContext, jxpathContext);
                     varStatus = getStringValue(startForEach.varStatus, jexlContext, jxpathContext);
                 } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(), ev.location, exc);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                    throw JXTException.get(exc.getMessage(), ev.location, exc);
                 }
                 MyJexlContext localJexlContext = new MyJexlContext(jexlContext);
                 MyVariables localJXPathVariables = new MyVariables((MyVariables)jxpathContext.getVariables());
@@ -2746,7 +2743,7 @@
                         try {
                             value = ptr.getNode();
                         } catch (Exception exc) {
-                            throw new SAXParseException(exc.getMessage(), ev.location, null);
+                            throw JXTException.get(exc.getMessage(), ev.location, null);
                         }
                     } else {
                         localJXPathContext = jxpathContextFactory.newContext(jxpathContext, value);
@@ -2782,7 +2779,7 @@
                     try {
                         val = getValue(startWhen.test, jexlContext, jxpathContext, Boolean.TRUE);
                     } catch (Exception e) {
-                        throw new SAXParseException(e.getMessage(), ev.location, e);
+                        throw JXTException.get(e.getMessage(), ev.location, e);
                     }
                     boolean result;
                     if (val instanceof Boolean) {
@@ -2813,7 +2810,7 @@
                         value = getNode(startSet.value, jexlContext, jxpathContext);
                     }
                 } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(), ev.location, exc);
+                    throw JXTException.get(exc.getMessage(), ev.location, exc);
                 }
                 if (value == null) {
                     NodeList nodeList = toDOMNodeList("set", startSet, jexlContext, macroCall);
@@ -2854,9 +2851,7 @@
                                 try {
                                     val = getNode(expr, jexlContext, jxpathContext);
                                 } catch (Exception e) {
-                                    throw new SAXParseException(e.getMessage(), ev.location, e);
-                                } catch (Error err) {
-                                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                                    throw JXTException.get(e.getMessage(), ev.location, e);
                                 }
                                 attributeValue = val != null ? val : "";
                             } else {
@@ -2873,9 +2868,7 @@
                                         try {
                                             val = getValue(expr, jexlContext, jxpathContext);
                                         } catch (Exception e) {
-                                            throw new SAXParseException(e.getMessage(), ev.location, e);
-                                        } catch (Error err) {
-                                            throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                                            throw JXTException.get(e.getMessage(), ev.location, e);
                                         }
                                         buf.append(val != null ? val.toString() : "");
                                     }
@@ -2936,9 +2929,7 @@
                                 try {
                                     val = getValue(expr, jexlContext, jxpathContext);
                                 } catch (Exception e) {
-                                    throw new SAXParseException(e.getMessage(), ev.location, e);
-                                } catch (Error err) {
-                                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                                    throw JXTException.get(e.getMessage(), ev.location, e);
                                }
                                buf.append(val != null ? val.toString() : "");
                             }
@@ -2956,9 +2947,7 @@
                         consumer.characters(chars, 0, chars.length);
                     }
                 } catch (Exception e) {
-                    throw new SAXParseException(e.getMessage(), ev.location, e);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                    throw JXTException.get(e.getMessage(), ev.location, e);
                 }
             } else if (ev instanceof StartFormatDate) {
                 StartFormatDate startFormatDate = (StartFormatDate)ev;
@@ -2969,9 +2958,7 @@
                         consumer.characters(chars, 0, chars.length);
                     }
                 } catch (Exception e) {
-                    throw new SAXParseException(e.getMessage(), ev.location, e);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                    throw JXTException.get(e.getMessage(), ev.location, e);
                 }
             } else if (ev instanceof StartPrefixMapping) {
                 StartPrefixMapping startPrefixMapping = (StartPrefixMapping)ev;
@@ -2988,8 +2975,8 @@
                     try {
                         String str = XMLUtils.serializeNode(nodeList.item(i), omit);  
                         buf.append(StringUtils.substringAfter(str, ">")); // cut the XML header
-                    } catch (ProcessingException e) {
-                        throw new SAXParseException(e.getMessage(), startJXComment.location, e);
+                    } catch (Exception e) {
+                        throw JXTException.get(e.getMessage(), startJXComment.location, e);
                     }
                 }
                 char[] chars = new char[buf.length()];
@@ -3038,7 +3025,7 @@
                         consumer.characters(ch, 0, ch.length);
                     }
                 } catch (Exception e) {
-                    throw new SAXParseException(e.getMessage(), ev.location, e);
+                    throw JXTException.get(e.getMessage(), ev.location, e);
                 }
             } else if (ev instanceof StartTemplate) {
                 // EMPTY
@@ -3053,9 +3040,7 @@
                     StartElement call = (StartElement)val;
                     execute(consumer, jexlContext, jxpathContext, call, call.next, call.endElement);
                 } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(), ev.location, exc);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                    throw JXTException.get(exc.getMessage(), ev.location, exc);
                 }
                 ev = startEval.endInstruction.next;
                 continue;
@@ -3064,9 +3049,7 @@
                 try {
                     execute(consumer, jexlContext, jxpathContext, null, macroCall.next, macroCall.endElement);
                 } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(), ev.location, exc);
-                } catch (Error err) {
-                    throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                    throw JXTException.get(exc.getMessage(), ev.location, exc);
                 }
                 ev = startEval.endInstruction.next;
                 continue;
@@ -3097,9 +3080,7 @@
                             try {
                                 val = getValue(expr, jexlContext, jxpathContext);
                             } catch (Exception exc) {
-                                throw new SAXParseException(exc.getMessage(), ev.location, exc);
-                            } catch (Error err) {
-                                throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                                throw  JXTException.get(exc.getMessage(), ev.location, exc);
                             }
                             buf.append(val != null ? val.toString() : "");
                         }
@@ -3146,7 +3127,7 @@
                         }
                     }
                 } catch (Exception exc) {
-                    throw new SAXParseException(exc.getMessage(), ev.location, exc);
+                    throw  JXTException.get(exc.getMessage(), ev.location, exc);
                 }
                 finally {
                     resolver.release(input);
@@ -3161,15 +3142,13 @@
                         selectJexl = new MyJexlContext(jexlContext);
                         fillContext(obj, selectJexl);
                     } catch (Exception exc) {
-                        throw new SAXParseException(exc.getMessage(), ev.location, exc);
-                    } catch (Error err) {
-                        throw new SAXParseException(err.getMessage(), ev.location, new ErrorHolder(err));
+                        throw  JXTException.get(exc.getMessage(), ev.location, exc);
                     }
                 }
                 try {
                     execute(consumer, selectJexl, selectJXPath, macroCall, doc.next, doc.endDocument);
                 } catch (Exception exc) {
-                        throw new SAXParseException("Exception occurred in imported template " + uri + ": "+ exc.getMessage(), ev.location, exc);
+                        throw  JXTException.get("Exception occurred in imported template " + uri + ": "+ exc.getMessage(), ev.location, exc);
                 }
                 ev = startImport.endInstruction.next;
                 continue;

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=227047&r1=227046&r2=227047&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 Tue Aug  2 10:22:04 2005
@@ -49,7 +49,6 @@
 import org.apache.cocoon.environment.Session;
 import org.apache.cocoon.environment.SourceResolver;
 import org.apache.cocoon.util.ExceptionUtils;
-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;

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedException.java Tue Aug  2 10:22:04 2005
@@ -15,16 +15,20 @@
  */
 package org.apache.cocoon.util.location;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.avalon.framework.CascadingException;
 
 /**
- * A cascading and located <code>Exception</code>.
+ * A cascading and located <code>Exception</code>. It is also {@link MultiLocatable} to easily build
+ * stack traces.
  * 
  * @version $Id$
  */
-public class LocatedException extends CascadingException implements LocatableException {
-    
-    private Location location;
+public class LocatedException extends CascadingException implements LocatableException, MultiLocatable {
+
+    private List locations;
 
     public LocatedException(String message) {
         super(message, null);
@@ -36,16 +40,20 @@
     
     public LocatedException(String message, Location location) {
         super(message, null);
-        this.location = location;
+        addLocation(location);
     }
     
     public LocatedException(String message, Throwable thr, Location location) {
         super(message, thr);
-        this.location = location;
+        addLocation(location);
     }
 
     public Location getLocation() {
-        return this.location;
+        return locations == null ? null : (Location)locations.get(0);
+    }
+
+    public List getLocations() {
+        return locations;
     }
 
     public String getRawMessage() {
@@ -53,7 +61,25 @@
     }
 
     public String getMessage() {
-        return this.location == null ? super.getMessage() :
-            super.getMessage() + " (" + this.location.toString() + ")";
+        if (this.locations == null) {
+            return super.getMessage();
+        }
+
+        // Produce a Java-like stacktrace with locations
+        StringBuffer buf = new StringBuffer(super.getMessage());
+        for (int i = 0; i < locations.size(); i++) {
+            buf.append("\n\tat ").append(locations.get(i));
+        }
+        return buf.toString();
+    }
+    
+    public void addLocation(Location loc) {
+        if (loc == null || loc.equals(Location.UNKNOWN))
+            return;
+
+        if (locations == null) {
+            this.locations = new ArrayList(1); // Start small
+        }
+        locations.add(loc);
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java Tue Aug  2 10:22:04 2005
@@ -15,16 +15,20 @@
  */
 package org.apache.cocoon.util.location;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.avalon.framework.CascadingRuntimeException;
 
 /**
- * A cascading and located <code>RuntimeException</code>.
+ * A cascading and located <code>RuntimeException</code>. It is also {@link MultiLocatable} to easily build
+ * stack traces.
  * 
  * @version $Id$
  */
-public class LocatedRuntimeException extends CascadingRuntimeException implements LocatableException {
+public class LocatedRuntimeException extends CascadingRuntimeException implements LocatableException, MultiLocatable {
     
-    private Location location;
+    private List locations;
 
     public LocatedRuntimeException(String message) {
         super(message, null);
@@ -36,16 +40,20 @@
     
     public LocatedRuntimeException(String message, Location location) {
         super(message, null);
-        this.location = location;
+        addLocation(location);
     }
     
     public LocatedRuntimeException(String message, Throwable thr, Location location) {
         super(message, thr);
-        this.location = location;
+        addLocation(location);
     }
 
     public Location getLocation() {
-        return this.location;
+        return locations == null ? null : (Location)locations.get(0);
+    }
+
+    public List getLocations() {
+        return locations;
     }
 
     public String getRawMessage() {
@@ -53,7 +61,40 @@
     }
 
     public String getMessage() {
-        return this.location == null ? super.getMessage() :
-            super.getMessage() + " (" + this.location.toString() + ")";
+        if (this.locations == null) {
+            return super.getMessage();
+        }
+
+        // Produce a Java-like stacktrace with locations
+        StringBuffer buf = new StringBuffer(super.getMessage());
+        for (int i = 0; i < locations.size(); i++) {
+            buf.append("\n\tat ").append(locations.get(i));
+        }
+        return buf.toString();
+    }
+    
+    public void addLocation(Location loc) {
+        if (loc == null || loc.equals(Location.UNKNOWN))
+            return;
+
+        if (locations == null) {
+            this.locations = new ArrayList(1); // Start small
+        }
+        locations.add(loc);
+    }
+
+    /**
+     * A convenience method to get a located exception for an existing <code>Throwable</code>.
+     * If the throwable already is {@link MultiLocatable}, the location is added to its location
+     * list. Otherwise, a new <code>LocatedRuntimeException</code> is created.
+     */
+    public static LocatedRuntimeException getLocatedException(String description, Throwable thr, Location location) {
+        if (thr instanceof LocatedRuntimeException) {
+            LocatedRuntimeException re = (LocatedRuntimeException)thr;
+            re.addLocation(location);
+            return re;
+        }
+        
+        return new LocatedRuntimeException(description, thr, location);
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/LocationAttributes.java Tue Aug  2 10:22:04 2005
@@ -78,6 +78,20 @@
     }
     
     /**
+     * Returns the {@link Location} pointed to by a SAX <code>Locator</code>.
+     * 
+     * @param locator the locator (can be null)
+     * @return the location
+     */
+    public static Location getLocation(Locator locator) {
+        if (locator == null || locator.getSystemId() == null) {
+            return Location.UNKNOWN;
+        }
+        
+        return new Location(locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
+    }
+    
+    /**
      * Returns the {@link Location} of an element (SAX flavor).
      * 
      * @param attrs the element's attributes that hold the location information

Added: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/MultiLocatable.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/MultiLocatable.java?rev=227047&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/MultiLocatable.java (added)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/MultiLocatable.java Tue Aug  2 10:22:04 2005
@@ -0,0 +1,27 @@
+package org.apache.cocoon.util.location;
+
+import java.util.List;
+
+/**
+ * An extension of {@link Location} for classes that can hold a list of locations.
+ * It will typically be used to build location stacks.
+ * <p>
+ * The <em>first</em> location of the collection returned by {@link #getLocations()} should be
+ * be identical to the result of {@link org.apache.cocoon.util.location.Locatable#getLocation()}.
+ * <p>
+ * If the list of locations designates a call stack, then its first element should be the deepmost
+ * location of this stack. This is consistent with the need for <code>getLocation()</code> to
+ * return the most precise location.
+ * 
+ * @version $Id$
+ */
+public interface MultiLocatable extends Locatable {
+    
+    /**
+     * Return the list of locations.
+     * 
+     * @return a list of locations, or <code>null</code> if there are no locations.
+     */
+    public List getLocations();
+
+}

Propchange: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/MultiLocatable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/util/location/MultiLocatable.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/branches/BRANCH_2_1_X/src/webapp/stylesheets/system/exception2html.xslt
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/webapp/stylesheets/system/exception2html.xslt?rev=227047&r1=227046&r2=227047&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/webapp/stylesheets/system/exception2html.xslt (original)
+++ cocoon/branches/BRANCH_2_1_X/src/webapp/stylesheets/system/exception2html.xslt Tue Aug  2 10:22:04 2005
@@ -72,16 +72,7 @@
           <div id="locations">
             <xsl:for-each select="ex:locations/*">
               <xsl:sort select="position()" order="descending"/>
-              <p>
-                 <strong>
-                    <xsl:for-each select="str:split(., '&#10;')">
-                       <xsl:if test="normalize-space(.)">
-                         <xsl:value-of select="."/><br/>
-                       </xsl:if>
-                    </xsl:for-each>
-                 </strong>
-                 <xsl:call-template name="dump-location"/>
-              </p>
+              <xsl:apply-templates select="."/>
             </xsl:for-each>
           </div>
         </xsl:if>
@@ -138,6 +129,36 @@
         </p>
       </body>
     </html>
+  </xsl:template>
+
+  <xsl:template match="ex:location">
+       <p>
+          <strong>
+             <xsl:for-each select="str:split(., '&#10;')">
+                <xsl:if test="normalize-space(.)">
+                  <xsl:value-of select="."/>
+                </xsl:if>
+             </xsl:for-each>
+          </strong>
+          <br/>
+          <xsl:call-template name="dump-location"/>
+       </p>
+  </xsl:template>
+  
+  <xsl:template match="ex:location-list">
+     <p>
+        <strong>
+           <xsl:for-each select="str:split(ex:description, '&#10;')">
+              <xsl:if test="normalize-space(.)">
+                <xsl:value-of select="."/>
+              </xsl:if>
+           </xsl:for-each>
+        </strong>
+        <xsl:for-each select="ex:location">
+           <br/>
+           <xsl:call-template name="dump-location"/>
+         </xsl:for-each>
+     </p>
   </xsl:template>
 
   <xsl:template match="ex:stacktrace|ex:full-stacktrace">