[thelist] Java Instantiation

Keith Dahlby dahlbyk at softhome.net
Sun Jun 29 12:42:21 CDT 2003


James,

Thanks for the quick response. First, regarding the use of SAX1 instead of 
SAX2, I'm trying to optimize a simple Java interface for Kweelt 
[kweelt.sourceforge.net], an XQuery framework. Kweelt was developed several 
years ago, and uses SAX1 for its XML parsing. (On a side note, I'm trying 
to learn SAX2...do you have any recommended resources?)

The instantiation question arises from the code I'm trying to optimize. The 
relevant code is:
DocumentHandler h =
     (DocumentHandler) Class
         .forName("xacute.util.ToStringHandler")
         .newInstance();
String nodeFactory = "xacute.impl.xdom.NodeFactoryXerces";
NodeFactory nf =
     (NodeFactory) Class.forName(nodeFactory).newInstance();

As the interface must be closely coupled with Kweelt's libraries 
(xacute.*), I should be safe to use this instead:
DocumentHandler h = new xacute.util.ToStringHandler();
NodeFactory nf = new xacute.impl.xdom.NodeFactoryXerces();

The code I'm trying to optimize was written by the professor of a Database 
Systems class I'm taking...I'm just trying to make sure that I'm not 
missing something. Thanks again for your help.

Cheers ~ K

At 09:42 AM 6/29/2003, you wrote:
>Keith,
>
>Class.forName("XYZ") is used to dynamically load a
>class without hardcoding the class name. Prior to
>calling this method, you have to have "XYZ" defined in
>your environment (there are multiple ways of doing
>this both dynamically and using config files).
>
>In your case, "org.xml.sax.DocumentHandler" is an
>interface that needs to be implemented by a
>standard-compliant SAX parser. So, any parser
>implementation that implements that interface, can be
>"plugged-in" using dynamic class loading features
>without having to change the code that contains the
>line
>Class.forName("org.xml.sax.DocumentHandler").newInstance();
>
>As you can see, there is a great value in doing this
>as this guarantees that you are not tying yourself to
>any specific parser implementation.
>
>Last but not least, the DocumentHandler interface
>  is a part of SAX1 implementation which was deprecated
>in SAX2 and replaced with ContentHandler interface. So
>if you are writing new code, I would definitely
>recommend using SAX2.
>
>Hope this helps,
>James



More information about the thelist mailing list