Welcome to this Blog. I am Software Engineer and work for Zühlke Engineering AG in Bern. This is my private blog, in which I will post mainly about technical stuff like Software Engineering or IT related topics. The views expressed herein do not necessarily represent those of my employer.

 

 
 

 

JiBX is one of the fastest XML object mapping frameworks out there (check out this performance test). Furthermore, it is very flexible in binding XML structures to objects. In this blog post I will describe, how a basic JiBX project can be set up with Maven2. The focus of this entry is on getting in touch with JiBX (and a little bit with Maven).
To use JiBX in our project, we just have to add the dependency to our pom.xml. JiBX is fast, but to get it’s high performance, it uses byte code enhancement. For this reason, the byte code need’s to be edited by JiBX after the compilation phase. This is done with a Maven JiBX plugin, which is executed in the build phase “process-classes”.

Besides, I also wanted a JAR file, which contains all dependencies and has a defined main class (to which I will come to later). The creation of the JAR file is done with the Maven Assembly.

Check out the pom.xml for this test run.

After setting up the pom.xml, we can begin to develop a mapping example with Eclipse. JiBX has two main parts on which we have to focus, binding and the runtime part.
In the binding part, JiBX enhances the byte code (based on a binding definition) to enable the mapping between Java objects and a XML files. In the runtim part, the JiBX runtime components are used to perform the marshal (objects to XML) or unmarshal (XML to objects) tasks.

As you have seen in the pom.xml, the JiBX binding files should be located in src/main/jibx. The names of the JiBX files should end with -binding.xml.

Our goal is to map an XML file, based on following schema CustomerSchema.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://stefanjaeger.ch/CustomerSchema"
  xmlns:sja="http://stefanjaeger.ch/CustomerSchema"
  elementFormDefault="qualified">
  <element name="customer">
    <complexType>
      <sequence>
        <element ref="sja:person" maxOccurs="1" />
        <element ref="sja:city" maxOccurs="1" />
      </sequence>
    </complexType>
  </element>
  <element name="person">
    <complexType>
      <sequence>
        <element ref="sja:first-name" maxOccurs="1" />
        <element ref="sja:last-name" maxOccurs="1" />
      </sequence>
    </complexType>
  </element>
  <element name="first-name" type="string" />
  <element name="last-name" type="string" />
  <element name="city" type="string" />
</schema>

Our XML Customer1.xml file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<customer xmlns="http://stefanjaeger.ch/CustomerSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://stefanjaeger.ch/CustomerSchema CustomerSchema.xsd">
  <person>
    <first-name>Stefan</first-name>
    <last-name>Jaeger</last-name>
  </person>
  <city>Davos Dorf</city>
</customer>

We want to map the XML in the two objects Customer and Person (to simplify these lines of code, the getter, setter and toString methods are not described).

public class Customer {
  private Person person;
  private String city;
}
 
public class Person {
  private String firstName;
  private String lastName;
}

To map the XML file to these classes, we create following binding definition. Because we use a XML schema, we have to define the namespace. Also the fully qualified name of the mapped class Customer need’s to be used. Because the class Person is in relation with the Customer class, JiBX recognizes automatically the fully qualified name of Person.

<?xml version="1.0" encoding="UTF-8"?>
<binding>
  <mapping name="customer" class="ch.stefanjaeger.jibx.Customer">
  <namespace uri="http://stefanjaeger.ch/CustomerSchema" default="elements"/>
    <structure name="person" field="person">
      <value name="first-name" field="firstName" />
      <value name="last-name" field="lastName" />
    </structure>
    <value name="city" field="city" />
  </mapping>
</binding>

Now, we have a XML file, some Java classes and a binding between them. What we now need is an application, which does the unmarshalling of the XML file.

public class JiBXDemoApplication {
  public static void main(String[] args) throws Exception {
    IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
    IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
    Customer customer = (Customer) uctx.unmarshalDocument(
      new FileInputStream("Customer1.xml"), null);
 
    System.out.println(customer.toString());
  }
}

We are finished now. Just build the project with the command mvn package and let’s see, how the test run is working. Remember to place the XML schema and the XML file to the same location as the JAR file (after mvn package, it should be located in /target/).

The command

java -jar jibx_playground-1.0-SNAPSHOT-jar-with-dependencies.jar

results in

ch.stefanjaeger.jibx.Customer@12d03f9
[person=ch.stefanjaeger.jibx.Person@15dfd77
 [firstName=Stefan,lastName=Jaeger]
,city=Davos Dorf]

I have to admit, that I did the same example with ANT. It tooks me a little bit longer… From today on, I’m a Maven fan ;-)

Download here the source code of this example. You only have to run mvn package to build the project.

 

 

 
 

 

If you press CTRL + O in Eclipse, a list with all members and methods shows up. Red highlighted are private members/methods, green public ones. But unfortunately, inherited methods are invisible.

image

Now, just press CTRL + O again and you can see all inherited methods.

image

I know, the hint is mentioned in the bottom right corner, but I didn’t noticed this until today…

 

 

 
 

 

How many times in your lifetime have you overwritten the functions equals(), hashCode() or toString()? I did many times…

The Apache Commons Lang Package offers three basic classes for building the equals(), hashCode() or toString() method.

  1. EqualsBuilder
    JavaDoc of the EqualsBuilder
    All available fields can be append to the EqualsBuilder, which checks, if two objects are equals. It works with all primitive datatypes and objects (if they have implemented the equals method, of course).
     
  2. HashCodeBuilder
    JavaDoc of the HashCodeBuilder
    If you overwrite the equals() method, you also have to overwrite the hashCode() method. This builder has almost the same methods like the EqualsBuilder, but you should define two randomly chosen odd numbers in the constructor. Ideally the numbers are unique over all classes in the program.
     
  3. ToStringBuilder
    JavaDoc of the ToStringBuilder
    Appends all the desired members and prints something like Person@7f54[name=Stephen,age=29,smoker=false]

 

 

 
 

 

I have to admit that I have spend in recent years a lot of my free time into developing with PHP. For me, PHP offers a lot of nice features and it is a easy to write script language. Of course, it’s not Java and it lacks in some features (like type safety, maintainability, and so on).

 

But in PHP there were two functions, which I used a lot. Implode and Explode. These two functions offered a easy way to split or concatenate strings. Let me make two examples.

 

explode: splits the string “a;b;c;d” into the array (“a”,”b”,”c”,”d”)
implode: concatenate the elements of the array (“a”,”b”,”c”,”d”) into the string “a;b;c;d”

 

Of course, the delimiter (the ; in the example) can be chosen individually.

 

Now, I was searching for such functions in Java. Especially the implode method I missed a lot. But there is a open source framework, which holds the solution. It’s the Lang package of the Apache Commons project (http://commons.apache.org/lang/). Inside this framework, there is the StringUtils class, which offers some interesting string operations.

 

StringUtils.join(Collection collection, String separator) is the same as implode.
StringUtils.split(String str, String separatorChars) is the same as explode.

 

Of course, StringUtils offers some other nice features. Just try it out!

 

 

 
 

 

If you double click on a tab in Eclipse, the tab get’s opened all over the window. Now, if there is a build process running in the console, the console is always popping up. This is a little bit annoying. But there is a simple trick to solve this situation. Just disable “Show Console When Standard Out Changes” and the console will not pop up again.

image

 

 

 

« Newer PostsOlder Posts »