« July 2008 | Main | September 2008 »

August 2008

Aug 21, 2008

Welcome to zvikico.com

I'm happy to announce that my blog is now located at blog.zvikico.com. For now, it can also be reached at www.zvikico.com or just zvikico.com

I should've done this on the first day I started blogging and I'm sorry I didn't, as all the existing links point to my original zvikico.typepad.com. This will make it painful if I ever want to move from TypePad to a different platform (did somebody say Squarespace?  I'll be blogging about it soon). Nevertheless, I finally did it. 

It wasn't an easy setup and I'm still not sure if I did it correctly, but, for now, it seems to work. The TypePad documentation on the matter is helpful, yet inaccurate (or just plain wrong in some cases). One of the annoying things about it is that trial and error had to be done on my "production" blog and every change in the DNS records takes time to propagate through the net. If you have any issues with the blog or the feed, please let me know.

On the same note of moving, I'll be moving to a new apartment next week. I enjoy living in the heart of Tel Aviv, so I'm moving to a bigger place about 3 minutes walk from my current location. One of the main reasons is to have a proper home office. This also means I'll be off the grid for a few days during the transition. 

Aug 17, 2008

The Best JSP/Struts/JSF Development Tool Is Now Free

My previous project was based on Struts 1.x. With Struts, it was always a love-hate relationships. On one end, it was elegant and it turned some of the HTML/JavaScript work into Java work, which is a plus if you're a Java developer. On the other hand it introduced tons of XML configuration files which were a nightmare to manage. To top that, it has an incredibly steep learning curve. I used to train developers in Struts, and, IMHO, when it comes to novice developers (i.e. fresh graduates), the chances of fully understanding Struts in the first couple of months is less than 50%. 

The sheer amount of XML configuration in Struts is overwhelming. I'm talking about thousands of lines. At some point, we were working with one single file, shared by a team of about 20 developers. You can imagine the chaos. 

So, we started looking for a tool. I personally did a test drive on all the tools supporting Struts, including light alternatives like MyEclipse and behemoths like IBM RAD (used to be WSAD back then). There was only one tool which was acceptable in our case and it was called NitroX, developed by a small company called M7. Later on, it was acquired by BEA and renamed BEA Workshop, not to be confused with their incumbent BEA Workshop for WebLogic. The two tools were later merged and, eventually, found their way to the new owner, Oracle. It is now called Oracle Workshop for WebLogic. BTW, the only close alternative at the time was Exadel Studio, which was acquired by JBoss and became JBoss Developer Studio.

Last week, Oracle made an interesting move and decided to give it away for free (requires registration). The full workshop was an expensive piece of software, priced at up to $1000, if I recall correctly. What most people don't realize today is that this tool includes some Oracle/BEA specific parts, but also includes generic support for Struts 1.x, JSF and Hibernate and plugins for supporting Tomcat, JBoss, Resin, Jetty, and WebSphere. It is also a great tool for JSP editing. One of the few tools which came really close to near-WYSIWYG visual editing, with customizable design-time representation for JSP custom tags. 

If you're using any of these technologies, I suggest you check it out. I haven't tried all the features, but I know that the Struts/JSF/JSP support is as good as it gets. It currently officially supports only Windows and Linux and can be downloaded here. It is a true hidden gem now, tucked away in the humongous Oracle web site.  

Aug 06, 2008

Efficient XML Processing Using SAX and Java Enums

I had a chance to do some XML crunching lately. I used a design pattern for this purpose which is both efficient and elegant. I'm not sure who thought of this first, so I won't name it after myself (:-), but it's well worth writing about it.

Very Short Introduction to XML Processing in Java
To refresh your memory, here's a short overview of the popular ways for working with XMLs in Java. Briefly, there are two leading methods:

  • SAX - Simple API for XML - event driven method where you write a processor which receives events while the XML is being read. This is also known as "stream parser". Events include Start Document, Start Element, End Element, etc.
  • DOM - Document Object Model - means the XML is modeled a graph of nodes that may be traversed by code with methods like Get Children, Get Parent, etc.

The SAX approach is considered very fast and memory efficient, while the DOM is usually easier to handle by code, especially if the processing requires information from multiple nodes. On top of these two approaches there are many add-on tools that make the developer life a lot easier but usually take their performance toll. Most noticeably, the simplest approach, IMHO, is to take tools like JAXB or XML-Beans, which bind the XML into instances of custom classes which are modeled automatically according to the XML structure definition (DTD or Schema).

Choosing SAX
If you're looking for efficient XML processing, SAX is the way to go. It's the hard-core method: you don't get much and you pay accordingly. Writing an XML processor with SAX means implementing an interface called ContentHandler. Here's an example:

class MyContentHandler implements org.xml.sax.ContentHandler {

 public void startDocument() throws SAXException {
  ...
 }

 public void startElement(String uri, String localName, String name, 
  Attributes atts) throws SAXException {
  ...
 }

 public void endElement(String uri, String localName, String name) 
  throws SAXException {
  ...
 }
 ...
}

If your XML has many types of elements to handle, your startElement method is going to be a lengthy method with a very ugly string matching if/else block. For example, parsing XHTML would look a bit like this:

public void startElement(String uri, String localName, String name, 
 Attributes atts) throws SAXException {

 String lowerCaseName = localName.toLowerCase();

 if (lowerCaseName.equals("html")) {
  ...
 } else if (lowerCaseName.equals("head")) {
  ...
 } else if (lowerCaseName.equals("title")) {
  ...				
 } ...
}

You wanted hard core and you got it.

Enums to the Rescue
One of my favorite features in Java 5 is enumerations (enums). Since I started using them, I keep finding new uses and new design patterns which are more elegant with enums. If you're not fully familiar with enums, stop reading this article and spend the time trying out the Enums - it will be worth it.

The solution I offer is simple: each element has a corresponding enum constant which holds the code for handling that particular elements. The enum constant name will have the same name as the element name. The correct constant is selected using the Enum static method valueOf. It's a very elegant code which looks like this:

private enum ElementType {
 html {
  void startElement(Attributes atts) throws SAXException {
   ...
  }
 },
 head {
  void startElement(Attributes atts) throws SAXException {
   ...
  }
 },
 title {
  void startElement(Attributes atts) throws SAXException {
   ...
  }
 };

 abstract void startElement(Attributes atts) throws SAXException;
}

public void startElement(String uri, String localName, String name, 
 Attributes atts) throws SAXException {
 try {
  ElementType.valueOf(ElementType.class, localName.toLowerCase())
   .startElement(atts);
 } catch (IllegalArgumentException e) {
  ...
 }
}

The above code is based on the built-in valueOf method for choosing the right constant. The solution I presented is also limited by the fact that enum constants must have Java identifier names. Most noticeably, you cannot use a dash in the element name.

The Hash Table Alternative
If you look at the implementation of the valueOf method in java.lang.Enum, you'll see that it is working with a hash table behind the scenes, with enum constant names as keys. In fact, the same solution, less elegant, could be achieved by working with such a map, which holds instances, possibly anonymous classes, that implement an interface for handling the element (much like the enums above).

private interface ElementHandler {
 void startElement(Attributes atts) throws SAXException;
}

private static final Map<String, ElementHandler>  elements;

static {
 elements = new HashMap<String, ElementHandler>();
 
 elements.put("head", new ElementHandler(){
  public void startElement(Attributes atts) throws SAXException {
   ...
  }});
 ...
}

public void startElement(String uri, String localName, String name, 
 Attributes atts) throws SAXException {
 ElementHandler elementHandler = elements.get(localName.toLowerCase());

 if (elementHandler != null) {
  elementHandler.startElement(atts);
 } else {
  ...
 }
}

The above is simpler than using if/else, but it's not as elegant as using enums. As to performance, it depends on the map implementation and the number of element types in the XML. In case you have just a few element type, if/else will be more efficient for sure. Nevertheless, I'm not sure that's worth the awkward code involved. The enum option seems to me the easiest to maintain and expand.

What do you think?

About nWire

  • nWire Logo
    Browse & visualize all code association in one extremely powerful view, quick search for methods, fields and more. Boost your coding productivity in Eclipse™.
    Watch a 4 minute demo that will change the way you look at your code.
    Visit nwiresoftware.com

Twitter Updates

    follow me on Twitter
    My Photo

    My Other Accounts

    Delicious Digg Facebook FriendFeed Google Talk LinkedIn Reddit Twitter

    AddThis Social Bookmark Button
    Blog powered by TypePad
    Member since 05/2007