Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Saturday, December 22, 2007

Jibx with Netbeans

There are times when we face the need to marshall and unmarshall java objects. What better than XML for this! Most programmers can write their own XML parsers and unparsers, why do that when we have a beautiful utility tool. Infact there are several tools. Jibx is one of them, it is offers better performance than many other rivals by using the pull-event mechansim, to top that it is free for use and opensourced. Jibx essentially asks the users to provide a mapping between java objects and xml data. This mapping is provided by users in form of xml file. Lets call it binding xml. Then the users are asked to compile the the project and compile the bindings. The byte code is modified (another reason for the high performance) during this process.

A sample code snippet:
##############################

package ajibx_learning;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ResourceBundle;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;


public class Main {
/**
* Unmarshal the sample document from a file, then marshal it back out to
* another file.
*/
public static void main(String[] args) {
String ajibxProperties = "ajibx_learning.bizProperties";
String fileInput = "fileOutput";
String fileOutput = "fileInput";
String className = "className";
File fOut = null;
File fIn = null;
Object bean = null;
try {

ResourceBundle rbundle = ResourceBundle.getBundle(ajibxProperties);

fOut = new File(rbundle.getString(fileInput));
fIn = new File(rbundle.getString(fileOutput));
Class c = Class.forName(rbundle.getString(className));
bean = c.newInstance();
} catch (Exception ex) {
ex.printStackTrace();
System.out.print("Exiting ...");
return;
}
System.out.print("Unmarshalling ...");
// unmarshal customer information from file
bean = unMarshall(fIn, bean);
// you can add code here to alter the unmarshalled customer
System.out.print("marshalling ...");
// marshal object back out to file (with nice indentation, as UTF-8)
marshall(bean, fOut);
}

public static Object unMarshall( File fileIn, Object bean){
boolean status = true;
try {
// note that you can use multiple bindings with the same class, in
// which case you need to use the getFactory() call that takes the
// binding name as the first parameter
Class beanClass = bean.getClass();
IBindingFactory bfact = BindingDirectory.getFactory(beanClass);
// unmarshal customer information from file
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
if(!fileIn.exists()) {
System.out.println("File doesnt' exist: "+fileIn.getAbsoluteFile());
return status=false;
}
FileInputStream in = new FileInputStream(fileIn);
bean = uctx.unmarshalDocument(in, null);
in.close();
} catch(Exception e){
status = false;
e.printStackTrace();
} finally {
if(status)
return bean;
else
return null;
}

}

public static boolean marshall( Object bean, File fileOut){
boolean status = true;
try {
// note that you can use multiple bindings with the same class, in
// which case you need to use the getFactory() call that takes the
// binding name as the first parameter
Class beanClass = bean.getClass();
IBindingFactory bfact = BindingDirectory.getFactory(beanClass);
// marshal object back out to file (with nice indentation, as UTF-8)
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
if(fileOut.exists()){
fileOut.delete();
}
if(fileOut.createNewFile()) {
System.out.println("Creating file: "+fileOut.getAbsoluteFile());
}
else {
System.out.println("Failed to create file: "+fileOut.getAbsoluteFile());
return status=false;
}
FileOutputStream out = new FileOutputStream(fileOut);
mctx.marshalDocument(bean, "UTF-8", null, out);
out.flush();
out.close();
} catch(Exception e) {
status = false;
e.printStackTrace();
} finally {
return status;
}
}
}
###############################

I decided to use the following properties file
#bizProperties.properties
# the input file is the source of xml data
fileInput = D:\\temp\\jibx-gen\\Example1.xml
# the outfile is written by the program
fileOutput = D:\\temp\\jibx-gen\\Gen-Example1_2.xml
# the bean which I intended to use
className = beandef.Person
###############################

I then decided to use the good old 'ant' script to compile this. The issue with ant scripts was that, the code cannt be debugged straight away in IDE. So, I decided to have look at the IDE generated ant script. This itself deserved another posting!!. The expressive power of ant is better illustrated in such examples. The modifications done to build.xml are given below. Note that I was using Netbeans5.5.1
##############################

blah blah of netbeans...
Please replace < with <

<!-- modified for the sake of Jibx bindings -->
<property file="nbproject/buildExample1.properties"/>
<!-- JiBX binding compiler task definition -->

<taskdef name="bind" classname="org.jibx.binding.ant.CompileTask" classpath="${file.reference.jibx-bind.jar}"></taskdef>
<!--
-->
<target name="-post-compile">
<echo message= "${file.reference.jibx-bind.jar}" >
</echo>

<!-- Run JiBX binding compiler -->
<bind verbose="true" load="true" >
<classpath path="${run.classpath}"></classpath>
<bindingfileset dir="${binding.dir}" includes="${binding.filenamepattern}" >
</bindingfileset>
</bind>

</target>
</project>
</quote>
###################################

The modifications are done to the "-post-compile" target. The prefix "-" indicates that this target cannot be called directly. This also uses a "buildExample1.properties" file. The binding information is given this file. The bindings are compiled using this information. In case you are perplexed, the actual information for build is given in build-impl.xml. The build.xml is just a wrapper. Some of the tasks are exposed to the user for allowing customisation and in this case "-post-compile" was choosen.
The contents of buildExample1.properties:
##################################
# The directory containing binding files
binding.dir= D:\\temp\\jibx-gen
# for nested files use **/*binding*.xml, for this example we dont' need it
binding.filenamepattern = *binding*.xml
###################################
The complete code for bean and xml used are also given
##############################
package beandef;

import beandef.Address;

public class Person {
private String name;
private String surname;
private Address address;
/** Creates a new instance of Person */
public Person() {
}

}

package beandef;

public class Address {

private String houseno, street1, street2;
/** Creates a new instance of Address */
public Address() {
}

}
##################################
A good source for learning jibx is http://jibx.sourceforge.net/tutorial/binding-tutorial.html. The above mechanism shows how we can debug projects using netbeans even when there is a need for custom ant tasks.

No comments:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.