Javolution 6.0.0 java
javolution.xml.XMLFormat< T > Class Template Referenceabstract
Collaboration diagram for javolution.xml.XMLFormat< T >:
[legend]

Classes

class  Default
 
class  InputElement
 
class  OutputElement
 

Public Member Functions

boolean isReferenceable ()
 
newInstance (Class<? extends T > cls, InputElement xml) throws XMLStreamException
 
abstract void write (T obj, OutputElement xml) throws XMLStreamException
 
abstract void read (InputElement xml, T obj) throws XMLStreamException
 

Protected Member Functions

 XMLFormat ()
 

Static Private Attributes

static final String NULL = "Null"
 

Detailed Description

This class represents the format base class for XML serialization and deserialization.

Instances of this class are typically retrieved from the XMLContext (OSGi service or not). [code] @Format(xml=GraphicXML.class) 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. public static class GraphicXML extends XMLFormat<Graphic> { 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]

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 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]

<MyList> <String value="John Doe"> <Null> <Map> <Key class="String" value="ONE">

<Key class="String" value="TWO">

</Map> </MyList> [/code]

The list can be read back with the following code: [code] // Reads back to a FastTable instance. InputStream in = new FileInputStream("C:/list.xml"); XMLObjectReader reader = new XMLObjectReader().setInput(in).setBinding(binding); FastTable table = reader.read("MyList", FastTable.class); reader.close(); [/code]

Note: Any type for which a text format is defined can be represented as a XML attribute.

Author
Jean-Marie Dautelle
Version
5.4, December 1, 2009

Definition at line 116 of file XMLFormat.java.

Constructor & Destructor Documentation

◆ XMLFormat()

javolution.xml.XMLFormat< T >.XMLFormat ( )
protected

Default constructor.

Definition at line 126 of file XMLFormat.java.

126 {}

Member Function Documentation

◆ isReferenceable()

boolean javolution.xml.XMLFormat< T >.isReferenceable ( )

Indicates if the object serialized through this format can be referenced to (default true). This method can be overriden to return false if serialized objects are manipulated "by value".

Returns
true if serialized object may hold a reference; false otherwise.
See also
XMLReferenceResolver

Definition at line 137 of file XMLFormat.java.

137  {
138  return true;
139  }

Referenced by javolution.xml.XMLFormat< T >.OutputElement.add().

Here is the caller graph for this function:

◆ newInstance()

T javolution.xml.XMLFormat< T >.newInstance ( Class<? extends T >  cls,
InputElement  xml 
) throws XMLStreamException

Allocates a new object of the specified class from the specified XML input element. By default, this method returns an object created using the public no-arg constructor of the specified class. XML formats may override this method in order to use private/multi-arg constructors.

Parameters
clsthe class of the object to return.
xmlthe XML input element.
Returns
the object corresponding to the specified XML element.

Definition at line 152 of file XMLFormat.java.

153  {
154  try {
155  return cls.newInstance();
156  } catch (InstantiationException e) {
157  throw new XMLStreamException(e);
158  } catch (IllegalAccessException e) {
159  throw new XMLStreamException(e);
160  }
161  }

Referenced by javolution.xml.XMLFormat< T >.InputElement.readInstanceOf(), and javolution.xml.internal.XMLContextImpl.searchFormat().

Here is the caller graph for this function:

◆ read()

abstract void javolution.xml.XMLFormat< T >.read ( InputElement  xml,
obj 
) throws XMLStreamException
abstract

Parses an XML input element into the specified object.

Parameters
xmlthe XML element to parse.
objthe object created through newInstance and to setup from the specified XML element.

Referenced by javolution.xml.XMLFormat< T >.InputElement.readInstanceOf().

Here is the caller graph for this function:

◆ write()

abstract void javolution.xml.XMLFormat< T >.write ( obj,
OutputElement  xml 
) throws XMLStreamException
abstract

Formats an object into the specified XML output element.

Parameters
objthe object to format.
xmlthe XMLElement destination.

Referenced by javolution.xml.XMLFormat< T >.OutputElement.add().

Here is the caller graph for this function:

Member Data Documentation

◆ NULL

final String javolution.xml.XMLFormat< T >.NULL = "Null"
staticprivate

Holds null representation.

Definition at line 121 of file XMLFormat.java.

Referenced by javolution.xml.XMLFormat< T >.OutputElement.add(), and javolution.xml.XMLFormat< T >.InputElement.getNext().


The documentation for this class was generated from the following file: