FileDocCategorySizeDatePackage
DocumentBuilderImpl.javaAPI DocJava SE 5 API14094Fri Aug 26 14:55:54 BST 2005com.sun.org.apache.xerces.internal.jaxp

DocumentBuilderImpl

public class DocumentBuilderImpl extends DocumentBuilder implements JAXPConstants
author
Rajiv Mordani
author
Edwin Goei
version
$Id: DocumentBuilderImpl.java,v 1.24 2004/02/24 23:15:58 mrglavas Exp $

Fields Summary
private EntityResolver
er
private ErrorHandler
eh
private final DOMParser
domParser
private boolean
enableSP
private final Schema
grammar
private final SecurityManager
secureProcessing
null if the secure processing is disabled. otherwise a valid {@link SecureProcessing} object.
private final boolean
xincludeAware
Constructors Summary
protected DocumentBuilderImpl(DocumentBuilderFactory dbf, Hashtable dbfAttrs)

 

        
            
      
        grammar = dbf.getSchema();
    	secureProcessing = new SecurityManager();    
        this.domParser = new DOMParser(new JAXPConfiguration(grammar));
        this.xincludeAware = dbf.isXIncludeAware();
        
        domParser.setFeature(
                Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE,
                xincludeAware);

        // If validating, provide a default ErrorHandler that prints
        // validation errors with a warning telling the user to set an
        // ErrorHandler
        if (dbf.isValidating()) {
            setErrorHandler(new DefaultValidationErrorHandler());
        }

        domParser.setFeature(Constants.SAX_FEATURE_PREFIX +
                             Constants.VALIDATION_FEATURE, dbf.isValidating());

        // "namespaceAware" == SAX Namespaces feature
        domParser.setFeature(Constants.SAX_FEATURE_PREFIX +
                             Constants.NAMESPACES_FEATURE,
                             dbf.isNamespaceAware());

        // Set various parameters obtained from DocumentBuilderFactory
        domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
                             Constants.INCLUDE_IGNORABLE_WHITESPACE,
                             !dbf.isIgnoringElementContentWhitespace());
        domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
                             Constants.CREATE_ENTITY_REF_NODES_FEATURE,
                             !dbf.isExpandEntityReferences());
        domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
                             Constants.INCLUDE_COMMENTS_FEATURE,
                             !dbf.isIgnoringComments());
        domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
                             Constants.CREATE_CDATA_NODES_FEATURE,
                             !dbf.isCoalescing());

        setDocumentBuilderFactoryAttributes(dbfAttrs);
		if( enableSP)
			domParser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, secureProcessing);
    
Methods Summary
public org.w3c.dom.DOMImplementationgetDOMImplementation()

        return DOMImplementationImpl.getDOMImplementation();
    
final com.sun.org.apache.xerces.internal.parsers.DOMParsergetDOMParser()

          return domParser;
    
public javax.xml.validation.SchemagetSchema()

Get a reference to the the GrammarCache being used by the XML processor.

If no cache is being used, null is returned.

return
GrammarCache being used or null if none in use

        return grammar;
    
public booleanisNamespaceAware()

        try {
            return domParser.getFeature(Constants.SAX_FEATURE_PREFIX +
                                        Constants.NAMESPACES_FEATURE);
        } catch (SAXException x) {
            throw new IllegalStateException(x.getMessage());
        }
    
public booleanisValidating()

        try {
            return domParser.getFeature(Constants.SAX_FEATURE_PREFIX +
                                        Constants.VALIDATION_FEATURE);
        } catch (SAXException x) {
            throw new IllegalStateException(x.getMessage());
        }
    
public booleanisXIncludeAware()

         return xincludeAware;
    
public org.w3c.dom.DocumentnewDocument()
Non-preferred: use the getDOMImplementation() method instead of this one to get a DOM Level 2 DOMImplementation object and then use DOM Level 2 methods to create a DOM Document object.

        return new com.sun.org.apache.xerces.internal.dom.DocumentImpl();
    
public org.w3c.dom.Documentparse(org.xml.sax.InputSource is)

        if (is == null) {
            throw new IllegalArgumentException(
                DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 
                "jaxp-null-input-source", null));
        }

        if (er != null) {
            domParser.setEntityResolver(er);
        }

        if (eh != null) {
            domParser.setErrorHandler(eh);      
        }

        domParser.parse(is);
        return domParser.getDocument();
    
public voidreset()

Reset this DocumentBuilder to its original configuration.

DocumentBuilder is reset to the same state as when it was created with {@link DocumentBuilderFactory#newDocumentBuilder()}. reset() is designed to allow the reuse of existing DocumentBuilders thus saving resources associated with the creation of new DocumentBuilders.

The reset DocumentBuilder is not guaranteed to have the same {@link EntityResolver} or {@link ErrorHandler} Objects, e.g. {@link Object#equals(Object obj)}. It is guaranteed to have a functionally equal EntityResolver and ErrorHandler.

since
1.5

        if(domParser != null){
            try{
                //we dont need to worry about any properties being set on this object because 
                //DocumentBuilder doesn't provide any way to set the properties
                //once it is created.
                domParser.reset();
            }
            //xxx: underlying implementation reset throws XNIException what should we do in this case ?
            //other question is why underlying implementation should throw an exception is it because 
            //of properties being set.. if there was any propery that is not being supported
            //exception would have been thrown when setting it on the underlying implementation.
            catch(XNIException ex){
                //coninue.
            }
        }
    
private voidsetDocumentBuilderFactoryAttributes(java.util.Hashtable dbfAttrs)
Set any DocumentBuilderFactory attributes of our underlying DOMParser Note: code does not handle possible conflicts between DOMParser attribute names and JAXP specific attribute names, eg. DocumentBuilderFactory.setValidating()

        if (dbfAttrs == null) {
            // Nothing to do
            return;
        }

        // TODO: reroute those properties to use new JAXP1.3 API. -KK
        
        for (Enumeration e = dbfAttrs.keys(); e.hasMoreElements();) {
            String name = (String)e.nextElement();
            Object val = dbfAttrs.get(name);
            if (val instanceof Boolean) {
                // Assume feature
				if (Constants.FEATURE_SECURE_PROCESSING.equals(name)){
					enableSP = ((Boolean)val).booleanValue();
				}else
	                domParser.setFeature(name, ((Boolean)val).booleanValue());
            } else {
                // Assume property
                if (JAXP_SCHEMA_LANGUAGE.equals(name)) {
                    // JAXP 1.2 support
                    //None of the properties will take effect till the setValidating(true) has been called                                        
                    if ( W3C_XML_SCHEMA.equals(val) ) {
                        if( isValidating() ) {
                            domParser.setFeature(
                                Constants.XERCES_FEATURE_PREFIX +
                                Constants.SCHEMA_VALIDATION_FEATURE, true);
                            //also set the schema full checking to true.
                            domParser.setFeature(Constants.XERCES_FEATURE_PREFIX +
                                     Constants.SCHEMA_FULL_CHECKING,
                                     true);
                            // this should allow us not to emit DTD errors, as expected by the 
                            // spec when schema validation is enabled
                            domParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
                        }
                    }
        		} else if(JAXP_SCHEMA_SOURCE.equals(name)){
               		if( isValidating() ) {
						String value=(String)dbfAttrs.get(JAXP_SCHEMA_LANGUAGE);
						if(value !=null && W3C_XML_SCHEMA.equals(value)){
            				domParser.setProperty(name, val);
						}else{
                            throw new IllegalArgumentException(
                                DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 
                                "jaxp-order-not-supported",
                                new Object[] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE}));
						}
					}
            	} 
				/*else if(name.equals(Constants.ENTITY_EXPANSION_LIMIT)){
					String elimit = (String)value;
					if(elimit != null && elimit != ""){
						int val = Integer.parseInt(elimit);
						secureProcessing.setEntityExpansionLimit(val);
					}
				}else if(name.equals(Constants.MAX_OCCUR_LIMIT)){
					String mlimit = (String)value;
					if(mlimit != null && mlimit != ""){
						int val = Integer.parseInt(mlimit);
						secureProcessing.setMaxOccurNodeLimit(val);
					}
        		}*/ else {
                    // Let Xerces code handle the property
                    domParser.setProperty(name, val);
				}
			}
		}
	
public voidsetEntityResolver(org.xml.sax.EntityResolver er)

        this.er = er;
    
public voidsetErrorHandler(org.xml.sax.ErrorHandler eh)

        // If app passes in a ErrorHandler of null, then ignore all errors
        // and warnings
        this.eh = (eh == null) ? new DefaultHandler() : eh;