changeset 384:823752e3d392

Fixed building XML documents from JSON strings. artifacts/trunk@4321 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 27 Apr 2012 10:56:38 +0000
parents b076c9e9fdfd
children e08f1475facc
files ChangeLog artifacts-common/src/main/java/de/intevation/artifacts/common/utils/JSON.java artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java
diffstat 3 files changed, 29 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Apr 25 07:47:56 2012 +0000
+++ b/ChangeLog	Fri Apr 27 10:56:38 2012 +0000
@@ -1,3 +1,11 @@
+2012-04-27	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/JSON.java:
+	  Added toJSONString() method.
+
+	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java:
+	  Fixed building XML documents from JSON strings.
+	
 2012-04-25  Raimund Renkert <raimund.renkert@intevation.de>
 
 	* artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java:
--- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/JSON.java	Wed Apr 25 07:47:56 2012 +0000
+++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/JSON.java	Fri Apr 27 10:56:38 2012 +0000
@@ -11,6 +11,7 @@
 import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.ByteArrayInputStream;
+import java.io.StringWriter;
 
 import java.nio.charset.Charset;
 import java.nio.charset.UnsupportedCharsetException;
@@ -97,6 +98,14 @@
         return sb.toString();
     }
 
+    public static String toJSONString(Map<String, Object> map) {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        write(pw, map);
+        pw.flush();
+        return sw.toString();
+    }
+
 
     public static void write(PrintWriter out, Map<String, Object> map) {
         writeObject(out, map);
--- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Wed Apr 25 07:47:56 2012 +0000
+++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/XMLUtils.java	Fri Apr 27 10:56:38 2012 +0000
@@ -481,6 +481,14 @@
                 element.setAttribute(entry.getKey(), entry.getValue());
             }
         }
+
+        void finish(Element element) {
+            setAttributes(element);
+            for (Node child: children) {
+                element.appendChild(child);
+            }
+        }
+
         void add(Node node) {
             children.add(node);
         }
@@ -500,13 +508,13 @@
                 Element element = document.createElement("map");
                 BuildResult subResult = recursiveBuild(
                     (Map<String, Object>)entry, document);
-                subResult.setAttributes(element);
+                subResult.finish(element);
                 result.add(element);
             }
             else if (entry instanceof List) {
                 Element element = document.createElement("list");
                 BuildResult subResult = recursiveBuild((List)entry, document);
-                subResult.setAttributes(element);
+                subResult.finish(element);
                 result.add(element);
             }
             else {
@@ -532,13 +540,13 @@
                 Element element = document.createElement(entry.getKey());
                 BuildResult subResult = recursiveBuild(
                     (Map<String, Object>)value, document);
-                subResult.setAttributes(element);
+                subResult.finish(element);
                 result.add(element);
             }
             else if (value instanceof List) {
                 Element element = document.createElement(entry.getKey());
                 BuildResult subResult = recursiveBuild((List)value, document);
-                subResult.setAttributes(element);
+                subResult.finish(element);
                 result.add(element);
             }
             else {

http://dive4elements.wald.intevation.org