Mybatis框架基础支持层——解析器模块(2)
解析器模块,核心类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)的更多相关文章
- Mybatis框架基础支持层——日志模块(8)
前言: java开发中常用的日志框架有Log4j,Log4j2,Apache Commons Log,java.util.logging,slf4j等,这些工具对外的接口不尽相同.为了统一这些工具的接 ...
- Mybatis框架基础支持层——反射工具箱之泛型解析工具TypeParameterResolver(4)
简介:TypeParameterResolver是一个工具类,提供一系列的静态方法,去解析类中的字段.方法返回值.方法参数的类型. 在正式介绍TypeParameterResolver之前,先介绍一个 ...
- Mybatis框架基础支持层——反射工具箱之Reflector&ReflectorFactory(3)
说明:Reflector是Mybatis反射工具的基础,每个Reflector对应一个类,在Reflector中封装有该类的元信息, 以及基于类信息的一系列反射应用封装API public class ...
- Mybatis框架基础支持层——反射工具箱之MetaClass(7)
简介:MetaClass是Mybatis对类级别的元信息的封装和处理,通过与属性工具类的结合, 实现了对复杂表达式的解析,实现了获取指定描述信息的功能 public class MetaClass { ...
- Mybatis框架基础支持层——反射工具箱之实体属性Property工具集(6)
本篇主要介绍mybatis反射工具中用到的三个属性工具类:PropertyTokenizer.PropertyNamer.PropertyCopier. PropertyTokenizer: 主要用来 ...
- Mybatis框架基础支持层——反射工具箱之对象工厂ObjectFactory&DefaultObjectFactory(5)
ObjectFactory官方简介:MyBatis每次创建结果集对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成. 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认 ...
- 精尽 MyBatis 源码分析 - 基础支持层
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...
- (一) Mybatis源码分析-解析器模块
Mybatis源码分析-解析器模块 原创-转载请说明出处 1. 解析器模块的作用 对XPath进行封装,为mybatis-config.xml配置文件以及映射文件提供支持 为处理动态 SQL 语句中的 ...
- MyBatis 框架 基础应用
1.ORM的概念和优势 概念: 对象关系映射(Object Relational Mapping,简称ORM)是通过使用描述对象和数据库之间映射的元数据,将面向对象语言程序中的对象自动持久化到关系数据 ...
随机推荐
- maven项目(多模块)
在eclipse下构建maven项目,该项目由多个子模块组成. 1.创建一个父项目 NEW -->project-->maven-->maven Project,点击下一步,进入ne ...
- Speech and Booth Demo in Maker Faire Shenzhen 2018
2018年10月12日-14日,受主办方的邀请,有幸参加了这次的Maker Faire Shenzhen 2018,并参与了Maker Faire Shenzhen 2018 论坛-创客的城市共创(C ...
- 【高速接口-RapidIO】1、RapidIO协议概述
一.RapidIO背景介绍 RapidIO是由Motorola和Mercury等公司率先倡导的一种高性能. 低引脚数. 基于数据包交换的互连体系结构,是为满足和未来高性能嵌入式系统需求而设计的一种开放 ...
- Android逆向之静态分析
想必打过CTF的小伙伴多多少少都触过Android逆向,所以斗哥将给大家整一期关于Android逆向的静态分析与动态分析.本期先带来Android逆向的静态分析,包括逆向工具使用.文件说明.例题解析等 ...
- ECDSA数字签名算法
一.ECDSA概述 椭圆曲线数字签名算法(ECDSA)是使用椭圆曲线密码(ECC)对数字签名算法(DSA)的模拟.ECDSA于1999年成为ANSI标准,并于2000年成为IEEE和NIST标准.它在 ...
- SpringBoot中集成redis
转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...
- 树莓派配置wifi链接
使用树莓派链接网络是必然的,这里讲一讲如何连接到wifi. 扫描WIFI sudo iwlist wlan0 scan 扫描后得到以下结果 这里的“ESSID”是无线网的名称. 添加有密码的WIFI网 ...
- ubuntu设置IP地址&修改vi模式键盘上下键错位
解决ubuntu上面使用vi 出现方向键错乱的情况 编辑/etc/vim/vimrc.tiny 使用root权限操作:将“set compatible”改成“set nocompatible” 新增一 ...
- HP服务器设置iLO
HP服务器设置iLO步凑 1.开机出现界面—按下F11进入Boot Menu: 2.选择Generic USB Boot回车: 3.选择System Configuration回车: 4.选择iLO ...
- 微信小程序:防止多次点击跳转(函数节流)和防止表单组件输入内容多次验证(函数防抖)
一.函数节流(throttle) **函数节流:一个函数执行一次后,只有大于设定的执行周期后才会执行第二次**.有个需要频繁触发函数,出于优化性能角度,在规定时间内,只让函数触发的第一次生效,后面不生 ...