Recently in java Category

I was working on a RSS to JSON XSL transform for some work related stuff. For reasons not to be disclosed, the updated timestamp of the item was to be parsed and serialized into JSON as an array of integers. There were bunch of problems to that. XSL or EXSL, for that matter, does not have support for parsing RFC822 dates. This can be solved by writing my own template for parse the date or even better write a static java method and use that in the transformation. I took the later approach (coz its easier) because its easier and having a our code base could use a utility method to parse RFC822 dates. Even better I wrote it so that it will take in a RFC822 date string and return a Atom date (ISO whatever same as XSD date). So now my transformation can parse the date and give me the individual bits i needed and also our codebase has a nice utility method(this such a lame reason...). Parsing a RFC822 date (for that any matter any standard format date) is a bit tricky thing in java. Well, its not all that tricky now that I have figured it. We have the DateFormat class we can use to parse the date string to date. But as RFC822 date though a single format has there are parts of the dates which are optional e.g. second, Day of the Week. so we need multiple DateFormat instances (actually SimpleDateFormat) to support the format fully ( I realized this only after my transform started failing for feed which were not providing "seconds" values in the timestamp). Below is the code snippet to parse the date string.
public static final SimpleDateFormat rfc822DateFormats[] = new SimpleDateFormat[] { new SimpleDateFormat("EEE, d MMM yy HH:mm:ss z"), new SimpleDateFormat("EEE, d MMM yy HH:mm z"), new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z"), new SimpleDateFormat("EEE, d MMM yyyy HH:mm z"), new SimpleDateFormat("d MMM yy HH:mm z"), new SimpleDateFormat("d MMM yy HH:mm:ss z"), new SimpleDateFormat("d MMM yyyy HH:mm z"), new SimpleDateFormat("d MMM yyyy HH:mm:ss z"), };
I want to note that the order in which this objects are applied is important. Mainly because of the effect of using "yy" and "yyyy". You can lookup the javadocs for the pattern to see what I am saying. Closing note: I like the fact that Atom has adopted the ISO date standard, I think its more widely supported.
Let me start by saying..JAXP 1.3 rocks..why?
  • It supports XPath (duh !!)
  • Dom level 3
  • And other stuff i am still to discover.
I don't get, is it just me or XPath in jaxp is really really late. But good that its here and good news is can be used with JDK 1.4 and probably JDK 1.3 (haven't tested it). So the installation process.
  1. go to jaxp.dev.java.net
  2. download jaxp installation class file which has been compiled for JDK 1.4
  3. java -cp . JAXPR******
  4. this gives u 5 jar files (dom.jar,sax.jar,xercesImpl.jar,xalan.jar(*sigh*),jaxp-api.jar)
  5. copy all jars in <java_home>/jre/lib/endorsed folder.
  6. tada !!
The updating process documented on the JAXP website suggests that jaxp-api.jar should not be put in endorsed folder and should be put some where on the CLASSPATH. But as the JAXP 1.2 classes packaged with JDK 1.4 are part of rt.jar, these classes will take precedence when loading classes over any classes on the CLASSPATH. In order to avoid this, you need to have jaxp-api.jar also in the endorsed directory. As per the Endorsed Standards Override Mechanism classes specified in the endorsed directory are loaded before any internal class. so there you have it..you are off you using jaxp 1.3. I look forward to exploring more of this release.

About this Archive

This page is a archive of recent entries in the java category.

India is the previous category.

links is the next category.

Find recent content on the main index or look in the archives to find all content.