[Practicalxml-commits] SF.net SVN: practicalxml:[124] branches/dev-1.1/src
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2009-09-08 16:06:23
|
Revision: 124
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=124&view=rev
Author: kdgregory
Date: 2009-09-08 16:06:13 +0000 (Tue, 08 Sep 2009)
Log Message:
-----------
implement Xml2Json conversion
Added Paths:
-----------
branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/
branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java
branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/
branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java
Added: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java
===================================================================
--- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java (rev 0)
+++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java 2009-09-08 16:06:13 UTC (rev 124)
@@ -0,0 +1,73 @@
+// Copyright 2008-2009 severally by the contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package net.sf.practicalxml.converter.json;
+
+import java.util.Iterator;
+import java.util.List;
+
+import net.sf.practicalxml.DomUtil;
+
+import org.w3c.dom.Element;
+
+
+/**
+ * Handles the actual work of converting XML to JSON.
+ */
+public class Xml2JsonConverter
+{
+ /**
+ * Appends the contents of the specified element to an existing buffer.
+ * Returns the buffer as a convenience.
+ */
+ public StringBuffer convert(Element elem, StringBuffer buf)
+ {
+ String text = DomUtil.getText(elem);
+ if (text != null)
+ return appendText(text, buf);
+ else
+ return appendChildren(elem, buf);
+ }
+
+
+//----------------------------------------------------------------------------
+// Internals
+//----------------------------------------------------------------------------
+
+ private StringBuffer appendText(String text, StringBuffer buf)
+ {
+ buf.append('"')
+ .append(text)
+ .append('"');
+ return buf;
+ }
+
+
+ private StringBuffer appendChildren(Element elem, StringBuffer buf)
+ {
+ buf.append("{");
+ List<Element> children = DomUtil.getChildren(elem);
+ for (Iterator<Element> childItx = children.iterator() ; childItx.hasNext() ; )
+ {
+ Element child = childItx.next();
+ buf.append(DomUtil.getLocalName(child))
+ .append(": ");
+ convert(child, buf);
+ if (childItx.hasNext())
+ buf.append(", ");
+ }
+ buf.append("}");
+ return buf;
+ }
+}
Property changes on: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/json/Xml2JsonConverter.java
___________________________________________________________________
Added: svn:executable
+ *
Added: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java
===================================================================
--- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java (rev 0)
+++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java 2009-09-08 16:06:13 UTC (rev 124)
@@ -0,0 +1,87 @@
+// Copyright 2008-2009 severally by the contributors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package net.sf.practicalxml.converter.json;
+
+import org.w3c.dom.Element;
+
+import net.sf.practicalxml.builder.ElementNode;
+import net.sf.practicalxml.converter.AbstractConversionTestCase;
+
+import static net.sf.practicalxml.builder.XmlBuilder.*;
+
+
+public class TestXml2JsonConverter
+extends AbstractConversionTestCase
+{
+ public TestXml2JsonConverter(String testName)
+ {
+ super(testName);
+ }
+
+
+//----------------------------------------------------------------------------
+// Support Code
+//----------------------------------------------------------------------------
+
+ public void convertAndAssert(String expected, ElementNode rootNode)
+ {
+ Element root = rootNode.toDOM().getDocumentElement();
+ StringBuffer buf = new Xml2JsonConverter()
+ .convert(root, new StringBuffer());
+ assertEquals(expected, buf.toString());
+ }
+
+
+//----------------------------------------------------------------------------
+// Test Cases
+//----------------------------------------------------------------------------
+
+ public void testEmptyConversion() throws Exception
+ {
+ convertAndAssert(
+ "{}",
+ element("data"));
+ }
+
+
+ public void testSingleChild() throws Exception
+ {
+ convertAndAssert(
+ "{foo: \"bar\"}",
+ element("data",
+ element("foo", text("bar"))));
+ }
+
+
+ public void testTwoChildren() throws Exception
+ {
+ convertAndAssert(
+ "{foo: \"bar\", argle: \"bargle\"}",
+ element("data",
+ element("foo", text("bar")),
+ element("argle", text("bargle"))));
+ }
+
+
+ public void testChildAndGrandchildren() throws Exception
+ {
+ convertAndAssert(
+ "{foo: \"bar\", argle: {biz: \"baz\", fizz: \"buzz\"}}",
+ element("data",
+ element("foo", text("bar")),
+ element("argle",
+ element("biz", text("baz")),
+ element("fizz", text("buzz")))));
+ }
+}
Property changes on: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestXml2JsonConverter.java
___________________________________________________________________
Added: svn:executable
+ *
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|