Author: mrdon
Date: Sat Apr 19 20:20:57 2008
New Revision: 649874

URL: http://svn.apache.org/viewvc?rev=649874&view=rev
Log:
Better error handling for XSLT transform errors
WW-2594

Modified:
    
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java
    
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java

Modified: 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java?rev=649874&r1=649873&r2=649874&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java
 (original)
+++ 
struts/struts2/trunk/core/src/main/java/org/apache/struts2/views/xslt/XSLTResult.java
 Sat Apr 19 20:20:57 2008
@@ -28,6 +28,7 @@
 import java.util.Map;
 
 import javax.servlet.http.HttpServletResponse;
+import javax.xml.transform.ErrorListener;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Source;
 import javax.xml.transform.Templates;
@@ -41,6 +42,7 @@
 
 import org.apache.struts2.ServletActionContext;
 import org.apache.struts2.StrutsConstants;
+import org.apache.struts2.StrutsException;
 
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
@@ -327,6 +329,24 @@
                 transformer = 
TransformerFactory.newInstance().newTransformer();
 
             transformer.setURIResolver(getURIResolver());
+            transformer.setErrorListener(new ErrorListener() {
+
+                public void error(TransformerException exception)
+                        throws TransformerException {
+                    throw new StrutsException("Error transforming result", 
exception);
+                }
+
+                public void fatalError(TransformerException exception)
+                        throws TransformerException {
+                    throw new StrutsException("Fatal error transforming 
result", exception);
+                }
+
+                public void warning(TransformerException exception)
+                        throws TransformerException {
+                    LOG.warn(exception.getMessage(), exception);
+                }
+                
+            });
 
             String mimeType;
             if (templates == null)

Modified: 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java
URL: 
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java?rev=649874&r1=649873&r2=649874&view=diff
==============================================================================
--- 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java
 (original)
+++ 
struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/xslt/XSLTResultTest.java
 Sat Apr 19 20:20:57 2008
@@ -152,7 +152,26 @@
         assertTrue(out.startsWith("<?xml version=\"1.0\" 
encoding=\"UTF-8\"?>"));
         assertTrue(out.indexOf("<validators>") > -1);
     }
-
+    
+    public void testTransformWithError() throws Exception {
+        result = new XSLTResult(){
+            protected URIResolver getURIResolver() {
+                return new URIResolver() {
+                    public Source resolve(String href, String base) throws 
TransformerException {
+                        throw new TransformerException("Some random error");
+                    }
+                };
+            }
+        };
+        result.setLocation("XSLTResultTest4.xsl");
+        try {
+            result.execute(mai);
+            fail("Should have thrown an exception");
+        } catch (Exception ex) {
+            assertEquals("Error transforming result", ex.getMessage());
+        }
+    }
+    
     protected void setUp() throws Exception {
         super.setUp();
         request = new MockHttpServletRequest();


Reply via email to