|
|
Disclaimer:
These pages about different languages / apis / best practices were mostly jotted down quckily and rarely corrected afterwards. The languages / apis / best practices may have changed over time (e.g. the facebook api being a prime example), so what was documented as a good way to do something at the time might be outdated when you read it (some pages here are over 15 years old). Just as a reminder. Jibx: mapping xml to javajust random scribblings I made when developing with jibx
Used for mapping XML to Java objects.
ImportantUse the attribute set-method, get-method instead of field.Supposedly, that way JIBX does NOT need to modify the compiled class, it just verifies that the settters/getters exist in the class. However, for me it stills edit the compiled class... InheritancePerson inherits from MediaObject.java
mediaobject_binddef.xml:
<binding>
<mapping ordered="true" abstract="true" class="com.example.app.content.model.MediaObject">
<value name="firstname" field="headline" />
<value name="lastname" field="text" />
</mapping>
</binding>
person_binddef.xml:
<binding>
<include path="mediaobject_binddef.xml"/>
<mapping ordered="true" name="customer" class="com.example.app.content.model.Person" extends="com.example.app.content.model.MediaObject">
<structure map-as="com.example.app.content.model.MediaObject" />
<value name="pppp" field="name" />
</mapping>
</binding>
Mailing listhttp://www.nabble.com/JiBX---XML-Data-Binding-for-Java-f4284.htmlAttributes
<sometag><customer id=\"42">"
+ "<firstname>AName11</firstname>"
+ "<lastname>LastName</lastname>" + "<pppp>oooo</pppp>"
+ "</customer></sometag>
<binding>
<include path="mediaobject_binddef.xml" />
<mapping ordered="true" name="sometag"
class="com.example.app.content.model.Person"
extends="com.example.app.content.model.MediaObject">
<structure name="customer">
<value style="attribute" name="id" field="someid" />
<structure
map-as="com.example.app.content.model.MediaObject" />
<value name="pppp" field="name" />
</structure>
</mapping>
</binding>
Mapping - Custom classeshttp://jibx.sourceforge.net/tutorial/binding-structures.html
<structure name="place_of_birth" usage="optional">
<structure name="geographic_item" field="placeOfBirth">
<value name="id" field="myId" usage="optional" />
<value name="name" field="name" usage="optional" />
</structure>
</structure>
Whatever{
Geograhpy placeOfBirth;
}
Geography {
String myId;
String name;
}
Mapping - DateWithin the <mapping tag>
<format label="Date_dd.mm.yyyy" type="java.util.Date"
serializer="com.example.app.util.JibxSerializer.serializeGermanDate"
deserializer="com.example.app.util.JibxSerializer.deserializeGermanDate" />
<value name="date_of_birth" field="dateOfBirth"
usage="optional" format="Date_dd.mm.yyyy"/>
public class JibxSerializer {
private static SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");
public static String serializeGermanDate(java.util.Date date) {
if(date==null)
return null;
return sdf.format(date);
}
public static java.util.Date deserializeGermanDate(String date)
throws ParseException {
if(date==null)
return null;
return sdf.parse(date);
}
}
Mapping - java.sql.Time
The problem:
I am reading in some xml where I have a time that I map to an
attribute of type java.sql.Time.
The problem is that the input time will be stored with an additional
hour in the object,
e.g. 23:45 in the xml will be 00:45 in the object
13:15 in xml will be 14:15 in the object
in the binding:
...
<value name="timeSent" field="timeCreated"/>
...
in the class:
...
private Time timeCreated;
...
my input xml:
...
<timeSent>14:42:00</timeSent>
...
after parsing the xml, the object contains the time 15:42:00.
The solution:
<format label="TIME_serializer" type="java.sql.Time"
serializer="com.example.app.util.JibxSerializer.serializeTime"
deserializer="com.example.app.util.JibxSerializer.deserializeTime" />
<value name="timeSent" field="timeCreated" usage="optional" format="TIME_serializer"/>
private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
public static String serializeTime(java.sql.Time time) {
if (time == null)
return null;
return timeFormat.format(time);
}
public static java.sql.Time deserializeTime(String time)
throws ParseException {
if (time == null)
return null;
Date date = timeFormat.parse(time);
Time t = new Time(date.getTime());
return t;
}
Mapping - collectionsnationalites is abstract List, so we must say which factory to use when instantiating objects
<structure name="nationality" usage="optional">
<collection field="nationalities" factory="org.jibx.runtime.Utility.arrayListFactory">
<structure name="geographic_item" type="com.application.content.model.Geography">
<value name="id" field="someId" usage="optional" />
<value name="name" field="name" usage="optional" />
</structure>
</collection>
</structure>
Inner classes
<structure name="position" type="com.wahtever.content.model.Person$Position">
<value name="name" field="name" usage="optional" />
</structure>
Multiple bindingsgive the name of the binding to use to the getFactory method (the name is the same as the file name as default)
m_bindingFactory = BindingDirectory.getFactory("chunk_person_binddef", mclas);
Ignore elements in the xml
<structure name="element_name_to_ignore" usage="optional"/>
How to use in unit testhttp://www.sosnoski.com/jibx-wiki/space/usage-questions/junit-test-bindings
private static final String MY_CLASS = "com.example.app.test.mock.Person";
private static final IBindingFactory m_bindingFactory;
static {
try {
// set paths to be used for loading referenced classes
URL[] urls = Loader.getClassPaths();
String[] paths = new String[urls.length];
for (int i = 0; i < urls.length; i++) {
paths[i] = urls[i].getFile();
}
ClassCache.setPaths(paths);
ClassFile.setPaths(paths);
// find the binding definition
ClassLoader loader = OurClassTest.class.getClassLoader();
InputStream is = loader.getResourceAsStream("com/example/app/binddef/person1Binddef.xml");
if (is == null) {
throw new RuntimeException("binding definition not found");
}
// process the binding
BoundClass.reset();
MungedClass.reset();
BindingDefinition.reset();
BindingDefinition def = Utility.loadBinding("person1Binddef.xml",
"binding", is, null, true);
def.generateCode(false);
// output the modified class files
MungedClass.fixChanges(true);
// look up the mapped class and associated binding factory
Class mclas = Class.forName(MY_CLASS);
m_bindingFactory = BindingDirectory.getFactory(mclas);
} catch (JiBXException e) {
throw new RuntimeException("JiBXException: " + e.getMessage());
} catch (IOException e) {
throw new RuntimeException("IOException: " + e.getMessage());
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFoundException: "
+ e.getMessage());
}
}
*/
/**
* * Read a schema definition into model from stream. * *
*
* @param is
* schema input stream *
* @return schema element *
* @throws Exception
*/
/*
protected Person readSchema(InputStream is) throws Exception {
IUnmarshallingContext ictx = m_bindingFactory
.createUnmarshallingContext();
return (Person) ictx.unmarshalDocument(is, null);
}*/
EclipseHow to bind the compiler into eclipse with ANThttp://jibx.sourceforge.net/bindcomp.html More programming related pages |
|