您的位置:首页 > 其它

mybatis--源码解读---XML的解析

2016-05-27 14:43 477 查看
    mybatis解析xml,写了两个基础的解析工具类,一个是:org.apace.ibatis.parsing.XPathParse,另外一个是:org.apache.ibatis.XNode,这次,我们直接的贴XPathParse注解说明

    XPathParse:主要做了三件事情:(使用的是java本土的dom解析,解析xml有三种方式:dom、sax、djiest(记不清名字了,好像是鬼子写的一个工具))

                                       第一件:载入xml的资源,形成document,

                                       第二件:设置一些初始化的属性

                                       第三件:给出来了16个获取解析值的方法,不过都是在一个基础的工具方法上获取的

/**
* Created by 刘旭 on 2016-05-27.
* 应该这个就是解析的执行类了吧
*/
public class XPathParser {
/**
* 还好定义的这个几个变量都是jdk自己拥有的,不是自己构造的,不然,真是慌忙啊
*/
private Document document;
private boolean validation;
private EntityResolver entityResolver;
private Properties variables;
private XPath xpath;

/**
* 下面还是秉承着框架的风格,:给出来一个超级的构方法,然后是进行一个参数的组合  给了  16 个构造函数
*/
public XPathParser(String xml) {
commonConstructor(false, null, null);
this.document = createDocument(new InputSource(new StringReader(xml)));
}

public XPathParser(Reader reader) {
commonConstructor(false, null, null);
this.document = createDocument(new InputSource(reader));
}

public XPathParser(InputStream inputStream) {
commonConstructor(false, null, null);
this.document = createDocument(new InputSource(inputStream));
}

public XPathParser(Document document) {
commonConstructor(false, null, null);
this.document = document;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
* 下面  16 个 方法都是在这个 private Object evaluate(String expression, Object root, QName returnType)方法的基础上实现的,可以说是这个方法的后代
* 也是  16 个 方法
*/
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);
}

/**
* 这个应该是最原始的解析xml文件中的信息了
*
* @param expression 表达式,其实就是标签,不知道是不是可以使用正则表达式的形式
* @param root       从哪个节点开始
* @param returnType 归还什么类型
* @return 解析出来的对象
*/
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);
}
}

/**
* 构造的解析xml文件形成dom文档形式的方法
*
* @param inputSource 输入的一个流吧,看着是输入源
* @return Document
*/
private Document createDocument(InputSource inputSource) {
// important: this must only be called AFTER common constructor
try {
/**
* 首先给一个工厂,然后是给出来一个建造类
*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validation);

factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);

DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(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 e) {
throw new BuilderException("Error creating document instance.  Cause: " + e, e);
}
}

/**
* 构造一些基础的设置信息
*
* @param validation     是否验证(不知道到底有什么用)
* @param variables      参数变量
* @param entityResolver 实体解析
*/
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();
}
}


最大的感触就是:这些个框架,最喜欢给出来一个实际承受体,然后很多方法都是对参数的组合,这个类的构造方法就是,给了一个最基本的构造方法,然后就是,给出了16个组合的构造方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息