Author: thorsten
Date: Thu Oct  9 04:09:28 2008
New Revision: 703132

URL: http://svn.apache.org/viewvc?rev=703132&view=rev
Log:
FOR-1127
Switching to use contract exception and enhancing the code around the exception 
handling in general. 

Further adding note about the possibility of enhancing the contract processing. 
It is possible to allow contracts to fail if they are not critical for the 
overall result. This is a nice possible future feature. One can imaging to use 
an @critical='true|false' to indicate whether to fail or not.

e.g.  
<forrest:contract 
    critical="false"
    name="siteinfo-meta-navigation" 
    dataURI="cocoon://#{$getRequest}.navigation.xml"/>

with this tag the dispatcher will not fail if the contact fails.

Fixing problem for non-xml output. Instead of assuming the existence of a root 
element we now check if there is one and if not we create one.

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

Modified: 
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XMLStructurer.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/XMLStructurer.java?rev=703132&r1=703131&r2=703132&view=diff
==============================================================================
--- 
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XMLStructurer.java
 (original)
+++ 
forrest/branches/dispatcher_rewrite/plugins/org.apache.forrest.plugin.internal.dispatcher/src/java/org/apache/forrest/dispatcher/impl/XMLStructurer.java
 Thu Oct  9 04:09:28 2008
@@ -23,6 +23,7 @@
 import org.apache.forrest.dispatcher.api.Resolver;
 import org.apache.forrest.dispatcher.api.Structurer;
 import org.apache.forrest.dispatcher.config.DispatcherBean;
+import org.apache.forrest.dispatcher.exception.ContractException;
 import org.apache.forrest.dispatcher.exception.DispatcherException;
 import org.apache.forrest.dispatcher.factories.ContractFactory;
 import org.apache.forrest.dispatcher.impl.helper.Captions;
@@ -197,6 +198,10 @@
           StartElement start = getEventFactory().createStartElement("", "",
               replaceFirst);
           writer.add((XMLEvent) start);
+        }else if(element.equals("/")){
+          StartElement start = getEventFactory().createStartElement("", "",
+              "result");
+          writer.add((XMLEvent) start);
         }
 
         injectResult(writer, element);
@@ -204,7 +209,11 @@
           EndElement end = getEventFactory().createEndElement("", "",
               replaceFirst);
           writer.add((XMLEvent) end);
-        }
+        }else if(element.equals("/")){
+          EndElement end = getEventFactory().createEndElement("", "",
+              "result");
+          writer.add((XMLEvent) end);
+    }
 
       }
 
@@ -214,7 +223,7 @@
   }
 
   private void processContract(XMLStreamReader reader)
-      throws XMLStreamException, DispatcherException, IOException {
+      throws DispatcherException, XMLStreamException{
     boolean process = true;
     String elementName = null;
     String name = "", data = null;
@@ -241,10 +250,24 @@
       case XMLStreamConstants.END_ELEMENT:
         elementName = reader.getLocalName();
         if (elementName.equals(Captions.CONTRACT_ELEMENT)) {
-          InputStream resultStream = contract.execute(dataStream, localParams);
-          StreamHelper.closeStream(dataStream);
-          processContractResult(resultStream);
-          StreamHelper.closeStream(resultStream);
+          try {
+            InputStream resultStream = contract.execute(dataStream, 
localParams);
+            StreamHelper.closeStream(dataStream);
+            processContractResult(resultStream);
+            StreamHelper.closeStream(resultStream);
+          } catch (Exception e) {
+            /*
+             *  FOR-1127
+             *  Here we can inject custom handler for allowing that contracts 
can
+             *  throw exceptions but the overall structurer will not fail at 
whole.
+             *  
+             *  Imaging contract "xyz" will fail. Now we throw an exception 
and abort 
+             *  processing. However it may be desirable that the process 
continues 
+             *  since the contract may not be critical for the overall result.
+             *  
+             */
+            throw new ContractException(name,"\n",e);
+          }
           process = false;
         }
         break;