ingo@108: package de.intevation.flys.artifacts.states;
ingo@108:
ingo@108: import javax.xml.xpath.XPathConstants;
ingo@108:
ingo@108: import org.apache.log4j.Logger;
ingo@108:
ingo@108: import org.w3c.dom.Node;
ingo@113: import org.w3c.dom.NodeList;
ingo@108:
ingo@113: import de.intevation.artifactdatabase.data.DefaultStateData;
ingo@108: import de.intevation.artifactdatabase.state.State;
ingo@108:
ingo@108: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@108:
ingo@108:
ingo@108: /**
ingo@108: * @author Ingo Weinzierl
ingo@108: */
ingo@108: public class StateFactory {
ingo@108:
ingo@108: /** The logger used in this class */
ingo@108: private static Logger logger = Logger.getLogger(StateFactory.class);
ingo@108:
ingo@108: /** The XPath to the classname of the state */
ingo@108: public static final String XPATH_STATE = "@state";
ingo@108:
ingo@113: /** The XPath to the data items of the state relative to the state node. */
ingo@113: public static final String XPATH_DATA = "data";
ingo@113:
ingo@113: /** The XPath to the data name relative to the data node.*/
ingo@113: public static final String XPATH_DATA_NAME = "@name";
ingo@113:
ingo@113: /** The XPath to the data type relative to the data node.*/
ingo@113: public static final String XPATH_DATA_TYPE = "@type";
ingo@113:
ingo@113: /** The XPath to the data description relative to the data node.*/
ingo@113: public static final String XPATH_DATA_DESCRIPTION = "@description";
ingo@113:
ingo@108:
ingo@108: /**
ingo@108: * Creates a new State based on the configured class provided by
ingo@108: * stateConf
.
ingo@108: *
ingo@108: * @param stateConf The configuration of the state.
ingo@108: *
ingo@108: * @return a State.
ingo@108: */
ingo@108: public static State createState(Node stateConf) {
ingo@108: String clazz = (String) XMLUtils.xpath(
ingo@108: stateConf, XPATH_STATE, XPathConstants.STRING);
ingo@108:
ingo@108: State state = null;
ingo@108:
ingo@108: try {
ingo@108: logger.debug("Create a new State for class: " + clazz);
ingo@108: state = (State) Class.forName(clazz).newInstance();
ingo@108: state.setup(stateConf);
ingo@113:
ingo@113: initializeStateData(state, stateConf);
ingo@108: }
ingo@108: catch (InstantiationException ie) {
ingo@108: logger.error(ie, ie);
ingo@108: }
ingo@108: catch (IllegalAccessException iae) {
ingo@108: logger.error(iae, iae);
ingo@108: }
ingo@108: catch (ClassNotFoundException cnfe) {
ingo@108: logger.error(cnfe, cnfe);
ingo@108: }
ingo@108:
ingo@108: return state;
ingo@108: }
ingo@113:
ingo@113:
ingo@113: /**
ingo@113: * This method extracts the configured input data of a state and adds new
ingo@113: * StateData objects to the State.
ingo@113: *
ingo@113: * @param state The state.
ingo@113: * @param stateConf The state configuration node.
ingo@113: */
ingo@113: protected static void initializeStateData(State state, Node stateConf) {
ingo@113: NodeList dataList = (NodeList) XMLUtils.xpath(
ingo@113: stateConf, XPATH_DATA, XPathConstants.NODESET);
ingo@113:
ingo@113: if (dataList == null || dataList.getLength() == 0) {
ingo@113: logger.debug("The state has no input data configured.");
ingo@113:
ingo@113: return;
ingo@113: }
ingo@113:
ingo@113: int items = dataList.getLength();
ingo@113:
ingo@113: logger.debug("The state has " + items + " data items configured.");
ingo@113:
ingo@113: for (int i = 0; i < items; i++) {
ingo@113: Node data = dataList.item(i);
ingo@113:
ingo@113: String name = (String) XMLUtils.xpath(
ingo@113: data, XPATH_DATA_NAME, XPathConstants.STRING);
ingo@113: String type = (String) XMLUtils.xpath(
ingo@113: data, XPATH_DATA_TYPE, XPathConstants.STRING);
ingo@113: String desc = (String) XMLUtils.xpath(
ingo@113: data, XPATH_DATA_DESCRIPTION, XPathConstants.STRING);
ingo@113:
ingo@113: if (name == null || name.equals("")) {
ingo@113: logger.warn("No name for data item at pos " + i + " found.");
ingo@113: continue;
ingo@113: }
ingo@113:
ingo@113: if (type == null || type.equals("")) {
ingo@113: logger.warn("No type for data item at pos " + i + " found.");
ingo@113: logger.warn("Default type 'string' used.");
ingo@113: type = "string";
ingo@113: }
ingo@113:
ingo@2204: logger.debug("add StateData '" + name + "' (type '" + type + "')");
ingo@2204: state.addData(name, new DefaultStateData(name, desc, type));
ingo@113: }
ingo@113: }
ingo@108: }
ingo@108: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :