Author: thorsten
Date: Thu Oct  9 03:56:17 2008
New Revision: 703131

URL: http://svn.apache.org/viewvc?rev=703131&view=rev
Log:
Switching to use contract exception and enhancing the code around the exception 
handling in general. Using the errorListener to be able to terminate processing 
if transformer exception happens. Before the transformer.transform method 
hardly threw an exception even if the transformation had terminated with error. 
Now as soon as an error happens we throw an exception which allow to determine 
the root cause of a problem more easy.

Modified:
    
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java

Modified: 
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java
URL: 
http://svn.apache.org/viewvc/forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java?rev=703131&r1=703130&r2=703131&view=diff
==============================================================================
--- 
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java
 (original)
+++ 
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/helper/XSLContractHelper.java
 Thu Oct  9 03:56:17 2008
@@ -33,17 +33,17 @@
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.stream.util.XMLEventAllocator;
+import javax.xml.transform.ErrorListener;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.URIResolver;
 import javax.xml.transform.stream.StreamSource;
 
-import org.apache.forrest.dispatcher.exception.DispatcherException;
+import org.apache.forrest.dispatcher.exception.ContractException;
 import org.apache.forrest.dispatcher.impl.XSLContract;
 import org.xml.sax.SAXException;
 
@@ -72,6 +72,8 @@
   public void prepareTransformation(Source xslSource, boolean 
allowXmlProperties, Map<String, Object> params)
       throws TransformerConfigurationException, ParserConfigurationException, 
SAXException, IOException {
     TransformerFactory transFact = TransformerFactory.newInstance();
+    ErrorListener listener = new LoggingErrorListener(log);
+   // transFact.setErrorListener(listener);
     if(null != uriResolver){
       transFact.setURIResolver(uriResolver);
     }
@@ -80,6 +82,8 @@
     transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
     transformer.setOutputProperty(OutputKeys.INDENT, "yes");
     transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+    // set errorListener
+    transformer.setErrorListener(listener);
     // do we allow xml properties?
     if(allowXmlProperties){
       DocumentBuilder builder = DocumentBuilderFactory.newInstance()
@@ -149,10 +153,10 @@
    * @param stream
    * @param contract
    * @throws XMLStreamException
-   * @throws DispatcherException 
+   * @throws ContractException 
    */
   public void setTemplate(InputStream stream, XSLContract contract)
-      throws XMLStreamException, DispatcherException {
+      throws XMLStreamException, ContractException {
     XMLStreamReader reader = getNSReader(stream);
     boolean process = true;
     while (process) {
@@ -185,7 +189,7 @@
     }
   }
 
-  private Source processTemplate(XMLStreamReader reader) throws 
DispatcherException{
+  private Source processTemplate(XMLStreamReader reader) throws 
ContractException{
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     try {
       XMLEventWriter writer = getWriter(out);
@@ -229,7 +233,7 @@
       writer.flush();
       log.debug(out.toString());
     } catch (XMLStreamException e) {
-      throw new DispatcherException("Fatal error in contract",e);
+      throw new ContractException("Fatal error in contract.\n"+e);
     }
     Source source = new StreamSource(StreamHelper.switchStream(out));
     return source;
@@ -303,8 +307,16 @@
     return contractName;
   }
 
-  public void transform(InputStream dataStream, Result streamResult) throws 
TransformerException {
+  public void transform(InputStream dataStream, Result streamResult) throws 
ContractException {
     Source dataSource = new StreamSource(dataStream);
-    transformer.transform(dataSource, streamResult);
+    try {
+      transformer.transform(dataSource, streamResult);
+    } catch (Exception e) {
+      String message ="The xsl transformation has thrown an 
exception."+"\n"+e+"\n\nproblem->solution:\n" +
+               "- org.apache.xpath.XPathException: Can not convert #STRING to 
a NodeList!\n" +
+               "-> Try to activate \"allowXml\" and/or \"shrink\". If this is 
not working try the contract " +
+               "xsl standalone and make sure it is not a xsl specific 
problem.";
+    throw new ContractException(message);
+    }
   }
 }