XPath for XML

XPath is a language used to navigate through the XML document. It’s used to identify elements in the XML document. It is so limber that technologies like XQuery and XPointer are built on it. XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.

While using XPath, the xml document is treated as tree of nodes. See the example below

<?xml version="1.0" encoding="ISO-8859-1"?></pre>
<bookstore>
  <book>
    <title lang="en">The Joke</title>
    <author>Milan Kundera</author>
    <price>350</price>
  </book>
  <book>
    <title lang="en">After Dark</title>   
    <author>Haruki Mukarami</author>   
    <price>450</price>
  </book>
</bookstore>

Here, the tag <bookstore> is the root node. The tag <author> is the element node and the attribute lang is the attribute node. The nodes also have the hierarchical properties. For instance, the nodes children of the node <book>. Also, <title>,<author> and <price> are siblings.

XPath uses the following expressions to parse through the XML Document.

/bookstore/book - returns all the &lt;book&gt; nodes which are children of &lt;bookstore&gt;
bookstore//book - returns all book elements that are descendant of the &lt;bookstore&gt; element,
//@lang - returns all attributes that are named lang

Specific nodes can also be identified in XPath

/bookstore/book[1] - Returns the first book element
/bookstore//book[last()-1] - Returns the second last element
/bookstore/book[position()&lt;3] - Returns the first two book elements
//title[@lang] - Returns all title elements that has an attribute lang
/bookstore/book[price&gt;350]/title - Returns the titles of all books which has price more than 350

XPath supports wildcard characters as well

/bookstore/* - Selects all the children of the bookstore elements
//title[@*] - Select all the title elements which has an attribute

The detailed list of parse syntax can be found here..

So, till now, everything was pretty simple. Here comes the most flexible and useful feature of XPath 

XPath Axes

An axis defines a node-set relative to the current node. When we say, ‘the children of the current node’, the children defines a nodeset and thus children is an axes. Similarly parent, sibling, attribute are all axes W3C gives the complete list here..

From the examples we saw above, there are two types of location paths in XPath – absolute and relative

An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash

</pre>
An absolute location path:
/step/step/...
A relative location path:
step/step/...

A step in the examples above can consist of

  • an axis (defines the tree-relationship between the selected nodes and the current node)
  • a node (identifies a node within an axis)
  • zero or more predicates (to further refine the selected node-set)

Generalizing it, a step would look like this

axis:node[predicate]

take away the axis and predicate and you are left with the kind of steps we saw in the above examples.

See some more examples


/bookstore/child::book - Select all the book nodes which are children of bookstore

/bookstore/book/attribute::* - Select all the attributes of the book node

child::*/child::price - Select all the price grandchildren of the current node

See that the first two example are absolute paths and the last one is a relative path.

XPath can be evaluated via javascript or through a PL like Java. I will soon chalk a post on that. Also, I really need to have another post dedicated to XPath axes..

renderSnake – Better Way to create HTML

We live in an age where everything is open and innovations spring out every second. All those algorithms, all those concepts and all those frameworks are easily avalable to he who is willing to look. Unless you are not staring down from the tip of the cutting edge, you can pretty much get help on any problem. If someone had a similar problem before and you have the solution available, why re-invent the wheel..

When I was in the task of creating an HTML report generating tool, I did not have to look much. Found the perfect framework – renderSnake. renderSnake is a component based HTML generation framework. Its purpose is to support the creation of Web applications that are better maintainable, allows for easier reuse, have testable UI components and produces compact HTML in an efficient way. Let’s start off with an example.

So, let’s create a plain old HTML.

</pre>
<h4>Two rows and three columns:</h4>
<table border="1">
<tbody>
<tr>
<td>Haruki Murakami</td>
<td>Gregory Roberts</td>
</tr>
<tr>
<td>A Wild Sheep Chase</td>
<td>Shantaram</td>
</tr>
</tbody>
</table>
<pre>

And the java code to create the HTML 

HtmlCanvas html = new HtmlCanvas();
html
 .html()
  .body()
   .h4()
    .content("Two rows and three columns:")
     .table(border("1"))
      .tr()
       .td()
        .content("Haruki Murakami")
       .td()
        .content("Gregory Roberts")
      ._tr()
      .tr()
       .td()
        .content("A Wild Sheep Chase")
       .td()
        .content("Shantaram")
      ._tr()
     ._table()
    ._body()
  ._html();

HTMLCanvas

Using a Canvas, you can write code that produces HTML. The Canvas has methods for all HTML tags. There are two methods for writing an opening tag, one without and one with attributes. The Canvas maintains a stack of nested tags. Proper closing of tags is required, according to the rules of HTML (some end tags are required, some are optional and some are forbidden).

Each tag method returns the Canvas instance, allowing for a fluent programming interface. Using source indentation you can visually verify the correct structure. In the above example, the variable html is an instance of HtmlCanvas.

HtmlAttributes

Using an HtmlAttributes instance, you can add attribtues to tags such as href, class and id. The Java classes HtmlAttributes and HtmlAttributesFactory provides convenience methods for creating instances. This allows for a fluent programming interface.

//This one is using HtmlAttributes :

html.body(new HtmlAttributes("key","value")); 

//This one is using the HTMLAttributesFactory

.a(href("http://goto.com")) 
                .img(src("picture.png").alt("some picture")) 
                ._a(); 

All the attributes for a given tag can be set using the corresponding method. In the above example, the href, img and alt properties are set.

I am yet to find a limitation for renderSnake. You can get more examples here and here..

URL, URI & URN.. cakewalk? Not for me atleast..

Prologue

I have always been confused on the difference between the 3 most popular acronyms in web based computing – URL, URI and URN. The concept always seemed to evade me..

This post is more of a ‘talking to myself’ thing. I can almost imagine me reading this months from now, and feeling grateful that I finally wrote this down.

The Journey

I started off with a good old Google search and I should say, the initial results were truly disheartening. The first results that came up was this one by damnhandy.com. Even though the intentions are true, this blog is very much misleading as it is pretty clear from the comments. The blog was followed by this and again by this and it gets pretty clear if you read all three. (A trilogy on Uniform Resources)

You could always visit the official w3c documentation. Unless you think in binary and have bytestreams for breakfast, you won’t understand much. You are better off reading couple of interesting posts in StackOverflow.

The Real Deal

So here is what I learned from the journey…

Let me talk about URI and URL first. Expanding them would be URI – Uniform Resource Identifier and URL – Uniform Resource Locator.

Both of them are used to provide details about resources ( a file, a server, a port.. ). URI is an identifier. As the name says, it identifies a resource. Let me make myself clear with some examples.

  1. http://thisisrinda.files.wordpress.com/2011/10/mojojojo.jpg
  2. buttercup.jpg
  3. Room 1738

See the first example, it identifies a resource ( a jpeg image with the name mojojojo ). The second example does the same, it identifies a resource. The difference between example #1 and #2, the first one gives more detail ( the location… ). But we don’t need that, all we need to do is identify the resource. A real world example would be #3, a hotel room number. There can be trizillions of Room 1738s in the planet. But all we need is an identification and that’s exactly what an URI does – an Identifier.

Moving to URL, it’s the locator. With URI, you identify the resource, and with URL, you locate the resource. In the example #1 above, we have the complete location of the resource, and that makes example #1 an URL. So, the first example is an URI and an URL – you can identify and locate the resource. But examples #2 and #3 are not URLs, they won’t help you locate the image or the hotel room. However, both these are URIs.

  1. D:/images/buttercup.jpg
  2. Room 1738, Queen Elizabeth Hotel, Montreal, Canada

Hope you get the bigger picture. One is just an identifier, while the other one is a locator.

All URLs are URIs but all URIs are not URLs..

What then are URNs ?

The wiki page says :

A uniform resource name (URN) is the historical name for a uniform resource identifier (URI) that uses the urn: scheme.

It’s an URI.. just that it uses the urn: scheme.. What and how an urn: scheme is implemented is totally out of scope here.. you can read on that here..

example of URN

  1. urn:isbn:9781402531996
  2. urn:isan:0000-0000-9E59-0000-O-0000-0000-2

The first example identified Umberto Eco’s ‘Baudolino’ and the second example identifies a very popular movie. It’s just the same as an URI, the only difference is that it uses the urn: scheme.

Epilogue

So, that does it. Do let me know if you think what i just said is nonsense or if you want me to elaborate on any point.

A Requiem for Marker Interfaces

Having written about Java serialization previously, I thought I’d put in some research into the concept of ‘Marker Interfaces’ or ‘Tag Interfaces’. Well, getting matters straight, technically, the word ‘Marker’ is an invention of bloggers and technical writers. Java specification says nothing about an interface being a ‘Marker Interface’. You won’t find the word ‘Marker’ at all..

A marker interface is an interface with no method declarations. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. These Marker interfaces are used by other code to test for permission to do something.

Some well-known examples are

java.io.Serializable – object implement it can be serialized using ObjectOutputStream.
java.lang.Clonable – objects clone method may be called

They are also known as tag interfaces since they tag all the derived classes into a category based on their purpose

On Interfaces..

Well, what exactly is an interface.

One, I would say an interface is a contract. It is a contract which says the class which implements the interface will implement all the methods specified by the interface.

Two, I say interface is an ‘is a’ relationship. For instance, take a class Sedan which implements the interface Car. We can say that the Seday is a Car. Better example, ArrayList implements List and we say ArrayList is a List.

Let’s take the case of a marker interface Serializable. A class, say Document implements Serializable. Document does not have to implement any methods because the interface Serializable does not specify any methods. This is not right. The Serializable interface does not behave similar to the other interfaces.

Also, it does not make sense to say Document is a Serializable. Instead, Document is Serializable. Again, the interface Serializable does not fit in with the rest. Let me add that, for a marker interface the relationship is, ‘IS’. Class Document is Serializable, or the class Person is Clonable.

Interface Serializable..

This is perhaps the most popular Marker Interface. I won’t talk about serializing an object, you can read more on that. here..

1. FileOutputStream fos = new FileOutputStream("temp.out");
2. ObjectOutputStream oos = new ObjectOutputStream(fos);
3. Match m1 = new Match();
4. oos.writeObject(m1);

In the snippet of code above, I am trying to serialize an object m1 of Type Match. The ObjectOutputStream then attempts to serialize the object. If you inspect the ObjectOutputStream source, you can see a check for Serializable happening.


private void writeObject0(Object obj, boolean unshared) trows IOException
.
.
else if (obj instanceof Enum) {
 writeEnum((Enum) obj, desc, unshared);
} 
else if (obj instanceof Serializable) {
 writeOrdinaryObject(obj, desc, unshared);
} 
else {
 if (extendedDebugInfo) {
  throw new NotSerializableException(
   cl.getName() + "\n" + debugInfoStack.toString());
 } else {
  throw new NotSerializableException(cl.getName());
}.
.

They are doing a check to see if the Object to be serialized is of the type Serializable. If it’s not, a suitable exception is thrown.

So, what do we benfit from implementing the Serializable interface here?

‘Marker Interface Pattern’

A marker interface provides a metadata to the class. The metadata being ‘this class is serializable’. Or in cases like the Clonable interface, the marker interface states the class is ‘Clonable’. There is a wiki page on ‘Marker Interface Pattern’.

“The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata.”

So, the concept of Marker Interfaces came about in Java at a time when there was no support for metadata. But now we have support in terms of Annotations. I can debate that, whatever is achieved through Marker interfaces, can be achieved through Annotations too.

A worthy substitute?

So, is annotation a worthy substitute for Marker Interfaces? I say yes. See the example below.


01: @interface Drivable{
02: }
03: @Drivable
04: public class Car{
05: public void performAction(Object obj){    
06:    if (!obj.getClass().isAnnotationPresent(Drivable.class)) {         
07:       throw new IllegalArgumentException("cannot perform action...");    
08:    }     
09:    else {        
10:       //do stuff as require    
11:    }
12: }

Pretty straightforward stuff. Using a custom annotation ‘Drivable’, we are checking if a class, in this case Car, is Drivable which perfectly satisfies the ‘IS’ rule.

Having said all this, I must add using annotation comes with a catch. The check for ‘IS’ can occur only at runtime, but for a marker interface, it can throw a compile time error. Plus, annotation work on reflection which is a pretty heavy operation.

As a verdict, I can say that going by core Object Oriented norms, Annotations can beat Marker Interfaces anyday.