Author: rwhitcomb
Date: Tue Oct 29 17:40:11 2013
New Revision: 1536829

URL: http://svn.apache.org/r1536829
Log:
PIVOT-924: Provide a standard mechanism in BXMLSerializer to report exceptions
so that a particular application can subclass and report exceptions in a 
different
way than writing to System.err.

Modified:
    pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java

Modified: pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java?rev=1536829&r1=1536828&r2=1536829&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/beans/BXMLSerializer.java Tue Oct 29 
17:40:11 2013
@@ -105,7 +105,7 @@ public class BXMLSerializer implements S
         }
     }
 
-    private static class AttributeInvocationHandler implements 
InvocationHandler {
+    private class AttributeInvocationHandler implements InvocationHandler {
         private ScriptEngine scriptEngine;
         private String event;
         private String script;
@@ -130,8 +130,7 @@ public class BXMLSerializer implements S
                     scriptEngine.setBindings(bindings, 
ScriptContext.ENGINE_SCOPE);
                     scriptEngine.eval(script);
                 } catch (ScriptException exception) {
-                    System.err.println(exception);
-                    System.err.println(script);
+                    reportException(exception, script);
                 }
             }
 
@@ -449,13 +448,13 @@ public class BXMLSerializer implements S
                 throw new SerializationException(exception);
             }
         } catch (IOException exception) {
-            logException();
+            logException(exception);
             throw exception;
         } catch (SerializationException exception) {
-            logException();
+            logException(exception);
             throw exception;
         } catch (RuntimeException exception) {
-            logException();
+            logException(exception);
             throw exception;
         }
 
@@ -1274,7 +1273,7 @@ public class BXMLSerializer implements S
                 try {
                     scriptEngine.eval(script);
                 } catch (ScriptException exception) {
-                    System.err.println(exception);
+                    reportException(exception, script);
                     break;
                 }
 
@@ -1347,7 +1346,7 @@ public class BXMLSerializer implements S
                                 scriptLocation.openStream()));
                             scriptEngine.eval(scriptReader);
                         } catch (ScriptException exception) {
-                            exception.printStackTrace();
+                            reportException(exception);
                         } finally {
                             if (scriptReader != null) {
                                 scriptReader.close();
@@ -1374,8 +1373,7 @@ public class BXMLSerializer implements S
                     try {
                         scriptEngine.eval(script);
                     } catch (ScriptException exception) {
-                        System.err.println(exception);
-                        System.err.println(script);
+                        reportException(exception, script);
                     }
                 }
 
@@ -1410,7 +1408,7 @@ public class BXMLSerializer implements S
         return xmlStreamReader.getLocation();
     }
 
-    private void logException() {
+    private void logException(Throwable exception) {
         Location streamReaderlocation = xmlStreamReader.getLocation();
         String message = "An error occurred at line number " + 
streamReaderlocation.getLineNumber();
 
@@ -1420,7 +1418,25 @@ public class BXMLSerializer implements S
 
         message += ":";
 
-        System.err.println(message);
+        reportException(new SerializationException(message, exception));
+    }
+
+    private void reportException(ScriptException exception, String script) {
+        reportException(new SerializationException("Failed to execute 
script:\n"+script, exception));
+    }
+
+    /**
+     * Hook used for standardized reporting of exceptions during this process.
+     * <p>Subclasses should override this method in order to do something 
besides
+     * print to <tt>System.err</tt>.
+     */
+    protected void reportException(Throwable exception) {
+        String message = exception.getLocalizedMessage();
+        if (message == null || message.isEmpty()) {
+            message = exception.getClass().getSimpleName();
+        }
+        System.err.println("Exception: " + message);
+        exception.printStackTrace(System.err);
     }
 
     @Override


Reply via email to