forrest-svn mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thors...@apache.org
Subject svn commit: r350221 - in /forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher: ContractBean.java DispatcherHelper.java transformation/DispatcherTransformer.java
Date Thu, 01 Dec 2005 10:21:41 GMT
Author: thorsten
Date: Thu Dec  1 02:21:35 2005
New Revision: 350221

URL: http://svn.apache.org/viewcvs?rev=350221&view=rev
Log:
cleaned up the code from not needed fragments. Added more comments to the classes, enhanced
error message throwing and overall readablity of the code. Enhanced matching the state of
the transformer, whether it is inside or outside of the structurer. With this it is now possible
to have the structurer within other xml like <root><structurer/></root>.
Moved helper classes to the dispatcherHelper.

Modified:
    forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java
    forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
    forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java

Modified: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java
URL: http://svn.apache.org/viewcvs/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java?rev=350221&r1=350220&r2=350221&view=diff
==============================================================================
--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java
(original)
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java
Thu Dec  1 02:21:35 2005
@@ -17,7 +17,6 @@
 package org.apache.forrest.dispatcher;
 
 import java.beans.Beans;
-import java.util.HashMap;
 
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.OutputKeys;
@@ -57,66 +56,114 @@
      * </p>
      * 
      * <pre>
-     *                       &lt;forrest:contract name=&quot;nav-section&quot;
dataURI=&quot;cocoon://index.navigation.xml&quot;/&gt;
+     *                           &lt;forrest:contract name=&quot;nav-section&quot;
dataURI=&quot;cocoon://index.navigation.xml&quot;/&gt;
      * </pre>
      */
     public static final String CONTRACT_ELEMENT = "contract";
 
     /**
-     * Each contract can contact business services to get the data model. The
-     * attribute "CONTRACT_NUGGET_ATTRIBUTE" contains the uri to contact for
-     * business data
+     * Each contract can contact external/internal business services to get the
+     * data model. The attribute "CONTRACT_NUGGET_ATTRIBUTE" is the identifier
+     * for the uri of the business data
      */
     public static final String CONTRACT_NUGGET_ATTRIBUTE = "dataURI";
 
     /**
-     * Each contract must have an unique identifier. The attribute "CONTRACT_ID_ATTRIBUTE"
-     * contains this id
+     * Each contract must have an unique identifier. The attribute
+     * "CONTRACT_ID_ATTRIBUTE" stands for this id
      */
     public static final String CONTRACT_ID_ATTRIBUTE = "name";
-    
+
     /**
-     * Each contract implementation needs to define the input format for the transformation.

-     * ATM we only support xsl. The attribute "CONTRACT_IMPL_INPUT_FORMAT_ATTRIBUTE"
-     * defines the input format for transformation
+     * Each contract implementation needs to define the input format for the
+     * transformation. ATM we only support xsl. The attribute
+     * "CONTRACT_IMPL_INPUT_FORMAT_ATTRIBUTE" defines the input format for
+     * transformation
      */
     public static final String CONTRACT_IMPL_INPUT_FORMAT_ATTRIBUTE = "inputFormat";
 
     /**
-     * Each contract implementation needs to define the input format for the transformation.

-     * ATM we only support xsl. The attribute "CONTRACT_IMPL_INPUT_FORMAT_ATTRIBUTE"
-     * defines the input format for transformation
+     * Each contract implementation needs to store the input format within a
+     * root element
      */
     public static final String CONTRACT_IMPL_ROOT_ELEMENT = "forrest:template";
     
-    private static final String PROPERTY_ID_ATTRIBUTE = "name";
-
-    private HashMap contract;
-
-    private Element structurerContract;
+    /**
+     * Each contract can have properties, which are definite e.g. in the
+     * structurer index.fv and used in the contract.
+     * 
+     * <pre>
+     *                           &lt;forrest:contract name=&quot;nav-main-testing&quot;
nugget=&quot;cocoon://index.navigation.xml&quot;&gt;
+     *                            &lt;forrest:property name=&quot;nav-main-testing-test1&quot;
&gt;Just a test&lt;/forrest:property&gt;
+     *                           &lt;/forrest:contract&gt;
+     * </pre>
+     */
+    public static final String PROPERTY_ELEMENT = "property";
+    
+    /**
+     * Each property must have an unique identifier. The attribute
+     * "PROPERTY_ID_ATTRIBUTE" stands for this id
+     */
+    static public final String PROPERTY_ID_ATTRIBUTE = "name";
 
     private Element[] propertyList;
 
+    /**
+     * The implementation of the contract
+     */
     private Document contractImpl;
 
+    /**
+     * The description of the contract
+     */
     private Element contractDescription;
 
+    /**
+     * The usage instruction of this contract for the structurer
+     */
     private Element contractUsage;
 
+    /**
+     * The raw data that should be used in the transformation. Either coming
+     * from the business service or from the calling class (as e.g. foo element
+     * document)
+     */
     private Document contractRawData;
 
+    /**
+     * The transformer that will transform the raw data with the contract
+     * implementation
+     */
     private Transformer contractTransformer;
 
+    /**
+     * The resulting data from the transformation
+     */
     private DOMResult contractResultData;
 
+    /**
+     * Does this contract request a business service
+     */
     private boolean isNugget = false;
 
+    /**
+     * Does the contract has properties
+     */
     private boolean hasProperties = false;
 
+    /**
+     * The name of the contract
+     */
     private String contractName;
 
+    /**
+     * The uri of the business service
+     */
     private String nuggetUri;
 
+    /*
+     * Helper
+     */
     protected NamespaceHelper namespaceHelper;
 
     protected DispatcherHelper dispatcherHelper;
@@ -124,17 +171,6 @@
     protected ServiceManager manager;
 
     /**
-     * @param element
-     * @param namespaceHelper
-     * @deprecated
-     */
-    public ContractBean(Element element, NamespaceHelper namespaceHelper) {
-        this.contract = new HashMap();
-        this.namespaceHelper = namespaceHelper;
-        this.loadStructurer(element);
-    }
-
-    /**
      * The ContractBean contains all fields to work with contracts. It is a
      * helper bean.
      * 
@@ -149,101 +185,99 @@
     public ContractBean(ServiceManager manager)
             throws ParserConfigurationException {
         this.manager = manager;
-        this.contract = new HashMap();
         dispatcherHelper = new DispatcherHelper(manager);
     }
 
     /**
-     * The ContractBean contains all fields to work with contracts. It is a
-     * helper bean.
-     * 
-     * Helpers (forrest:contracts) mainly adapt and transform the presentation
-     * model (pm), but also help with any limited business processing that is
-     * initiated from the structurer by forrest:properties by passing this
-     * information to the transfomer.
-     * 
-     * @throws ParserConfigurationException
+     * Recycle the component
      */
-    public ContractBean() {}
+    public void recycle() {
+        this.contractDescription = null;
+        this.contractImpl = null;
+        this.contractName = null;
+        this.contractRawData = null;
+        this.contractResultData = null;
+        this.contractTransformer = null;
+        this.contractUsage = null;
+        this.dispatcherHelper = null;
+        this.hasProperties = false;
+        this.isNugget = false;
+        this.namespaceHelper = null;
+        this.nuggetUri = null;
+        this.propertyList = null;
+    }
 
     /**
-     * @param structurer
-     * @deprecated
+     * initialize the contract (normally done after recycle) Do not use to
+     * create a new instance!!!
+     * 
+     * @throws ParserConfigurationException
      */
-    private void loadStructurer(Element structurer) {
-        this.setStructurerContract(structurer);
-        this.setNugget(structurer.hasAttribute(CONTRACT_NUGGET_ATTRIBUTE));
-        this.setHasProperties(structurer.hasChildNodes());
-        if (hasProperties) {
-            this.propertyList = namespaceHelper.getChildren(structurer);
-        }
-        this.setContractName(structurer.getAttribute(CONTRACT_ID_ATTRIBUTE));
-        /*
-         * if (this.isNugget) { this.setNuggetUri(structurer
-         * .getAttribute(CONTRACT_NUGGET_ATTRIBUTE)); }
-         */
+    public void initialize() throws ParserConfigurationException {
+        dispatcherHelper = new DispatcherHelper(this.manager);
     }
 
     /**
      * setContractImpl(String contractUri)
      * 
-     * This method set the actual contract implementation via a URI.
-     * 
-     * Here we set the description and the usage instruction of the contract
-     * implementation. As well we prepare the transformer. The simplest form is
-     * a contract that does not need a pm. It can provide all data through the
-     * transformation. A more dynamic contract would provide properties to the
-     * transformation (contractImpl) to apply limited business logic.
+     * This method invokes the setting of the actual contract implementation via
+     * an URI.
      * 
      * @param contractUri
      * @throws Exception
      */
     public void setContractImpl(String contractUri) throws Exception {
-        Document contractImpl = dispatcherHelper.getDocument(contractUri);
-        this.contractImpl = contractImpl;
-        contractImplHelper(contractImpl);
+        Document _contractImpl = dispatcherHelper.getDocument(contractUri);
+        this.contractImpl = _contractImpl;
+        contractImplHelper(this.contractImpl);
     }
 
     /**
-     * setContractImpl(String contractUri)
+     * setContractImpl(Document _contractImpl)
      * 
-     * This method set the actual contract implementation via a URI.
-     * 
-     * Here we set the description and the usage instruction of the contract
-     * implementation. As well we prepare the transformer. The simplest form is
-     * a contract that does not need a pm. It can provide all data through the
-     * transformation. A more dynamic contract would provide properties to the
-     * transformation (contractImpl) to apply limited business logic.
+     * This method invokes the setting of the actual contract implementation via
+     * a document.
      * 
      * @param contractUri
      * @throws Exception
      */
-    public void setContractImpl(Document contractImpl) throws Exception {
-        this.contractImpl = contractImpl;
-        contractImplHelper(contractImpl);
+    public void setContractImpl(Document _contractImpl) throws Exception {
+        this.contractImpl = _contractImpl;
+        contractImplHelper(this.contractImpl);
     }
 
     /**
-     * @param contractImpl
+     * contractImplHelper(Document _contractImpl)
+     * 
+     * This method set the actual contract implementation via a document.
+     * 
+     * Here we set the description and the usage instruction of the contract
+     * implementation. As well we prepare the transformer. The simplest form is
+     * a contract that does not need a pm. It can provide all data through the
+     * transformation. A more dynamic contract would provide properties to the
+     * transformation (contractImpl) to apply limited business logic.
+     * 
+     * @param _contractImpl
      * @throws Exception
      * @throws TransformerFactoryConfigurationError
      * @throws TransformerConfigurationException
      * @throws IllegalArgumentException
      */
-    private void contractImplHelper(Document contractImpl) throws Exception,
+    private void contractImplHelper(Document _contractImpl) throws Exception,
             TransformerFactoryConfigurationError,
             TransformerConfigurationException, IllegalArgumentException {
-        NodeList template = contractImpl
+        NodeList template = _contractImpl
                 .getElementsByTagName(CONTRACT_IMPL_ROOT_ELEMENT);
         if (template.getLength() == 1) {
             Element templateElement = (Element) template.item(0);
-            String format = templateElement.getAttribute(CONTRACT_IMPL_INPUT_FORMAT_ATTRIBUTE);
+            String format = templateElement
+                    .getAttribute(CONTRACT_IMPL_INPUT_FORMAT_ATTRIBUTE);
             if ("".equals(format) | format == null) {
-                throw new DispatcherException(DispatcherException.ERROR_500 + "\n"
-                        + "component: ContractBean" + "\n"
+                throw new DispatcherException(DispatcherException.ERROR_500
+                        + "\n" + "component: ContractBean" + "\n"
                         + "message: inputFormat cannot be null");
             } else if ("xsl".equals(format)) {
-                NodeList list_transformer = contractImpl
+                NodeList list_transformer = _contractImpl
                         .getElementsByTagName("xsl:stylesheet");
                 if (list_transformer.getLength() == 1) {
                     Element node = (Element) list_transformer.item(0);
@@ -272,59 +306,24 @@
                     this.contractTransformer = transformer;
                 }
             } else {
-                throw new DispatcherException( DispatcherException.ERROR_404+ "\n"
-                        + "component: ContractBean" + "\n"
-                        + "message: inputFormat=\""+format+"\" not implemented");
+                throw new DispatcherException(DispatcherException.ERROR_404
+                        + "\n" + "component: ContractBean" + "\n"
+                        + "message: inputFormat=\"" + format
+                        + "\" not implemented");
             }
         }
-        NodeList description = contractImpl.getElementsByTagName("description");
+        NodeList description = _contractImpl.getElementsByTagName("description");
         if (description.getLength() == 1) {
             Element node = (Element) description.item(0);
             this.contractDescription = node;
         }
-        NodeList usage = contractImpl.getElementsByTagName("usage");
+        NodeList usage = _contractImpl.getElementsByTagName("usage");
         if (usage.getLength() == 1) {
             Element node = (Element) usage.item(0);
             this.contractUsage = node;
         }
     }
 
-    public HashMap getContract() {
-        return contract;
-    }
-
-    public void put(Object key, Object element) {
-        this.contract.put(key, element);
-    }
-
-    public String getContractName() {
-        return contractName;
-    }
-
-    public void setContractName(String contractName) {
-        this.contractName = contractName;
-    }
-
-    public boolean isHasProperties() {
-        return hasProperties;
-    }
-
-    public void setHasProperties(boolean hasProperties) {
-        this.hasProperties = hasProperties;
-    }
-
-    public boolean isNugget() {
-        return isNugget;
-    }
-
-    public void setNugget(boolean isNugget) {
-        this.isNugget = isNugget;
-    }
-
-    public String getNuggetUri() {
-        return nuggetUri;
-    }
-
     /**
      * The presentation model (contractRawData) has to be requested by the
      * contract if it needs it. This is be done by setting the
@@ -336,7 +335,7 @@
      * </p>
      * 
      * <pre>
-     *                      &lt;forrest:contract name=&quot;nav-section&quot;
dataURI=&quot;cocoon://index.navigation.xml&quot;/&gt;
+     *                          &lt;forrest:contract name=&quot;nav-section&quot;
dataURI=&quot;cocoon://index.navigation.xml&quot;/&gt;
      * </pre>
      * 
      * @param nuggetUri
@@ -348,10 +347,6 @@
         this.contractRawData = rawData;
     }
 
-    public Node getContractRawData() {
-        return contractRawData;
-    }
-
     /**
      * This method invokes the transformation of the this.contractRawData with
      * the this.contractTransformer (make sure you set them before). The result
@@ -359,19 +354,72 @@
      * 
      * @throws TransformerException
      */
-    public void setContractResultData() throws TransformerException {
+    public void setContractResultData() throws DispatcherException {
         if (this.getContractRawData() == null
                 || this.contractTransformer == null) {
-            throw new TransformerException(
-                    "You need to invoke first the transfomer and the rawData.");
+            throw new DispatcherException(
+                    DispatcherException.ERROR_500
+                            + "\n"
+                            + "component: ContractBean"
+                            + "\n"
+                            + "message: Could not transform the result data in contractBean."
+                            + "\n"
+                            + "You need to invoke first the transfomer and the rawData.");
         } else {
             DOMSource source = new DOMSource(contractRawData);
             DOMResult result = new DOMResult();
-            this.contractTransformer.transform(source, result);
+            try {
+                this.contractTransformer.transform(source, result);
+            } catch (TransformerException e) {
+                throw new DispatcherException(
+                        DispatcherException.ERROR_500
+                                + "\n"
+                                + "component: ContractBean"
+                                + "\n"
+                                + "message: Could not transform the result data in contractBean."
+                                + "\n"
+                                + "While trying to transform the raw data with the transformer,
following error was thrown:\n"
+                                + e);
+            }
             this.setContractResultData(result);
         }
     }
 
+    /*
+     * Simple getter and setter methods
+     */
+    public String getContractName() {
+        return contractName;
+    }
+
+    public void setContractName(String contractName) {
+        this.contractName = contractName;
+    }
+
+    public boolean isHasProperties() {
+        return hasProperties;
+    }
+
+    public void setHasProperties(boolean hasProperties) {
+        this.hasProperties = hasProperties;
+    }
+
+    public boolean isNugget() {
+        return isNugget;
+    }
+
+    public void setNugget(boolean isNugget) {
+        this.isNugget = isNugget;
+    }
+
+    public String getNuggetUri() {
+        return nuggetUri;
+    }
+
+    public Node getContractRawData() {
+        return contractRawData;
+    }
+
     public void setContractRawData(Document contractRawData) {
         this.contractRawData = contractRawData;
     }
@@ -384,14 +432,6 @@
         this.contractTransformer = contractTransformer;
     }
 
-    public Element getStructurerContract() {
-        return structurerContract;
-    }
-
-    public void setStructurerContract(Element structurerContract) {
-        this.structurerContract = structurerContract;
-    }
-
     public Document getContractImpl() {
         return contractImpl;
     }
@@ -428,26 +468,4 @@
         this.contractResultData = contractResultData;
     }
 
-    public void recycle() {
-        this.contract = null;
-        this.contractDescription = null;
-        this.contractImpl = null;
-        this.contractName = null;
-        this.contractRawData = null;
-        this.contractResultData = null;
-        this.contractTransformer = null;
-        this.contractUsage = null;
-        this.dispatcherHelper = null;
-        this.hasProperties = false;
-        this.isNugget = false;
-        this.namespaceHelper = null;
-        this.nuggetUri = null;
-        this.propertyList = null;
-        this.structurerContract = null;
-    }
-
-    public void initialize() throws ParserConfigurationException {
-        this.contract = new HashMap();
-        dispatcherHelper = new DispatcherHelper(this.manager);
-    }
 }

Modified: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
URL: http://svn.apache.org/viewcvs/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java?rev=350221&r1=350220&r2=350221&view=diff
==============================================================================
--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
(original)
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/DispatcherHelper.java
Thu Dec  1 02:21:35 2005
@@ -28,7 +28,11 @@
 import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.apache.excalibur.source.SourceNotFoundException;
 import org.apache.lenya.xml.NamespaceHelper;
+import org.w3c.dom.DOMException;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 
 public class DispatcherHelper extends Beans {
@@ -87,6 +91,23 @@
         builder.endDocument();
         return builder.getDocument();
 
+    }
+    
+    /**
+     * void setAttributesDOM(Attributes attr, Node xpathNode)
+     * sets all attributes to the node (like <xsl:copy-of select="@*"/>)
+     * 
+     * @param attr
+     * @param xpathNode
+     * @throws DOMException
+     */
+    public void setAttributesDOM(Attributes attr, Node xpathNode)
+            throws DOMException {
+        for (int i = 0; i < attr.getLength(); i++) {
+            String localName = attr.getLocalName(i);
+            String value = attr.getValue(i);
+            ((Element) xpathNode).setAttribute(localName, value);
+        }
     }
 
     public DispatcherHelper(ServiceManager manager)

Modified: forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
URL: http://svn.apache.org/viewcvs/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java?rev=350221&r1=350220&r2=350221&view=diff
==============================================================================
--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
(original)
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/transformation/DispatcherTransformer.java
Thu Dec  1 02:21:35 2005
@@ -24,10 +24,6 @@
 
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
 
 import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.parameters.Parameters;
@@ -44,24 +40,17 @@
 import org.apache.excalibur.source.SourceValidity;
 import org.apache.excalibur.xml.xpath.XPathProcessor;
 import org.apache.forrest.dispatcher.ContractBean;
+import org.apache.forrest.dispatcher.DispatcherException;
 import org.apache.forrest.dispatcher.DispatcherHelper;
 import org.apache.lenya.xml.NamespaceHelper;
-import org.apache.xpath.CachedXPathAPI;
-import org.apache.xpath.XPathAPI;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Document;
-import org.w3c.dom.DocumentFragment;
-import org.w3c.dom.DocumentType;
 import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.DocumentBuilder;
-import org.w3c.dom.DOMImplementation;
 
 public class DispatcherTransformer extends AbstractSAXTransformer implements
         Disposable, CacheableProcessingComponent {
@@ -73,7 +62,7 @@
      * format for the request.
      * 
      * <pre>
-     *                          &lt;&lt;strong&gt;forrest:view&lt;/strong&gt;
format=&quot;html&quot;/&gt;
+     *                                              &lt;&lt;strong&gt;forrest:view&lt;/strong&gt;
format=&quot;html&quot;/&gt;
      * </pre>
      */
     public static final String STRUCTURER_ELEMENT = "view";
@@ -85,7 +74,7 @@
      * <strong>format</strong>.
      * 
      * <pre>
-     *                          &lt;forrest:view &lt;strong&gt;format&lt;/strong&gt;=&quot;html&quot;/&gt;
+     *                                              &lt;forrest:view &lt;strong&gt;format&lt;/strong&gt;=&quot;html&quot;/&gt;
      * </pre>
      */
     public static final String STRUCTURER_FORMAT_ATTRIBUTE = "type";
@@ -102,7 +91,7 @@
      * layout via e.g. css. In html for example
      * 
      * <pre>
-     *                          &lt;forrest:hook name=&quot;test&quot;/&gt;
+     *                                              &lt;forrest:hook name=&quot;test&quot;/&gt;
      * </pre>
      * 
      * <p>
@@ -110,7 +99,7 @@
      * </p>
      * 
      * <pre>
-     *                          &lt;div id=&quot;test&quot;/&gt;
+     *                                              &lt;div id=&quot;test&quot;/&gt;
      * </pre>
      */
     public static final String DISPATCHER_HOOK_ELEMENT = "hook";
@@ -120,7 +109,7 @@
      * css. In html for example
      * 
      * <pre>
-     *                          &lt;forrest:css url=&quot;pelt.basic.css&quot;
media=&quot;screen&quot; theme=&quot;Pelt&quot;/&gt;
+     *                                              &lt;forrest:css url=&quot;pelt.basic.css&quot;
media=&quot;screen&quot; theme=&quot;Pelt&quot;/&gt;
      * </pre>
      * 
      * <p>
@@ -128,25 +117,11 @@
      * </p>
      * 
      * <pre>
-     *                          &lt;link media=&quot;screen&quot; href=&quot;../themes/pelt.basic.css&quot;
title=&quot;Pelt&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;
/&gt;
+     *                                              &lt;link media=&quot;screen&quot;
href=&quot;../themes/pelt.basic.css&quot; title=&quot;Pelt&quot; rel=&quot;stylesheet&quot;
type=&quot;text/css&quot; /&gt;
      * </pre>
      */
     public static final String DISPATCHER_CSS_ELEMENT = "css";
 
-    /**
-     * Each contract can have properties, which are definite e.g. in the
-     * structurer index.fv and used in the contract.
-     * 
-     * <pre>
-     *                          &lt;forrest:contract name=&quot;nav-main-testing&quot;
nugget=&quot;cocoon://index.navigation.xml&quot;&gt;
-     *                           &lt;forrest:property name=&quot;nav-main-testing-test1&quot;
&gt;Just a test&lt;/forrest:property&gt;
-     *                          &lt;/forrest:contract&gt;
-     * </pre>
-     */
-    public static final String CONTRACT_PROPERTY_ELEMENT = "property";
-
-    public static final String CONTRACT_PROPERTY_ID_ATTRIBUTE = "name";
-
     private String propertyID;
 
     private boolean insideProperties = false;
@@ -159,8 +134,6 @@
 
     protected String currentFormat;
 
-    private boolean insideContract = false;
-
     protected ContractBean contract;
 
     /**
@@ -179,16 +152,10 @@
 
     private DOMBuilder builder, dispatcherBuilder;
 
-    private DOMUtil domUtil;
-
-    private DocumentFragment frag;
-
     private Element rootNode;
 
     private XPathProcessor processor;
 
-    private CachedXPathAPI xpathApi;
-
     private Document document;
 
     private NamespaceHelper dispatcher;
@@ -201,7 +168,7 @@
 
     private DispatcherHelper dispatcherHelper;
 
-    private HashMap domHash;
+    private boolean insideStructurer=false;
 
     /**
      * Constructor Set the namespace
@@ -266,27 +233,15 @@
             Parameters par) throws ProcessingException, SAXException,
             IOException {
         super.setup(resolver, objectModel, src, par);
-        // domUtil = new DOMUtil();
-        /*
-         * DOMImplementation domImpl = new
-         * org.apache.xerces.dom.DOMImplementationImpl(); DocumentType docType =
-         * domImpl.createDocumentType("rootElementName", "public ID", "system
-         * ID"); Document doc = domImpl.createDocument("", "rootElementName",
-         * docType);
-         * 
-         */
         this.processor = null;
         this.dispatcherHelper = null;
-        this.xpathApi = null;
         this.contract = null;
         try {
-            this.domHash = new HashMap();
-            this.xpathApi = new CachedXPathAPI();
             this.dispatcherHelper = new DispatcherHelper(manager);
             this.processor = (XPathProcessor) this.manager
                     .lookup(XPathProcessor.ROLE);
         } catch (Exception e) {
-            String error = " dispatcherError:\n Could not set up the dispatcherHelper!\n
DispatcherStack: "
+            String error = "dispatcherError:\n Could not set up the dispatcherHelper!\n DispatcherStack:
"
                     + e;
             getLogger().error(error);
             throw new ProcessingException(error);
@@ -296,7 +251,8 @@
         this.requestedFormat = parameters.getParameter(
                 STRUCTURER_FORMAT_ATTRIBUTE, null);
         if (requestedFormat == null) {
-            String error = " dispatcherError:\n You have to set the \"type\" parameter in
the sitemap!";
+            String error = "dispatcherError:\n"
+                    + "You have to set the \"type\" parameter in the sitemap!";
             getLogger().error(error);
             throw new ProcessingException(error);
         }
@@ -306,19 +262,19 @@
             Attributes attr) throws SAXException {
         // Process start element event
         // Are we inside of properties? If so we need to record the elements.
-        if (this.insideProperties) {
-            if (this.includeNodes) {
-                try {
-                    this.builder.startElement(uri, name, raw, attr);
-                } catch (SAXException e) {
-                    this.insideProperties = false;
-                    String error = " dispatcherError:\n The contract \""
-                            + contract.getContractName()
-                            + "\" has thrown in the property with ID \""
-                            + this.propertyID + "\" following error: " + e;
-                    getLogger().error(error);
-                    throw new SAXException(error);
-                }
+        if (this.insideProperties & this.includeNodes) {
+            try {
+                this.builder.startElement(uri, name, raw, attr);
+            } catch (SAXException e) {
+                this.insideProperties = false;
+                String error = "dispatcherError: "
+                        + DispatcherException.ERROR_500 + "\n"
+                        + "The contract \"" + contract.getContractName()
+                        + "\" has thrown in the property with ID \""
+                        + this.propertyID
+                        + "\" an error.\n\n DispatcherStack:\n " + e;
+                getLogger().error(error);
+                throw new SAXException(error);
             }
         } else if (DispatcherHelper.DISPATCHER_NAMESPACE_URI.equals(uri)) {
             /*
@@ -329,24 +285,19 @@
             }
             if (STRUCTURER_ELEMENT.equals(name))
                 structurerProcessingStart(attr);
-            else if (DISPATCHER_HOOK_ELEMENT.equals(name)) {
-                if (this.includeNodes) {
-                    hookProcessingStart(name, raw, attr);
-                }
-            } else if (ContractBean.CONTRACT_ELEMENT.equals(name)) {
-                if (this.includeNodes) {
-                    contractProcessingStart(attr);
-                }
-            } else if (CONTRACT_PROPERTY_ELEMENT.equals(name)) {
+            else if (DISPATCHER_HOOK_ELEMENT.equals(name) & this.includeNodes)
+                hookProcessingStart(name, raw, attr);
+            else if (ContractBean.CONTRACT_ELEMENT.equals(name)
+                    & this.includeNodes)
+                contractProcessingStart(attr);
+            else if (ContractBean.PROPERTY_ELEMENT.equals(name)
+                    & this.includeNodes) {
                 this.insideProperties = true;
-                if (this.includeNodes)
-                    propertyProcessingStart(uri, name, raw, attr);
-            } else {
-                if (this.includeNodes)
-                    super.startElement(uri, name, raw, attr);
+                propertyProcessingStart(uri, name, raw, attr);
             }
         } else {
-            super.startElement(uri, name, raw, attr);
+            if (!this.insideProperties & this.includeNodes)
+                super.startElement(uri, name, raw, attr);
         }
     }
 
@@ -361,7 +312,7 @@
             throws DOMException, SAXException {
         /* create a DOM node from the current sax event */
         Element currentElement = dispatcher.getDocument().createElement(name);
-        setAttributesDOM(attr, currentElement);
+        dispatcherHelper.setAttributesDOM(attr, currentElement);
         if (path == null || path.equals("")) {
             path = raw;
             this.rootNode.appendChild(currentElement);
@@ -378,8 +329,9 @@
                 else
                     xpathNode.appendChild(currentElement);
             } catch (Exception e) {
-                String error = " dispatcherError:\n Could not set up xpath!\n\n DispatcherStack:\n
"
-                        + e;
+                String error = "dispatcherError: "
+                        + DispatcherException.ERROR_500 + "\n"
+                        + "Could not set up xpath!\n\n DispatcherStack:\n " + e;
                 getLogger().error(error);
                 throw new SAXException(error);
             }
@@ -399,7 +351,7 @@
         xpathNode = DOMUtil
                 .selectSingleNode(rootNode, tempPath, this.processor);
         if (attr != null)
-            setAttributesDOM(attr, xpathNode);
+            dispatcherHelper.setAttributesDOM(attr, xpathNode);
     }
 
     /**
@@ -416,20 +368,6 @@
         return xpathNode;
     }
 
-    /**
-     * @param attr
-     * @param xpathNode
-     * @throws DOMException
-     */
-    private void setAttributesDOM(Attributes attr, Node xpathNode)
-            throws DOMException {
-        for (int i = 0; i < attr.getLength(); i++) {
-            String localName = attr.getLocalName(i);
-            String value = attr.getValue(i);
-            ((Element) xpathNode).setAttribute(localName, value);
-        }
-    }
-
     public void endElement(String uri, String name, String raw)
             throws SAXException {
         if (getLogger().isDebugEnabled()) {
@@ -439,24 +377,19 @@
         if (this.insideProperties) {
             propertyProcessingEnd(uri, name, raw);
         } else if (DispatcherHelper.DISPATCHER_NAMESPACE_URI.equals(uri)) {
-            if (STRUCTURER_ELEMENT.equals(name)) {
+            if (STRUCTURER_ELEMENT.equals(name))
                 structurerProcessingEnd(raw);
-            } else if (ContractBean.CONTRACT_ELEMENT.equals(name)) {
-                if (this.includeNodes)
-                    contractProcessingEnd();
-            } else if (DISPATCHER_HOOK_ELEMENT.equals(name)) {
-                if (this.includeNodes) {
-                    if (path.lastIndexOf("/") > -1)
-                        path = path.substring(0, path.lastIndexOf("/"));
-                    else
-                        path = null;
-                }
-            } else {
-                if (this.includeNodes)
-                    super.endElement(uri, name, raw);
-            }
+            else if (ContractBean.CONTRACT_ELEMENT.equals(name)
+                    & this.includeNodes)
+                contractProcessingEnd();
+            else if (DISPATCHER_HOOK_ELEMENT.equals(name) & this.includeNodes)
+                if (path.lastIndexOf("/") > -1)
+                    path = path.substring(0, path.lastIndexOf("/"));
+                else
+                    path = null;
         } else {
-            super.endElement(uri, name, raw);
+            if (!this.insideProperties & this.includeNodes)
+                super.endElement(uri, name, raw);
         }
     }
 
@@ -486,6 +419,7 @@
             if (path == null)
                 path = "result/";
             this.includeNodes = true;
+            this.insideStructurer=true;
             this.recording = true;
             try {
                 dispatcherHelper.setNamespaceHelper(
@@ -501,11 +435,14 @@
                 // we create the path node for the result node
                 DOMUtil.selectSingleNode(rootNode, path, this.processor);
             } catch (Exception e) {
-                String error = " dispatcherError:\n could not setup dispatcherHelper! \""
-                        + "\n dispatcherErrorStack:\n" + e;
-                getLogger().error(error + e);
+                String error = "dispatcherError: "
+                        + DispatcherException.ERROR_500
+                        + "\n"
+                        + "Could not setup dispatcherHelper!\n\n DispatcherStack:\n "
+                        + e;
+                getLogger().error(error);
                 this.recording = false;
-                throw new SAXException(error + e);
+                throw new SAXException(error);
             }
 
         } else {
@@ -524,6 +461,7 @@
             XMLUtils.valueOf(new IncludeXMLConsumer(super.xmlConsumer),
                     this.rootNode);
             this.recording = false;
+            this.insideStructurer=false;
         }
         if (getLogger().isDebugEnabled()) {
             getLogger().debug(
@@ -539,15 +477,18 @@
      * @throws SAXException
      */
     private void contractProcessingStart(Attributes attr) throws SAXException {
-        this.insideContract = true;
         try {
             if (contract == null)
                 contract = new ContractBean(this.manager);
             else
                 contract.initialize();
         } catch (ParserConfigurationException e) {
-            throw new SAXException("originally ParserConfigurationException"
-                    + e);
+            String error = DispatcherException.ERROR_500 + "\n"
+                    + "component: ContractBean" + "\n"
+                    + "message: Could not setup contractBean." + "\n" + "\n\n"
+                    + "dispatcherErrorStack:\n" + e;
+            getLogger().error(error);
+            throw new SAXException(error);
         }
         for (int i = 0; i < attr.getLength(); i++) {
             String localName = attr.getLocalName(i);
@@ -563,12 +504,16 @@
                     contract.setContractImpl(doc);
                     // contract.setContractImpl(contractUri);
                 } catch (Exception e) {
-                    String error = " dispatcherError:\n The contract \""
+                    String error = "dispatcherError: "
+                            + DispatcherException.ERROR_500
+                            + "\n"
+                            + "The contract \""
                             + contract.getContractName()
-                            + "\" has thrown following errorStack by resolving the implementation
from \""
-                            + contractUri + "\".\n dispatcherErrorStack:\n";
-                    getLogger().error(error + e);
-                    throw new SAXException(error + e);
+                            + "\" has thrown an exception by resolving the implementation
from \""
+                            + contractUri + "\".\n\n"
+                            + "dispatcherErrorStack:\n" + e;
+                    getLogger().error(error);
+                    throw new SAXException(error);
                 }
                 if (getLogger().isDebugEnabled()) {
                     getLogger().debug(
@@ -585,12 +530,16 @@
                     contract.setContractRawData(doc);
                     // contract.setNuggetUri(value);
                 } catch (Exception e) {
-                    String error = " dispatcherError:\n The contract \""
+                    String error = "dispatcherError: "
+                            + DispatcherException.ERROR_500
+                            + "\n"
+                            + "The contract \""
                             + contract.getContractName()
-                            + "\" has thrown following errorStack by resolving raw data from
\""
-                            + value + "\".\n dispatcherErrorStack:\n ";
-                    getLogger().error(error + e);
-                    throw new SAXException(error + e);
+                            + "\" has thrown thrown an exception by resolving raw data from
\""
+                            + value + "\".\n\n" + "dispatcherErrorStack:\n "
+                            + e;
+                    getLogger().error(error);
+                    throw new SAXException(error);
                 }
                 if (getLogger().isDebugEnabled()) {
                     getLogger().debug(
@@ -616,19 +565,16 @@
                 foo = dispatcherHelper.createDocument("foo");
                 contract.setContractRawData(foo);
             } catch (Exception e) {
-                String error = " dispatcherError:\n The contract \""
+                String error = "dispatcherError: "
+                        + DispatcherException.ERROR_500
+                        + "\n"
+                        + "The contract \""
                         + contract.getContractName()
-                        + "\" has thrown following errorStack by creating the dummy foo document."
-                        + ".\n dispatcherErrorStack:\n ";
-                getLogger().error(error + e);
-                throw new SAXException(error + e);
+                        + "\" has thrown thrown an exception by creating the dummy foo document."
+                        + ".\n\n" + "dispatcherErrorStack:\n " + e;
+                getLogger().error(error);
+                throw new SAXException(error);
             }
-            /*
-             * Document fooDoc =// (Document)
-             * dispatcherHelper.getNamespaceHelper().getDocument();
-             * fooDoc.createElement("foo");
-             * contract.getFooData().getOwnerDocument();
-             */
         }
     }
 
@@ -699,14 +645,16 @@
                 }
             }
         } catch (Exception e) {
-            String error = " dispatcherError:\n The contract \""
+            String error = "dispatcherError: "
+                    + DispatcherException.ERROR_500
+                    + "\n"
+                    + "The contract \""
                     + contract.getContractName()
-                    + "\" has thrown following error while trying to transform the final
markup: "
-                    + e;
+                    + "\" has thrown thrown an exception while trying to transform the final
markup. \n\n"
+                    + "dispatcherErrorStack:\n " + e;
             getLogger().error(error);
             throw new SAXException(error);
         } finally {
-            this.insideContract = false;
             this.contract.recycle();
         }
     }
@@ -723,15 +671,14 @@
         for (int i = 0; i < attr.getLength(); i++) {
             String localName = attr.getLocalName(i);
             String value = attr.getValue(i);
-            if (CONTRACT_PROPERTY_ID_ATTRIBUTE.equals(localName)) {
+            if (ContractBean.PROPERTY_ID_ATTRIBUTE.equals(localName)) 
                 this.propertyID = value;
-            }
         }
         if (this.propertyID.equals("") | this.propertyID == null) {
-            String error = " dispatcherError:\n The contract \""
-                    + contract.getContractName()
+            String error = "dispatcherError: " + DispatcherException.ERROR_500
+                    + "\n" + "The contract \"" + contract.getContractName()
                     + "\" has no identifier attribute \""
-                    + CONTRACT_PROPERTY_ID_ATTRIBUTE + "\" in the " + raw;
+                    + ContractBean.PROPERTY_ID_ATTRIBUTE + "\" in the " + raw;
             getLogger().error(error);
             throw new SAXException(error);
         }
@@ -749,7 +696,7 @@
      */
     private void propertyProcessingEnd(String uri, String name, String raw)
             throws SAXException {
-        if (CONTRACT_PROPERTY_ELEMENT.equals(name)) {
+        if (ContractBean.PROPERTY_ELEMENT.equals(name)) {
             this.insideProperties = false;
             if (this.includeNodes) {
                 this.builder.endElement(uri, name, raw);
@@ -779,33 +726,24 @@
     }
 
     public void characters(char c[], int start, int len) throws SAXException {
-        if (this.insideProperties) {
-            if (this.includeNodes)
-                this.builder.characters(c, start, len);
-        } else {
-            if (this.includeNodes)
-                super.contentHandler.characters(c, start, len);
-        }
+        if (this.insideProperties & this.includeNodes)
+            this.builder.characters(c, start, len);
+        else if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
+            super.contentHandler.characters(c, start, len);
     }
 
     public void startCDATA() throws SAXException {
-        if (this.insideProperties) {
-            if (this.includeNodes)
-                this.builder.startCDATA();
-        } else {
-            if (this.includeNodes)
-                super.lexicalHandler.startCDATA();
-        }
+        if (this.insideProperties & this.includeNodes)
+            this.builder.startCDATA();
+        else if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
+            super.lexicalHandler.startCDATA();
     }
 
     public void endCDATA() throws SAXException {
-        if (this.insideProperties) {
-            if (this.includeNodes)
-                this.builder.endCDATA();
-        } else {
-            if (this.includeNodes)
-                super.lexicalHandler.endCDATA();
-        }
+        if (this.insideProperties & this.includeNodes)
+            this.builder.endCDATA();
+        else if (!this.insideProperties & this.includeNodes&!this.insideStructurer)
+            super.lexicalHandler.endCDATA();
     }
 
     /** BEGIN SAX ContentHandler handlers * */
@@ -813,9 +751,8 @@
     public void startPrefixMapping(String prefix, String uri)
             throws SAXException {
         super.startPrefixMapping(prefix, uri);
-        if (this.insideProperties) {
-            if (this.includeNodes)
-                this.builder.startPrefixMapping(prefix, uri);
+        if (this.insideProperties & this.includeNodes) {
+            this.builder.startPrefixMapping(prefix, uri);
         } else {
             storePrefixMapping(prefix, uri);
         }



Mime
View raw message