解析器模块,核心类XPathParser

/**
* 封装了用于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();
} }

Mybatis框架基础支持层——解析器模块(2)的更多相关文章

  1. Mybatis框架基础支持层——日志模块(8)

    前言: java开发中常用的日志框架有Log4j,Log4j2,Apache Commons Log,java.util.logging,slf4j等,这些工具对外的接口不尽相同.为了统一这些工具的接 ...

  2. Mybatis框架基础支持层——反射工具箱之泛型解析工具TypeParameterResolver(4)

    简介:TypeParameterResolver是一个工具类,提供一系列的静态方法,去解析类中的字段.方法返回值.方法参数的类型. 在正式介绍TypeParameterResolver之前,先介绍一个 ...

  3. Mybatis框架基础支持层——反射工具箱之Reflector&ReflectorFactory(3)

    说明:Reflector是Mybatis反射工具的基础,每个Reflector对应一个类,在Reflector中封装有该类的元信息, 以及基于类信息的一系列反射应用封装API public class ...

  4. Mybatis框架基础支持层——反射工具箱之MetaClass(7)

    简介:MetaClass是Mybatis对类级别的元信息的封装和处理,通过与属性工具类的结合, 实现了对复杂表达式的解析,实现了获取指定描述信息的功能 public class MetaClass { ...

  5. Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)

    本篇主要介绍mybatis反射工具中用到的三个属性工具类:PropertyTokenizer.PropertyNamer.PropertyCopier. PropertyTokenizer: 主要用来 ...

  6. Mybatis框架基础支持层——反射工具箱之对象工厂ObjectFactory&DefaultObjectFactory(5)

    ObjectFactory官方简介:MyBatis每次创建结果集对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成. 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认 ...

  7. 精尽 MyBatis 源码分析 - 基础支持层

    该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...

  8. (一) Mybatis源码分析-解析器模块

    Mybatis源码分析-解析器模块 原创-转载请说明出处 1. 解析器模块的作用 对XPath进行封装,为mybatis-config.xml配置文件以及映射文件提供支持 为处理动态 SQL 语句中的 ...

  9. MyBatis 框架 基础应用

    1.ORM的概念和优势 概念: 对象关系映射(Object Relational Mapping,简称ORM)是通过使用描述对象和数据库之间映射的元数据,将面向对象语言程序中的对象自动持久化到关系数据 ...

随机推荐

  1. 包建强的培训课程(14):Android与ReactNative

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  2. webpack之牛刀小试 打包并压缩html、js

    1.创建项目文件夹test,在文件夹下创建src文件夹用来存放源码,在src文件夹下创建index.html/index.js两件文件. 我们的最终目的是将这两个文件打包压缩并输出到/test/dis ...

  3. [转] 如何用kaldi训练好的模型做特定任务的在线识别

    转自:http://blog.csdn.net/inger_h/article/details/52789339 在已经训练好模型的情况下,需要针对一个新任务做在线识别应该怎么做呢? 一种情况是,用已 ...

  4. [翻译][架构设计]The Clean Architecture

    原文地址:The Clean Architecture The Clean Architecture Over the last several years we've seen a whole ra ...

  5. 站在JAVA数据结构的视角看待简单表结构

    1.前言: 我们提到程序中的集合的时候,往往脑海中会浮现出, 如ArrayList和LinkedList以及和HashMap.当然在提到ArrayList和LinkedList的时候,我们大多数的人都 ...

  6. IntelliJ IDEA 的下载和安装

    下载 官网地址:https://www.jetbrains.com/idea/ 直接点击 DOWNLOAD 下载 接下来跳转到一个页面,可以看到第一个红框中是选择操作系统的,IDEA分为收费的旗舰版和 ...

  7. JS 实现触发下载内容(H5 download)

    概述 我对使用js控制下载非常感兴趣,在网上查资料的时候碰巧看到了相关实现方法,记录下来供以后开发时参考,相信对其他人也有用. 参考资料: JS前端创建html或json文件并浏览器导出下载 理解DO ...

  8. LabVIEW(三):定时与触发

    一.定时 多功能数据采集板卡的时钟特性,举例为M系列定时引擎:板卡上控制采集和波形发生的三个时钟:AI Sample Clock.AI Convert Clock.AO Sample Clock.所有 ...

  9. tomcat编译超过64k大小的jsp文件报错原因

    今天遇到一个问题,首先是在tomcat中间件上跑的web项目,一个jsp文件,因为代码行数实在是太多了,更新了几个版本之后编译报错了,页面打开都是报500的错误,500的报错,知道http协议返回码的 ...

  10. 使用线程统计信息(Thread Statistics)

    可在session log中使用线程统计信息来判断source,target或组的性能瓶颈 默认情况下,Integration Service在运行session时,使用一个reader thread ...