public abstract class XMLFormat<T> extends Object
This class represents the format base class for XML serialization and deserialization.
Application classes typically define a default XML format for their
instances using protected static Due to the sequential nature of XML serialization/deserialization,
formatting/parsing of XML attributes should always be performed before
formatting/parsing of the XML content. The mapping between classes and XML formats can be overriden
through Note: Any type for which a text format is
XMLFormat
class members.
Formats are inherited by sub-classes. For example:[code]
public abstract class Graphic implements XMLSerializable {
private boolean _isVisible;
private Paint _paint; // null if none.
private Stroke _stroke; // null if none.
private Transform _transform; // null if none.
// XML format with positional associations (members identified by their position),
// see XML package description for examples of name associations.
protected static final XMLFormat GRAPHIC_XML = new XMLFormat
(Graphic.class) {
public void write(Graphic g, OutputElement xml) {
xml.setAttribute("isVisible", g._isVisible);
xml.add(g._paint); // First.
xml.add(g._stroke); // Second.
xml.add(g._transform); // Third.
}
public void read(InputElement xml, Graphic g) {
g._isVisible = xml.getAttribute("isVisible", true);
g._paint = xml.getNext();
g._stroke = xml.getNext();
g._transform = xml.getNext();
return g;
}
};
}[/code]
XMLBinding
instances.
Here is an example of serialization/deserialization:[code]
// Creates a list holding diverse objects.
List list = new ArrayList();
list.add("John Doe");
list.add(null);
Map map = new FastMap();
map.put("ONE", 1);
map.put("TWO", 2);
list.add(map);
// Use of custom binding.
XMLBinding binding = new XMLBinding();
binding.setAlias(FastMap.class, "Map");
binding.setAlias(String.class, "String");
binding.setAlias(Integer.class, "Integer");
// Formats the list to XML .
OutputStream out = new FileOutputStream("C:/list.xml");
XMLObjectWriter writer = new XMLObjectWriter().setOutput(out).setBinding(binding);
writer.write(list, "MyList", ArrayList.class);
writer.close();[/code]
Here is the output list.xml
document produced:[code]
known
can be represented as
a XML attribute.
Modifier and Type | Class and Description |
---|---|
static class |
XMLFormat.InputElement
This class represents an input XML element (unmarshalling).
|
static class |
XMLFormat.OutputElement
This class represents an output XML element (marshalling).
|
Modifier | Constructor and Description |
---|---|
protected |
XMLFormat(Class<T> forClass)
Defines the default XML format bound to the specified class.
|
Modifier and Type | Method and Description |
---|---|
Class<T> |
getBoundClass()
Returns the class/interface statically bound to this format or
null if none. |
static <T> XMLFormat<T> |
getInstance(Class<? extends T> forClass)
Returns the default format for the specified class/interface.
|
boolean |
isReferenceable()
Indicates if the object serialized through this format can be referenced
to (default
true ). |
T |
newInstance(Class<T> cls,
XMLFormat.InputElement xml)
Allocates a new object of the specified class from the specified
XML input element.
|
abstract void |
read(XMLFormat.InputElement xml,
T obj)
Parses an XML input element into the specified object.
|
String |
toString()
Returns textual information about this format.
|
abstract void |
write(T obj,
XMLFormat.OutputElement xml)
Formats an object into the specified XML output element.
|
protected XMLFormat(Class<T> forClass)
null
then the format is unbound
(unbound formats are used by custom binding
instances).
The static binding is unique and can only be overriden by custom
XMLBinding
. For example:[code]
// Overrides default binding for java.util.Collection.
class MyBinding extends XMLBinding {
XMLFormatforClass
- the root class/interface to associate to this XML format
or null
if this format is not bound.IllegalArgumentException
- if a XMLFormat is already bound to
the specified class.public static <T> XMLFormat<T> getInstance(Class<? extends T> forClass)
Returns the default format for the specified class/interface.
If there no direct mapping for the specified class, the mapping
for the specified class interfaces is searched, if none is found
the mapping for the parents classes is searched, if still none is
found the format for java.lang.Object
is returned.
A default xml format exists for the following predefined types:
The default XML representation (java.lang.Object) consists of the
of a "value" attribute holding its textual representation
(see
TextFormat.getInstance(java.lang.Class<? extends T>)
).
public final Class<T> getBoundClass()
null
if none.public boolean isReferenceable()
true
). This method can be overriden to return
false
if serialized objects are manipulated "by value".true
if serialized object may hold a reference;
false
otherwise.XMLReferenceResolver
public T newInstance(Class<T> cls, XMLFormat.InputElement xml) throws XMLStreamException
cls
- the class of the object to return.xml
- the XML input element.XMLStreamException
public abstract void write(T obj, XMLFormat.OutputElement xml) throws XMLStreamException
obj
- the object to format.xml
- the XMLElement
destination.XMLStreamException
public abstract void read(XMLFormat.InputElement xml, T obj) throws XMLStreamException
xml
- the XML element to parse.obj
- the object created through newInstance(java.lang.Class<T>, javolution.xml.XMLFormat.InputElement)
and to setup from the specified XML element.XMLStreamException
Copyright © 2005–2017 Javolution. All rights reserved.