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/09/13 21:36:08 UTC

svn commit: r280632 - in /cocoon/trunk/src: java/org/apache/cocoon/ java/org/apache/cocoon/components/flow/javascript/ java/org/apache/cocoon/components/notification/ java/org/apache/cocoon/components/treeprocessor/ java/org/apache/cocoon/generation/ j...

Author: sylvain
Date: Tue Sep 13 12:35:46 2005
New Revision: 280632

URL: http://svn.apache.org/viewcvs?rev=280632&view=rev
Log:
Refactor location stuff, allow for extensible location finders

Added:
    cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationUtils.java   (with props)
Removed:
    cocoon/trunk/src/java/org/apache/cocoon/util/ExceptionUtils.java
Modified:
    cocoon/trunk/src/java/org/apache/cocoon/Cocoon.java
    cocoon/trunk/src/java/org/apache/cocoon/ProcessingException.java
    cocoon/trunk/src/java/org/apache/cocoon/components/flow/javascript/LocationTrackingDebugger.java
    cocoon/trunk/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java
    cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java
    cocoon/trunk/src/java/org/apache/cocoon/generation/ExceptionGenerator.java
    cocoon/trunk/src/java/org/apache/cocoon/selection/ExceptionSelector.java
    cocoon/trunk/src/java/org/apache/cocoon/transformation/TraxTransformer.java
    cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedException.java
    cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java
    cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationAttributes.java
    cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationImpl.java
    cocoon/trunk/src/java/org/apache/cocoon/util/log/CocoonLogFormatter.java
    cocoon/trunk/src/test/org/apache/cocoon/util/location/LocationTestCase.java

Modified: cocoon/trunk/src/java/org/apache/cocoon/Cocoon.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/Cocoon.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/Cocoon.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/Cocoon.java Tue Sep 13 12:35:46 2005
@@ -46,6 +46,9 @@
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.environment.Session;
 import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.cocoon.util.location.Location;
+import org.apache.cocoon.util.location.LocationImpl;
+import org.apache.cocoon.util.location.LocationUtils;
 
 import org.apache.commons.lang.SystemUtils;
 import org.apache.excalibur.source.Source;
@@ -78,6 +81,54 @@
                    Contextualizable,
                    Serviceable {
 
+    // Register the location finder for Avalon configuration objects and exceptions
+    static {
+        LocationUtils.addFinder(new LocationUtils.LocationFinder() {
+
+            public Location getLocation(Object obj, String description) {
+                if (obj instanceof Configuration) {
+                    Configuration config = (Configuration)obj;
+                    String locString = config.getLocation();
+                    Location result = LocationUtils.parse(locString);
+                    if (LocationUtils.isKnown(result)) {
+                        // Add description
+                        StringBuffer desc = new StringBuffer().append('<');
+                        // Unfortunately Configuration.getPrefix() is not public
+                        try {
+                            if (config.getNamespace().startsWith("http://apache.org/cocoon/sitemap/")) {
+                                desc.append("map:");
+                            }
+                        } catch (ConfigurationException e) {
+                            // no namespace: ignore
+                        }
+                        desc.append(config.getName()).append('>');
+                        return new LocationImpl(desc.toString(), result);
+                    } else {
+                        return result;
+                    }
+                }
+                
+                if (obj instanceof Exception) {
+                    // Many exceptions in Cocoon have a message like "blah blah at file://foo/bar.xml:12:1"
+                    String msg = ((Exception)obj).getMessage();
+                    if (msg == null) return null;
+                    
+                    int pos = msg.lastIndexOf(" at ");
+                    if (pos != -1) {
+                        return LocationUtils.parse(msg.substring(pos + 4));
+                    } else {
+                        // Will try other finders
+                        return null;
+                    }
+                }
+                
+                // Try next finders.
+                return null;
+            }
+        });
+        
+    }
+    
     static Cocoon instance;
 
     /** The root Cocoon logger */

Modified: cocoon/trunk/src/java/org/apache/cocoon/ProcessingException.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/ProcessingException.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/ProcessingException.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/ProcessingException.java Tue Sep 13 12:35:46 2005
@@ -156,73 +156,4 @@
             throw (ProcessingException)multiloc;
         }
     }
-
-
-//    public String toString() {
-//        StringBuffer s = new StringBuffer();
-//        s.append(super.toString());
-//        final Throwable t = getCause();
-//        if(t!=null) {
-//            s.append(": ");
-//            // be more verbose try to get location info
-//            s.append( extraInfo(t) );
-//            s.append(t.toString());
-//        }
-//        return s.toString();
-//    }
-//    
-//    /**
-//     * Examine Throwable and try to figure out location information.
-//     * <p>
-//     *   At the moment only SAXParseException, and TransformerException
-//     *   are considered.
-//     * </p>
-//     *
-//     * @return String containing location information of the format
-//     *  <code>{file-name}:{line}:{column}:</code>, if no location info is 
-//     *  available return empty string
-//     */
-//    private String extraInfo( Throwable t ) {
-//        StringBuffer sb = new StringBuffer();
-//        if (t instanceof SAXParseException) {
-//            SAXParseException spe = (SAXParseException)t;
-//            sb.append( String.valueOf(spe.getSystemId()));
-//            sb.append( ":" );
-//            sb.append( String.valueOf(spe.getLineNumber()));
-//            sb.append( ":" );
-//            sb.append( String.valueOf(spe.getColumnNumber()));
-//            sb.append( ":" );
-//        } else if (t instanceof TransformerException) {
-//            TransformerException transformerException = (TransformerException) t;
-//            SourceLocator sourceLocator = transformerException.getLocator();
-//            
-//            if( null != sourceLocator ) {
-//                sb.append( String.valueOf(sourceLocator.getSystemId()));
-//                sb.append( ":" );
-//                sb.append( String.valueOf(sourceLocator.getLineNumber()));
-//                sb.append( ":" );
-//                sb.append( String.valueOf(sourceLocator.getColumnNumber()));
-//                sb.append( ":" );
-//            }
-//        }
-//        return sb.toString();
-//    }
-//    
-//    public void printStackTrace() {
-//        super.printStackTrace();
-//        if(getCause()!=null)
-//            getCause().printStackTrace();
-//    }
-//    
-//    public void printStackTrace( PrintStream s ) {
-//        super.printStackTrace(s);
-//        if(getCause()!=null)
-//            getCause().printStackTrace(s);
-//    }
-//    
-//    public void printStackTrace( PrintWriter s ) {
-//        super.printStackTrace(s);
-//        if(getCause()!=null)
-//            getCause().printStackTrace(s);
-//    }
 }

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/flow/javascript/LocationTrackingDebugger.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/flow/javascript/LocationTrackingDebugger.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/flow/javascript/LocationTrackingDebugger.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/flow/javascript/LocationTrackingDebugger.java Tue Sep 13 12:35:46 2005
@@ -19,10 +19,13 @@
 import java.util.List;
 
 import org.apache.cocoon.ProcessingException;
-import org.apache.cocoon.util.ExceptionUtils;
 import org.apache.cocoon.util.location.Location;
 import org.apache.cocoon.util.location.LocationImpl;
+import org.apache.cocoon.util.location.LocationUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.mozilla.javascript.Context;
+import org.mozilla.javascript.EcmaError;
+import org.mozilla.javascript.JavaScriptException;
 import org.mozilla.javascript.Scriptable;
 import org.mozilla.javascript.debug.DebugFrame;
 import org.mozilla.javascript.debug.DebuggableScript;
@@ -40,6 +43,34 @@
  * @version $Id$
  */
 public class LocationTrackingDebugger implements Debugger {
+    
+    static {
+        // Register what's needed to analyze exceptions produced by Rhino
+        ExceptionUtils.addCauseMethodName("getWrappedException");
+        LocationUtils.addFinder(new LocationUtils.LocationFinder() {
+
+            public Location getLocation(Object obj, String description) {
+                if (obj instanceof EcmaError) {
+                    EcmaError ex = (EcmaError)obj;
+                    if (ex.getSourceName() != null) {
+                        return new LocationImpl(ex.getName(), ex.getSourceName(), ex.getLineNumber(), ex.getColumnNumber());
+                    } else {
+                        return Location.UNKNOWN;
+                    }
+        
+                } else if (obj instanceof JavaScriptException) {
+                    JavaScriptException ex = (JavaScriptException)obj;
+                    if (ex.sourceName() != null) {
+                        return new LocationImpl(description, ex.sourceName(), ex.lineNumber(), -1);
+                    } else {
+                        return Location.UNKNOWN;
+                    }
+                }
+                
+                return null;
+            } 
+        });
+    }
     
     private List locations;
     private Throwable throwable;

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/notification/DefaultNotifyingBuilder.java Tue Sep 13 12:35:46 2005
@@ -15,8 +15,8 @@
  */
 package org.apache.cocoon.components.notification;
 
-import org.apache.cocoon.util.ExceptionUtils;
 import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.xml.sax.SAXParseException;
 
 import javax.xml.transform.SourceLocator;

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/treeprocessor/DefaultTreeBuilder.java Tue Sep 13 12:35:46 2005
@@ -46,6 +46,7 @@
 import org.apache.cocoon.sitemap.SitemapParameters;
 import org.apache.cocoon.util.location.Location;
 import org.apache.cocoon.util.location.LocationImpl;
+import org.apache.cocoon.util.location.LocationUtils;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
 
@@ -514,7 +515,7 @@
         }
         desc.append('>');
         
-        Location rawLoc = LocationImpl.get(config);
+        Location rawLoc = LocationUtils.getLocation(config);
         return new LocationImpl(desc.toString(), rawLoc.getURI(), rawLoc.getLineNumber(), rawLoc.getColumnNumber());
     }
 

Modified: cocoon/trunk/src/java/org/apache/cocoon/generation/ExceptionGenerator.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/generation/ExceptionGenerator.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/generation/ExceptionGenerator.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/generation/ExceptionGenerator.java Tue Sep 13 12:35:46 2005
@@ -23,12 +23,13 @@
 import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.SourceResolver;
-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.LocationUtils;
 import org.apache.cocoon.util.location.MultiLocatable;
 import org.apache.cocoon.xml.AttributesImpl;
 import org.apache.commons.lang.SystemUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
@@ -72,8 +73,8 @@
         handler.startElement(EXCEPTION_NS, "exception-report", "ex:exception-report", attr);
         
         // Root exception location
-        Location loc = ExceptionUtils.getLocation(root);        
-        if (loc != null) {
+        Location loc = LocationUtils.getLocation(root);        
+        if (LocationUtils.isKnown(loc)) {
             attr.clear();
             dumpLocation(loc, attr, handler);
         }
@@ -87,8 +88,8 @@
         handler.startElement(EXCEPTION_NS, "cocoon-stacktrace", "ex:cocoon-stacktrace", attr);
         Throwable current = thr;
         while (current != null) {
-            loc = ExceptionUtils.getLocation(current);
-            if (loc != null) {
+            loc = LocationUtils.getLocation(current);
+            if (LocationUtils.isKnown(loc)) {
                 // One or more locations: dump it
                 handler.startElement(EXCEPTION_NS, "exception", "ex:exception", attr);
                 

Modified: cocoon/trunk/src/java/org/apache/cocoon/selection/ExceptionSelector.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/selection/ExceptionSelector.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/selection/ExceptionSelector.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/selection/ExceptionSelector.java Tue Sep 13 12:35:46 2005
@@ -23,7 +23,7 @@
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.util.ClassUtils;
-import org.apache.cocoon.util.ExceptionUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
 
 /**
  * In a &lt;map:handle-errors>, selects depending on the exception that caused the error.

Modified: cocoon/trunk/src/java/org/apache/cocoon/transformation/TraxTransformer.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/transformation/TraxTransformer.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/transformation/TraxTransformer.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/transformation/TraxTransformer.java Tue Sep 13 12:35:46 2005
@@ -48,8 +48,8 @@
 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.location.Location;
+import org.apache.cocoon.util.location.LocationUtils;
 import org.apache.cocoon.xml.XMLConsumer;
 import org.apache.commons.lang.BooleanUtils;
 import org.apache.excalibur.source.Source;
@@ -209,14 +209,14 @@
 
         public void warning(TransformerException ex) throws TransformerException {
             if (getLogger().isWarnEnabled()) {
-                Location loc = ExceptionUtils.getLocation(ex);
+                Location loc = LocationUtils.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);
+                Location loc = LocationUtils.getLocation(ex);
                 getLogger().error("Error at " + loc == null ? inputSource.getURI() : loc.toString(), ex);
             }
         }
@@ -613,7 +613,7 @@
         } catch(Exception e) {
             if (transformerException != null) {
                 // Ignore the fake RuntimeException sent by Xalan
-                Location loc = ExceptionUtils.getLocation(transformerException);
+                Location loc = LocationUtils.getLocation(transformerException);
                 if (loc == null) {
                     // No location: if it's just a wrapper, consider only the wrapped exception.
                     Throwable realEx = transformerException.getCause();

Modified: cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedException.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedException.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedException.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedException.java Tue Sep 13 12:35:46 2005
@@ -19,8 +19,9 @@
 import java.util.Collections;
 import java.util.List;
 
-import org.apache.avalon.framework.CascadingException;
-import org.apache.cocoon.util.ExceptionUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
+import org.apache.commons.lang.exception.NestableException;
+
 
 /**
  * A cascading and located <code>Exception</code>. It is also {@link MultiLocatable} to easily build
@@ -29,7 +30,7 @@
  * @since 2.1.8
  * @version $Id$
  */
-public class LocatedException extends CascadingException implements LocatableException, MultiLocatable {
+public class LocatedException extends NestableException implements LocatableException, MultiLocatable {
 
     private List locations;
 
@@ -63,7 +64,7 @@
      * of locatable exceptions.
      * 
      * @param self the current locatable exception
-     * @param cause the cause of <code>self</code>
+     * @param cause a cause of <code>self</code>
      */
     public static void addCauseLocations(MultiLocatable self, Throwable cause) {
         if (cause == null || cause instanceof Locatable) {
@@ -73,9 +74,15 @@
         // Add parent location first
         addCauseLocations(self, ExceptionUtils.getCause(cause));
         // then ourselve's
-        Location loc = ExceptionUtils.getLocation(cause);
-        if (loc != null) {
-            loc = new LocationImpl("[cause location]", loc.getURI(), loc.getLineNumber(), loc.getColumnNumber());
+        Location loc = LocationUtils.getLocation(cause);
+        if (LocationUtils.isKnown(loc)) {
+            // Get the exception's short name
+            String name = cause.getClass().getName();
+            int pos = name.lastIndexOf('.');
+            if (pos != -1) {
+                name = name.substring(pos+1);
+            }
+            loc = new LocationImpl("[" + name + "]", loc.getURI(), loc.getLineNumber(), loc.getColumnNumber());
             self.addLocation(loc);
         }
     }
@@ -119,7 +126,7 @@
     }
     
     public void addLocation(Location loc) {
-        if (loc == null || loc.equals(Location.UNKNOWN))
+        if (LocationUtils.isUnknown(loc))
             return;
 
         if (locations == null) {

Modified: cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/util/location/LocatedRuntimeException.java Tue Sep 13 12:35:46 2005
@@ -19,34 +19,50 @@
 import java.util.Collections;
 import java.util.List;
 
-import org.apache.avalon.framework.CascadingRuntimeException;
+import org.apache.commons.lang.exception.NestableRuntimeException;
 
 /**
  * A cascading and located <code>RuntimeException</code>. It is also {@link MultiLocatable} to easily build
- * stack traces.
+ * location stack traces.
+ * <p>
+ * If a <code>LocatedRuntimeException</code> is built with a location and a cause which is also a
+ * <code>LocatedRuntimeException</code>, then the default behavior is to add the location to the cause
+ * exception and immediately rethrow the cause. This avoids exception nesting and builds a location
+ * stack.
  * 
  * @since 2.1.8
  * @version $Id$
  */
-public class LocatedRuntimeException extends CascadingRuntimeException implements LocatableException, MultiLocatable {
+public class LocatedRuntimeException extends NestableRuntimeException implements LocatableException, MultiLocatable {
     
     private List locations;
 
     public LocatedRuntimeException(String message) {
-        this(message, null, null);
+        this(message, null, null, true);
     }
     
-    public LocatedRuntimeException(String message, Throwable cause) {
-        this(message, cause, null);
+    public LocatedRuntimeException(String message, Throwable cause) throws LocatedRuntimeException {
+        this(message, cause, null, true);
     }
     
     public LocatedRuntimeException(String message, Location location) {
-        this(message, null, location);
-        addLocation(location);
+        this(message, null, location, true);
     }
     
-    public LocatedRuntimeException(String message, Throwable cause, Location location) {
+    public LocatedRuntimeException(String message, Throwable cause, Location location) throws LocatedRuntimeException {
+        this(message, cause, location, true);
+    }
+
+    public LocatedRuntimeException(String message, Throwable cause, Location location, boolean rethrowLocated)
+        throws LocatedRuntimeException {
         super(message, cause);
+        if (rethrowLocated && cause instanceof LocatedRuntimeException) {
+            LocatedRuntimeException lreCause = (LocatedRuntimeException)cause;
+            lreCause.addLocation(location);
+            // Rethrow the cause
+            throw lreCause;
+        }
+        
         LocatedException.addCauseLocations(this, cause);
         addLocation(location);
     }
@@ -68,34 +84,12 @@
     }
     
     public void addLocation(Location loc) {
-        if (loc == null || loc.equals(Location.UNKNOWN))
+        if (LocationUtils.isUnknown(loc))
             return;
 
         if (locations == null) {
             this.locations = new ArrayList(1); // Start small
         }
         locations.add(LocationImpl.get(loc));
-    }
-
-    /**
-     * Build a located exception given an existing exception and the location where
-     * this exception was catched. If the exception is already a <code>LocatedRuntimeException</code>,
-     * then the location is added to the original exception's location chain and the result is
-     * the original exception (and <code>description</code> is ignored. Otherwise, a new
-     * <code>LocatedRuntimeException</code> is built, wrapping the original exception.
-     * 
-     * @param message a message (can be <code>null</code>)
-     * @param thr the original exception (can be <code>null</code>)
-     * @param location the location (can be <code>null</code>)
-     * @return a located exception
-     */
-    public static LocatedRuntimeException getLocatedException(String message, Throwable thr, Location location) {
-        if (thr instanceof LocatedRuntimeException) {
-            LocatedRuntimeException re = (LocatedRuntimeException)thr;
-            re.addLocation(location);
-            return re;
-        }
-        
-        return new LocatedRuntimeException(message, thr, location);
     }
 }

Modified: cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationAttributes.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationAttributes.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationAttributes.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationAttributes.java Tue Sep 13 12:35:46 2005
@@ -15,7 +15,6 @@
  */
 package org.apache.cocoon.util.location;
 
-import org.apache.cocoon.xml.AbstractXMLPipe;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
 import org.xml.sax.Attributes;
@@ -256,10 +255,12 @@
      * @see org.apache.cocoon.util.location.LocationAttributes
      * @since 2.1.8
      */
-    public static class Pipe extends AbstractXMLPipe {
+    public static class Pipe implements ContentHandler {
         
         private Locator locator;
         
+        private ContentHandler nextHandler;
+        
         /**
          * Create a filter. It has to be chained to another handler to be really useful.
          */
@@ -271,27 +272,55 @@
          * @param next the next handler in the chain.
          */
         public Pipe(ContentHandler next) {
-            setContentHandler(next);
+            nextHandler = next;
         }
 
         public void setDocumentLocator(Locator locator) {
             this.locator = locator;
-            super.setDocumentLocator(locator);
+            nextHandler.setDocumentLocator(locator);
         }
         
         public void startDocument() throws SAXException {
-            super.startDocument();
-            super.startPrefixMapping(LocationAttributes.PREFIX, LocationAttributes.URI);
+            nextHandler.startDocument();
+            nextHandler.startPrefixMapping(LocationAttributes.PREFIX, LocationAttributes.URI);
         }
         
         public void endDocument() throws SAXException {
             endPrefixMapping(LocationAttributes.PREFIX);
-            super.endDocument();
+            nextHandler.endDocument();
         }
 
         public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException {
             // Add location attributes to the element
-            super.startElement(uri, loc, raw, LocationAttributes.addLocationAttributes(locator, attrs));
+            nextHandler.startElement(uri, loc, raw, LocationAttributes.addLocationAttributes(locator, attrs));
+        }
+
+        public void endElement(String arg0, String arg1, String arg2) throws SAXException {
+            nextHandler.endElement(arg0, arg1, arg2);
+        }
+
+        public void startPrefixMapping(String arg0, String arg1) throws SAXException {
+            nextHandler.startPrefixMapping(arg0, arg1);
+        }
+
+        public void endPrefixMapping(String arg0) throws SAXException {
+            nextHandler.endPrefixMapping(arg0);
+        }
+
+        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
+            nextHandler.characters(arg0, arg1, arg2);
+        }
+
+        public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
+            nextHandler.ignorableWhitespace(arg0, arg1, arg2);
+        }
+
+        public void processingInstruction(String arg0, String arg1) throws SAXException {
+            nextHandler.processingInstruction(arg0, arg1);
+        }
+
+        public void skippedEntity(String arg0) throws SAXException {
+            nextHandler.skippedEntity(arg0);
         }
     }
 }

Modified: cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationImpl.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationImpl.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationImpl.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationImpl.java Tue Sep 13 12:35:46 2005
@@ -17,9 +17,7 @@
 
 import java.io.Serializable;
 
-import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.commons.lang.ObjectUtils;
-import org.xml.sax.Locator;
 
 /**
  * A simple immutable and serializable implementation of {@link Location}.
@@ -34,7 +32,7 @@
     private final String description;
     
     // Package private: outside this package, use Location.UNKNOWN.
-    static final LocationImpl UNKNOWN = new LocationImpl(null, null);
+    static final LocationImpl UNKNOWN = new LocationImpl(null, null, -1, -1);
 
     /**
      * The string representation of an unknown location: "<code>[unknown location]</code>".
@@ -84,8 +82,15 @@
     }
     
     /**
+     * Create a location from an existing one, but with a different description
+     */
+    public LocationImpl(String description, Location location) {
+        this(description, location.getURI(), location.getLineNumber(), location.getColumnNumber());
+    }
+    
+    /**
      * Obtain a <code>LocationImpl</code> from a {@link Location}. If <code>location</code> is
-     * alredy a <code>LocationImpl</code>, it is returned, otherwise it is copied.
+     * already a <code>LocationImpl</code>, it is returned, otherwise it is copied.
      * <p>
      * This method is useful when an immutable and serializable location is needed, such as in locatable
      * exceptions.
@@ -101,88 +106,6 @@
         } else {
             return new LocationImpl(location);
         }
-    }
-
-    /**
-     * Parse a location string of the form "<code><em>uri</em>:<em>line</em>:<em>column</em></code>" (e.g.
-     * "<code>path/to/file.xml:3:40</code>") to a Location object.
-     * 
-     * @param text the text to parse
-     * @return the location (possibly UNKNOWN if text was null or in an incorrect format)
-     */
-    public static LocationImpl get(String text) throws IllegalArgumentException {
-        if (text == null || text.length() == 0) {
-            return UNKNOWN;
-        }
-
-        // Do we have a description?
-        String description;
-        int uriStart = text.lastIndexOf(" - "); // lastIndexOf to allow the separator to be in the description
-        if (uriStart > -1) {
-            description = text.substring(0, uriStart);
-            uriStart += 3; // strip " - "
-        } else {
-            description = null;
-            uriStart = 0;
-        }
-        
-        try {
-            int colSep = text.lastIndexOf(':');
-            if (colSep > -1) {
-                int column = Integer.parseInt(text.substring(colSep + 1));
-                
-                int lineSep = text.lastIndexOf(':', colSep - 1);
-                if (lineSep > -1) {
-                    int line = Integer.parseInt(text.substring(lineSep + 1, colSep));
-                    return new LocationImpl(description, text.substring(uriStart, lineSep), line, column);
-                }
-            } else {
-                // unkonwn?
-                if (text.endsWith(UNKNOWN_STRING)) {
-                    return new LocationImpl(description, null);
-                }
-            }
-        } catch(Exception e) {
-            // Ignore: handled below
-        }
-        
-        return UNKNOWN;
-    }
-
-    /**
-     * Returns the {@link Location} pointed to by a SAX <code>Locator</code>.
-     * 
-     * @param locator the locator (can be null)
-     * @param description a description for the location (can be null)
-     * @return the location (possibly UNKNOWN)
-     */
-    public static LocationImpl get(Locator locator, String description) {
-        if (locator == null || locator.getSystemId() == null) {
-            return UNKNOWN;
-        }
-        
-        return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
-    }
-    
-    /**
-     * Returns the {@link Location} of an Avalon <code>Configuration</code> object.
-     * 
-     * 
-     */
-    public static LocationImpl get(Configuration config) {
-        if (config == null) {
-            return UNKNOWN;
-        }
-        
-        // Why in hell is "if (config instanceof Locatable)" producing a compilation error???
-        Object obj = config;
-        // We may have a locatable implementation of configuration
-        if (obj instanceof Locatable) {
-            return get(((Locatable)obj).getLocation());
-        }
-        
-        String locString = config.getLocation();
-        return get(locString);
     }
     
     /**

Added: cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationUtils.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationUtils.java?rev=280632&view=auto
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationUtils.java (added)
+++ cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationUtils.java Tue Sep 13 12:35:46 2005
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ * 
+ * Licensed 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.cocoon.util.location;
+
+import javax.xml.transform.SourceLocator;
+import javax.xml.transform.TransformerException;
+
+import org.xml.sax.Locator;
+import org.xml.sax.SAXParseException;
+
+/**
+ * Location-related utility methods.
+ *
+ * @version $Id$
+ * @since 2.1.8
+ */
+public class LocationUtils {
+    
+    private static LocationFinder[] finders = new LocationFinder[] {};
+    
+    /**
+     * An finder or object locations
+     *
+     * @since 2.1.8
+     */
+    public interface LocationFinder {
+        /**
+         * Get the location of an object
+         * @param obj the object for which to find a location
+         * @param description and optional description to be added to the object's location
+         * @return the object's location or <code>null</code> if object's class isn't handled
+         *         by this finder.
+         */
+        Location getLocation(Object obj, String description);
+    }
+
+    private LocationUtils() {
+        // Forbid instanciation
+    }
+    
+    /**
+     * Parse a location string of the form "<code><em>uri</em>:<em>line</em>:<em>column</em></code>" (e.g.
+     * "<code>path/to/file.xml:3:40</code>") to a Location object. Additionally, a description may
+     * also optionally be present, separated with an hyphen (e.g. "<code>foo - path/to/file.xml:3.40</code>").
+     * 
+     * @param text the text to parse
+     * @return the location (possibly <code>null</code> if text was null or in an incorrect format)
+     */
+    public static LocationImpl parse(String text) throws IllegalArgumentException {
+        if (text == null || text.length() == 0) {
+            return null;
+        }
+
+        // Do we have a description?
+        String description;
+        int uriStart = text.lastIndexOf(" - "); // lastIndexOf to allow the separator to be in the description
+        if (uriStart > -1) {
+            description = text.substring(0, uriStart);
+            uriStart += 3; // strip " - "
+        } else {
+            description = null;
+            uriStart = 0;
+        }
+        
+        try {
+            int colSep = text.lastIndexOf(':');
+            if (colSep > -1) {
+                int column = Integer.parseInt(text.substring(colSep + 1));
+                
+                int lineSep = text.lastIndexOf(':', colSep - 1);
+                if (lineSep > -1) {
+                    int line = Integer.parseInt(text.substring(lineSep + 1, colSep));
+                    return new LocationImpl(description, text.substring(uriStart, lineSep), line, column);
+                }
+            } else {
+                // unkonwn?
+                if (text.endsWith(LocationImpl.UNKNOWN_STRING)) {
+                    return LocationImpl.UNKNOWN;
+                }
+            }
+        } catch(Exception e) {
+            // Ignore: handled below
+        }
+        
+        return LocationImpl.UNKNOWN;
+    }
+
+    /**
+     * Checks if a location is known, i.e. it is not null nor equal to {@link Location#UNKNOWN}.
+     * 
+     * @param location the location to check
+     * @return <code>true</code> if the location is known
+     */
+    public static boolean isKnown(Location location) {
+        return location != null && !Location.UNKNOWN.equals(location);
+    }
+
+    /**
+     * Checks if a location is unknown, i.e. it is either null or equal to {@link Location#UNKNOWN}.
+     * 
+     * @param location the location to check
+     * @return <code>true</code> if the location is unknown
+     */
+    public static boolean isUnknown(Location location) {
+        return location == null || Location.UNKNOWN.equals(location);
+    }
+
+    /**
+     * Add a {@link LocationFinder} to the list of finders that will be queried for an object's
+     * location by {@link #getLocation(Object, String)}.
+     * 
+     * @param finder the location finder to add
+     */
+    public static void addFinder(LocationFinder finder) {
+        if (finder == null) {
+            return;
+        }
+
+        synchronized(LocationUtils.class) {
+            int length = finders.length;
+            LocationFinder[] newFinders = new LocationFinder[length + 1];
+            System.arraycopy(finders, 0, newFinders, 0, length);
+            newFinders[length] = finder;
+            finders = newFinders;
+        }
+    }
+    
+    /**
+     * Get the location of an object. Some well-known located classes built in the JDK are handled
+     * by this method. Handling of other located classes can be handled by adding new location finders.
+     * 
+     * @param obj the object of which to get the location
+     * @return the object's location, or {@link Location#UNKNOWN} if no location could be found
+     */
+    public static Location getLocation(Object obj) {
+        return getLocation(obj, null);
+    }
+    
+    /**
+     * Get the location of an object. Some well-known located classes built in the JDK are handled
+     * by this method. Handling of other located classes can be handled by adding new location finders.
+     * 
+     * @param obj the object of which to get the location
+     * @param description an optional description of the object's location, used if a Location object
+     *        has to be created.
+     * @return the object's location, or {@link Location#UNKNOWN} if no location could be found
+     */
+    public static Location getLocation(Object obj, String description) {
+        if (obj instanceof Locatable) {
+            return ((Locatable)obj).getLocation();
+        }
+        
+        // Check some well-known locatable exceptions
+        if (obj instanceof SAXParseException) {
+            SAXParseException spe = (SAXParseException)obj;
+            if (spe.getSystemId() != null) {
+                return new LocationImpl(description, spe.getSystemId(), spe.getLineNumber(), spe.getColumnNumber());
+            } else {
+                return Location.UNKNOWN;
+            }
+        }
+        
+        if (obj instanceof TransformerException) {
+            TransformerException ex = (TransformerException)obj;
+            SourceLocator locator = ex.getLocator();
+            if (locator != null && locator.getSystemId() != null) {
+                return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
+            } else {
+                return Location.UNKNOWN;
+            }
+        }
+        
+        if (obj instanceof Locator) {
+            Locator locator = (Locator)obj;
+            if (locator.getSystemId() != null) {
+                return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
+            } else {
+                return Location.UNKNOWN;
+            }
+        }
+
+        Location result;
+        LocationFinder[] currentFinders = finders;
+        for (int i = 0; i < currentFinders.length; i++) {
+            result = currentFinders[i].getLocation(obj, description);
+            if (result != null) {
+                return result;
+            }
+        }
+        
+        return Location.UNKNOWN;
+    }
+}

Propchange: cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/src/java/org/apache/cocoon/util/location/LocationUtils.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: cocoon/trunk/src/java/org/apache/cocoon/util/log/CocoonLogFormatter.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/util/log/CocoonLogFormatter.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/util/log/CocoonLogFormatter.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/util/log/CocoonLogFormatter.java Tue Sep 13 12:35:46 2005
@@ -20,9 +20,9 @@
 
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.Request;
-import org.apache.cocoon.util.ExceptionUtils;
 
 import org.apache.commons.lang.ClassUtils;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.commons.lang.time.FastDateFormat;
 import org.apache.log.ContextMap;
 import org.apache.log.LogEvent;

Modified: cocoon/trunk/src/test/org/apache/cocoon/util/location/LocationTestCase.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/test/org/apache/cocoon/util/location/LocationTestCase.java?rev=280632&r1=280631&r2=280632&view=diff
==============================================================================
--- cocoon/trunk/src/test/org/apache/cocoon/util/location/LocationTestCase.java (original)
+++ cocoon/trunk/src/test/org/apache/cocoon/util/location/LocationTestCase.java Tue Sep 13 12:35:46 2005
@@ -32,7 +32,7 @@
 
     public void testParse() throws Exception {
         String str = "<map:generate> - path/to/file.xml:1:40";
-        Location loc = LocationImpl.get(str);
+        Location loc = LocationUtils.getLocation(str);
         
         assertEquals("<map:generate>", loc.getDescription());
         assertEquals("URI", "path/to/file.xml", loc.getURI());
@@ -42,7 +42,7 @@
     }
     
     public void testEquals() throws Exception {
-        Location loc1 = LocationImpl.get(str);
+        Location loc1 = LocationUtils.getLocation(str);
         Location loc2 = new LocationImpl(null, "path/to/file.xml", 1, 40);
         
         assertEquals("locations", loc1, loc2);