Esquire Theme by Matthew Buchanan
Social icons by Tim van Damme

22

Jul

Parsing and Converting common formats in Java

If you are creating a Web Application in Java you need to know how to correctly and efficiently convert all kinds of string represented data. Below are some of the best Java libraries for parsing and converting various formats in the web world.

Creating URIs:
UriBuilder is perhaps the most elegant way to create URI’s safely (that includes URL since URI is a superset).

Encoding and Decoding URIs:
Spring UriUtils for various times in which you only need to decode/encode certain parts of the URL. Whatever the case you almost always do not want to use java.net.URLEncoder which is about form encoding and not URL/URI encoding. The name URLEncoder is a misnomer and should probably be called FormEncoding.


Parsing URI Query parameters:

You would think parsing Query parameters would be built into Java but is not. Its a shame because people often try to write their own URI Query parsers… bad idea.
Commons HttpClient
URLEncodedUtils



Escaping for XML, HTML, Java, and JS:
Commons StringEscapeUtils
Taglib Util
The taglib util is infact the same as the famous fn:escapeForXml() you see in JSTL.

Converting HTML to Text:
Jsoup.parse(html.replace(“ ”, “”)).text();

Gracefully downgrading a UTF-8 String with weird characters to ASCII:

Some times you need to turn a string into ASCII either to be used for friendly URLs or for search engine query synonyms.


Parsing and Formating Dates:
Joda Time http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#parse(java.lang.String)

While it may seem like a good idea to use SimpleDateFormat it isn’t if you plan on doing this thread safe (it is not so you will have recreate the object or thread-local it).

For formatting you can use Joda but I prefer Commons FastDateFormat which is much faster than SimpleDateFormat and thread safe.

Parsing JSON:
Jackson http://jackson.codehaus.org/