6.4.3. Bootstrapping
The last
addition in DOM Level 3 I want to cover is arguably the most
important: the ability to bootstrap. I mentioned earlier that in
creating DOM structures, you are forced to use
vendor-specific code (unless you're
using JAXP, which I'll cover in Chapter 9, "JAXP").
This is a bad thing, of course, as it knocks out vendor-independence.
For the sake of discussion, I'll repeat a code fragment that
creates a DOM Document object using a
DOMImplementation here:
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import org.apache.xerces.dom.DOMImplementationImpl;
// Class declaration and other Java constructs
DOMImplementation domImpl = DOMImplementationImpl.getDOMImplementation( );
Document doc = domImpl.createDocument( );
// And so on...
The problem is that there is no way to get a
DOMImplementation without importing and using a
vendor's implementation class. The solution is to use a factory
that provides DOMImplementation instances. Of
course, the factory is actually providing a vendor's
implementation of
DOMImplementation (I know, I know, it's a
bit confusing). Vendors can set system properties or provide their
own versions of this factory so that it returns the implementation
class they want. The resulting code to create DOM trees then looks
like this:
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.DOMImplementationFactory;
// Class declaration and other Java constructs
DOMImplementation domImpl =
DOMImplementationFactory.getDOMImplementation( );
Document doc = domImpl.createDocument( );
// And so on...
The class being added is DOMImplementationFactory,
and should solve most of your vendor-independence issues once
it's in place. Look for this as the flagship of DOM Level 3, as
it's one of the most requested features for current levels of
DOM.