Skip to content

API Details

Alessandro Vurro edited this page Oct 20, 2016 · 27 revisions

On this page are described in detail all the components of JMapperAPI.

General Tips

in order to improve readability and simplify the implementation it is strongly recommended to import static methods provided by JMapperAPI:

...
import static com.googlecode.jmapper.api.JMapperAPI.*;
...

The root

The starting point is JMapperAPI, you need to create it to define a configuration:

JMapperAPI api = new JMapperAPI();

An instance of api permits to do two actions: add mapped classes and print the XML as String format:

  • add(..) takes as input an instance of MappedClass.
  • toXStream() returns the bean xstream annotated, to print the xml just call the method toString() on it.

Mapped Classes

To define a Mapped Class what you need is create an instance of MappedClass passing the class or a the literal name of that:

MappedClass clazz = new MappedClass(aClass.class);
//or
MappedClass clazz = new MappedClass("package.aClass");

and after add it in api instance:

api.add(clazz);

for readability is recommended to use static methods provided by JMapperAPI (in this case mappedClass method):

api.add(mappedClass(aClass.class));
//or
api.add(mappedClass("package.aClass"));

as JMapperAPI MappedClass has only two methods:

  • add(..) method takes as input an instance of Attribute, Global or Conversion.
  • toXStream() method returns the bean with xstream annotations, to print the xml just call the method toString() on it.

Attributes

To define an Attribute what you need is create an instance of Attribute passing the literal name of that:

Attribute attribute = new Attribute("attributeName");
//or name with custom methods
Attribute attribute = new Attribute("attributeName","customGet","customSet");

and after add it in MappedClass instance:

mappedClass.add(attribute);

for readability is recommended to use static methods provided by JMapperAPI (in this case attribute method):

mappedClass.add(attribute("attributeName"));

Attribute has methods:

  • value(..) that takes as input an instance of TargetAttribute or the target name as String literal.
  • targetAttributes(..) that takes as input one or more instance of TargetAttribute or one or more target names as String literal.
  • targetClasses(..) that takes as input one or more classes, the defined classes are the classes of target fields, the relationship is defined by position.
  • customGet(..) that takes as input the custom get method name.
  • customSet(..) that takes as input the custom set method name.
  • toXStream() method returns the bean with xstream annotations, to print the xml just call the method toString() on it.

Target Attributes

To define a Target Attribute what you need is create an instance of TargetAttribute passing the literal name of that:

TargetAttribute targetAttribute = new TargetAttribute("targetAttributeName");
//or name with custom methods
TargetAttribute targetAttribute = new TargetAttribute("targetAttributeName","customGet","customSet");

and after add it in Attribute instance:

attribute.value(targetAttribute);

for readability is recommended to use static methods provided by JMapperAPI (in this case targetAttribute method):

attribute.add(targetAttribute("targetAttributeName"));

TargetAttribute has methods:

  • customGet(..) that takes as input the custom get method name.
  • customSet(..) that takes as input the custom set method name.
  • toXStream() method returns the bean with xstream annotations, to print the xml just call the method toString() on it.

Global

To define a Global what you need is create an instance of Global using the empty constructor:

Global global = new Global();

and after add it in MappedClass instance:

mappedClass.add(global);

for readability is recommended to use static methods provided by JMapperAPI (in this case attribute method):

mappedClass.add(global());

Global has methods:

  • value(..) that takes as input an instance of TargetAttribute or the target name as String literal.
  • includedAttributes(..) that takes as input one or more instance of LocalAttribute or one or more target names as String literal.
  • excludedAttributes(..) that takes as input one or more attributes names as String literal.
  • targetClasses(..) that takes as input one or more classes, the defined classes are the classes of target fields, the relationship is defined by position.
  • toXStream() method returns the bean with xstream annotations, to print the xml just call the method toString() on it.

Local Attributes

To define a Local Attribute what you need is create an instance of LocalAttribute passing the literal name of that:

LocalAttribute localAttribute = new LocalAttribute ("localAttributeName");
//or name with custom methods
LocalAttribute localAttribute = new LocalAttribute ("localAttributeName","customGet","customSet");

and after add it in includedAttributes method:

includedAttributes.value(localAttribute);

for readability is recommended to use static methods provided by JMapperAPI (in this case localAttribute method):

includedAttributes.add(localAttribute("localAttributeName","customGet","customSet"));

LocalAttribute has methods:

  • customGet(..) that takes as input the custom get method name.
  • customSet(..) that takes as input the custom set method name.
  • toXStream() method returns the bean with xstream annotations, to print the xml just call the method toString() on it.

Conversion

To define a Conversion what you need is create an instance of Conversion passing the literal name of that:

Conversion conversion = new Conversion("conversionName");

and after add it in MappedClass instance:

mappedClass.add(conversion);

for readability is recommended to use static methods provided by JMapperAPI (in this case conversion method):

mappedClass.add(conversion("conversionName"));

Conversion has methods:

  • type(..) that takes as input the enumeration Type (that can be STATIC or 'DYNAMIC).
  • from(..) that takes as input one or more source fields.
  • to(..) that takes as input one or more destination fields.
  • body(..) that takes as input the body of the conversion.
  • avoidSet(..) that takes as input a boolean.
  • toXStream() method returns the bean with xstream annotations, to print the xml just call the method toString() on it.
Clone this wiki locally