一、前言
关于hibernate(3.0.1)源码包中org.hibernate.util.XMLHelper处理类, 分别创建createSAXReader基于org.dom4j.io.SAXReader解析器、创建createDOMReader基于org.dom4j.io.DOMReader解析器,详情参见代码说明部分。
二、源码说明
1.XMLHelper类
package org.hibernate.util;@b@@b@import java.util.List;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.dom4j.DocumentFactory;@b@import org.dom4j.Element;@b@import org.dom4j.io.DOMReader;@b@import org.dom4j.io.SAXReader;@b@import org.xml.sax.EntityResolver;@b@import org.xml.sax.ErrorHandler;@b@import org.xml.sax.SAXParseException;@b@@b@public final class XMLHelper@b@{@b@ private static final Log log = LogFactory.getLog(XMLHelper.class);@b@ public static final EntityResolver DEFAULT_DTD_RESOLVER = new DTDEntityResolver();@b@ private DOMReader domReader;@b@ private SAXReader saxReader;@b@@b@ public SAXReader createSAXReader(String file, List errorsList, EntityResolver entityResolver)@b@ {@b@ if (this.saxReader == null) this.saxReader = new SAXReader();@b@ this.saxReader.setEntityResolver(entityResolver);@b@ this.saxReader.setErrorHandler(new ErrorLogger(file, errorsList));@b@ this.saxReader.setMergeAdjacentText(true);@b@ this.saxReader.setValidation(true);@b@ return this.saxReader;@b@ }@b@@b@ public DOMReader createDOMReader()@b@ {@b@ if (this.domReader == null) this.domReader = new DOMReader();@b@ return this.domReader;@b@ }@b@@b@ public static Element generateDom4jElement(String elementName)@b@ {@b@ return DocumentFactory.getInstance().createElement(elementName);@b@ }@b@@b@ static Log access$000()@b@ {@b@ return log;@b@ }@b@@b@ public static class ErrorLogger@b@ implements ErrorHandler@b@ {@b@ private String file;@b@ private List errors;@b@@b@ ErrorLogger(String file, List errors)@b@ {@b@ this.file = file;@b@ this.errors = errors; }@b@@b@ public void error(SAXParseException error) {@b@ XMLHelper.access$000().error("Error parsing XML: " + this.file + '(' + error.getLineNumber() + ") " + error.getMessage());@b@ this.errors.add(error); }@b@@b@ public void fatalError(SAXParseException error) {@b@ error(error); }@b@@b@ public void warning(SAXParseException warn) {@b@ XMLHelper.access$000().warn("Warning parsing XML: " + this.file + '(' + warn.getLineNumber() + ") " + warn.getMessage());@b@ }@b@ }@b@}
2.DTDEntityResolver类
package org.hibernate.util;@b@@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.ObjectInputStream;@b@import java.io.Serializable;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.xml.sax.EntityResolver;@b@import org.xml.sax.InputSource;@b@@b@public class DTDEntityResolver@b@ implements EntityResolver, Serializable@b@{@b@ private static final Log log = LogFactory.getLog(DTDEntityResolver.class);@b@ private static final String URL = "http://hibernate.sourceforge.net/";@b@ private transient ClassLoader resourceLoader;@b@@b@ public DTDEntityResolver()@b@ {@b@ this.resourceLoader = super.getClass().getClassLoader();@b@ }@b@@b@ public DTDEntityResolver(ClassLoader resourceLoader)@b@ {@b@ this.resourceLoader = resourceLoader;@b@ }@b@@b@ public InputSource resolveEntity(String publicId, String systemId) {@b@ if ((systemId != null) && (systemId.startsWith("http://hibernate.sourceforge.net/"))) {@b@ log.debug("trying to locate " + systemId + " in classpath under org/hibernate/");@b@@b@ String path = "org/hibernate/" + systemId.substring("http://hibernate.sourceforge.net/".length());@b@ InputStream dtdStream = (this.resourceLoader == null) ? super.getClass().getResourceAsStream(path) : this.resourceLoader.getResourceAsStream(path);@b@@b@ if (dtdStream == null) {@b@ log.debug(systemId + " not found in classpath");@b@ return null;@b@ }@b@@b@ log.debug("found " + systemId + " in classpath");@b@ InputSource source = new InputSource(dtdStream);@b@ source.setPublicId(publicId);@b@ source.setSystemId(systemId);@b@ return source;@b@ }@b@@b@ return null;@b@ }@b@@b@ private void readObject(ObjectInputStream ois)@b@ throws IOException, ClassNotFoundException@b@ {@b@ ois.defaultReadObject();@b@ this.resourceLoader = super.getClass().getClassLoader();@b@ }@b@}