Calitha Commons
- Platform: Java 6 (1.6)
- Status: Released
- Current version: 1.0
- License: GNU Lesser General Public License
- Download
- View online documentation
Main library dependencies
Additional library dependencies for ANT building
Summary
This Java library has varied classes to provide common functionality for an application. Two examples of such functionality will be discussed here. Documentation of the entire package can be viewed online, and is available for download in the repository.
SimpleDOMParser
Any developer that uses a programmatic XML DOM Parser in a product, realizes that the default settings often are not good enough. DTD's are fetched from remote locations, XSL templates are being parsed over and over, and other problems occur that make the product not perform as good as it should. The solution is to change the configuration of the XML parser. But with all those scattered, yet related, settings it is easy to miss one.
SimpleDOMParser is a wrapper around the Xerces DOMParser, with an easy to use API. The most powerful feature is to pass an implementation of IParserConfiguration to the constructor. The com.calitha.xml package has several implementations of the IParserConfiguration. Two examples:
- CachingValidatingConfiguration:
Caches all external entities, such as DTD's or XML Schema's. Parsed XML is alyways validated. Performance is improved by not having any slow internet connections to retrieve entities after the first time. - PreParsedGrammarsConfiguration:
This configuration contains a grammar pool. All grammars (DTD's and schema's) that are used in the XML documents you want to parse, must be added to the pool. This will pre-parse any grammar. The XML parser will not obtain any new external entities for validating. This means the start-up time is slightly longer, but your application will work with high performance, and still have the full ability to validate the XML.
XML Resource Bundle
Java 1.6 has introduced
ResourceBundle.Control
which makes it possible to extend the functionality of the
ResourceBundle
and load resources from different formats.
This XML Resource Bundle loads both text and binary resources from special XML files.
These XML resource bundle files must be part of the
http://namespaces.calitha.com/resourcebundle/1.0 and will be validated against
the XML ResourceBundle Schema
used for this XML application.
The XML Resource Bundle supports the following resource types:
- Text - plain text resource, useful in displaying messages.
- WWW - a resource designated by an URL. These could be local files, using the file: protocol, but also remote files, for example using the http: or ftp: protocol can included.
- Base64 - allows you to embed binary data in XML, such as an image.
- Java - 'classic' Java resource, that can be embedded in a jar file on the classpath. This is usefull for mapping long Java resource names to more convenient ones.
Synchronization proxy
The idea for this improvement came from the desire to have a new keyword
similar to synchronized, but would use a read or write lock.
A synchronization proxy allows you to turn an ordinary object into a
synchronized object as if each call to the method has the
synchronized
keyword. This is a nice feature to have, but what's even nicer
is that a proxy can also be used to read annotation of methods
and determine if a read or write lock is needed. This functionality
provides improved concurrency in objects that have rare write operations
and frequent read operations. The new annotations feature of Java 1.5 to
declare run-time meta-data and the (older) proxy support in Java makes
such a custom improvement possible.
Here is some example Java code to demonstrate how to use the RWLock annotation
to declare to the synchronization proxy that a read or write lock is needed.
@RWLock(RWLock.Type.READ)
public long getValue()
{
return value;
}
@RWLock(RWLock.Type.WRITE)
public void setValue(long value)
{
this.value = value;
}