1. 程式人生 > >Mybatis框架基礎支持層——解析器模塊(2)

Mybatis框架基礎支持層——解析器模塊(2)

聯網 ring erro 由於 led nodelist 調用 行操作 tab

解析器模塊,核心類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)