DocumentBuilderImplpublic class DocumentBuilderImpl extends DocumentBuilder implements JAXPConstants
Fields Summary |
---|
private static final String | NAMESPACES_FEATUREFeature identifier: namespaces. | private static final String | INCLUDE_IGNORABLE_WHITESPACEFeature identifier: include ignorable white space. | private static final String | CREATE_ENTITY_REF_NODES_FEATUREFeature identifier: create entiry ref nodes feature. | private static final String | INCLUDE_COMMENTS_FEATUREFeature identifier: include comments feature. | private static final String | CREATE_CDATA_NODES_FEATUREFeature identifier: create cdata nodes feature. | private static final String | XINCLUDE_FEATUREFeature identifier: XInclude processing | private static final String | XMLSCHEMA_VALIDATION_FEATUREfeature identifier: XML Schema validation | private static final String | VALIDATION_FEATUREFeature identifier: validation | private static final String | SECURITY_MANAGERProperty identifier: security manager. | private org.apache.xerces.parsers.DOMParser | domParser | private final Schema | grammar | private org.apache.xerces.xni.parser.XMLComponent | fSchemaValidator | private org.apache.xerces.xni.parser.XMLComponentManager | fSchemaValidatorComponentManager | private org.apache.xerces.impl.validation.ValidationManager | fSchemaValidationManager | private UnparsedEntityHandler | fUnparsedEntityHandler | private final ErrorHandler | fInitErrorHandlerInitial ErrorHandler | private final EntityResolver | fInitEntityResolverInitial EntityResolver |
Constructors Summary |
---|
DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features)
this(dbf, dbfAttrs, features, false);
| DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features, boolean secureProcessing)
domParser = new DOMParser();
// If validating, provide a default ErrorHandler that prints
// validation errors with a warning telling the user to set an
// ErrorHandler
if (dbf.isValidating()) {
fInitErrorHandler = new DefaultValidationErrorHandler();
setErrorHandler(fInitErrorHandler);
}
else {
fInitErrorHandler = domParser.getErrorHandler();
}
domParser.setFeature(VALIDATION_FEATURE, dbf.isValidating());
// "namespaceAware" == SAX Namespaces feature
domParser.setFeature(NAMESPACES_FEATURE, dbf.isNamespaceAware());
// Set various parameters obtained from DocumentBuilderFactory
domParser.setFeature(INCLUDE_IGNORABLE_WHITESPACE,
!dbf.isIgnoringElementContentWhitespace());
domParser.setFeature(CREATE_ENTITY_REF_NODES_FEATURE,
!dbf.isExpandEntityReferences());
domParser.setFeature(INCLUDE_COMMENTS_FEATURE,
!dbf.isIgnoringComments());
domParser.setFeature(CREATE_CDATA_NODES_FEATURE,
!dbf.isCoalescing());
// Avoid setting the XInclude processing feature if the value is false.
// This will keep the configuration from throwing an exception if it
// does not support XInclude.
if (dbf.isXIncludeAware()) {
domParser.setFeature(XINCLUDE_FEATURE, true);
}
// If the secure processing feature is on set a security manager.
if (secureProcessing) {
domParser.setProperty(SECURITY_MANAGER, new SecurityManager());
}
this.grammar = dbf.getSchema();
if (grammar != null) {
XMLParserConfiguration config = domParser.getXMLParserConfiguration();
XMLComponent validatorComponent = null;
/** For Xerces grammars, use built-in schema validator. **/
if (grammar instanceof XSGrammarPoolContainer) {
validatorComponent = new XMLSchemaValidator();
fSchemaValidationManager = new ValidationManager();
fUnparsedEntityHandler = new UnparsedEntityHandler(fSchemaValidationManager);
config.setDTDHandler(fUnparsedEntityHandler);
fUnparsedEntityHandler.setDTDHandler(domParser);
domParser.setDTDSource(fUnparsedEntityHandler);
fSchemaValidatorComponentManager = new SchemaValidatorConfiguration(config,
(XSGrammarPoolContainer) grammar, fSchemaValidationManager);
}
/** For third party grammars, use the JAXP validator component. **/
else {
validatorComponent = new JAXPValidatorComponent(grammar.newValidatorHandler());
fSchemaValidatorComponentManager = config;
}
config.addRecognizedFeatures(validatorComponent.getRecognizedFeatures());
config.addRecognizedProperties(validatorComponent.getRecognizedProperties());
config.setDocumentHandler((XMLDocumentHandler) validatorComponent);
((XMLDocumentSource)validatorComponent).setDocumentHandler(domParser);
domParser.setDocumentSource((XMLDocumentSource) validatorComponent);
fSchemaValidator = validatorComponent;
}
// Set features
setFeatures(features);
// Set attributes
setDocumentBuilderFactoryAttributes(dbfAttrs);
// Initial EntityResolver
fInitEntityResolver = domParser.getEntityResolver();
|
Methods Summary |
---|
public org.w3c.dom.DOMImplementation | getDOMImplementation()
return DOMImplementationImpl.getDOMImplementation();
| org.apache.xerces.parsers.DOMParser | getDOMParser()
return domParser;
| public javax.xml.validation.Schema | getSchema()
return grammar;
| public boolean | isNamespaceAware()
try {
return domParser.getFeature(NAMESPACES_FEATURE);
}
catch (SAXException x) {
throw new IllegalStateException(x.getMessage());
}
| public boolean | isValidating()
try {
return domParser.getFeature(VALIDATION_FEATURE);
}
catch (SAXException x) {
throw new IllegalStateException(x.getMessage());
}
| public boolean | isXIncludeAware()Gets the XInclude processing mode for this parser
try {
return domParser.getFeature(XINCLUDE_FEATURE);
}
catch (SAXException exc) {
return false;
}
| public org.w3c.dom.Document | newDocument()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 org.apache.xerces.dom.DocumentImpl();
| public org.w3c.dom.Document | parse(org.xml.sax.InputSource is)
if (is == null) {
throw new IllegalArgumentException(
DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN,
"jaxp-null-input-source", null));
}
if (fSchemaValidator != null) {
if (fSchemaValidationManager != null) {
fSchemaValidationManager.reset();
fUnparsedEntityHandler.reset();
}
resetSchemaValidator();
}
domParser.parse(is);
Document doc = domParser.getDocument();
domParser.dropDocumentReferences();
return doc;
| public void | reset()
/** Restore the initial error handler. **/
if (domParser.getErrorHandler() != fInitErrorHandler) {
domParser.setErrorHandler(fInitErrorHandler);
}
/** Restore the initial entity resolver. **/
if (domParser.getEntityResolver() != fInitEntityResolver) {
domParser.setEntityResolver(fInitEntityResolver);
}
| private void | resetSchemaValidator()
try {
fSchemaValidator.reset(fSchemaValidatorComponentManager);
}
// This should never be thrown from the schema validator.
catch (XMLConfigurationException e) {
throw new SAXException(e);
}
| private void | setDocumentBuilderFactoryAttributes(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;
}
for (Enumeration e = dbfAttrs.keys(); e.hasMoreElements();) {
String name = (String)e.nextElement();
Object val = dbfAttrs.get(name);
if (val instanceof Boolean) {
// Assume feature
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(XMLSCHEMA_VALIDATION_FEATURE, 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 {
// Let Xerces code handle the property
domParser.setProperty(name, val);
}
}
}
| public void | setEntityResolver(org.xml.sax.EntityResolver er)
domParser.setEntityResolver(er);
| public void | setErrorHandler(org.xml.sax.ErrorHandler eh)
domParser.setErrorHandler(eh);
| private void | setFeatures(java.util.Hashtable features)
if (features != null) {
for (Enumeration e = features.keys(); e.hasMoreElements();) {
String feature = (String)e.nextElement();
boolean value = ((Boolean)features.get(feature)).booleanValue();
domParser.setFeature(feature, value);
}
}
|
|