Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,027 additions
and
23 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
java/XFlat/src/com/thoughtworks/xstream/io/xml/JDom2Reader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
java/XFlat/src/com/thoughtworks/xstream/io/xml/JDom2Writer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.