Javolution 6.0.0 java
javolution.xml.XMLBinding Class Reference
Inheritance diagram for javolution.xml.XMLBinding:
[legend]
Collaboration diagram for javolution.xml.XMLBinding:
[legend]

Public Member Functions

 XMLBinding ()
 
void setAlias (Class<?> cls, QName qName)
 
final void setAlias (Class<?> cls, String alias)
 
void setClassAttribute (QName classAttribute)
 
final void setClassAttribute (String name)
 
void reset ()
 

Protected Member Functions

XMLFormat<?> getFormat (Class<?> forClass) throws XMLStreamException
 
Class<?> readClass (XMLStreamReader reader, boolean useAttributes) throws XMLStreamException
 
void writeClass (Class<?> cls, XMLStreamWriter writer, boolean useAttributes) throws XMLStreamException
 

Static Package Attributes

static final XMLBinding DEFAULT = new XMLBinding()
 

Private Attributes

QName _classAttribute = QName.valueOf("class")
 
final FastMap< Class<?>, QName_classToAlias = new FastMap<Class<?>, QName>()
 
final FastMap< QName, Class<?> > _aliasToClass = new FastMap<QName, Class<?>>()
 

Static Private Attributes

static final long serialVersionUID = 6611041662550083919L
 

Detailed Description

This class represents the binding between Java classes and their XML representation (XMLFormat).

Custom XML bindings can also be used to alias class names and ensure that the XML representation is:

  • Impervious to obfuscation.
  • Unaffected by any class refactoring.
  • Can be mapped to multiple implementations. For example:[code]

    // Creates a binding to serialize Swing components into high-level XML // and deserialize the same XML into SWT components. XMLBinding swingBinding = new XMLBinding(); swingBinding.setAlias(javax.swing.JButton.class, "Button"); swingBinding.setAlias(javax.swing.JTable.class, "Table"); ... XMLBinding swtBinding = new XMLBinding(); swtBinding.setAlias(org.eclipse.swt.widgets.Button.class, "Button"); swtBinding.setAlias(org.eclipse.swt.widgets.Table.class, "Table"); ...

    // Writes Swing Desktop to XML. XMLObjectWriter writer = new XMLObjectWriter().setBinding(swingBinding); writer.setOutput(new FileOutputStream("C:/desktop.xml")); writer.write(swingDesktop, "Desktop", SwingDesktop.class); writer.close();

    // Reads back high-level XML to a SWT implementation!
    XMLObjectReader reader = new XMLObjectReader().setXMLBinding(swtBinding); reader.setInput(new FileInputStream("C:/desktop.xml")); SWTDesktop swtDesktop = reader.read("Desktop", SWTDesktop.class); reader.close(); [/code]


More advanced bindings can also be created through sub-classing.[code]

// XML binding using reflection.
public ReflectionBinding extends XMLBinding {
    protected XMLFormat getFormat(Class forClass) {
        Field[] fields = forClass.getDeclaredFields();
        return new XMLReflectionFormat(fields);
    }
}

// XML binding read from DTD input source.
public DTDBinding extends XMLBinding {
    public DTDBinding(InputStream dtd) {
        ...
    }
}

// XML binding overriding default formats.
public MyBinding extends XMLBinding {
    // Non-static formats use unmapped XMLFormat instances.
    XMLFormat<String> myStringFormat = new XMLFormat<String>(null) {...}
    XMLFormat<Collection> myCollectionFormat = new XMLFormat<Collection>(null) {...}
    protected XMLFormat getFormat(Class forClass) throws XMLStreamException {
        if (String.class.equals(forClass))
             return myStringFormat;
        if (Collection.class.isAssignableFrom(forClass))
             return myCollectionFormat;
        return super.getFormat(cls);
    }
}
[/code]

The default XML binding implementation supports all static XML formats (static members of the classes being mapped) as well as the following types:

  • java.lang.Object (empty element)
  • java.lang.Class
  • java.lang.String
  • java.lang.Appendable
  • java.util.Collection
  • java.util.Map
  • java.lang.Object[]
  • all primitive types wrappers (e.g. Boolean, Integer ...)
Author
Jean-Marie Dautelle
Version
5.4, December 1, 2009

Definition at line 100 of file XMLBinding.java.

Constructor & Destructor Documentation

◆ XMLBinding()

javolution.xml.XMLBinding.XMLBinding ( )

Default constructor.

Definition at line 125 of file XMLBinding.java.

125 {}

Member Function Documentation

◆ getFormat()

XMLFormat<?> javolution.xml.XMLBinding.getFormat ( Class<?>  forClass) throws XMLStreamException
protected

Returns the XML format for the specified class/interface. The default implementation returns the XMLContext#getFormat for the specified class.

Parameters
forClassthe class for which the XML format is returned.
Returns
the XML format for the specified class (never null).

Definition at line 179 of file XMLBinding.java.

180  {
181  return XMLContext.getFormat(forClass);
182  }

References javolution.xml.XMLContext.getFormat().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ readClass()

Class<?> javolution.xml.XMLBinding.readClass ( XMLStreamReader  reader,
boolean  useAttributes 
) throws XMLStreamException
protected

Reads the class corresponding to the current XML element.

This method is called by XMLFormat.InputElement#getNext() XMLFormat.InputElement#get(String) and XMLFormat.InputElement#get(String, String) to retrieve the Java class corresponding to the current XML element.

If useAttributes is set, the default implementation reads the class name from the class attribute; otherwise the class name (or alias) is read from the current element qualified name.

Parameters
readerthe XML stream reader.
useAttributesindicates if the element's attributes should be used to identify the class (e.g. when the element name is specified by the user then attributes have to be used).
Returns
the corresponding class.
Exceptions
XMLStreamException

Definition at line 203 of file XMLBinding.java.

204  {
205  try {
206  QName classQName;
207  if (useAttributes) {
208  if (_classAttribute == null)
209  throw new XMLStreamException(
210  "Binding has no class attribute defined, cannot retrieve class");
211  classQName = QName.valueOf(reader.getAttributeValue(
214  if (classQName == null)
215  throw new XMLStreamException(
216  "Cannot retrieve class (class attribute not found)");
217  } else {
218  classQName = QName.valueOf(reader.getNamespaceURI(),
219  reader.getLocalName());
220  }
221 
222  // Searches aliases with namespace URI.
223  Class<?> cls = _aliasToClass.get(classQName);
224  if (cls != null)
225  return cls;
226 
227  // Searches aliases without namespace URI.
228  cls = _aliasToClass.get(QName.valueOf(classQName.getLocalName()));
229  if (cls != null)
230  return cls;
231 
232  // Finally convert the qualified name to a class (ignoring namespace URI).
233  cls = Class.forName(classQName.getLocalName().toString());
234  if (cls == null)
235  throw new XMLStreamException(
236  "Class "
237  + classQName.getLocalName()
238  + " not found (see javolution.lang.Reflection to support additional class loader)");
239  _aliasToClass.put(classQName, cls);
240  return cls;
241  } catch (ClassNotFoundException ex) {
242  throw new RuntimeException(ex);
243  }
244  }

References javolution.xml.XMLBinding._aliasToClass, javolution.xml.XMLBinding._classAttribute, javolution.xml.QName.getLocalName(), javolution.xml.QName.getNamespaceURI(), and javolution.xml.QName.valueOf().

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

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset()

void javolution.xml.XMLBinding.reset ( )

Definition at line 291 of file XMLBinding.java.

291  {
292  _classAttribute = QName.valueOf("class");
293  _aliasToClass.clear();
294  _classToAlias.clear();
295  }

References javolution.xml.XMLBinding._aliasToClass, javolution.xml.XMLBinding._classAttribute, javolution.xml.XMLBinding._classToAlias, and javolution.xml.QName.valueOf().

Here is the call graph for this function:

◆ setAlias() [1/2]

void javolution.xml.XMLBinding.setAlias ( Class<?>  cls,
QName  qName 
)

Sets the qualified alias for the specified class.

Parameters
clsthe class being aliased.
qNamethe qualified name.

Definition at line 133 of file XMLBinding.java.

133  {
134  _classToAlias.put(cls, qName);
135  _aliasToClass.put(qName, cls);
136  }

References javolution.xml.XMLBinding._aliasToClass, and javolution.xml.XMLBinding._classToAlias.

Referenced by javolution.xml.XMLBinding.setAlias().

Here is the caller graph for this function:

◆ setAlias() [2/2]

final void javolution.xml.XMLBinding.setAlias ( Class<?>  cls,
String  alias 
)

Convenient method equivalent to setAlias(cls, QName.valueOf(alias)).

Parameters
clsthe class being aliased.
aliasthe alias for the specified class.

Definition at line 145 of file XMLBinding.java.

145  {
146  setAlias(cls, QName.valueOf(alias));
147  }

References javolution.xml.XMLBinding.setAlias(), and javolution.xml.QName.valueOf().

Here is the call graph for this function:

◆ setClassAttribute() [1/2]

void javolution.xml.XMLBinding.setClassAttribute ( QName  classAttribute)

Sets the qualified name of the attribute holding the class identifier. If the local name is null the class attribute is never read/written (which may prevent unmarshalling).

Parameters
classAttributethe qualified name of the class attribute or null.

Definition at line 157 of file XMLBinding.java.

157  {
158  _classAttribute = classAttribute;
159  }

References javolution.xml.XMLBinding._classAttribute.

Referenced by javolution.xml.XMLBinding.setClassAttribute().

Here is the caller graph for this function:

◆ setClassAttribute() [2/2]

final void javolution.xml.XMLBinding.setClassAttribute ( String  name)

Convenience method equivalent to setClassAttribute(QName.valueOf(name)).

Parameters
namethe name of the class attribute or null.

Definition at line 167 of file XMLBinding.java.

167  {
168  setClassAttribute(name == null ? null : QName.valueOf(name));
169  }

References javolution.xml.XMLBinding.setClassAttribute(), and javolution.xml.QName.valueOf().

Here is the call graph for this function:

◆ writeClass()

void javolution.xml.XMLBinding.writeClass ( Class<?>  cls,
XMLStreamWriter  writer,
boolean  useAttributes 
) throws XMLStreamException
protected

Writes the specified class to the current XML element attributes or to a new element if the element attributes cannot be used.

This method is called by XMLFormat.OutputElement#add(Object) and XMLFormat.OutputElement#add(Object, String) and XMLFormat.OutputElement#add(Object, String, String) to identify the Java class corresponding to the XML element.

Parameters
clsthe class to be written.
writerthe XML stream writer.
useAttributesindicates if the element's attributes should be used to identify the class (e.g. when the element name is specified by the user then attributes have to be used).
Exceptions
XMLStreamException

Definition at line 264 of file XMLBinding.java.

265  {
266  QName qName = (QName) _classToAlias.get(cls);
267  String name = qName != null ? qName.toString() : cls.getName();
268  if (useAttributes) {
269  if (_classAttribute == null)
270  return;
271  if (_classAttribute.getNamespaceURI() == null) {
272  writer.writeAttribute(_classAttribute.getLocalName(), name);
273  } else {
274  writer.writeAttribute(_classAttribute.getNamespaceURI(),
275  _classAttribute.getLocalName(), name);
276  }
277  } else {
278  if (qName != null) {
279  if (qName.getNamespaceURI() == null) {
280  writer.writeStartElement(qName.getLocalName());
281  } else {
282  writer.writeStartElement(qName.getNamespaceURI(),
283  qName.getLocalName());
284  }
285  } else {
286  writer.writeStartElement(name);
287  }
288  }
289  }

References javolution.xml.XMLBinding._classAttribute, javolution.xml.XMLBinding._classToAlias, javolution.xml.QName.getLocalName(), javolution.xml.QName.getNamespaceURI(), and javolution.xml.QName.toString().

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

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _aliasToClass

final FastMap<QName, Class<?> > javolution.xml.XMLBinding._aliasToClass = new FastMap<QName, Class<?>>()
private

Holds the alias (QName) to class mapping.

Definition at line 120 of file XMLBinding.java.

Referenced by javolution.xml.XMLBinding.readClass(), javolution.xml.XMLBinding.reset(), and javolution.xml.XMLBinding.setAlias().

◆ _classAttribute

QName javolution.xml.XMLBinding._classAttribute = QName.valueOf("class")
private

◆ _classToAlias

final FastMap<Class<?>, QName> javolution.xml.XMLBinding._classToAlias = new FastMap<Class<?>, QName>()
private

Holds the class to alias (QName) mapping.

Definition at line 115 of file XMLBinding.java.

Referenced by javolution.xml.XMLBinding.reset(), javolution.xml.XMLBinding.setAlias(), and javolution.xml.XMLBinding.writeClass().

◆ DEFAULT

final XMLBinding javolution.xml.XMLBinding.DEFAULT = new XMLBinding()
staticpackage

Holds the default instance used by readers/writers (thread-safe).

Definition at line 105 of file XMLBinding.java.

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

◆ serialVersionUID

final long javolution.xml.XMLBinding.serialVersionUID = 6611041662550083919L
staticprivate

Definition at line 297 of file XMLBinding.java.


The documentation for this class was generated from the following file:
javolution.xml.QName.getLocalName
CharSequence getLocalName()
Definition: QName.java:140
javolution.xml.XMLBinding._classToAlias
final FastMap< Class<?>, QName > _classToAlias
Definition: XMLBinding.java:115
javolution.xml.QName.valueOf
static QName valueOf(CharSequence name)
Definition: QName.java:82
javolution.xml.XMLBinding._aliasToClass
final FastMap< QName, Class<?> > _aliasToClass
Definition: XMLBinding.java:120
javolution.xml.QName.getNamespaceURI
CharSequence getNamespaceURI()
Definition: QName.java:150
javolution.xml.XMLBinding.setClassAttribute
void setClassAttribute(QName classAttribute)
Definition: XMLBinding.java:157
javolution.xml.XMLBinding.setAlias
void setAlias(Class<?> cls, QName qName)
Definition: XMLBinding.java:133
javolution.xml.XMLBinding._classAttribute
QName _classAttribute
Definition: XMLBinding.java:110