public class XStreamDOM extends Object
This object captures a subset of XML infoset that XStream understands. Namely, no XML namespace, no mixed content.
You use it as a field in your class (that itself participates in an XStream persistence),
and have it receive the portion of that XML. Then later you can use unmarshal(XStream)
to convert this sub-tree to an object with a possibly separate XStream instance.
The reverse operation is from(XStream, Object)
method, which marshals an object
into XStreamDOM
.
You can also use this class to parse an entire XML document into a DOM like tree with
from(HierarchicalStreamReader)
and writeTo(HierarchicalStreamWriter)
.
These two methods support variants that accept other forms.
Whereas the above methods read from and write to HierarchicalStreamReader
and,
HierarchicalStreamWriter
, we can also create HierarchicalStreamReader
that read from DOM and HierarchicalStreamWriter
that writes to DOM. See
newReader()
and newWriter()
for those operations.
XStreamDOM
can be used as a type of a field of another class that's itself XStream-enabled,
such as this:
class Foo { XStreamDOM bar; }With the following XML:
<foo> <bar> <payload> ... </payload> </bar> </foo>
The XStreamDOM
object in the bar field will have the "payload" element in its tag name
(which means the bar element cannot have multiple children.)
Because XStream wants to use letters like '$' that's not legal as a name char in XML,
the XML data model that it thinks of (unescaped) is actually translated into the actual
XML-compliant infoset via XmlFriendlyReplacer
. This translation is done by
HierarchicalStreamReader
and HierarchicalStreamWriter
, transparently
from Converter
s. In XStreamDOM
, we'd like to hold the XML infoset
(escaped form, in XStream speak), so in our XStreamDOM.ConverterImpl
we go out of the way
to cancel out this effect.
Modifier and Type | Class and Description |
---|---|
static class |
XStreamDOM.ConverterImpl |
static class |
XStreamDOM.WriterImpl |
Modifier and Type | Field and Description |
---|---|
static com.thoughtworks.xstream.io.xml.XmlFriendlyReplacer |
REPLACER |
Constructor and Description |
---|
XStreamDOM(String tagName,
Map<String,String> attributes,
List<XStreamDOM> children) |
XStreamDOM(String tagName,
Map<String,String> attributes,
String value) |
Modifier and Type | Method and Description |
---|---|
XStreamDOM |
expandMacro(VariableResolver<String> vars)
Recursively expands the variables in text and attribute values and return the new DOM.
|
static XStreamDOM |
from(com.thoughtworks.xstream.io.HierarchicalStreamReader in) |
static XStreamDOM |
from(InputStream in) |
static XStreamDOM |
from(Reader in) |
static XStreamDOM |
from(com.thoughtworks.xstream.XStream xs,
Object obj)
Marshals the given object with the given XStream into
XStreamDOM and return it. |
String |
getAttribute(int index) |
String |
getAttribute(String name) |
int |
getAttributeCount() |
Map<String,String> |
getAttributeMap() |
List<XStreamDOM> |
getChildren() |
String |
getTagName() |
String |
getValue() |
com.thoughtworks.xstream.io.HierarchicalStreamReader |
newReader()
Returns a new
HierarchicalStreamReader that reads a sub-tree rooted at this node. |
static XStreamDOM.WriterImpl |
newWriter()
Returns a new
HierarchicalStreamWriter for marshalling objects into XStreamDOM . |
<T> T |
unmarshal(com.thoughtworks.xstream.XStream xs)
Unmarshals this DOM into an object via the given XStream.
|
<T> T |
unmarshal(com.thoughtworks.xstream.XStream xs,
T root) |
void |
writeTo(com.thoughtworks.xstream.io.HierarchicalStreamWriter w) |
void |
writeTo(OutputStream os)
Writes this
XStreamDOM into OutputStream . |
void |
writeTo(Writer w) |
public String getTagName()
public <T> T unmarshal(com.thoughtworks.xstream.XStream xs)
public <T> T unmarshal(com.thoughtworks.xstream.XStream xs, T root)
public XStreamDOM expandMacro(VariableResolver<String> vars)
Util.replaceMacro(String, VariableResolver)
, so any unresolved
references will be left as-is.public int getAttributeCount()
public String getAttribute(int index)
public String getValue()
public List<XStreamDOM> getChildren()
public com.thoughtworks.xstream.io.HierarchicalStreamReader newReader()
HierarchicalStreamReader
that reads a sub-tree rooted at this node.public static XStreamDOM.WriterImpl newWriter()
HierarchicalStreamWriter
for marshalling objects into XStreamDOM
.
After the writer receives the calls, call XStreamDOM.WriterImpl.getOutput()
to obtain the populated tree.public void writeTo(OutputStream os)
XStreamDOM
into OutputStream
.public void writeTo(Writer w)
public void writeTo(com.thoughtworks.xstream.io.HierarchicalStreamWriter w)
public static XStreamDOM from(com.thoughtworks.xstream.XStream xs, Object obj)
XStreamDOM
and return it.public static XStreamDOM from(InputStream in)
public static XStreamDOM from(Reader in)
public static XStreamDOM from(com.thoughtworks.xstream.io.HierarchicalStreamReader in)
Copyright © 2018. All rights reserved.