/[Apache-SVN]/forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java
ViewVC logotype

Diff of /forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java	2005/12/01 09:41:11	350220
+++ forrest/trunk/whiteboard/plugins/org.apache.forrest.plugin.internal.structurer/src/java/org/apache/forrest/dispatcher/ContractBean.java	2005/12/01 10:21:35	350221
@@ -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 @@ public class ContractBean extends Beans
      * </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 @@ public class ContractBean extends Beans
     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 class ContractBean extends Beans
     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 @@ public class ContractBean extends Beans
                     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 @@ public class ContractBean extends Beans
      * </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 @@ public class ContractBean extends Beans
         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 @@ public class ContractBean extends Beans
      * 
      * @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 @@ public class ContractBean extends Beans
         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 @@ public class ContractBean extends Beans
         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);
-    }
 }

 

infrastructure at apache.org
ViewVC Help
Powered by ViewVC 1.1.26