Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/StateFactory.java @ 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 | 50273a391e53 |
children | 45f7762767e2 |
comparison
equal
deleted
inserted
replaced
112:0fab16cb4d44 | 113:f077df8ad54c |
---|---|
3 import javax.xml.xpath.XPathConstants; | 3 import javax.xml.xpath.XPathConstants; |
4 | 4 |
5 import org.apache.log4j.Logger; | 5 import org.apache.log4j.Logger; |
6 | 6 |
7 import org.w3c.dom.Node; | 7 import org.w3c.dom.Node; |
8 import org.w3c.dom.NodeList; | |
8 | 9 |
10 import de.intevation.artifactdatabase.data.DefaultStateData; | |
9 import de.intevation.artifactdatabase.state.State; | 11 import de.intevation.artifactdatabase.state.State; |
10 | 12 |
11 import de.intevation.artifacts.common.utils.XMLUtils; | 13 import de.intevation.artifacts.common.utils.XMLUtils; |
12 | 14 |
13 | 15 |
19 /** The logger used in this class */ | 21 /** The logger used in this class */ |
20 private static Logger logger = Logger.getLogger(StateFactory.class); | 22 private static Logger logger = Logger.getLogger(StateFactory.class); |
21 | 23 |
22 /** The XPath to the classname of the state */ | 24 /** The XPath to the classname of the state */ |
23 public static final String XPATH_STATE = "@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"; | |
24 | 38 |
25 | 39 |
26 /** | 40 /** |
27 * Creates a new State based on the configured class provided by | 41 * Creates a new State based on the configured class provided by |
28 * <code>stateConf</code>. | 42 * <code>stateConf</code>. |
39 | 53 |
40 try { | 54 try { |
41 logger.debug("Create a new State for class: " + clazz); | 55 logger.debug("Create a new State for class: " + clazz); |
42 state = (State) Class.forName(clazz).newInstance(); | 56 state = (State) Class.forName(clazz).newInstance(); |
43 state.setup(stateConf); | 57 state.setup(stateConf); |
58 | |
59 initializeStateData(state, stateConf); | |
44 } | 60 } |
45 catch (InstantiationException ie) { | 61 catch (InstantiationException ie) { |
46 logger.error(ie, ie); | 62 logger.error(ie, ie); |
47 } | 63 } |
48 catch (IllegalAccessException iae) { | 64 catch (IllegalAccessException iae) { |
52 logger.error(cnfe, cnfe); | 68 logger.error(cnfe, cnfe); |
53 } | 69 } |
54 | 70 |
55 return state; | 71 return state; |
56 } | 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.equals("")) { | |
107 logger.warn("No name for data item at pos " + i + " found."); | |
108 continue; | |
109 } | |
110 | |
111 if (type == null || type.equals("")) { | |
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 state.addData(name, new DefaultStateData(name, type, desc)); | |
118 } | |
119 } | |
57 } | 120 } |
58 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 121 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |