changeset 113:f077df8ad54c

The input data items of a state are read from configuration after the state has been setup. flys-artifacts/trunk@1302 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 07 Feb 2011 17:42:14 +0000
parents 0fab16cb4d44
children 394b9580ceae
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/winfo.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StateFactory.java
diffstat 3 files changed, 74 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Feb 07 11:37:33 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Feb 07 17:42:14 2011 +0000
@@ -1,3 +1,12 @@
+2011-02-07  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/states/StateFactory.java: The
+	  input data of a state is initialized with empty StateData objects after
+	  the State has been created.
+
+	* doc/conf/artifacts/winfo.xml: Renamed the input data nodes of the states
+	  which now fits better to the class name of the implementation.
+
 2011-02-07  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: The
--- a/flys-artifacts/doc/conf/artifacts/winfo.xml	Mon Feb 07 11:37:33 2011 +0000
+++ b/flys-artifacts/doc/conf/artifacts/winfo.xml	Mon Feb 07 17:42:14 2011 +0000
@@ -3,7 +3,7 @@
     <states>
 
         <state id="winfo_river" description="winfo_river" state="de.intevation.flys.artifacts.states.RiverSelect">
-            <inputvalue name="river" type="String" />
+            <data name="river" type="String" />
         </state>
 
         <transition transition="de.intevation.flys.artifacts.transitions.DefaultTransition">
@@ -12,7 +12,7 @@
         </transition>
 
         <state id="winfo_gauge" description="winfo_gauge" state="de.intevation.flys.artifacts.states.GaugeSelect">
-            <inputvalue name="gauge"  type="String" />
+            <data name="gauge"  type="String" />
         </state>
 
     </states>
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StateFactory.java	Mon Feb 07 11:37:33 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StateFactory.java	Mon Feb 07 17:42:14 2011 +0000
@@ -5,7 +5,9 @@
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
+import de.intevation.artifactdatabase.data.DefaultStateData;
 import de.intevation.artifactdatabase.state.State;
 
 import de.intevation.artifacts.common.utils.XMLUtils;
@@ -22,6 +24,18 @@
     /** The XPath to the classname of the state */
     public static final String XPATH_STATE = "@state";
 
+    /** The XPath to the data items of the state relative to the state node. */
+    public static final String XPATH_DATA = "data";
+
+    /** The XPath to the data name relative to the data node.*/
+    public static final String XPATH_DATA_NAME = "@name";
+
+    /** The XPath to the data type relative to the data node.*/
+    public static final String XPATH_DATA_TYPE = "@type";
+
+    /** The XPath to the data description relative to the data node.*/
+    public static final String XPATH_DATA_DESCRIPTION = "@description";
+
 
     /**
      * Creates a new State based on the configured class provided by
@@ -41,6 +55,8 @@
             logger.debug("Create a new State for class: " + clazz);
             state = (State) Class.forName(clazz).newInstance();
             state.setup(stateConf);
+
+            initializeStateData(state, stateConf);
         }
         catch (InstantiationException ie) {
             logger.error(ie, ie);
@@ -54,5 +70,52 @@
 
         return state;
     }
+
+
+    /**
+     * This method extracts the configured input data of a state and adds new
+     * StateData objects to the State.
+     *
+     * @param state The state.
+     * @param stateConf The state configuration node.
+     */
+    protected static void initializeStateData(State state, Node stateConf) {
+        NodeList dataList = (NodeList) XMLUtils.xpath(
+            stateConf, XPATH_DATA, XPathConstants.NODESET);
+
+        if (dataList == null || dataList.getLength() == 0) {
+            logger.debug("The state has no input data configured.");
+
+            return;
+        }
+
+        int items = dataList.getLength();
+
+        logger.debug("The state has " + items + " data items configured.");
+
+        for (int i = 0; i < items; i++) {
+            Node data = dataList.item(i);
+
+            String name = (String) XMLUtils.xpath(
+                data, XPATH_DATA_NAME, XPathConstants.STRING);
+            String type = (String) XMLUtils.xpath(
+                data, XPATH_DATA_TYPE, XPathConstants.STRING);
+            String desc = (String) XMLUtils.xpath(
+                data, XPATH_DATA_DESCRIPTION, XPathConstants.STRING);
+
+            if (name == null || name.equals("")) {
+                logger.warn("No name for data item at pos " + i + " found.");
+                continue;
+            }
+
+            if (type == null || type.equals("")) {
+                logger.warn("No type for data item at pos " + i + " found.");
+                logger.warn("Default type 'string' used.");
+                type = "string";
+            }
+
+            state.addData(name, new DefaultStateData(name, type, desc));
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org