Archive for December, 2008
JEdit Macro for XML/HTML Special Character Conversion
One of the common requirement I face often when posting text to blog/forum/Wiki etc. is character conversion:
| & | & |
| < | < |
| > | > |
| " | " |
| ' | ' |
I developed a small JEdit Macro for doing this. This macro can be executed from JEdit menu Macros > Run Other Macro..., or can be installed by copying to: ${user.home}/.jedit/macros/ (later executed from the menu Macros). This macro operates on selected text.
Search for the perfect Ajax auto-complete library
Yesterday we (Krithika and myself) spent some time finding a perfect Ajax auto-complete solution for one of our internal projects. We wanted to have a tag search box with auto-complete functionality. We had created a front-end layer for our Java tagging API (developed by Arun) using RESTful interface. This exposed searched result in JSON. So our need was for a Ajax library which provided auto-complete feature which satisfied following parameters:
- The code we need to incorporate for enabling auto-complete should be minimal.
- Should not be flashy.
- Should consume JSON content.
This seemed to be a trivial task, but it took quite some time to find and finalize on AutoComplete 1.2 by Beau Scott. This script was simple to integrate, and did not require us to configure any sophisticated CSS. It allowed us to consume our RESTful service without any modification.
During our search, we were surprised to find how badly certain popular Ajax libraries implemented this oft-used functionality. The famous Scriptaculous library’s implementation has the implementation detailed here: http://github.com/madrobby/scriptaculous/wikis/ajax-autocompleter. If you go through this document, you will see that the script requires the server to return data in this format:
<ul>
<li>your mom</li>
<li>yodel</li>
</ul>
This is bad design. A server code returning data as HTML. This cannot be consumed effectively by other clients. The next one which we picked up was YUI Autocomplete. The amount of configuration effort needed from our side scared us. We didn’t plunge deeper!
Another one which we tried was DevBridge’s Auto Complete. This looked simple to implement, and the look and feel was simplistic. But it had an issue: the call to the server-side code was passed as a query string (query=). We were using RESTful URL and we cannot modify our backend-code to comply to the requirements of the UI layer. But I did a little hack and made this script compliant to our requirement. But finally, we decided against using this patched-script because it had additional dependencies like images and CSS styles to be specified.
As said earlier, the script which matched all our requirements was AutoComplete 1.2 by Beau Scott.
The only worry for me from this exercise was the divide between the front-end developers and backend developers. Such a common functionality was not being addressed by popular frameworks without sacrificing functionality and addressing usability–and, more importantly, complying to established enterprise patterns like consuming data (like JSON and XML instead of HTML snippet) and support for RESTful URLs.
Hope 2009 annihilates this divide
WebService Client in JAX-WS (Java 6 and above)
Given a WSDL URL, you may generate the Java client classes using the JDK provided tool wsimport. The basic usage:
$ wsimport <wsdl_url>
The common command-line parameters supported by this tool:
| -p | specifies the target package |
| -d | folder where generated class files are placed |
Ruby's two most popular Web Frameworks merge
Ruby on Rails and Merb, two of the most popular OpenSource web frameworks for Ruby merged. The merged version will be released as Ruby on Rails 3.0.
On Pairing: By John De
John De, President of N-Brain, has to say this about pairing:
… I discovered that two heads really are better than one. Developers have different ways of interpreting code, and different ways of solving problems. Combining this rich diversity creates a strength unequaled by any single developer. Put me in a room with a junior programmer, and turn us loose on some task, and I guarantee you that I will gain new insight into the problem from this developer, and that our resulting solution will be stronger than anything I could have come up with alone.
Read full interview here: http://java.dzone.com/news/interview-john-de-goes-free-un.
JFXBuilder: JavaFX Visual Design Tool
The first design tool for JavaFX is available here: http://www.reportmill.com/jfx/. The product page does not discuss any licensing or pricing detail. The current version is available as WebStart application for free use.
JEdit, powerful search and replace using BeanShell
JEdit is my favorite text editor. And today I was pleased to find some powerful scripting capability in its search and replace functionality. Basically I wanted to enclose a RegularExpression pattern with:
<a href="">PATTERN</a>
The Search and Replace dialog provided an option Return value of a BeanShell snippet. So whatever RegularExpression pattern grouping I provided, I was able to access them using the BeanShell variable _1 to _9. So my final replace statement became:
"<a href\"\">" + _1 + "</a>"
Cool solution to a simple problem.

Implementing a DI Framework for WizTools.org RESTClient
I wanted very basic DI functionality for RESTClient. Just wanted a mock implementation to execute when doing a mvn test, and real implementation when executed otherwise. So the functionality was simple. My immediate requirement did not require constructor injection or setter injection. And all the beans instantiated had to be singletons. No other fancy requirement. So for this limited functionality, I was able to design a small framework in two classes: DIFramework.java and DIException.java. For mapping the implementation to the interface, it is managed using a simple property file with content:
org.wiztools.restclient.IGlobalOptions = org.wiztools.restclient.GlobalOptions |
The mock implementation in the test execution would be:
org.wiztools.restclient.IGlobalOptions = org.wiztools.restclient.MockGlobalOptions |
org.wiztools.restclient.IGlobalOptions is the interface, and org.wiztools.restclient.GlobalOptions and org.wiztools.restclient.MockGlobalOptions are the implementation. To get instance of implementation bean:
IGlobalOptions obj = DIFramework.getInstance(IGlobalOptions.class);
This would return two different beans in different execution environments based on the property control.
DI: Annotation or XML Configuration
Recently I have been looking at couple of DI containers in Java. These are: Google Guice and Spring Framework. I did not like Guice in the first look itself. It involved annotating my beans with Guice specific annotations. I felt this was defeating the purpose of POJO. XML configuration using Spring was more natural for me.
JavaDoc: Avoid Manually Escaping HTML Special Characters
I discovered this new tip: when writing JavaDoc comments, we do not need to escape HTML characters. The magic happens when we enclose such content inside {@literal ...}. So typically, a comment would look like:
{@literal x < y}
There is another JavaDoc quickie in this line. It is {@code ...}. This is also like {@literal ...}, but displays the code in a different mono-spaced font.
James A Gosling's Cool Projects
Gosling, father of Java, contributes to some cool projects too. I found the following two:
Google Native Client: ActiveX replacement
If there is one web-technology that I hate, it should be ActiveX. Google engineers seem to have faced the same issue. They developed Native Client. Now it is easier, more secure to develop and run native code in any browser in any platform. Thanks guys
Google Chrome comes out of Beta!
It is really surprising how fast it has come out of beta (compared to other Google products). View the official blog announcing it. For people using Windows XP and Vista (the only supported platforms of Google Chrome), the download is available here: http://www.google.com/chrome/.
JavaScript: Dynamically Creating and Manipulating DOM Elements
I was pleasantly surprised to see that JavaScript now supports XML-DOM methods for manipulating JavaScript DOM trees. A quick look at the features:
Init
Before we proceed with the examples, the setup part. All the examples that follow assume the existence of this in the HTML page:
<div id="placeholderId"/>
Creating new element
Let us assume the requirement of placing <br/> element inside the placeholder. The code:
var e = document.getElementById('placeholderId');
var eBr = document.createElement('br');
e.appendChild(eBr);
Adding attribute
Suppose we want to add the attribute id="brId", we would write:
eBr.setAttribute('id', 'brId');
Deleting attribute
eBr.removeAttribute('id');
Deleting element
var e = document.getElementById('brId');
var eParent = e.parentNode;
eParent.removeChild(e);
Creating elements with text element
Consider that we want to have the following paragraph element inside the placeholderId <div>:
<p align="right">Hello World!</p>
It can be achieved thus:
var e = document.getElementById('placeholderId');
var ePara = document.createElement('p');
ePara.setAttribute('align', 'right');
var eTxt = document.createTextNode('Hello World!');
ePara.appendChild(eTxt);
e.appendChild(ePara);
Happy hacking with this info
JBoss Application Server 5.0.0 GA Released
Download it from here: http://www.jboss.org/jbossas/downloads/. If I remember correctly, the first beta was released more than two years back.