mybatis,Spring等工具对xml文件正确性的验证

  我们知道mybatis或者spring都是使用xml文件作为配置文件,配置文件的格式都是定义在叫做.dtd或者.xsd文件中的,当工具在解析用户自己定义的xml文件的时候,如何才能知道用户自定义的文件是否正确的呢?我们不能在xml文件中乱写一些框架不认识的标签,比如在spring的xml文件中写如下<user>标签,毫无疑问会报错。那么框架是怎么来验证我们所写的标签是否正确的呢?

<user>
  <id>100</id>
</user>

  由于mybatis使用的是dom解析,利用JDK的dom解析API,如下:

 1     @Test
 2     public void docFactoryTest() throws Exception {
 3         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 4         factory.setValidating(true);
 5         factory.setNamespaceAware(false);
 6         factory.setIgnoringComments(true);
 7         factory.setIgnoringElementContentWhitespace(false);
 8         factory.setCoalescing(false);
 9         factory.setExpandEntityReferences(true);
10         DocumentBuilder builder = factory.newDocumentBuilder();
11         builder.setEntityResolver(new XMLMapperEntityResolver());      ...(此处省略一部分代码)
30         Document doc = builder.parse(Resources.getResourceAsStream("mybatis-config.xml"));
31         System.err.println(doc);

  在第11行中,用到了XMLMapperEntityResolver对象,这个对象定义如下:

 1 public class XMLMapperEntityResolver implements EntityResolver {
 2
 3   private static final Map<String, String> doctypeMap = new HashMap<String, String>();
 4
 5   private static final String IBATIS_CONFIG_PUBLIC = "-//ibatis.apache.org//DTD Config 3.0//EN".toUpperCase(Locale.ENGLISH);
 6   private static final String IBATIS_CONFIG_SYSTEM = "http://ibatis.apache.org/dtd/ibatis-3-config.dtd".toUpperCase(Locale.ENGLISH);
 7
 8   private static final String IBATIS_MAPPER_PUBLIC = "-//ibatis.apache.org//DTD Mapper 3.0//EN".toUpperCase(Locale.ENGLISH);
 9   private static final String IBATIS_MAPPER_SYSTEM = "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd".toUpperCase(Locale.ENGLISH);
10
11   private static final String MYBATIS_CONFIG_PUBLIC = "-//mybatis.org//DTD Config 3.0//EN".toUpperCase(Locale.ENGLISH);
12   private static final String MYBATIS_CONFIG_SYSTEM = "http://mybatis.org/dtd/mybatis-3-config.dtd".toUpperCase(Locale.ENGLISH);
13
14   private static final String MYBATIS_MAPPER_PUBLIC = "-//mybatis.org//DTD Mapper 3.0//EN".toUpperCase(Locale.ENGLISH);
15   private static final String MYBATIS_MAPPER_SYSTEM = "http://mybatis.org/dtd/mybatis-3-mapper.dtd".toUpperCase(Locale.ENGLISH);
16
17   private static final String MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd";
18   private static final String MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd";
19
20   static {
21     doctypeMap.put(IBATIS_CONFIG_SYSTEM, MYBATIS_CONFIG_DTD);
22     doctypeMap.put(IBATIS_CONFIG_PUBLIC, MYBATIS_CONFIG_DTD);
23
24     doctypeMap.put(IBATIS_MAPPER_SYSTEM, MYBATIS_MAPPER_DTD);
25     doctypeMap.put(IBATIS_MAPPER_PUBLIC, MYBATIS_MAPPER_DTD);
26
27     doctypeMap.put(MYBATIS_CONFIG_SYSTEM, MYBATIS_CONFIG_DTD);
28     doctypeMap.put(MYBATIS_CONFIG_PUBLIC, MYBATIS_CONFIG_DTD);
29
30     doctypeMap.put(MYBATIS_MAPPER_SYSTEM, MYBATIS_MAPPER_DTD);
31     doctypeMap.put(MYBATIS_MAPPER_PUBLIC, MYBATIS_MAPPER_DTD);
32   }
33
34   /*
35    * Converts a public DTD into a local one
36    *
37    * @param publicId The public id that is what comes after "PUBLIC"
38    * @param systemId The system id that is what comes after the public id.
39    * @return The InputSource for the DTD
40    *
41    * @throws org.xml.sax.SAXException If anything goes wrong
42    */
43   @Override
44   public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
45
46     if (publicId != null) {
47       publicId = publicId.toUpperCase(Locale.ENGLISH);
48     }
49     if (systemId != null) {
50       systemId = systemId.toUpperCase(Locale.ENGLISH);
51     }
52
53     InputSource source = null;
54     try {
55       String path = doctypeMap.get(publicId);
56       source = getInputSource(path, source);
57       if (source == null) {
58         path = doctypeMap.get(systemId);
59         source = getInputSource(path, source);
60       }
61     } catch (Exception e) {
62       throw new SAXException(e.toString());
63     }
64     return source;
65   }
66
67   private InputSource getInputSource(String path, InputSource source) {
68     if (path != null) {
69       InputStream in;
70       try {
71         in = Resources.getResourceAsStream(path);
72         source = new InputSource(in);
73       } catch (IOException e) {
74         // ignore, null is ok
75       }
76     }
77     return source;
78   }
79
80 }

  可以看到,当框架在解析xml文件的时候,会把xml文件开头的publicId和systemId传给EntityResolver,而EntityResolver对象就是从classpath中去寻找.dtd文件(文件就在org/apache/ibatis/builder/xml/目录下),在spring中还会存在.xsd文件,原理都是一样的,然后利用classpath中的.dtd文件进行验证。如果不指定这个.dtd文件,那么会从互联网上面下载.dtd文件,性能不好。

时间: 2024-08-26 15:15:07

mybatis,Spring等工具对xml文件正确性的验证的相关文章

Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)

ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件. 2.excel定义成模板,里面只填写了所需要的数据,有可能数据有问题. 3.在导入的时候就需要对每个excel单元格的数据进行验证. 4.验证完之后,若所有数据正确,那么批量保存.若有一点点错误,就不执行保存操作,并提示错误原因. 思路: 1.完美使用了Map的功能,先将xml中的数据存入map

Spring介绍及配置(XML文件配置和注解配置)

本节内容: Spring介绍 Spring搭建 Spring概念 Spring配置讲解 使用注解配置Spring 一.Spring介绍 1. 什么是Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由 RodJohnson 在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构

【MyBatis学习05】SqlMapConfig.xml文件中的配置总结

经过上两篇博文的总结,对mybatis中的dao开发方法和流程基本掌握了,这一节主要来总结一下mybatis中的全局配置文件SqlMapConfig.xml在开发中的一些常用配置,首先看一下该全局配置文件中都有哪些可以配置的东西: 配置内容 作用 <properties> 用来加载属性文件 <settings> 用来设置全局参数 <typeAliases> 用来设置类型的别名 <typeHandlers> 用来设置类型处理器 <objectFactor

Spring MVC 配置文件dispatcher-servlet.xml 文件详解

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/sch

MyBatis中SQL映射的XML文件

Mappers 既然MyBatis的行为已经由上篇介绍的MyBatis配置文件的元素配置完了,我们现在就要定义SQL映射语句了.但是,首先我们需要告诉MyBatis到哪里去找到这些配置.Java在这方面没有提供一个很好的方法,所以最佳的方式是告诉MyBatis去哪里去找映射文件.你可以使用相对于类路径的资源引用,或者字符表示,或url应用的完全限定名. MyBatis架构中,POJOs对象对象一个映射器接口,映射器接口和对应的SQL映射的XML在同一包下. SQL映射的XML文件 MyBatis

【转】Mybatis 3.1中 Mapper XML 文件 的学习详解

MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 95%的代码量.MyBatis 的构建就是聚焦于 SQL 的,使其远离于普通的方式. SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序): cache – 配置给定命名空间的缓存. cache-ref – 从其他命名空间引用缓存配置. resultMap – 最复杂,也是最有力量的元

Mybatis 3.1中 Mapper XML 文件 的学习详解

转:http://blog.csdn.net/zhll3377/article/details/8203440 MyBatis 真正的力量是在映射语句中.这里是奇迹发生的地方.对于所有的力量,SQL 映射的 XML 文件是相当的简单.当然如果你将它们和对等功能的 JDBC 代码来比较,你会发现映射文件节省了大约 95%的代码量.MyBatis 的构建就是聚焦于 SQL 的,使其远离于普通的方式. SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序): cache – 配置给定命名空间

Spring MVC 配置文件 web.xml文件详解

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.s

Spring MVC 配置文件dispatcher-servlet.xml 文件详解(转自 学无止境-yj)

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/sch