changeset 286:0fa8bf8a2295

Datacage: Better converter for incoming XML tree flys-client/trunk@1916 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 13 May 2011 09:06:13 +0000
parents 3e99953bac8d
children 2586149b9934
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/MetaDataServiceImpl.java flys-client/src/main/java/de/intevation/flys/client/server/meta/Converter.java
diffstat 3 files changed, 122 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Thu May 12 16:47:23 2011 +0000
+++ b/flys-client/ChangeLog	Fri May 13 09:06:13 2011 +0000
@@ -1,3 +1,11 @@
+2011-05-12	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+    * src/main/java/de/intevation/flys/client/server/meta/Converter.java:
+      New. Converts the incoming XML to a more informative way.
+
+    * src/main/java/de/intevation/flys/client/server/MetaDataServiceImpl.java:
+      Use the new converter.
+
 2011-05-12  Raimund Renkert <rrenkert@intevation.de>
 
 	  ISSUE-61
--- a/flys-client/src/main/java/de/intevation/flys/client/server/MetaDataServiceImpl.java	Thu May 12 16:47:23 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/MetaDataServiceImpl.java	Fri May 13 09:06:13 2011 +0000
@@ -26,6 +26,8 @@
 import de.intevation.flys.client.shared.model.DataCageTree;
 import de.intevation.flys.client.shared.model.DataCageNode;
 
+import de.intevation.flys.client.server.meta.Converter;
+
 public class MetaDataServiceImpl
 extends      RemoteServiceServlet
 implements   MetaDataService
@@ -54,7 +56,8 @@
         HttpClient client = new HttpClientImpl(url, locale);
 
         try {
-            return convert(client.callService(url, "metadata", doc));
+            Converter converter = new Converter();
+            return converter.convert(client.callService(url, "metadata", doc));
         }
         catch (ConnectionException ce) {
             System.err.println(ce.getLocalizedMessage());
@@ -62,27 +65,5 @@
 
         throw new ServerException(ERROR_NO_META_DATA_FOUND);
     }
-
-    protected static DataCageNode convert(Node node) {
-        String name = node.getLocalName();
-        DataCageNode dvn = new DataCageNode(name);
-        NodeList children = node.getChildNodes();
-        for (int i = 0, N = children.getLength(); i < N; ++i) {
-            dvn.addChild(convert(children.item(i)));
-        }
-        return dvn;
-    }
-
-    protected static DataCageTree convert(Document document) {
-        ArrayList<DataCageNode> roots = new ArrayList<DataCageNode>();
-        NodeList nodes = document.getChildNodes();
-        for (int i = 0, N = nodes.getLength(); i < N; ++i) {
-            Node node = nodes.item(i);
-            if (node.getNodeType() == Node.ELEMENT_NODE) {
-                roots.add(convert(nodes.item(i)));
-            }
-        }
-        return new DataCageTree(roots.get(0));
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/meta/Converter.java	Fri May 13 09:06:13 2011 +0000
@@ -0,0 +1,110 @@
+package de.intevation.flys.client.server.meta;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import de.intevation.flys.client.shared.model.DataCageTree;
+import de.intevation.flys.client.shared.model.DataCageNode;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
+
+public class Converter
+{
+    public interface NodeConverter
+    {
+        DataCageNode convert(Element node, Converter converter);
+
+    } // interface NodeConverter
+
+    public static class NameConverter implements NodeConverter {
+        public DataCageNode convert(Element node, Converter converter) {
+            System.err.println("NameConverter called");
+            DataCageNode out = new DataCageNode(node.getAttribute("name"));
+            converter.convertChildren(out, node);
+            return out;
+        }
+    } // class NameConverter
+
+    public static class I18NConverter implements NodeConverter {
+        public DataCageNode convert(Element node, Converter converter) {
+            System.err.println("I18NConverter called");
+            DataCageNode out =
+                new DataCageNode("${" + node.getLocalName() + "}");
+            converter.convertChildren(out, node);
+            return out;
+        }
+    } // I18NConverter
+
+    protected Map<String, NodeConverter> converters;
+
+    protected void convertChildren(DataCageNode parent, Element sub) {
+        System.err.println("convertChildren called");
+        NodeList children = sub.getChildNodes();
+        for (int i = 0, N = children.getLength(); i < N; ++i) {
+            Node child = children.item(i);
+            if (child.getNodeType() == Node.ELEMENT_NODE) {
+                String name = child.getLocalName();
+                NodeConverter converter = converters.get(name);
+                if (converter != null) {
+                    DataCageNode son = converter.convert(
+                        (Element)child, this);
+                    parent.addChild(son);
+                }
+            }
+        } // for all children
+    }
+
+    public Converter() {
+        converters = new HashMap<String, NodeConverter>();
+        registerConverters();
+    }
+
+    protected void registerConverters() {
+        System.err.println("register converters called");
+        converters.put("datacage",                    new NameConverter());
+        converters.put("river",                       new NameConverter());
+        converters.put("gauge",                       new NameConverter());
+        converters.put("historical",                  new NameConverter());
+        converters.put("discharge-table-nn",          new I18NConverter());
+        converters.put("discharge-table-gauge",       new I18NConverter());
+        converters.put("fixations",                   new I18NConverter());
+        converters.put("fixation",                    new I18NConverter());
+        converters.put("columns",                     new I18NConverter());
+        converters.put("column",                      new NameConverter());
+        converters.put("flood-protections",           new I18NConverter());
+        converters.put("flood-protection",            new I18NConverter());
+        converters.put("flood-water-marks",           new I18NConverter());
+        converters.put("flood-water-mark",            new I18NConverter());
+        converters.put("water-levels",                new I18NConverter());
+        converters.put("water-level",                 new I18NConverter());
+        converters.put("extra-longitudinal-sections", new I18NConverter());
+        converters.put("extra-longitudinal-section",  new I18NConverter());
+        converters.put("longitudinal-section",        new I18NConverter());
+    }
+
+    public DataCageTree convert(Document document) {
+        System.err.println("convert called");
+
+        ArrayList<DataCageNode> roots = new ArrayList<DataCageNode>();
+        NodeList nodes = document.getChildNodes();
+        for (int i = 0, N = nodes.getLength(); i < N; ++i) {
+            Node node = nodes.item(i);
+            if (node.getNodeType() == Node.ELEMENT_NODE) {
+                System.err.println("search for name: " + node.getLocalName());
+                NodeConverter converter = converters.get(node.getLocalName());
+                if (converter != null) {
+                    roots.add(converter.convert((Element)node, this));
+                }
+            }
+        }
+        return roots.isEmpty()
+            ? new DataCageTree()
+            : new DataCageTree(roots.get(0));
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org