diff flys-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java@d0a9acddbea2
children 821a02bbfb4e
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java	Thu Apr 25 12:31:32 2013 +0200
@@ -0,0 +1,341 @@
+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;
+import de.intevation.flys.client.shared.model.DoubleArrayData;
+import de.intevation.flys.client.shared.model.IntegerArrayData;
+import de.intevation.flys.client.shared.model.IntegerData;
+import de.intevation.flys.client.shared.model.IntegerOptionsData;
+import de.intevation.flys.client.shared.model.IntegerRangeData;
+import de.intevation.flys.client.shared.model.StringData;
+import de.intevation.flys.client.shared.model.StringOptionsData;
+import de.intevation.flys.client.shared.model.LongRangeData;
+
+import de.intevation.flys.client.shared.model.IntDataItem;
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+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 <i>art:type</i> attribute of
+     * <i>element</i>.
+     *
+     * @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");
+        String label = element.getAttributeNS(NS_URI, "label");
+
+        label = label != null && label.length() > 0 ? label : name;
+
+        try {
+            logger.debug("Create Data instance for: " + name + " | " + type);
+
+            if (type == null || type.length() == 0) {
+                return createDefaultData(element, name, label);
+            }
+
+            type = type.toLowerCase();
+
+            if (type.equals(StringData.TYPE)) {
+                return createStringData(element, name, label);
+            }
+            else if (type.equals(IntegerData.TYPE)) {
+                return createIntegerData(element, name, label);
+            }
+            else if (type.equals(StringOptionsData.TYPE)) {
+                return createStringOptionsData(element, name, label);
+            }
+            else if (type.equals(IntegerOptionsData.TYPE)) {
+                return createIntegerOptionsData(element, name, label);
+            }
+            else if (type.equals(IntegerRangeData.TYPE)) {
+                return createIntegerRangeData(element, name, label);
+            }
+            else if (type.equals(IntegerArrayData.TYPE)) {
+                return createIntegerArrayData(element, name, label);
+            }
+            else if (type.equals(DoubleArrayData.TYPE)) {
+                return createDoubleArrayData(element, name, label);
+            }
+            else if (type.equals(LongRangeData.TYPE)) {
+                return createLongRangeData(element, name, label);
+            }
+            else {
+                return createDefaultData(element, name, label);
+            }
+        }
+        catch (Exception e) {
+            logger.error("Error while data creation for: " + name);
+        }
+
+        return null;
+    }
+
+
+    /**
+     * 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, String label) {
+        logger.debug("Create new DefaultData");
+        return new DefaultData(name, label, "default", extractDataItems(ele));
+    }
+
+
+    /**
+     * This method creates a new instance of StringData which has a type
+     * "string" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of StringData.
+     */
+    protected static Data createStringData(Element ele, String name, String label) {
+        return new StringData(name, label, extractDataItems(ele));
+    }
+
+
+    /**
+     * This method creates a new instance of DefaultData which has a type
+     * "integer" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of IntegerData.
+     */
+    protected static Data createIntegerData(Element ele, String name, String label) {
+        return new IntegerData(name, label, extractDataItems(ele));
+    }
+
+
+    /**
+     * This method creates a new instance of StringOptionsData which has a type
+     * "options" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of StringOptionsData.
+     */
+    protected static Data createStringOptionsData(Element ele, String name, String label) {
+        return new StringOptionsData(name, label, extractDataItems(ele));
+    }
+
+
+    /**
+     * This method creates a new instance of DefaultData which has a type
+     * "intoptions" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of IntegerOptionsData.
+     */
+    protected static Data createIntegerOptionsData(Element ele, String name, String label) {
+        return new IntegerOptionsData(name, label, extractDataItems(ele));
+    }
+
+
+    /**
+     * This method creates a new instance of DefaultData which has a type
+     * "intrange" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of IntegerRangeData.
+     */
+    protected static Data createIntegerRangeData(Element ele, String name, String label) {
+        DataItem[] items    = extractDataItems(ele);
+        String     rawValue = items[0].getStringValue();
+
+        String[] minmax = rawValue.split(";");
+
+        return new IntegerRangeData(
+            name,
+            label,
+            Integer.valueOf(minmax[0]),
+            Integer.valueOf(minmax[1]));
+    }
+
+
+    /**
+     * This method creates a new instance of DefaultData which has a type
+     * "integerarray" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of IntegerArrayData.
+     */
+    protected static Data createIntegerArrayData(Element ele, String name, String label) {
+        IntDataItem[] items    = extractIntDataItems(ele);
+        return new IntegerArrayData(name, label, items);
+    }
+
+
+    /**
+     * This method creates a new instance of DefaultData which has a type
+     * "doublearray" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of DoubleArrayData.
+     */
+    protected static Data createDoubleArrayData(Element ele, String name, String label) {
+        DataItem[] items    = extractDataItems(ele);
+        String     rawValue = items[0].getStringValue();
+
+        String[] values  = rawValue.split(";");
+        double[] doubles = new double[values.length];
+
+        for (int i = 0; i < values.length; i++) {
+            try {
+                doubles[i] = Double.valueOf(values[i]);
+            }
+            catch (NumberFormatException nfe) {
+                logger.warn("Error while parsing DoubleArrayData: " + nfe);
+            }
+        }
+
+        return new DoubleArrayData(name, label, doubles);
+    }
+
+
+    /**
+     * This method extracts the art:item elements placed under <i>elements</i>.
+     *
+     * @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 data items found.");
+            return null;
+        }
+
+        int count = itemList.getLength();
+
+        DataItem[] items = new DataItem[count];
+
+        logger.debug("There are " + count + " data items in element.");
+
+        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");
+
+            logger.debug("Found data item:");
+            logger.debug("   label: " + label);
+            logger.debug("   value: " + value);
+
+            items[i] = new DefaultDataItem(label, label, value);
+        }
+
+        return items;
+    }
+
+
+    /**
+     * This method extracts the art:item elements placed under <i>elements</i>.
+     *
+     * @param element A data node that contains items.
+     *
+     * @return a list of DataItems.
+     */
+    protected static IntDataItem[] extractIntDataItems(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();
+
+        IntDataItem[] items = new IntDataItem[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");
+
+             try {
+                 int data = Integer.parseInt(value);
+                 items[i] = new IntDataItem(label, label, data);
+             }
+             catch(NumberFormatException nfe) {
+                 logger.debug(nfe, nfe);
+             }
+         }
+         return items;
+    }
+
+    /**
+     * This method creates a new instance of LongRangeData which has a type
+     * "longrange" set.
+     *
+     * @param ele The Data element.
+     * @param name The name of the Data instance.
+     *
+     * @return an instance of IntegerRangeData.
+     */
+    protected static Data createLongRangeData(Element ele, String name, String label) {
+        DataItem[] items    = extractDataItems(ele);
+        String     rawValue = items[0].getStringValue();
+
+        String[] minmax = rawValue.split(";");
+
+        return new LongRangeData(
+            name,
+            label,
+            Long.valueOf(minmax[0]),
+            Long.valueOf(minmax[1]));
+    }
+
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org