The Artima Developer Community
Sponsored Link

Design Forum
XML parser Factory instances

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Shivkumar CM

Posts: 4
Nickname: coderboy
Registered: Dec, 2005

XML parser Factory instances Posted: Dec 28, 2005 4:42 AM
Reply to this message Reply
Advertisement
The javax.xml.parsers package contains two instances of factory method patterns, one each for the SAX and DOM parsers. Note their participants:

SAX:

Creator: SAXParserFactory (Factory method: newSAXParser)
Product: SAXParser

DOM:

Creator: DocumentBuilderFactory (Factory method: NewDocumentBuilder)
Product: DocumentBuilder


Both, the creator as well as the product, in both instances of the pattern are abstract classes. Both factory classes have a static method called newInstance that returns an
instance of the Factory. The concrete class chosen for instantiating depends on the system properties. Once an application has obtained a reference to a factory, it can use it to configure and obtain parser instances.

A number XML parsing API's are available in for java. You can install any of them with your jdk. Each one of them would have provided concrete implementations of the creator and product classes. For example, on my system this code gives the following output.

import javax.xml.parsers.*;

public class FactoryTest{
public static void main(String args[])
throws Exception{
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
System.out.println("Creator: "+parserFactory.getClass());
System.out.println("Product: "+parser.getClass());

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
System.out.println("Creator: "+builderFactory.getClass());
System.out.println("Product: "+builder.getClass());

}
}

Output:
Creator: class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
Product: class com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl
Creator: class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImp
l
Product: class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl

I think the designers of the java have provided hooks that the vendor specific sub-classes to fill in.

Can the knowledgeable members of the forum throw some more light as to why Factory method pattern is used here?

Topic: Struts with Ajax problem Previous Topic   Next Topic Topic: Some tutorials in

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use