# HG changeset patch # User Ingo Weinzierl # Date 1328006094 0 # Node ID 1227878665b52743a6f59a96259443b07068192e # Parent eeee85c4d996137e1b66b483aa613e5baacc738d Introduced a DataFactory that should be used to create new Data instances. flys-client/trunk@3836 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r eeee85c4d996 -r 1227878665b5 flys-client/ChangeLog --- a/flys-client/ChangeLog Mon Jan 30 14:36:54 2012 +0000 +++ b/flys-client/ChangeLog Tue Jan 31 10:34:54 2012 +0000 @@ -1,3 +1,12 @@ +2012-01-31 Ingo Weinzierl + + * src/main/java/de/intevation/flys/client/server/DataFactory.java: New. + This class should be used to create new Data instances. + + * src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java: + Use DataFactory to create new Data instances for the old Data objects + contained in the static ui part of the Artifact's DESCRIBE document. + 2012-01-30 Raimund Renkert * src/main/java/de/intevation/flys/client/client/ui/chart/ManualPointsEditor.java: diff -r eeee85c4d996 -r 1227878665b5 flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java --- a/flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java Mon Jan 30 14:36:54 2012 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java Tue Jan 31 10:34:54 2012 +0000 @@ -373,14 +373,7 @@ for (int j = 0; j < size; j++) { Node dataNode = dataNodes.item(j); - String dName = XMLUtils.xpathString( - dataNode, "@art:name", ArtifactNamespaceContext.INSTANCE); - String dType = XMLUtils.xpathString( - dataNode, "@art:type", ArtifactNamespaceContext.INSTANCE); - - DataItem[] items = extractOldDataItems(dataNode); - - list.add(new DefaultData(dName, dName, dType, items)); + list.add(DataFactory.createDataFromElement((Element) dataNode)); data[i] = list; } @@ -391,45 +384,6 @@ /** - * This method extracts the data items from the data nodes that are placed - * in the static ui part of the DESCRIBE document. - * - * @param dataNode A data node that contains items. - * - * @return a list of DataItems. - */ - protected static DataItem[] extractOldDataItems(Node dataNode) { - NodeList itemList = (NodeList) XMLUtils.xpath( - dataNode, - XPATH_STATIC_ITEM_NODE, - XPathConstants.NODESET, - ArtifactNamespaceContext.INSTANCE); - - if (itemList == null || itemList.getLength() == 0) { - logger.debug("No old data items found."); - return null; - } - - int count = itemList.getLength(); - - DataItem[] items = new DataItem[count]; - - for (int i = 0; i < count; i++) { - Node tmp = itemList.item(i); - - String value = XMLUtils.xpathString( - tmp, "@art:value", ArtifactNamespaceContext.INSTANCE); - String label = XMLUtils.xpathString( - tmp, "@art:label", ArtifactNamespaceContext.INSTANCE); - - items[i] = new DefaultDataItem(label, label, value); - } - - return items; - } - - - /** * This method extracts the UIProvider specified by the data node. * * @param data The data node. diff -r eeee85c4d996 -r 1227878665b5 flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java Tue Jan 31 10:34:54 2012 +0000 @@ -0,0 +1,103 @@ +package de.intevation.flys.client.server; + +import javax.xml.xpath.XPathConstants; + +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +import org.apache.log4j.Logger; + +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.flys.client.shared.model.Data; +import de.intevation.flys.client.shared.model.DataItem; +import de.intevation.flys.client.shared.model.DefaultData; +import de.intevation.flys.client.shared.model.DefaultDataItem; + + +/** + * @author Ingo Weinzierl + */ +public class DataFactory { + + private static final Logger logger = Logger.getLogger(DataFactory.class); + + + public static final String NS_URI = ArtifactNamespaceContext.NAMESPACE_URI; + + + /** + * Creates a new Data instance based on the art:type attribute of + * element. + * + * @param element The Data element. + * + * @return a Data instance. + */ + public static Data createDataFromElement(Element element) { + String name = element.getAttributeNS(NS_URI, "name"); + String type = element.getAttributeNS(NS_URI, "type"); + + logger.debug("Create new Data instance for: " + name + " | " + type); + + if (type == null || type.length() == 0) { + return createDefaultData(element, name); + } + else { + return createDefaultData(element, name); + } + } + + + /** + * This method creates a new instance of DefaultData which has no real type + * set. + * + * @param ele The Data element. + * @param name The name of the Data instance. + * + * @return an instance of DefaultData. + */ + protected static Data createDefaultData(Element ele, String name) { + logger.debug("Create new DefaultData"); + return new DefaultData(name, name, "default", extractDataItems(ele)); + } + + + /** + * This method extracts the art:item elements placed under elements. + * + * @param element A data node that contains items. + * + * @return a list of DataItems. + */ + protected static DataItem[] extractDataItems(Element element) { + NodeList itemList = (NodeList) XMLUtils.xpath( + element, + "art:item", + XPathConstants.NODESET, + ArtifactNamespaceContext.INSTANCE); + + if (itemList == null || itemList.getLength() == 0) { + logger.debug("No old data items found."); + return null; + } + + int count = itemList.getLength(); + + DataItem[] items = new DataItem[count]; + + for (int i = 0; i < count; i++) { + Element tmp = (Element) itemList.item(i); + + String value = tmp.getAttributeNS(NS_URI, "value"); + String label = tmp.getAttributeNS(NS_URI, "label"); + + items[i] = new DefaultDataItem(label, label, value); + } + + return items; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :