DocumenLoader的类层次图:
接口中只定义了一个方法loadDocument:
public interface DocumentLoader {
Document loadDocument(InputSource var1, EntityResolver var2, ErrorHandler var3, int var4, boolean var5) throws Exception;
}
DefaultDocumentLoader实现了这个方法
- 创建DocumentBuilderFactory
- 使用Factory 创建DocumentBuilder
- 使用builder解析输入源DocumentBuilderImpl。第九篇详解
参数介绍:EntityResolver在第八篇,validationMode在第六篇
/**
valiadationMode:文档检验模式,dtd(2)或者xsd(3),一般为3
namespaceAware:自动探测namespace,一般为false
ErrorHanlder就是日志打印用的
*/
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
DocumentBuilderFactory factory = this.createDocumentBuilderFactory(validationMode, namespaceAware);
DocumentBuilder builder = this.createDocumentBuilder(factory, entityResolver, errorHandler);
return builder.parse(inputSource);
}
DocumentBuilderFactory的创建,实现类是com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl,除了实例化这个类以外,就做了一些基本的属性设置。
protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware) throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(namespaceAware);
if(validationMode != 0) {
factory.setValidating(true);
if(validationMode == 3) {
factory.setNamespaceAware(true);
try {
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
} catch (IllegalArgumentException var6) {
ParserConfigurationException pcex = new ParserConfigurationException("Unable to validate using XSD: Your JAXP provider [" + factory + "] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.");
pcex.initCause(var6);
throw pcex;
}
}
}
return factory;
}
DocumentBuilder 的创建,实际上是让上面的工厂来创建真正的builder,并把entityResolver绑定给它。
protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory, EntityResolver entityResolver, ErrorHandler errorHandler) throws ParserConfigurationException {
DocumentBuilder docBuilder = factory.newDocumentBuilder();
if(entityResolver != null) {
docBuilder.setEntityResolver(entityResolver);
}
if(errorHandler != null) {
docBuilder.setErrorHandler(errorHandler);
}
return docBuilder;
}
实际创建方法也是jdk中rt.jar中实现的,实际的builder是DocumentBuilderImpl。第九篇详解
public DocumentBuilder newDocumentBuilder()
throws ParserConfigurationException
{
/** Check that if a Schema has been specified that neither of the schema properties have been set. */
if (grammar != null && attributes != null) {//一般grammar=null,所以直接走后面
if (attributes.containsKey(JAXPConstants.JAXP_SCHEMA_LANGUAGE)) {
throw new ParserConfigurationException(
SAXMessageFormatter.formatMessage(null,
"schema-already-specified", new Object[] {JAXPConstants.JAXP_SCHEMA_LANGUAGE}));
}
else if (attributes.containsKey(JAXPConstants.JAXP_SCHEMA_SOURCE)) {
throw new ParserConfigurationException(
SAXMessageFormatter.formatMessage(null,
"schema-already-specified", new Object[] {JAXPConstants.JAXP_SCHEMA_SOURCE}));
}
}
try {
return new DocumentBuilderImpl(this, attributes, features, fSecureProcess);
} catch (SAXException se) {
// Handles both SAXNotSupportedException, SAXNotRecognizedException
throw new ParserConfigurationException(se.getMessage());
}
}