changeset 7824:2c221acebb76 minfo-opt

New method to extract multi attribute data.
author Raimund Renkert <rrenkert@intevation.de>
date Thu, 20 Mar 2014 13:35:45 +0100
parents c053b2d813f9
children b9decb8bfb21
files gwt-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java
diffstat 1 files changed, 55 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java	Thu Mar 20 13:35:13 2014 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/DataFactory.java	Thu Mar 20 13:35:45 2014 +0100
@@ -8,9 +8,13 @@
 
 package org.dive4elements.river.client.server;
 
+import java.util.HashMap;
+
 import javax.xml.xpath.XPathConstants;
 
 import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 import org.apache.log4j.Logger;
@@ -27,6 +31,8 @@
 import org.dive4elements.river.client.shared.model.IntegerData;
 import org.dive4elements.river.client.shared.model.IntegerOptionsData;
 import org.dive4elements.river.client.shared.model.IntegerRangeData;
+import org.dive4elements.river.client.shared.model.MultiAttributeData;
+import org.dive4elements.river.client.shared.model.MultiDataItem;
 import org.dive4elements.river.client.shared.model.StringData;
 import org.dive4elements.river.client.shared.model.StringOptionsData;
 import org.dive4elements.river.client.shared.model.LongRangeData;
@@ -91,6 +97,9 @@
             else if (type.equals(LongRangeData.TYPE)) {
                 return createLongRangeData(element, name, label);
             }
+            else if (type.equals(MultiAttributeData.TYPE)) {
+                return createMultiAttributeData(element, name, label);
+            }
             else {
                 return createDefaultData(element, name, label);
             }
@@ -103,6 +112,52 @@
     }
 
 
+    public static Data createMultiAttributeData(
+        Element element,
+        String name,
+        String label) {
+        return new MultiAttributeData(
+            name,
+            label,
+            extractMultiDataItems(element));
+    }
+
+
+    protected static DataItem[] extractMultiDataItems(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();
+
+        MultiDataItem[] items = new MultiDataItem[count];
+
+         for (int i = 0; i < count; i++) {
+             Element tmp = (Element) itemList.item(i);
+
+             HashMap<String, String> data = new HashMap<String, String>();
+             String label = tmp.getAttributeNS(NS_URI, "label");
+             NamedNodeMap attributes = tmp.getAttributes();
+             for (int j = 0; j < attributes.getLength(); j++) {
+                 Node n = attributes.item(j);
+                 if (n.getNodeName().equals("label")) {
+                     continue;
+                 }
+                 data.put(n.getNodeName(), n.getNodeValue());
+             }
+             items[i] = new MultiDataItem(label, label, data);
+         }
+         return items;
+    }
+
+
     /**
      * This method creates a new instance of DefaultData which has no real type
      * set.

http://dive4elements.wald.intevation.org