|
IntroductionThis page presents a simple example as a quick start, an overview of the OMaP architecture, the simple example revisited, the unit tests of the project and the mapping language definition. Quick StartSay our application uses objects of the The class definition
Mapping the classNow, let's write the code that writes objects mapped to XML format to the console.
The <?xml version="1.0" encoding="Cp1252"?>
<hello name="MyName"> <message><![CDATA[Hello, world!]]></message> </hello> What does the
|
alias net.sf.omap.example.Hello Hello Hello -> hello name @-> message |
Hello
instead of
net.sf.omap.example.Hello
.Hello
class to a hello
XML element.
name
property as an attribute with the same
name as the property.
message
property as child element with the same
name as the property.
The previous example shows an XML facade
(net.sf.omap.xml.XMLMapper
)
that makes OMaP usage simpler.
Behind that facade, everything is based on the net.sf.omap.MappingExpert
interface. An expert object registers all the mappers associated with all types.
A mapper can be registered as a default mapper or can be set in a named category.
A mapper must implement the net.sf.omap.ObjectMapper
interface.
As we said that a mapper expert contains all the registered mappers that can either be registered in the default mapper category or in a specific one, let's see what mapper resolution is applied when the expert is performing some mapping.
Next example shows a code snippet where the default mapping is used:
expert.map(anObject);
|
anObject
) is null the mapped value (the value
returned by the map
method) is null.Here are the steps followed by the expert object:
Object map(Object)
methodString
using the toString()
method.
Once the mapping is delegated to the mapper, the expert loses control but, as the mapper knows its expert object, it should call the expert when it needs to map a type it cannot handle.
If you are curious about how this should be implemented, see the default object to object mapper.
Here is a sample using a mapping for a given category:
expert.map("myCategory", anObject);
|
The expert object follows next steps:
MappingException
Object map(Object)
methodTo create a mapper either define a normal Java class, either use a script that defines the mapping, the scripting language is the mapping definition language.
The same mapping definition can be used for classes with different definitions. Using the mapping definition language has, at some extend, the advantage that you can change the classes and keep the same mapping definitions, see the SimpleXMLMappingTest test class.
Let's create two different mappings for our Hello
class,
using a lower level approach
(see TwoMappings.java).
Create an object to XML mapping expert object, and then register the two ways of mapping, name the first "short" and the second "long".
ObjectToXMLMappingExpert expert = new ObjectToXMLMappingExpert();
|
Hello o = new Hello();
|
System.out.println(expert.map("short", o));
|
<hello name="MyName"> </hello> <hello name="MyName"> <message><![CDATA[Hello, world!]]></message> </hello>
<mapping definition script> ::= [<alias section>] <mapping section> <alias section> ::= {<alias definition>} <alias definition> ::= alias <java class name> <alias> eol <alias> ::= <entity name> <mapping section> ::= {<mapping definition>} <mapping definition> ::= <entity name> [-> <entity name> ] eol <fields mapping> <fields mapping> ::= ident <entity name> [-> <entity name> [as 'format']] eol | ident <entity name> [@-> <entity name> [as 'format']] eol <entity name> ::= {character | - | .}
java.text.DecimalFormat
format
pattern definition, either the java.text.SimpleDateFormat
one.
hello-world
becomes helloWorld
or getHelloWorld
when used as bean property.a.b.3.c
is valid if 'b' leads to a list or an array object.
alias net.sf.omap.model.contact.ContactBook ContactBook alias net.sf.omap.model.contact.Contact Contact alias net.sf.omap.model.contact.Address Address alias net.sf.omap.model.contact.Phone Phone ContactBook -> book creation-date -> date as 'yyyyMMdd' all-contacts -> contacts Contact -> contact name -> name firstName @-> name lastName @-> name Phone -> phone number -> number Address -> address name