跟Allen学Mybatis之XPathParser源码浅谈

本文介绍了一个强大的XPath XML解析器,该解析器支持多种输入源,包括字符串、流和DOM文档,能够进行验证并处理变量替换。文章详细展示了如何使用XPath表达式从XML中抽取字符串、布尔值、数值和节点集合。

1> 根据java dom(org.w3c.dom开头的包)、SAX以及XPath语法解析XML文件的

2> 就是根据xml或流或document对象,验证,Properties,EntityResolver创建对象

package org.apache.ibatis.parsing;

import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.apache.ibatis.builder.BuilderException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XPathParser {
    private Document document;
    private boolean validation;  //是否开启验证,true开启,false关闭
    xsd的
    // publicId : null
    //systemId : http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    dtd:
   // publicId : -//SPRING//DTD BEAN//EN
    //systemId : http://www.springframework.org/dtd/spring-beans.dtd
    private EntityResolver entityResolver;  //项目本身就可以提供一个如何寻找DtD或xsd 的声明方法
    private Properties variables;
    private XPath xpath;
    // 更加 xml名称获取XPathParser对象
    public XPathParser(String xml) {
        this.commonConstructor(false, (Properties)null, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(new StringReader(xml)));
    }
     // 更加 流 获取XPathParser对象
    public XPathParser(Reader reader) {
        this.commonConstructor(false, (Properties)null, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(reader));
    }
    // 更加 流 获取XPathParser对象
    public XPathParser(InputStream inputStream) {
        this.commonConstructor(false, (Properties)null, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(inputStream));
    }
     // 根据Document对象获取XPathParser对象
    public XPathParser(Document document) {
        this.commonConstructor(false, (Properties)null, (EntityResolver)null);
        this.document = document;
    }

    public XPathParser(String xml, boolean validation) {
        this.commonConstructor(validation, (Properties)null, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(new StringReader(xml)));
    }

    public XPathParser(Reader reader, boolean validation) {
        this.commonConstructor(validation, (Properties)null, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(reader));
    }

    public XPathParser(InputStream inputStream, boolean validation) {
        this.commonConstructor(validation, (Properties)null, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(inputStream));
    }

    public XPathParser(Document document, boolean validation) {
        this.commonConstructor(validation, (Properties)null, (EntityResolver)null);
        this.document = document;
    }

    public XPathParser(String xml, boolean validation, Properties variables) {
        this.commonConstructor(validation, variables, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(new StringReader(xml)));
    }

    public XPathParser(Reader reader, boolean validation, Properties variables) {
        this.commonConstructor(validation, variables, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(reader));
    }

    public XPathParser(InputStream inputStream, boolean validation, Properties variables) {
        this.commonConstructor(validation, variables, (EntityResolver)null);
        this.document = this.createDocument(new InputSource(inputStream));
    }

    public XPathParser(Document document, boolean validation, Properties variables) {
        this.commonConstructor(validation, variables, (EntityResolver)null);
        this.document = document;
    }

    public XPathParser(String xml, boolean validation, Properties variables, EntityResolver entityResolver) {
        this.commonConstructor(validation, variables, entityResolver);
        this.document = this.createDocument(new InputSource(new StringReader(xml)));
    }

    public XPathParser(Reader reader, boolean validation, Properties variables, EntityResolver entityResolver) {
        this.commonConstructor(validation, variables, entityResolver);
        this.document = this.createDocument(new InputSource(reader));
    }

    public XPathParser(InputStream inputStream, boolean validation, Properties variables, EntityResolver entityResolver) {
        this.commonConstructor(validation, variables, entityResolver);
        this.document = this.createDocument(new InputSource(inputStream));
    }

    public XPathParser(Document document, boolean validation, Properties variables, EntityResolver entityResolver) {
        this.commonConstructor(validation, variables, entityResolver);
        this.document = document;
    }

    public void setVariables(Properties variables) {
        this.variables = variables;
    }

    public String evalString(String expression) {
        return this.evalString(this.document, expression);
    }

    public String evalString(Object root, String expression) {
        String result = (String)this.evaluate(expression, root, XPathConstants.STRING);
        result = PropertyParser.parse(result, this.variables);
        return result;
    }

    public Boolean evalBoolean(String expression) {
        return this.evalBoolean(this.document, expression);
    }

    public Boolean evalBoolean(Object root, String expression) {
        return (Boolean)this.evaluate(expression, root, XPathConstants.BOOLEAN);
    }

    public Short evalShort(String expression) {
        return this.evalShort(this.document, expression);
    }

    public Short evalShort(Object root, String expression) {
        return Short.valueOf(this.evalString(root, expression));
    }

    public Integer evalInteger(String expression) {
        return this.evalInteger(this.document, expression);
    }

    public Integer evalInteger(Object root, String expression) {
        return Integer.valueOf(this.evalString(root, expression));
    }

    public Long evalLong(String expression) {
        return this.evalLong(this.document, expression);
    }

    public Long evalLong(Object root, String expression) {
        return Long.valueOf(this.evalString(root, expression));
    }

    public Float evalFloat(String expression) {
        return this.evalFloat(this.document, expression);
    }

    public Float evalFloat(Object root, String expression) {
        return Float.valueOf(this.evalString(root, expression));
    }

    public Double evalDouble(String expression) {
        return this.evalDouble(this.document, expression);
    }

    public Double evalDouble(Object root, String expression) {
        return (Double)this.evaluate(expression, root, XPathConstants.NUMBER);
    }

    public List<XNode> evalNodes(String expression) {
        return this.evalNodes(this.document, expression);
    }

    public List<XNode> evalNodes(Object root, String expression) {
        List<XNode> xnodes = new ArrayList();
        NodeList nodes = (NodeList)this.evaluate(expression, root, XPathConstants.NODESET);

        for(int i = 0; i < nodes.getLength(); ++i) {
            xnodes.add(new XNode(this, nodes.item(i), this.variables));
        }

        return xnodes;
    }

    public XNode evalNode(String expression) {
        return this.evalNode(this.document, expression);
    }

    public XNode evalNode(Object root, String expression) {
        Node node = (Node)this.evaluate(expression, root, XPathConstants.NODE);
        return node == null ? null : new XNode(this, node, this.variables);
    }

    private Object evaluate(String expression, Object root, QName returnType) {
        try {
            return this.xpath.evaluate(expression, root, returnType);
        } catch (Exception var5) {
            throw new BuilderException("Error evaluating XPath.  Cause: " + var5, var5);
        }
    }

    private Document createDocument(InputSource inputSource) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(this.validation);
            factory.setNamespaceAware(false);
            factory.setIgnoringComments(true);
            factory.setIgnoringElementContentWhitespace(false);
            factory.setCoalescing(false);
            factory.setExpandEntityReferences(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setEntityResolver(this.entityResolver);
            builder.setErrorHandler(new ErrorHandler() {
                public void error(SAXParseException exception) throws SAXException {
                    throw exception;
                }

                public void fatalError(SAXParseException exception) throws SAXException {
                    throw exception;
                }

                public void warning(SAXParseException exception) throws SAXException {
                }
            });
            return builder.parse(inputSource);
        } catch (Exception var4) {
            throw new BuilderException("Error creating document instance.  Cause: " + var4, var4);
        }
    }

    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();
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值