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();
    }

}

原文地址:https://www.cnblogs.com/wly1-6/p/10281903.html

时间: 2024-07-30 15:03:05

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

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

说明:Reflector是Mybatis反射工具的基础,每个Reflector对应一个类,在Reflector中封装有该类的元信息, 以及基于类信息的一系列反射应用封装API public class Reflector { private static final String[] EMPTY_STRING_ARRAY = new String[0]; /** * 对应的类Class对象 */ private Class<?> type; /** * 类中可读属性的集合,就是存在相应的get

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

简介:MetaClass是Mybatis对类级别的元信息的封装和处理,通过与属性工具类的结合, 实现了对复杂表达式的解析,实现了获取指定描述信息的功能 public class MetaClass { private ReflectorFactory reflectorFactory; private Reflector reflector; /** * 构造函数私有 */ private MetaClass(Class<?> type, ReflectorFactory reflectorF

MyBatis源码分析-基础支持层反射模块Reflector/ReflectorFactory

本文主要介绍MyBatis的反射模块是如何实现的. MyBatis 反射的核心类Reflector,下面我先说明它的构造函数和成员变量.具体方法下面详解. org.apache.ibatis.reflection.Reflector public class Reflector { private final Class<?> type; //对应的Class 类型 //可读属性的名称集合,可读属性就是存在相应getter 方法的属性,初始值为空数纽 private final String[

Mybatis框架--基础使用的一些坑

1 mybatis的底层实现 使用dom4j将配置文件读取出来,使用动态代理动态创建代理对象,中间的调用方法和过程是使用java的反射机制 2 数据库字段和属性名不一致的问题 1 在查询语句中使用别名 <select id="selectBlog2" parameterType="int" resultType="Blog">        select        `id`,        `title`,        `aut

Spring整合Mybatis框架-为业务层添加声明式事务控制 要么都成功要么都失败

其实上面一个例子,是看不出事务控制的,接下来,我们模拟一下真实的业务场景,一次添加一批用户,我们现在想要的结果是:要么都添加成功,要么都添加失败 只需要在上一个小demo的基础上进行稍微的改动就可以 UserServiceImpl.java   循环去调用添加用的方法 测试方法: 1 @Test 2 public void testAdd(){ 3 logger.debug("testAdd !==================="); 4 5 try { 6 Application

mybatis-解析器模块

本文,我们来分享 MyBatis 的解析器模块,对应 parsing 包.如下图所示: 在 <精尽 MyBatis 源码解析 —— 项目结构一览> 中,简单介绍了这个模块如下: 解析器模块,主要提供了两个功能: 一个功能,是对 XPath 进行封装,为 MyBatis 初始化时解析 mybatis-config.xml 配置文件以及映射配置文件提供支持. 另一个功能,是为处理动态 SQL 语句中的占位符提供支持. 下面,我们就来看看具体的源码.因为 parsing 是基础支持层,所以建议胖友在

DRF - 解析器组件

DRF之解析器组件 引入 Django RestFramework帮助我们实现了处理application/json协议请求的数据,另外,我们也提到,如果不使用DRF,直接从request.body里面拿到原始的客户端请求的字节数据,经过decode,然后json反序列化之后,也可以得到一个Python字典类型的数据. 但是,这种方式并不被推荐,因为已经有了非常优秀的第三方工具,那就是Django RestFramework的解析器组件. 解析器组件的使用 首先,来看看解析器组件的使用,稍后我们

使用解析器

使用解析器是非常简单,可以使用它自己的词法分析器,但是,用fsyacc.exe 产生的解析器总是要求词法分析器.在这一小节,我们将讨论如何使用自己的词法分析器,以及与解析器联合. 警告 记住F# 编译器不能直接使用.fsl 和 .fsy 文件,需要用fslex.exe 和 fsyacc.exe 进行编译,创建.fs 文件,然后才能使用.通常,使用预编译事件实现,它在 Visual Studio 的项目属性对话框中. 为了使用自己的词法分析器,首先需要创建LexBuffer,表示处理的文本.Lex

框架 day65 Mybatis入门(基础知识:框架原理,入门[curd],开发dao层,全局与映射配置)

Mybatis 基础知识(一) 第一天:基础知识(重点) mybatis介绍 mybatis框架原理(掌握) mybaits入门程序(掌握) 用户信息进行增.删.改.查 mybatis开发dao层方法:(掌握) 原始dao开发方法(dao接口和实现类需要程序员编写) mapper代理开发方法(程序员只需要编写接口) SqlMapConfig.xml(mybatis全局配置文件)(掌握) mybatis输入映射(掌握) mybatis输出映射(掌握) mybatis动态sql(掌握)   1