Commits

rainerh  committed 5bb7350

FreeMarker template error! error.ftl]
Issue Number: WW-1353

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/xwork_1-2@1129e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 1658173
  • Branches xwork_1-2

Comments (0)

Files changed (1)

File src/java/com/opensymphony/xwork/util/location/LocationUtils.java

 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.List;
+import java.net.URL;
 
 import javax.xml.transform.SourceLocator;
 import javax.xml.transform.TransformerException;
 import org.xml.sax.SAXParseException;
 
 import org.w3c.dom.Element;
+import com.opensymphony.util.ClassLoaderUtil;
 
 /**
  * Location-related utility methods.
  */
 public class LocationUtils {
-    
+
     /**
      * The string representation of an unknown location: "<code>[unknown location]</code>".
      */
     public static final String UNKNOWN_STRING = "[unknown location]";
-    
+
     private static List finders = new ArrayList();
-    
+
     /**
      * An finder or object locations
      */
     private LocationUtils() {
         // Forbid instanciation
     }
-    
+
     /**
      * Builds a string representation of a location, in the
      * "<code><em>descripton</em> - <em>uri</em>:<em>line</em>:<em>column</em></code>"
         } else {
             result.append(UNKNOWN_STRING);
         }
-        
+
         return result.toString();
     }
 
             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));
         } catch(Exception e) {
             // Ignore: handled below
         }
-        
+
         return LocationImpl.UNKNOWN;
     }
 
             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.
     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.
         if (obj instanceof Location) {
             return (Location) obj;
         }
-        
+
         if (obj instanceof Locatable) {
             return ((Locatable)obj).getLocation();
         }
-        
+
         // Check some well-known locatable exceptions
         if (obj instanceof SAXParseException) {
             SAXParseException spe = (SAXParseException)obj;
                 return Location.UNKNOWN;
             }
         }
-        
+
         if (obj instanceof TransformerException) {
             TransformerException ex = (TransformerException)obj;
             SourceLocator locator = ex.getLocator();
                 return Location.UNKNOWN;
             }
         }
-        
+
         if (obj instanceof Locator) {
             Locator locator = (Locator)obj;
             if (locator.getSystemId() != null) {
                 return Location.UNKNOWN;
             }
         }
-        
+
         if (obj instanceof Element) {
             return LocationAttributes.getLocation((Element)obj);
         }
                     finders = newFinders;
                 }
             }
-            
+
             Location result = finder.getLocation(obj, description);
             if (result != null) {
                 return result;
             }
         }
 
+        if (obj instanceof Throwable) {
+                Throwable t = (Throwable) obj;
+                StackTraceElement[] stack = t.getStackTrace();
+                if (stack != null && stack.length > 0) {
+                    StackTraceElement trace = stack[0];
+                    if (trace.getLineNumber() >= 0) {
+                        String uri = trace.getClassName();
+                        if (trace.getFileName() != null) {
+                            uri = uri.replace('.','/');
+                            uri = uri.substring(0, uri.lastIndexOf('/') + 1);
+                            uri = uri + trace.getFileName();
+                            URL url = ClassLoaderUtil.getResource(uri, LocationUtils.class);
+                            if (url != null) {
+                                uri = url.toString();
+                            }
+                        }
+                        if (description == null) {
+                            StringBuilder sb = new StringBuilder();
+                            sb.append("Class: ").append(trace.getClassName()).append("\n");
+                            sb.append("File: ").append(trace.getFileName()).append("\n");
+                            sb.append("Method: ").append(trace.getMethodName()).append("\n");
+                            sb.append("Line: ").append(trace.getLineNumber());
+                            description = sb.toString();
+                        }
+                        return new LocationImpl(description, uri, trace.getLineNumber(), -1);
+                    }
+                }
+        }
+
         return Location.UNKNOWN;
     }
 }