class="java" name="code">
public final class XMLHelper {
public static final EntityResolver DEFAULT_DTD_RESOLVER = new DTDEntityResolver();
private DOMReader domReader;
private SAXReader saxReader;
/**
* @param errorHandler the sax error handler
* @param entityResolver an xml entity resolver
*
* @return Create and return a dom4j {@code SAXReader} which will append all validation errors
* to the passed error list
*/
public SAXReader createSAXReader(ErrorHandler errorHandler, EntityResolver entityResolver) {
SAXReader saxReader = resolveSAXReader();
saxReader.setEntityResolver( entityResolver );
saxReader.setErrorHandler( errorHandler );
return saxReader;
}
private SAXReader resolveSAXReader() {
if ( saxReader == null ) {
saxReader = new SAXReader();
saxReader.setMergeAdjacentText( true );
saxReader.setValidation( true );
}
return saxReader;
}
}
这里在createSAXReader方法的第一行就调用了resolveSAXReader(),
后者检查了saxReader是否为空, 如果为空, 则创建新的。
不为空则返回类的内部实例saxReader.