Title: [2075] trunk: Support JDOM2 (XSTR-735).

Diff

Modified: trunk/xstream/pom.xml (2074 => 2075)


--- trunk/xstream/pom.xml	2013-06-24 21:49:14 UTC (rev 2074)
+++ trunk/xstream/pom.xml	2013-06-24 21:50:53 UTC (rev 2075)
@@ -32,6 +32,11 @@
       <artifactId>jdom</artifactId>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>org.jdom</groupId>
+      <artifactId>jdom2</artifactId>
+      <optional>true</optional>
+    </dependency>
 
     <dependency>
       <groupId>joda-time</groupId>
@@ -300,12 +305,14 @@
                 <exclude>**/enums/*</exclude>
                 <exclude>**/basic/StringBuilder*</exclude>
                 <exclude>**/basic/UUID*</exclude>
+                <exclude>**/io/xml/JDom2*</exclude>
               </excludes>
               <testExcludes>
                 <exclude>**/annotations/*</exclude>
                 <exclude>**/enums/*</exclude>
                 <exclude>**/extended/*17Test*</exclude>
                 <exclude>**/reflection/PureJavaReflectionProvider15Test*</exclude>
+                <exclude>**/io/xml/JDom2*Test*</exclude>
                 <exclude>**/acceptance/Basic15TypesTest*</exclude>
                 <exclude>**/acceptance/Concurrent15TypesTest*</exclude>
               </testExcludes>

Added: trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Driver.java (0 => 2075)


--- trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Driver.java	                        (rev 0)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Driver.java	2013-06-24 21:50:53 UTC (rev 2075)
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ * 
+ * Created on 24. June 2012 by Joerg Schaible 
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.Writer;
+import java.net.URL;
+
+import org.jdom2.Document;
+import org.jdom2.JDOMException;
+import org.jdom2.input.SAXBuilder;
+
+import com.thoughtworks.xstream.io.AbstractDriver;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.io.StreamException;
+import com.thoughtworks.xstream.io.naming.NameCoder;
+
+/**
+ * @since upcoming
+ */
+public class JDom2Driver extends AbstractDriver {
+
+    public JDom2Driver() {
+        super(new XmlFriendlyNameCoder());
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Driver(NameCoder nameCoder) {
+        super(nameCoder);
+    }
+
+    public HierarchicalStreamReader createReader(Reader reader) {
+        try {
+            SAXBuilder builder = new SAXBuilder();
+            Document document = builder.build(reader);
+            return new JDom2Reader(document, getNameCoder());
+        } catch (IOException e) {
+            throw new StreamException(e);
+        } catch (JDOMException e) {
+            throw new StreamException(e);
+        }
+    }
+
+    public HierarchicalStreamReader createReader(InputStream in) {
+        try {
+            SAXBuilder builder = new SAXBuilder();
+            Document document = builder.build(in);
+            return new JDom2Reader(document, getNameCoder());
+        } catch (IOException e) {
+            throw new StreamException(e);
+        } catch (JDOMException e) {
+            throw new StreamException(e);
+        }
+    }
+
+    public HierarchicalStreamReader createReader(URL in) {
+        try {
+            SAXBuilder builder = new SAXBuilder();
+            Document document = builder.build(in);
+            return new JDom2Reader(document, getNameCoder());
+        } catch (IOException e) {
+            throw new StreamException(e);
+        } catch (JDOMException e) {
+            throw new StreamException(e);
+        }
+    }
+
+    public HierarchicalStreamReader createReader(File in) {
+        try {
+            SAXBuilder builder = new SAXBuilder();
+            Document document = builder.build(in);
+            return new JDom2Reader(document, getNameCoder());
+        } catch (IOException e) {
+            throw new StreamException(e);
+        } catch (JDOMException e) {
+            throw new StreamException(e);
+        }
+    }
+
+    public HierarchicalStreamWriter createWriter(Writer out) {
+        return new PrettyPrintWriter(out, getNameCoder());
+    }
+
+    public HierarchicalStreamWriter createWriter(OutputStream out) {
+        return new PrettyPrintWriter(new OutputStreamWriter(out));
+    }
+}
+
Property changes on: trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Driver.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Added: trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Reader.java (0 => 2075)


--- trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Reader.java	                        (rev 0)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Reader.java	2013-06-24 21:50:53 UTC (rev 2075)
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ * 
+ * Created on 24. June 2012 by Joerg Schaible 
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import com.thoughtworks.xstream.io.naming.NameCoder;
+import java.util.List;
+import org.jdom2.Document;
+import org.jdom2.Element;
+
+/**
+ * @since upcoming
+ */
+public class JDom2Reader extends AbstractDocumentReader {
+
+    private Element currentElement;
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Reader(Element root) {
+        super(root);
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Reader(Document document) {
+        super(document.getRootElement());
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Reader(Element root, NameCoder nameCoder) {
+        super(root, nameCoder);
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Reader(Document document, NameCoder nameCoder) {
+        super(document.getRootElement(), nameCoder);
+    }
+    
+    protected void reassignCurrentElement(Object current) {
+        currentElement = (Element) current;
+    }
+
+    protected Object getParent() {
+        return currentElement.getParentElement();
+    }
+
+    protected Object getChild(int index) {
+        return currentElement.getChildren().get(index);
+    }
+
+    protected int getChildCount() {
+        return currentElement.getChildren().size();
+    }
+
+    public String getNodeName() {
+        return decodeNode(currentElement.getName());
+    }
+
+    public String getValue() {
+        return currentElement.getText();
+    }
+
+    public String getAttribute(String name) {
+        return currentElement.getAttributeValue(encodeAttribute(name));
+    }
+
+    public String getAttribute(int index) {
+        return currentElement.getAttributes().get(index).getValue();
+    }
+
+    public int getAttributeCount() {
+        return currentElement.getAttributes().size();
+    }
+
+    public String getAttributeName(int index) {
+        return decodeAttribute(currentElement.getAttributes().get(index).getQualifiedName());
+    }
+
+    public String peekNextChild() {
+        List list = currentElement.getChildren();
+        if (null == list || list.isEmpty()) {
+            return null;
+        }
+        return decodeNode(((Element) list.get(0)).getName());
+    }
+}
+
Property changes on: trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Reader.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Added: trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Writer.java (0 => 2075)


--- trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Writer.java	                        (rev 0)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Writer.java	2013-06-24 21:50:53 UTC (rev 2075)
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ * 
+ * Created on 24. June 2012 by Joerg Schaible 
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import com.thoughtworks.xstream.io.naming.NameCoder;
+
+import org.jdom2.DefaultJDOMFactory;
+import org.jdom2.Element;
+import org.jdom2.JDOMFactory;
+
+
+/**
+ * @since upcoming
+ */
+public class JDom2Writer extends AbstractDocumentWriter {
+
+    private final JDOMFactory documentFactory;
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Writer(
+                      final Element container, final JDOMFactory factory,
+                      final NameCoder nameCoder) {
+        super(container, nameCoder);
+        documentFactory = factory;
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Writer(final Element container, final JDOMFactory factory) {
+        this(container, factory, new XmlFriendlyNameCoder());
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Writer(final JDOMFactory factory, final NameCoder nameCoder) {
+        this(null, factory, nameCoder);
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Writer(final JDOMFactory factory) {
+        this(null, factory);
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Writer(final Element container, final NameCoder nameCoder) {
+        this(container, new DefaultJDOMFactory(), nameCoder);
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Writer(final Element container) {
+        this(container, new DefaultJDOMFactory());
+    }
+
+    /**
+     * @since upcoming
+     */
+    public JDom2Writer() {
+        this(new DefaultJDOMFactory());
+    }
+
+    protected Object createNode(final String name) {
+        final Element element = documentFactory.element(encodeNode(name));
+        final Element parent = top();
+        if (parent != null) {
+            parent.addContent(element);
+        }
+        return element;
+    }
+
+    public void setValue(final String text) {
+        top().addContent(documentFactory.text(text));
+    }
+
+    public void addAttribute(final String key, final String value) {
+        top().setAttribute(documentFactory.attribute(encodeAttribute(key), value));
+    }
+
+    /**
+     * @since upcoming
+     */
+    private Element top() {
+        return (Element)getCurrent();
+    }
+}
Property changes on: trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/JDom2Writer.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Modified: trunk/xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java (2074 => 2075)


--- trunk/xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java	2013-06-24 21:49:14 UTC (rev 2074)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/io/DriverEndToEndTestSuite.java	2013-06-24 21:50:53 UTC (rev 2075)
@@ -54,6 +54,17 @@
         addDriverTest(new Dom4JDriver());
         addDriverTest(new DomDriver());
         addDriverTest(new JDomDriver());
+        if (JVM.is15()) {
+            JVM jvm = new JVM();
+            Class driverType = jvm.loadClass("com.thoughtworks.xstream.io.xml.JDom2Driver");
+            try {
+                addDriverTest((HierarchicalStreamDriver)driverType.newInstance());
+            } catch (InstantiationException e) {
+                throw new AssertionFailedError("Cannot instantiate " + driverType.getName());
+            } catch (IllegalAccessException e) {
+                throw new AssertionFailedError("Cannot access default constructor of " + driverType.getName());
+            }
+        }
         addDriverTest(new KXml2DomDriver());
         addDriverTest(new KXml2Driver());
         addDriverTest(new StaxDriver());

Added: trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2AcceptanceTest.java (0 => 2075)


--- trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2AcceptanceTest.java	                        (rev 0)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2AcceptanceTest.java	2013-06-24 21:50:53 UTC (rev 2075)
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ * 
+ * Created on 24. June 2012 by Joerg Schaible 
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import java.io.StringReader;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.jdom2.Document;
+import org.jdom2.input.SAXBuilder;
+import org.jdom2.output.Format;
+import org.jdom2.output.XMLOutputter;
+
+import com.thoughtworks.acceptance.someobjects.X;
+import com.thoughtworks.acceptance.someobjects.Y;
+import com.thoughtworks.xstream.XStream;
+
+public class JDom2AcceptanceTest extends TestCase {
+
+    private XStream xstream;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        xstream = new XStream();
+        xstream.alias("x", X.class);
+    }
+
+    public void testUnmarshalsObjectFromJDOM() throws Exception {
+        String xml =
+                "<x>" +
+                "  <aStr>joe</aStr>" +
+                "  <anInt>8</anInt>" +
+                "  <innerObj>" +
+                "    <yField>walnes</yField>" +
+                "  </innerObj>" +
+                "</x>";
+
+        Document doc = new SAXBuilder().build(new StringReader(xml));
+
+        X x = (X) xstream.unmarshal(new JDom2Reader(doc));
+
+        assertEquals("joe", x.aStr);
+        assertEquals(8, x.anInt);
+        assertEquals("walnes", x.innerObj.yField);
+    }
+
+    public void testMarshalsObjectToJDOM() {
+        X x = new X();
+        x.anInt = 9;
+        x.aStr = "zzz";
+        x.innerObj = new Y();
+        x.innerObj.yField = "ooo";
+
+        String expected =
+                "<x>\n" +
+                "  <aStr>zzz</aStr>\n" +
+                "  <anInt>9</anInt>\n" +
+                "  <innerObj>\n" +
+                "    <yField>ooo</yField>\n" +
+                "  </innerObj>\n" +
+                "</x>";
+
+        JDom2Writer writer = new JDom2Writer();
+        xstream.marshal(x, writer);
+        List result = writer.getTopLevelNodes();
+
+        assertEquals("Result list should contain exactly 1 element",
+                                                        1, result.size());
+
+        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setLineSeparator("\n"));
+
+        assertEquals(expected, outputter.outputString(result));
+    }
+}
+
Property changes on: trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2AcceptanceTest.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Added: trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2ReaderTest.java (0 => 2075)


--- trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2ReaderTest.java	                        (rev 0)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2ReaderTest.java	2013-06-24 21:50:53 UTC (rev 2075)
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ * 
+ * Created on 24. June 2012 by Joerg Schaible 
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+
+import org.jdom2.Document;
+import org.jdom2.input.SAXBuilder;
+
+import java.io.StringReader;
+
+public class JDom2ReaderTest extends AbstractXMLReaderTest {
+
+    // factory method
+    protected HierarchicalStreamReader createReader(String xml) throws Exception {
+        Document document = new SAXBuilder().build(new StringReader(xml));
+        return new JDom2Reader(document);
+    }
+
+    // inherits tests from superclass
+
+}
Property changes on: trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2ReaderTest.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Added: trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2WriterTest.java (0 => 2075)


--- trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2WriterTest.java	                        (rev 0)
+++ trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2WriterTest.java	2013-06-24 21:50:53 UTC (rev 2075)
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ * 
+ * Created on 24. June 2012 by Joerg Schaible 
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import org.jdom2.Element;
+
+public class JDom2WriterTest extends AbstractDocumentWriterTest {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        writer = new JDom2Writer();
+    }
+
+    protected DocumentReader createDocumentReaderFor(final Object node) {
+        return new JDom2Reader((Element)node);
+    }
+
+    // inherits tests from superclass
+}
Property changes on: trunk/xstream/src/test/com/thoughtworks/xstream/io/xml/JDom2WriterTest.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Modified: trunk/xstream-distribution/src/content/changes.html (2074 => 2075)


--- trunk/xstream-distribution/src/content/changes.html	2013-06-24 21:49:14 UTC (rev 2074)
+++ trunk/xstream-distribution/src/content/changes.html	2013-06-24 21:50:53 UTC (rev 2075)
@@ -52,6 +52,7 @@
     	<li>JIRA:XSTR-728: XStream creates invalid JSON with JsonHierarchicalStreamDriver for custom converters since
     	XStream 1.4.</li>
     	<li>JIRA:XSTR-300: New EnumToStringConverter to support custom string representations of Enum values.</li>
+    	<li>JIRA:XSTR-735: Support for JDOM2 with JDom2Driver, JDom2Reader and JDom2Writer.</li>
     </ul>
 
     <h2>Minor changes</h2>

Modified: trunk/xstream-distribution/src/content/download.html (2074 => 2075)


--- trunk/xstream-distribution/src/content/download.html	2013-06-24 21:49:14 UTC (rev 2074)
+++ trunk/xstream-distribution/src/content/download.html	2013-06-24 21:50:53 UTC (rev 2075)
@@ -62,7 +62,8 @@
       		<li><a href="" an XML pull parser (recommended).</li>
       		<li><a href="" or <a href="" an XML pull parser.</li>
       		<li><a href="" easy XML representation and manipulation framework.</li>
-      		<li><a href="" easy XML representation and manipulation.</li>
+      		<li><a href="" easy XML representation and manipulation (requires Java 1.2, superseded by JDOM2).</li>
+      		<li><a href="" easy XML representation and manipulation, successor of JDOM (requires Java 5).</li>
       		<li>StaX, the <a href="" implementation</a> of the <a href="" API for XML</a>.</li>
       		<li><a href="" an alternate open source StaX implementation.</li>
       		<li><a href="" another alternative XML API.</li>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to