Commits

Anonymous committed dc20836

Moved snippets from the XWorkException to Location, allowing you to easier
show snippets from arbitrary exceptions

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@1029e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (7)

src/java/com/opensymphony/xwork/XWorkException.java

     
     
     /**
-     * Gets a source code snippet with the default padding
-     */
-    public List getSnippet() {
-        return getSnippet(2);
-    }
-    
-    
-    /**
-     * Gets a source code snippet with the default padding
-     *
-     * @param padding The amount of lines before and after the error to include
-     */
-    public List getSnippet(int padding) {
-        List snippet = new ArrayList();
-        if (location != null && location.getLineNumber() > 0) {
-            try {
-                InputStream in = new URL(location.getURI()).openStream();
-                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
-                
-                int lineno = 0;
-                int errno = location.getLineNumber();
-                String line;
-                while ((line = reader.readLine()) != null) {
-                    lineno++;
-                    if (lineno >= errno - padding && lineno <= errno + padding) {
-                        snippet.add(line);
-                    }
-                }
-            } catch (Exception ex) {
-                // ignoring as snippet not available isn't a big deal
-            }
-        }
-        return snippet;
-    }
-            
-
-    /**
      * Returns a short description of this throwable object, including the 
      * location. If no detailed message is available, it will use the message
      * of the underlying exception if available.

src/java/com/opensymphony/xwork/util/location/Location.java

  */
 package com.opensymphony.xwork.util.location;
 
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
 
 /**
  * A location in a resource. The location is composed of the URI of the resource, and 
      * @return the column number (<code>-1</code> if unknown)
      */
     public int getColumnNumber();
-
+    
+    /**
+     * Gets a source code snippet with the default padding
+     *
+     * @param padding The amount of lines before and after the error to include
+     * @return A list of source lines
+     */
+    public List getSnippet(int padding);
 }

src/java/com/opensymphony/xwork/util/location/LocationImpl.java

  */
 package com.opensymphony.xwork.util.location;
 
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.Serializable;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * A simple immutable and serializable implementation of {@link Location}.
     public int getColumnNumber() {
         return this.column;
     }
+    
+    /**
+     * Gets a source code snippet with the default padding
+     *
+     * @param padding The amount of lines before and after the error to include
+     */
+    public List getSnippet(int padding) {
+        List snippet = new ArrayList();
+        if (getLineNumber() > 0) {
+            try {
+                InputStream in = new URL(getURI()).openStream();
+                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+                
+                int lineno = 0;
+                int errno = getLineNumber();
+                String line;
+                while ((line = reader.readLine()) != null) {
+                    lineno++;
+                    if (lineno >= errno - padding && lineno <= errno + padding) {
+                        snippet.add(line);
+                    }
+                }
+            } catch (Exception ex) {
+                // ignoring as snippet not available isn't a big deal
+            }
+        }
+        return snippet;
+    }
 
     public boolean equals(Object obj) {
         if (obj == this) {

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

 package com.opensymphony.xwork.util.location;
 
 import java.lang.ref.WeakReference;
+import java.net.URISyntaxException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 
 
 import org.w3c.dom.Element;
 
+import com.opensymphony.util.ClassLoaderUtil;
+
 /**
  * Location-related utility methods.
  */
                 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;
     }

src/test/com/opensymphony/xwork/XworkExceptionTest.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork;
-
-import com.opensymphony.xwork.util.location.*;
-import com.opensymphony.util.ClassLoaderUtil;
-import junit.framework.TestCase;
-
-import java.util.List;
-import java.net.URL;
-
-/**
- * XWorkException tests
- */
-public class XworkExceptionTest extends TestCase {
-
-    public void testGetSnippet() throws Exception {
-        URL url = ClassLoaderUtil.getResource("com/opensymphony/xwork/somefile.txt", getClass());
-        Location loc = new LocationImpl("foo", url.toString(), 3, 2);
-        XWorkException ex = new XWorkException("Some message", loc);
-        
-        List snippet = ex.getSnippet(1);
-        assertNotNull(snippet);
-        assertTrue("Wrong length: "+snippet.size(), 3 == snippet.size());
-        
-        assertTrue("is".equals(snippet.get(0)));
-        assertTrue("a".equals(snippet.get(1)));
-        assertTrue("file".equals(snippet.get(2)));
-    }
-    
-    public void testGetSnippetNoPadding() throws Exception {
-        URL url = ClassLoaderUtil.getResource("com/opensymphony/xwork/somefile.txt", getClass());
-        Location loc = new LocationImpl("foo", url.toString(), 3, 2);
-        XWorkException ex = new XWorkException("Some message", loc);
-        
-        List snippet = ex.getSnippet(0);
-        assertNotNull(snippet);
-        assertTrue("Wrong length: "+snippet.size(), 1 == snippet.size());
-        
-        assertTrue("a".equals(snippet.get(0)));
-    }
-}

src/test/com/opensymphony/xwork/util/location/LocationImplTest.java

 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.net.URL;
+import java.util.List;
+
+import com.opensymphony.util.ClassLoaderUtil;
+import com.opensymphony.xwork.XWorkException;
 
 import junit.framework.TestCase;
 
         
         assertSame("unknown location", Location.UNKNOWN, obj);
     }
+    
+    public void testGetSnippet() throws Exception {
+        URL url = ClassLoaderUtil.getResource("com/opensymphony/xwork/somefile.txt", getClass());
+        Location loc = new LocationImpl("foo", url.toString(), 3, 2);
+        
+        List snippet = loc.getSnippet(1);
+        assertNotNull(snippet);
+        assertTrue("Wrong length: "+snippet.size(), 3 == snippet.size());
+        
+        assertTrue("is".equals(snippet.get(0)));
+        assertTrue("a".equals(snippet.get(1)));
+        assertTrue("file".equals(snippet.get(2)));
+    }
+    
+    public void testGetSnippetNoPadding() throws Exception {
+        URL url = ClassLoaderUtil.getResource("com/opensymphony/xwork/somefile.txt", getClass());
+        Location loc = new LocationImpl("foo", url.toString(), 3, 2);
+        
+        List snippet = loc.getSnippet(0);
+        assertNotNull(snippet);
+        assertTrue("Wrong length: "+snippet.size(), 1 == snippet.size());
+        
+        assertTrue("a".equals(snippet.get(0)));
+    }
 }

src/test/com/opensymphony/xwork/util/location/LocationUtilsTest.java

         assertEquals("column", 40, loc.getColumnNumber());
         assertEquals("string representation", str, loc.toString());
     }
+    
+    public void testGetLocation_location() throws Exception {
+    		Location loc = new LocationImpl("desc", "sysId", 10, 4);
+    		assertTrue("Location should be the same", 
+				loc == LocationUtils.getLocation(loc, null));
+    }
+    
+    public void testGetLocation_exception() throws Exception {
+    		Exception e = new Exception();
+    		Location loc = LocationUtils.getLocation(e, null);
+    		
+    		assertTrue("Wrong sysId: "+loc.getURI(),
+    				"com/opensymphony/xwork/util/location/LocationUtilsTest.java"
+    				.equals(loc.getURI()));
+    }
 }