XMLTool – Java DSL for create XML

May 7th, 2011 thiamteck Posted in API, Java No Comments »

In Java, we usually create XML document through DOM API that come with standard JDK.

DOM API is good, but sometime the code we wrote with DOM API is verbose. Here is an example of the code required to generate a simple 8 lines XML. Yes, 13 line of Java code to generate a 8 lines XML.

Other options are using JDOM or dom4j that simplify DOM manipulation in Java.

If you just want a simple fluent API for create simple XML, XMLTool is another good option.

The sample code below quote from XMLTool Manual will demonstrate how to generate a XML with namespace via XMLTool’s fluent interface:

XMLTag doc = XMLDoc.newDocument()
    .addDefaultNamespace("http://www.w3.org/2002/06/xhtml2/")
    .addNamespace("wicket", "http://wicket.sourceforge.net/wicket-1.0")
    .addRoot("html")
    .addTag("wicket:border")
    .gotoRoot().addTag("head")
    .addNamespace("other", "http://other-ns.com")
    .gotoRoot().addTag("other:foo");

Will produce:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html xmlns="http://www.w3.org/2002/06/xhtml2/">
    <wicket:border xmlns:wicket="http://wicket.sourceforge.net/wicket-1.0"/>
    <head/>
    <other:foo xmlns:other="http://other-ns.com"/>
</html>

XMLTool is open sourced under Apache License 2.0. So feel free to give it a try.

AddThis Social Bookmark Button

Reduce Code Noise in Plain Old Java with Lombok

February 18th, 2010 thiamteck Posted in API, Java No Comments »

In dynamic languages such as Groovy, each attribute have its’ own pair of implied getter and setter method.

Lombok offer same convenience to plain old Java, via Annotation. Below is an example:

package test.lombok;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@EqualsAndHashCode(exclude="expired")
public @Data class User {

	private int id;
	private String username;
	private String email;
	@Getter(AccessLevel.PRIVATE) private boolean expired;
	
	public static void main(String args[]){
		// TODO 		
	}
}

The only annotation that do the magic is @Data. Getter and setter method for all field are available in JavaBean convention. You may optionally change the access modifier with annotation @Setter and @Getter.

Lombok is design to integrate with Eclipse, after update your Eclipse with Lombok, all methods implied will be available for code completion:

Lombok is more than just getter setter. toString(), equals(), and hashCode() methods are also available. As we know, even experienced developers might confuse about hashCode() and equals().Joshua Bloch spends chapters in his best selling book “Effective Java” to explain them.

So auto generate of hashCode() and equals() methods are really very handy, not just improve productivity, but also accuracy. You may customize the hashCode() and equals() with @EqualsAndHashCode. Lets add in some code to test the output:

package test.lombok;

import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

@EqualsAndHashCode(exclude="expired")
public @Data class User {

	private int id;
	private String username;
	private String email;
	@Getter(AccessLevel.PRIVATE) private boolean expired;
	
	public static void main(String args[]){
		 		
		User u1 = new User();
		u1.setId(10);
		u1.setUsername("Tom");
		u1.setEmail("tom@tom.com");
		u1.setExpired(false);
		
		System.out.println("U1 =====================");
		System.out.println(u1.toString()); // @Data, auto generated to String
		System.out.println(u1.hashCode());
		
		User u2 = new User();
		u2.setId(10);
		u2.setUsername("Tom");
		u2.setEmail("tom@tom.com");
		u2.setExpired(true);
		
		System.out.println("U2 =====================");
		System.out.println(u2.toString());
		System.out.println(u2.hashCode());
		
		System.out.println("U1 equals to U2? :" + u1.equals(u2));
		
		u2.setUsername("Jerry");
		
		System.out.println("U2 =====================");
		System.out.println(u2.toString());
		System.out.println(u2.hashCode());
		
		System.out.println("U1 equals to U2? :" + u1.equals(u2));
		
	}
}

And here are the output;

U1 =====================
User(id=10, username=Tom, email=tom@tom.com, expired=false)
954413294
U2 =====================
User(id=10, username=Tom, email=tom@tom.com, expired=true)
954413294
U1 equals to U2? :true
U2 =====================
User(id=10, username=Jerry, email=tom@tom.com, expired=true)
-1127824222
U1 equals to U2? :false

In conclusion, Lombok can help developers to improve productivity and improve readability of code. The implied hashCode() is the killer features of Lombok. The drawback is the JAR file with file size of 1.85MB.

Reference:
[1] http://www.ibm.com/developerworks/java/library/os-lombok/index.html

AddThis Social Bookmark Button

OpenSMPP/SMS Tools – Java Open Source SMPP API

November 14th, 2008 thiamteck Posted in API, Java No Comments »

As the name imply, OpenSMPP is an open source SMPP API that written in Java. It is conform to SMPP 3.4 while SMPP 5.0 is in the road map.

It is originate from Logica Open SMPP until version 1.3.  And was continue to be maintain in sourceforge. So you may found version 1.3.7 at OpenSMPP with some important bugs fixed. It is release for exisiting code that tie to Logica Open SMPP package namespace.

This API is handy for development of ESME that bind to SMSC. All PDU required to interact with SMSC is provided. Receiving PDU is by implements your own listener which make you code clean from thread and infinite loop.

It will really be better if there is something similar to ExceptionListener in JMS for developer to capture connection failure.

AddThis Social Bookmark Button