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

http://dive4elements.wald.intevation.org