[Practicalxml-commits] SF.net SVN: practicalxml:[149] branches/dev-1.1/src
Brought to you by:
kdgregory
From: Auto-Generated S. C. M. <pra...@li...> - 2009-09-22 21:14:04
|
Revision: 149 http://practicalxml.svn.sourceforge.net/practicalxml/?rev=149&view=rev Author: kdgregory Date: 2009-09-22 21:13:56 +0000 (Tue, 22 Sep 2009) Log Message: ----------- add JsonConverter as facade for Xml2JsonConverter / Json2XmlConverter Added Paths: ----------- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/JsonConverter.java branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java Added: branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/JsonConverter.java =================================================================== --- branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/JsonConverter.java (rev 0) +++ branches/dev-1.1/src/main/java/net/sf/practicalxml/converter/JsonConverter.java 2009-09-22 21:13:56 UTC (rev 149) @@ -0,0 +1,100 @@ +// 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; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import net.sf.practicalxml.converter.json.Json2XmlConverter; +import net.sf.practicalxml.converter.json.Json2XmlOptions; +import net.sf.practicalxml.converter.json.Xml2JsonConverter; +import net.sf.practicalxml.converter.json.Xml2JsonOptions; + + +/** + * Converts between XML DOMs and JSON (Javascript Object Notation) strings. + * See the {@link net.sf.practicalxml.converter.json package docs} for + * details. + * <p> + * This class provides static facade methods for + * {@link net.sf.practicalxml.converter.json.Json2XmlConverter} and + * {@link net.sf.practicalxml.converter.json.Xml2JsonConverter}. If static + * methods and throwaway objects offend you then use those classes directly. + */ +public class JsonConverter +{ + /** + * Creates a new DOM document from the passed JSON string, in which all + * elements are members of the specified namespace and will inherit the + * root's prefix (if any). + * + * @param json The source object. + * @param nsUri The namespace of the root element. This will be + * inherited by all child elements. + * @param rootName The qualified name given to the root element of the + * generated document. If a qualified name, all child + * elements will inherit its prefix. + * @param options Conversion options. + */ + public static Document convertToXml( + String json, String nsUri, String rootName, Json2XmlOptions... options) + { + return new Json2XmlConverter(json, options).convert().getOwnerDocument(); + } + + + /** + * Creates a new DOM document from the passed bean, without namespace. + * + * @param json The source object. + * @param rootName The name given to the root element of the produced + * document. + * @param options Conversion options. + */ + public static Document convertToXml( + String json, String rootName, Json2XmlOptions... options) + { + return new Json2XmlConverter(json, options).convert().getOwnerDocument(); + } + + + /** + * Creates a new JSON string from the root of the passed <code>Document + * </code>. + * + * @param dom The source document. + * @param options Conversion options. + */ + public static String convertToJson(Document dom, Xml2JsonOptions... options) + { + return convertToJson(dom.getDocumentElement(), options); + } + + + /** + * Creates a new JSON string from the the passed <code>Element</code>. + * This is useful when a DOM contains a tree of objects and you just + * want to convert one of them. + * + * @param dom The source element -- this may or may not be the + * root element of its document. + * @param options Conversion options. + */ + public static String convertToJson(Element root, Xml2JsonOptions... options) + { + return new Xml2JsonConverter().convert(root, new StringBuilder(256)) + .toString(); + } +} Added: branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java =================================================================== --- branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java (rev 0) +++ branches/dev-1.1/src/test/java/net/sf/practicalxml/converter/json/TestJsonConverter.java 2009-09-22 21:13:56 UTC (rev 149) @@ -0,0 +1,164 @@ +// 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 org.w3c.dom.Element; + +import net.sf.practicalxml.DomUtil; +import net.sf.practicalxml.OutputUtil; +import net.sf.practicalxml.XmlUtil; +import net.sf.practicalxml.converter.AbstractConversionTestCase; +import net.sf.practicalxml.converter.JsonConverter; + +import static net.sf.practicalxml.builder.XmlBuilder.*; + + +/** + * These testcases all try "out and back" conversions, starting from XML. + */ +public class TestJsonConverter +extends AbstractConversionTestCase +{ + public TestJsonConverter(String testName) + { + super(testName); + } + + +//---------------------------------------------------------------------------- +// Support Code +//---------------------------------------------------------------------------- + + +//---------------------------------------------------------------------------- +// Assertions +//---------------------------------------------------------------------------- + + /** + * Asserts that the two passed elements have the same number of children, + * that those children have the same local name (in document order), and + * that they have the same text values. + * <p> + * This should probably move into <code>DomAsserts</code> + */ + private void assertChildren(Element expected, Element actual) + { + List<Element> expectedChildren = DomUtil.getChildren(expected); + List<Element> actualChildren = DomUtil.getChildren(actual); + + assertEquals("child count", expectedChildren.size(), actualChildren.size()); + + int idx = 0; + Iterator<Element> expectedItx = expectedChildren.iterator(); + Iterator<Element> actualItx = actualChildren.iterator(); + while (expectedItx.hasNext()) + { + Element expectedElement = expectedItx.next(); + Element actualElement = actualItx.next(); + assertEquals("element " + idx + " local name", + DomUtil.getLocalName(expectedElement), + DomUtil.getLocalName(actualElement)); + assertEquals("element " + idx + " content", + DomUtil.getText(expectedElement), + DomUtil.getText(actualElement)); + assertEquals("element " + idx + " child count", + DomUtil.getChildren(expectedElement).size(), + DomUtil.getChildren(actualElement).size()); + } + } + + +//---------------------------------------------------------------------------- +// Test Cases +//---------------------------------------------------------------------------- + + public void testEmptyDocument() throws Exception + { + Element src = element("data") + .toDOM().getDocumentElement(); + String json = JsonConverter.convertToJson(src); + Element dst = JsonConverter.convertToXml(json, "test") + .getDocumentElement(); + + System.out.println(json); + assertChildren(src, dst); + } + + + public void testTwoChildrenOfRoot() throws Exception + { + Element src = element("data", + element("foo", text("bar")), + element("argle", text("bargle"))) + .toDOM().getDocumentElement(); + String json = JsonConverter.convertToJson(src); + Element dst = JsonConverter.convertToXml(json, "test") + .getDocumentElement(); + + assertChildren(src, dst); + } + + + public void testRepeatedElements() throws Exception + { + Element src = element("data", + element("foo", text("bar")), + element("foo", text("baz"))) + .toDOM().getDocumentElement(); + String json = JsonConverter.convertToJson(src); + Element dst = JsonConverter.convertToXml(json, "test", Json2XmlOptions.ARRAYS_AS_REPEATED_ELEMENTS) + .getDocumentElement(); + + assertChildren(src, dst); + } + + + public void testNestedElements() throws Exception + { + Element src = element("data", + element("foo", + element("argle", text("bargle"))), + element("bar", text("baz"))) + .toDOM().getDocumentElement(); + String json = JsonConverter.convertToJson(src); + Element dst = JsonConverter.convertToXml(json, "test") + .getDocumentElement(); + + System.out.println(json); + assertChildren(src, dst); + assertChildren((Element)src.getFirstChild(), (Element)dst.getFirstChild()); + } + + + public void testNamespaces() throws Exception + { + Element src = element("urn:foo", "argle:bargle", + element("urn:bar", "foo", + element("argle", text("bargle"))), + element("bar", text("baz"))) + .toDOM().getDocumentElement(); + String json = JsonConverter.convertToJson(src); + Element dst = JsonConverter.convertToXml(json, "test") + .getDocumentElement(); + + assertNull(dst.getNamespaceURI()); + assertNull(dst.getPrefix()); + assertChildren(src, dst); + assertChildren((Element)src.getFirstChild(), (Element)dst.getFirstChild()); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |