Permalink
Browse files

xstream pojo mappers

Signed-off-by: gburgett <gordon.burgett@gmail.com>
  • Loading branch information...
1 parent 8778bd6 commit 4312eb46e96f37e8dcfa3ac5013f4a5f25bea7ae @gburgett committed Mar 24, 2013
Binary file not shown.
Binary file not shown.
@@ -54,3 +54,7 @@ libs.mockito.classpath=\
${base}/mockito/mockito-all-1.9.5-rc1.jar
libs.mockito.src=\
${base}/mockito/mockito-all-1.9.5-rc1-2.jar
+libs.XStream.classpath=\
+ ${base}/XStream/xstream-1.4.4.jar;\
+ ${base}/XStream/xmlpull-1.1.3.1.jar
+libs.XStream.displayName=XStream
@@ -35,7 +35,8 @@ javac.classpath=\
${libs.Apache_Commons_Logging.classpath}:\
${libs.Hamcrest_Matchers.classpath}:\
${libs.JDOM-2.0.4.classpath}:\
- ${libs.JAXB-2.2.6.classpath}
+ ${libs.JAXB-2.2.6.classpath}:\
+ ${libs.XStream.classpath}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2004, 2005, 2006 Joe Walnes.
+ * Copyright (C) 2006, 2007, 2009, 2011 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 03. September 2004 by Joe Walnes
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import com.thoughtworks.xstream.io.naming.NameCoder;
+import java.util.List;
+import org.jdom2.Attribute;
+import org.jdom2.Document;
+import org.jdom2.Element;
+
+/**
+ * @author Laurent Bihanic
+ */
+public class JDom2Reader extends AbstractDocumentReader {
+
+ private Element currentElement;
+
+ public JDom2Reader(Element root) {
+ super(root);
+ }
+
+ public JDom2Reader(Document document) {
+ super(document.getRootElement());
+ }
+
+ /**
+ * @since 1.4
+ */
+ public JDom2Reader(Element root, NameCoder nameCoder) {
+ super(root, nameCoder);
+ }
+
+ /**
+ * @since 1.4
+ */
+ public JDom2Reader(Document document, NameCoder nameCoder) {
+ super(document.getRootElement(), nameCoder);
+ }
+
+ @Override
+ protected void reassignCurrentElement(Object current) {
+ currentElement = (Element) current;
+ }
+
+ @Override
+ protected Object getParent() {
+ // JDOM 1.0:
+ return currentElement.getParentElement();
+ }
+
+ @Override
+ protected Object getChild(int index) {
+ return currentElement.getChildren().get(index);
+ }
+
+ @Override
+ protected int getChildCount() {
+ return currentElement.getChildren().size();
+ }
+
+ @Override
+ public String getNodeName() {
+ return decodeNode(currentElement.getName());
+ }
+
+ @Override
+ public String getValue() {
+ return currentElement.getText();
+ }
+
+ @Override
+ public String getAttribute(String name) {
+ return currentElement.getAttributeValue(encodeAttribute(name));
+ }
+
+ @Override
+ public String getAttribute(int index) {
+ return ((Attribute) currentElement.getAttributes().get(index)).getValue();
+ }
+
+ @Override
+ public int getAttributeCount() {
+ return currentElement.getAttributes().size();
+ }
+
+ @Override
+ public String getAttributeName(int index) {
+ return decodeAttribute(((Attribute) currentElement.getAttributes().get(index)).getQualifiedName());
+ }
+
+ @Override
+ public String peekNextChild() {
+ List list = currentElement.getChildren();
+ if (null == list || list.isEmpty()) {
+ return null;
+ }
+ return decodeNode(((Element) list.get(0)).getName());
+ }
+
+}
@@ -0,0 +1,96 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package com.thoughtworks.xstream.io.xml;
+
+import com.thoughtworks.xstream.io.naming.NameCoder;
+import java.util.List;
+import org.jdom2.DefaultJDOMFactory;
+import org.jdom2.Element;
+import org.jdom2.JDOMFactory;
+
+/**
+ *
+ * @author Gordon
+ */
+public class JDom2Writer extends AbstractDocumentWriter {
+ private final JDOMFactory documentFactory;
+
+ public JDom2Writer(
+ final Element container, final JDOMFactory factory,
+ final NameCoder nameCoder) {
+ super(container, nameCoder);
+ documentFactory = factory;
+ }
+
+ public JDom2Writer(final Element container, final JDOMFactory factory) {
+ this(container, factory, new XmlFriendlyNameCoder());
+ }
+ public JDom2Writer(final JDOMFactory factory, final NameCoder nameCoder) {
+ this(null, factory, nameCoder);
+ }
+
+ public JDom2Writer(final JDOMFactory factory) {
+ this(null, factory);
+ }
+
+ public JDom2Writer(final Element container, final NameCoder nameCoder) {
+ this(container, new DefaultJDOMFactory(), nameCoder);
+ }
+
+
+ public JDom2Writer(final Element container) {
+ this(container, new DefaultJDOMFactory());
+ }
+
+ public JDom2Writer() {
+ this(new DefaultJDOMFactory());
+ }
+
+ @Override
+ 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;
+ }
+
+ @Override
+ public void setValue(final String text) {
+ top().addContent(documentFactory.text(text));
+ }
+
+ @Override
+ public void addAttribute(final String key, final String value) {
+ top().setAttribute(documentFactory.attribute(encodeAttribute(key), value));
+ }
+
+ private Element top() {
+ return (Element)getCurrent();
+ }
+
+ /**
+ * Casts the top level nodes as a list of JDOM2 elements.
+ * @return
+ */
+ @Override
+ public List<Element> getTopLevelNodes(){
+ return (List<Element>)super.getTopLevelNodes();
+ }
+
+ /**
+ * Gets the first top level node, or null if none exists.
+ * Useful if you expect only one top level node.
+ * @return The first top level node, or null.
+ */
+ public Element getTopLevelNode(){
+ List<Element> top = super.getTopLevelNodes();
+ if(top.isEmpty())
+ return null;
+
+ return top.get(0);
+ }
+}
@@ -25,7 +25,7 @@
* This PojoConverter maps Java beans to XML using {@link java.beans.XMLEncoder}.
* @author Gordon
*/
-public class JavaBeansPojoMapper implements PojoConverter {
+public class JavaBeansPojoConverter implements PojoConverter {
@Override
public ConversionService extend(ConversionService service) {
Oops, something went wrong.

0 comments on commit 4312eb4

Please sign in to comment.