teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.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: teichmann@5831: import org.dive4elements.artifactdatabase.data.DefaultStateData; teichmann@5831: import org.dive4elements.artifactdatabase.state.State; ingo@108: teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; ingo@108: ingo@108: ingo@108: /** ingo@108: * @author Ingo Weinzierl ingo@108: */ ingo@108: public class StateFactory { ingo@108: teichmann@8202: /** The log used in this class */ teichmann@8202: private static Logger log = 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 { teichmann@8202: log.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) { teichmann@8202: log.error(ie, ie); ingo@108: } ingo@108: catch (IllegalAccessException iae) { teichmann@8202: log.error(iae, iae); ingo@108: } ingo@108: catch (ClassNotFoundException cnfe) { teichmann@8202: log.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) { teichmann@8202: log.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: teichmann@8202: log.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: sascha@3453: if (name == null || name.length() == 0) { teichmann@8202: log.warn("No name for data item at pos " + i + " found."); ingo@113: continue; ingo@113: } ingo@113: sascha@3453: if (type == null || type.length() == 0) { teichmann@8202: log.warn("No type for data item at pos " + i + " found."); teichmann@8202: log.warn("Default type 'string' used."); ingo@113: type = "string"; ingo@113: } ingo@113: teichmann@8202: log.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 :