annotate flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/StateFactory.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StateFactory.java@e74e707ff650
children
rev   line source
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3453
diff changeset
1 package org.dive4elements.river.artifacts.states;
108
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
2
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
3 import javax.xml.xpath.XPathConstants;
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
4
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
5 import org.apache.log4j.Logger;
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
6
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
7 import org.w3c.dom.Node;
113
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
8 import org.w3c.dom.NodeList;
108
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
9
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3453
diff changeset
10 import org.dive4elements.artifactdatabase.data.DefaultStateData;
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3453
diff changeset
11 import org.dive4elements.artifactdatabase.state.State;
108
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
12
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 3453
diff changeset
13 import org.dive4elements.artifacts.common.utils.XMLUtils;
108
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
14
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
15
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
16 /**
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
17 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
18 */
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
19 public class StateFactory {
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
20
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
21 /** The logger used in this class */
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
22 private static Logger logger = Logger.getLogger(StateFactory.class);
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
23
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
24 /** The XPath to the classname of the state */
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
25 public static final String XPATH_STATE = "@state";
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
26
113
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
27 /** The XPath to the data items of the state relative to the state node. */
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
28 public static final String XPATH_DATA = "data";
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
29
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
30 /** The XPath to the data name relative to the data node.*/
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
31 public static final String XPATH_DATA_NAME = "@name";
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
32
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
33 /** The XPath to the data type relative to the data node.*/
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
34 public static final String XPATH_DATA_TYPE = "@type";
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
35
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
36 /** The XPath to the data description relative to the data node.*/
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
37 public static final String XPATH_DATA_DESCRIPTION = "@description";
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
38
108
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
39
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
40 /**
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
41 * Creates a new State based on the configured class provided by
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
42 * <code>stateConf</code>.
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
43 *
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
44 * @param stateConf The configuration of the state.
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
45 *
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
46 * @return a State.
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
47 */
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
48 public static State createState(Node stateConf) {
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
49 String clazz = (String) XMLUtils.xpath(
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
50 stateConf, XPATH_STATE, XPathConstants.STRING);
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
51
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
52 State state = null;
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
53
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
54 try {
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
55 logger.debug("Create a new State for class: " + clazz);
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
56 state = (State) Class.forName(clazz).newInstance();
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
57 state.setup(stateConf);
113
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
58
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
59 initializeStateData(state, stateConf);
108
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
60 }
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
61 catch (InstantiationException ie) {
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
62 logger.error(ie, ie);
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
63 }
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
64 catch (IllegalAccessException iae) {
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
65 logger.error(iae, iae);
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
66 }
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
67 catch (ClassNotFoundException cnfe) {
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
68 logger.error(cnfe, cnfe);
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
69 }
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
70
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
71 return state;
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
72 }
113
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
73
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
74
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
75 /**
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
76 * This method extracts the configured input data of a state and adds new
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
77 * StateData objects to the State.
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
78 *
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
79 * @param state The state.
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
80 * @param stateConf The state configuration node.
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
81 */
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
82 protected static void initializeStateData(State state, Node stateConf) {
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
83 NodeList dataList = (NodeList) XMLUtils.xpath(
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
84 stateConf, XPATH_DATA, XPathConstants.NODESET);
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
85
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
86 if (dataList == null || dataList.getLength() == 0) {
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
87 logger.debug("The state has no input data configured.");
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
88
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
89 return;
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
90 }
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
91
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
92 int items = dataList.getLength();
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
93
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
94 logger.debug("The state has " + items + " data items configured.");
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
95
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
96 for (int i = 0; i < items; i++) {
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
97 Node data = dataList.item(i);
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
98
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
99 String name = (String) XMLUtils.xpath(
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
100 data, XPATH_DATA_NAME, XPathConstants.STRING);
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
101 String type = (String) XMLUtils.xpath(
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
102 data, XPATH_DATA_TYPE, XPathConstants.STRING);
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
103 String desc = (String) XMLUtils.xpath(
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
104 data, XPATH_DATA_DESCRIPTION, XPathConstants.STRING);
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
105
3453
e74e707ff650 Replace string comparison with empty string with length equals zero comparison
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2204
diff changeset
106 if (name == null || name.length() == 0) {
113
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
107 logger.warn("No name for data item at pos " + i + " found.");
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
108 continue;
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
109 }
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
110
3453
e74e707ff650 Replace string comparison with empty string with length equals zero comparison
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2204
diff changeset
111 if (type == null || type.length() == 0) {
113
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
112 logger.warn("No type for data item at pos " + i + " found.");
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
113 logger.warn("Default type 'string' used.");
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
114 type = "string";
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
115 }
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
116
2204
45f7762767e2 Bugfix: use correct order of parameters to call DefaultStateData constructor.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 113
diff changeset
117 logger.debug("add StateData '" + name + "' (type '" + type + "')");
45f7762767e2 Bugfix: use correct order of parameters to call DefaultStateData constructor.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 113
diff changeset
118 state.addData(name, new DefaultStateData(name, desc, type));
113
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
119 }
f077df8ad54c The input data items of a state are read from configuration after the state has been setup.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 108
diff changeset
120 }
108
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
121 }
50273a391e53 Added missing StateFactory class of the last commit.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
122 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org