package textbender.g.xml.dom.bootstrap; // Copyright 2005-2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import javax.xml.parsers.*; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.DOMImplementation; import org.w3c.dom.ls.DOMImplementationLS; import textbender.g.lang.ThreadSafe; import textbender.g.util.logging.LoggerX; /** Utilities for the DOM implementation registry. */ public @ThreadSafe final class DOMImplementationRegistryX { private DOMImplementationRegistryX() {} /** Fetches a standard implementation from the registry. */ public static DOMImplementation implementation() { DOMImplementation implementation = null; // thus far final String features = "XML 3.0"; implementation = implementation( features ); if( implementation == null ) { LoggerX.i(DOMImplementationRegistryX.class).config( "no implementation '" + features +"' registered, creating default instead" ); implementation = buildImplementation(); } return implementation; } /** Fetches a standard 'Load and Save' implementation from the registry. */ public static DOMImplementationLS implementationLS() { DOMImplementationLS implementation = null; // thus far final String features = "XML 3.0 LS"; implementation = (DOMImplementationLS)implementation( features ); if( implementation == null ) { LoggerX.i(DOMImplementationRegistryX.class).config( "no implementation '" + features +"' registered, creating default instead" ); try { implementation = (DOMImplementationLS)buildImplementation(); } catch( ClassCastException x ) { throw new RuntimeException( DOMImplementationRegistry.PROPERTY + "=" + System.getProperty(DOMImplementationRegistry.PROPERTY) + ", no implementation has features: " + features, x ); } } return implementation; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** Constructs a DOM implementation, using the old DocumentBuilderFactory. */ private static DOMImplementation buildImplementation() { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); return documentBuilder.getDOMImplementation(); } catch( ParserConfigurationException x ) { throw new RuntimeException( x ); } // not expected } /** Fetches an implementation from the registry, having specific features. * This method simply calls DOMImplementationRegistry.getDOMImplementation(), * and serves to document its quirky behaviour. * It returns null (at least in bare Sun JDK 1.5) if system property * "org.w3c.dom.DOMImplementationSourceList" not explicitly defined. * Apparently, no implementations are registered by default. * So, if you want the built-in default, * see instead {@linkplain #buildImplementation() buildImplementation}(). * [But if you explicitly add Xerces jars (2.7.1) to the class path, * then you do get default implementations, without specifying that property. * Presumeably, Xerces' standard version of the DOMImplementationRegistry * is slightly altered, with a pre-stuffed list.] * * @param features per DOMImplementationRegistry.getDOMImplementation * * @return DOM implementation; or null if none exists */ private static DOMImplementation implementation( final String features ) { try { DOMImplementationRegistry domRegistry = DOMImplementationRegistry.newInstance(); return domRegistry.getDOMImplementation( features ); } catch( Exception x ) { throw new RuntimeException( x ); } // not expected: ClassNotFoundException InstantiationException } }