com.thebuzzmedia.sjxp
Class XMLParser.Location

java.lang.Object
  extended by com.thebuzzmedia.sjxp.XMLParser.Location
Enclosing class:
XMLParser<T>

 class XMLParser.Location
extends Object

Simple and fast class used to mock the behavior of a stack in the form of a string for the purposes of "pushing" and "popping" the parser's current location within an XML document as it processes START and END_TAG events.

Performance is optimized by using a StringBuilder who's length is chopped (which just adjusts an int value) to simulate a "pop" off the top.

Performance

As of SJXP 2.0 String object creation and char[] duplication (e.g. System.arraycopy(Object, int, Object, int, int)) has been completely removed and replaced with using simple integer hash codes.

The performance improvement is huge over the original toString-based method of matching IRule's locationPaths against the parser's current location.

Author:
Riyad Kalla (software@thebuzzmedia.com)

Constructor Summary
XMLParser.Location()
          Creates a new empty location.
 
Method Summary
 void clear()
          Used to clear all the internal state of the location.
 Integer getCachedHashCode()
          Used to get a cached Integer version of the int hashCode() return value.
 int hashCode()
          Overridden to calculate the hash code of this location using the exact same hash code calculation that String.hashCode() uses.
 void pop()
          "Pops" the last pushed path element off the "stack" by re-adjusting the StringBuilder's length to what it was before the last element was appended.
 void push(String localName, String namespaceURI)
          "Pushes" a new local name and optional namespace URI onto the "stack" by appending it to the current location path that represents the parser's location inside of the XML doc.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLParser.Location

public XMLParser.Location()
Creates a new empty location.

Method Detail

hashCode

public int hashCode()
Overridden to calculate the hash code of this location using the exact same hash code calculation that String.hashCode() uses. This allows us to say a String with the content "/library/book/title" is equal to an instance of this class representing the same location when doing lookups in a Map.

This method calculates the hash code and then caches it, followup calls to push(String, String) or pop() invalidate the cached hash code allowing it to be recalculated again on the next call.

Overrides:
hashCode in class Object

getCachedHashCode

public Integer getCachedHashCode()
Used to get a cached Integer version of the int hashCode() return value.

To avoid unnecessary Integer allocations, this method caches up to a certain number of Integer instances, re-using them every time the same hash code value comes back up and creating new instances when it doesn't.

If a larger number of Integer instances are created than the underlying cache can hold, then a new instance will be created and returned like normal.

Design

The reason this works so well for parsing XML is because of the nested, tag-matching structure of XML. When considering unique paths inside of an XML doc (e.g. "/library", "/library/book", etc.) there are typically not that many; maybe 20, 50 or less than a 100 in most cases.

Once the hash code Integer values for these unique paths is created and cached, once we re-encounter that path again and again, we don't need to recreate that hash code Integer, we can just use the one from the previous occurrence.

Returns:
a cached Integer version of the int hashCode() return value.

clear

public void clear()
Used to clear all the internal state of the location.


push

public void push(String localName,
                 String namespaceURI)
"Pushes" a new local name and optional namespace URI onto the "stack" by appending it to the current location path that represents the parser's location inside of the XML doc.

Parameters:
localName - The local name of the tag (e.g. "title").
namespaceURI - Optionally, the full qualifying namespace URI for this tag.

pop

public void pop()
"Pops" the last pushed path element off the "stack" by re-adjusting the StringBuilder's length to what it was before the last element was appended.

This effectively chops the last element off the path without doing a more costly StringBuilder.delete(int, int) operation that would incur a call to System.arraycopy(Object, int, Object, int, int) by simply adjusting a single int counter inside of StringBuilder.


Copyright 2011 The Buzz Media, LLC