解析器模块,核心类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. Product and Sum in Category Theory

    Even if you are not a functional programmer, the notion of product type should be familiar to you, e ...

  2. 第61节:Java中的DOM和Javascript技术

    Java中的DOM和Javascript技术 DOM是一门技术,是文档对象模型.所需的文档只有标记型文档,如我们所学的html文档(文档中的所有标签都封装成为对象了) DOM: 为Document O ...

  3. NumPy 学习笔记(二)

    数组索引: 1.ndarray对象的内容可以通过索引或切片来访问和修改,与 Python 中 list 的切片操作一样:   由于数组可能是多维的,所以必须为数组的每个维度指定一个切片,使用切片时返回 ...

  4. vim常用命令行备忘总结

    一 窗口切换 1 :sp    水平切换当前窗口 2 :vsp 垂直切换当前窗口 3 :clo 关闭活动窗口 4 : on 只保留活动窗口 5 : ctrl + w  在窗口间循环切换  ctrl + ...

  5. 良好的JavaScript编码风格(语法规则)

    编码风格 1.概述 "编程风格"(programming style)指的是编写代码的样式规则.不同的程序员,往往有不同的编程风格. 有人说,编译器的规范叫做"语法规则& ...

  6. Anaconda+MINGW+theano+keras安装

    前言:这几天算是被这东西困扰的十分难受,博客园和csdn各种逛,找教程,大家说法不一,很多方法也不一定适用,有些方法有待进一步完善.这里我借鉴了许多大神们的方法,以及自己的一些心得,希望对你们有一些帮 ...

  7. maven发布jar包到nexus

    在编码过程中,有些通用的代码模块,有时候我们不想通过复制拷贝来粗暴地复用,因为这样不仅体现不了变化,也不利于统一管理.这里我们使用maven deploy的方式,将通用的模块打成jar包,发布到nex ...

  8. 利用 ELK 搭建 Docker 容器化应用日志中心

    利用 ELK 搭建 Docker 容器化应用日志中心 概述 应用一旦容器化以后,需要考虑的就是如何采集位于 Docker 容器中的应用程序的打印日志供运维分析.典型的比如SpringBoot应用的日志 ...

  9. 分布式事务解决方案以及 .Net Core 下的实现(上)

    数据一致性是构建业务系统需要考虑的重要问题 , 以往我们是依靠数据库来保证数据的一致性.但是在微服务架构以及分布式环境下实现数据一致性是一个很有挑战的的问题.最近在研究分布式事物,分布式的解决方案有很 ...

  10. 动车上的书摘-java网络 连接服务器

    摘要: 摘要: 原创出处: http://www.cnblogs.com/Alandre/ 泥沙砖瓦浆木匠 希望转载,保留摘要,谢谢! 应该有些延迟,你会看到黑幕中弹出 来自USA的X原子的计量时间: ...