Kirk Evans Blog

.NET From a Markup Perspective

Sax.net and Fun with JLCA 2.0

I have a project that I have been putting off for several months now.  For my own edification, I have been implementing a .net implementation of a SAX parser.  My first attempt at an implementation was written by hand while looking at the spec. Things that bogged me down included figuring out what was part of SAX1 and what was part of SAX2, copying and pasting the comments from the java docs to the C# files, converting the comment syntax to C# XML code comments, and deciding just how how faithful I should be to the spec. 

Being recently reminded of this project, I decided to pick it back up and see where I left off.  The project was in shambles. The SAX spec is pretty closely tied to the Java language, so there were several comments in my code reminding me that I couldn’t make up my mind on which way to go.  Should I fit System.Xml underneath the presribed interfaces, or simply yank out the parts of SAX that just don’t make sense replicating in light of what System.Xml already provides?  Should I verge from the SAX spec knowing that anything less than faithful adherence would rankle the feathers of the purists? Comments are welcome.

I decided to take a different approach by letting the Java Language Conversion Assistant (JLCA) decide for me in most of the minute cases.  JLCA is a tool used from within Visual Studio .NET to automatically convert Java-language code into Microsoft Visual C#. Using it is dead-simple.

  1. Open Visual Studio .NET.
  2. Click the File/Open/Convert menu item. 
  3. Select Java Language Conversion Assistant.  Choose the option to create a new solution or include in the current solution and click OK. Click Next on the welcome wizard screen.
  4. The next screen asks what you want to convert. For the SAX project, I chose the second option, “A directory containing the project’s files.“
  5. The next screen asks for the directory containing the files you want to convert. I chose a parent folder that contained the entire org.xml.sax download.
  6. Finally, I specified the name of the project, the directory where the converted files should reside, and the type of project (here, it is a Class Library).  Click next a few times, and the conversion begins.

Having worked through the SAX code previously (actually, twice previously, but that is bad source code management on my part), I was under no impression that it would seamlessly convert. I was amazed at the items it did convert, and the conversion report was a very nice surprise.

 

The JLCA provides a formatted conversion report to let you know where to look for potential issues.

Here, you can see that the JLCA provides a list of the errors, issues, and warnings. Even cooler is that the plus sign to the left of an item expands to show the items within a file that had issues.

The JLCA conversion report shows the items within a file that had conversion problems.

This image shows the break-down of items by type and severity, and notice the items with type “To Do”: these problems end up as “UPGRADE_TODO” comments in the code, which are subsequently picked up by the Visual Studio .NET task list:

When conversion problems occur, JLCA marks the code with a comment.

With literally a handful of keystrokes I was able to convert a significant amount of java language code to C#, leaving me with the more interesting points of the conversion and getting rid of the monotony for me.  I still have my anal-retentive tasks to perform, such as upgrading the comments to work with VS.NET’s code comment builder (and Roeder’s Commenter as well).  But the JLCA took a large amount of that pain away from me quickly.