Skip to main content

Tip: Basics of bootstrapping with DOM, Part 2

Improving DOM Level 1 and 2 bootstrapping

Brett McLaughlin (brett@newInstance.com), Author and Editor, O'Reilly Media Inc.
Photo of Brett McLaughlin
Brett McLaughlin has been working in computers since the Logo days (Remember the little triangle?). He currently specializes in building application infrastructure using the Java language and Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.

Summary:  In this tip, you'll learn about a better way to bootstrap in your DOM applications. This builds upon the previous tip, which examined the abilities that DOM natively provides for this task.

View more content in this series

Date:  01 Dec 2002
Level:  Intermediate
Also available in:   Japanese

Activity:  2980 views
Comments:  

In my last tip, I explained the basics of DOM bootstrapping, and in particular how those basics behaved when using DOM Level 1 or 2. You learned how to obtain your vendor's implementation of the DOMImplementation class, and how to use it to produce Document and DocumentType objects. And most importantly, you saw the various problems with the bootstrapping that DOM provides for.

As a short refresher, I'll summarize what those problems are before moving on to a solution:

  1. A dependence is created on a specific parser
  2. In many cases, a dependence is created on a specific version of a specific parser
  3. Changes to the DOM implementation or parser require code-level changes and recompilation
  4. Changes to the DOM implementation require CLASSPATH changes

Of these various issues, only one is really acceptable in a production environment -- the last one, 4, changes to an application's CLASSPATH. All the other limitations are serious ones, and affect your ability to have a reusable (and sometimes resellable) application.

If you recall, the last tip left off with the code fragment shown in Listing 1.

DOMImplementation domImpl =
    new org.apache.xerces.dom.DOMImplementationImpl();
DocumentType docType = 
    domImpl.createDocumentType("rootElementName", "public ID", "system ID");
Document doc = domImpl.createDocument("", "rootElementName", docType);

While this is not a horrible solution for bootstrapping, it is not a particularly good one. Instead of using this approach and accepting its limitations, let's look at a better solution. Ideally, you need to be able to remove all of the vendor-specific code from the bootstrapping process, and specify that information outside of the application.

Ideally, you could set a system property in your code or at application startup (through the -D flag to the java process); I prefer org.w3c.dom.DOMImplementationClass as the name of this property. The value of this property is the implementation class that the code needs to instantiate an instance of DOMImplementation. In the examples, I've been using Apache Xerces, so this property's value is org.apache.xerces.dom.DOMImplementationImpl.

Once you get this basic idea in place, it's fairly simple to write a helper class that reads in that property, instantiates it, and returns the new object to the code that needs to get a DOM instance up and running. Listing 2 shows such a helper class.

package com.ibm.xml;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.DOMImplementation;

public class DOMFactory {

    /** System property name */
    private static final String IMPL_PROPERTY_NAME =
        "org.w3c.dom.DOMImplementationClass";

    /** Initialization flag */
    private static boolean initialized = false;

    /** The DOMImplementation to use */
    private static DOMImplementation domImpl;

    private static void initialize() throws Exception {
        domImpl = 
            (DOMImplementation)Class.forName(
                System.getProperty(IMPL_PROPERTY_NAME)).newInstance();
        initialized = true;
    }

    public static Document newDocument(String namespaceURI, String rootQName,
                                       DocumentType docType) 
        throws Exception {
        
        if (!initialized) {
            initialize();
        }

        return domImpl.createDocument(namespaceURI, rootQName, docType); 
    }

    public static DocumentType newDocumentType(String rootQName,
                                               String publicId, String systemId)
        throws Exception {
    
        if (!initialized) {
            initialize();
        }

        return domImpl.createDocumentType(rootQName, publicId, systemId);
    }
}

This turns out to be pretty straightforward. The entry points for this class are the two public methods, each of which returns one of the two DOM startup classes. These are the same two classes that a DOMImplementation generates, so this provides some uniformity in your use of the helper class. Both methods ensure that initialization has occurred, and then produce the requested object.

Initialization isn't that complex, either. The system property returns a class name (assuming it's used correctly!), and that class name is instantiated through the Class.forName().newInstance() mechanism. Once that's done, it's easy to store the object and use it to generate objects.

So there you have it! A better bootstrapping approach is really that simple. Just add this class to your class path, along with whatever parser JAR file you need, and set the system property before you start parsing. You'll be able to obtain DOM types without even dipping into vendor-specific code. This should tide you over until DOM Level 3 is in public release, and an even better approach to bootstrapping shows up. Curious? Good! I'll detail how DOM Level 3 is set to work in the next tip, and give you a jump on what's coming down the pipe. Until then, I'll see you online!


Resources

  • Part 1 of this series of tips on bootstrapping with DOM explains what bootstrapping is, explores the problems associated with it, and lays the basics for use in DOM Levels 1 and 2 (developerWorks, November 2002). Part 3 explains the changes to DOM Level 3 that relate to bootstrapping and improve upon DOM Levels 1 and 2 (developerWorks, December 2002).

  • Read about the DOM API on W3C.org.

  • Learn about the structure of a DOM document, and how to use Java technology to create a document from an XML file, make changes to it, and retrieve output, in Nicholas Chase's tutorial "Understanding DOM" (developerWorks, August 2001).

  • Find more XML resources on the developerWorks XML zone.

  • IBM trial software: Build your next development project with trial software available for download directly from developerWorks.

  • Find out how you can become an IBM Certified Developer in XML and related technologies.

  • Want us to send you useful XML tips like this every week? Sign up for the developerWorks XML Tips newsletter.

About the author

Photo of Brett McLaughlin

Brett McLaughlin has been working in computers since the Logo days (Remember the little triangle?). He currently specializes in building application infrastructure using the Java language and Java-related technologies. He has spent the last several years implementing these infrastructures at Nextel Communications and Allegiance Telecom, Inc. Brett is one of the co-founders of the Java Apache project Turbine, which builds a reusable component architecture for Web application development using Java servlets. He is also a contributor of the EJBoss project, an open source EJB application server, and Cocoon, an open source XML Web-publishing engine.

Comments



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12193
ArticleTitle=Tip: Basics of bootstrapping with DOM, Part 2
publish-date=12012002
author1-email=brett@newInstance.com
author1-email-cc=htc@us.ibm.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Special offers