/**
* 封装了用于xml解析的类XPath、Document和EntityResolver
*/
public class XPathParser {
/**
* 将xml文件读入内存,并构建一棵树,
* 通过树结构对各个节点node进行操作
*/
private Document document;
/**
* 是否开启xml文本格式验证
*/
private boolean validation;
/**
* 用于加载本地DTD文件
*
* 默认情况下,对xml文档进行验证时,会根据xml文件开始
* 位置指定的网址加载对应的dtd或xsd文件,当由于网络原因,易导致验证过慢,
* 在实践中往往会提前设置EntityResolver接口对象加载本地的dtd文件,
* 从而避免联网加载;
*
* XMLMapperEntityResolver implement EntityResolver
*/
private EntityResolver entityResolver;
/**
* mybatis核心配置文件中标签<properties>定义的键值对
*/
private Properties variables;
/**
* 为查询xml文本而设计的语言,配合Document使用
* XPath之于xml好比Sql之于database
*/
private XPath xpath;
public XPathParser(String xml) {
commonConstructor(false, null, null);
this.document = createDocument(new InputSource(new StringReader(xml)));
}
//...一系列构造方法(略)
public void setVariables(Properties variables) {
this.variables = variables;
}
/**
* XPath提供的一系列eval*()方法用于解析boolean、short、int、String、Node等类型信息,
* 通过调用XPath.evaluate()方法查找指定路径的节点或者属性,并进行相应的类型转换
*/
public String evalString(String expression) {
return evalString(document, expression);
}
public String evalString(Object root, String expression) {
String result = (String) evaluate(expression, root, XPathConstants.STRING);
result = PropertyParser.parse(result, variables);
return result;
}
public Boolean evalBoolean(String expression) {
return evalBoolean(document, expression);
}
public Boolean evalBoolean(Object root, String expression) {
return (Boolean) evaluate(expression, root, XPathConstants.BOOLEAN);
}
public Short evalShort(String expression) {
return evalShort(document, expression);
}
public Short evalShort(Object root, String expression) {
return Short.valueOf(evalString(root, expression));
}
public Integer evalInteger(String expression) {
return evalInteger(document, expression);
}
public Integer evalInteger(Object root, String expression) {
return Integer.valueOf(evalString(root, expression));
}
public Long evalLong(String expression) {
return evalLong(document, expression);
}
public Long evalLong(Object root, String expression) {
return Long.valueOf(evalString(root, expression));
}
public Float evalFloat(String expression) {
return evalFloat(document, expression);
}
public Float evalFloat(Object root, String expression) {
return Float.valueOf(evalString(root, expression));
}
public Double evalDouble(String expression) {
return evalDouble(document, expression);
}
public Double evalDouble(Object root, String expression) {
return (Double) evaluate(expression, root, XPathConstants.NUMBER);
}
public List<XNode> evalNodes(String expression) {
return evalNodes(document, expression);
}
public List<XNode> evalNodes(Object root, String expression) {
List<XNode> xnodes = new ArrayList<XNode>();
NodeList nodes = (NodeList) evaluate(expression, root, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
xnodes.add(new XNode(this, nodes.item(i), variables));
}
return xnodes;
}
public XNode evalNode(String expression) {
return evalNode(document, expression);
}
public XNode evalNode(Object root, String expression) {
Node node = (Node) evaluate(expression, root, XPathConstants.NODE);
if (node == null) {
return null;
}
return new XNode(this, node, variables);
}
private Object evaluate(String expression, Object root, QName returnType) {
try {
return xpath.evaluate(expression, root, returnType);
} catch (Exception e) {
throw new BuilderException("Error evaluating XPath. Cause: " + e, e);
}
}
/**
* 创建Dom对象,调用此方法之前必须调用commonConstructor方法完成XPathParser初始化
*/
private Document createDocument(InputSource inputSource) {
// important: this must only be called AFTER common constructor
try {
//创建DocumentBuilderFactory对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/**
* 对DocumentBuilderFactory对象进行一系列参数配置
*/
factory.setValidating(validation);
factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);
//创建DocumentBuilder对象
DocumentBuilder builder = factory.newDocumentBuilder();
/**
* 对DocumentBuilder对象进行一系列参数配置
*/
builder.setEntityResolver(entityResolver);
builder.setErrorHandler(new ErrorHandler() {
@Override
public void error(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
throw exception;
}
@Override
public void warning(SAXParseException exception) throws SAXException {
}
});
//加载xml文件
return builder.parse(inputSource);
} catch (Exception e) {
throw new BuilderException("Error creating document instance. Cause: " + e, e);
}
}
/**
* 初始化XPathParser
*/
private void commonConstructor(boolean validation, Properties variables, EntityResolver entityResolver) {
this.validation = validation;
this.entityResolver = entityResolver;
this.variables = variables;
XPathFactory factory = XPathFactory.newInstance();
this.xpath = factory.newXPath();
}
}