mybatis-XMLMapperEntityResolver 找对应的dtd文件

URL和mybatis的dtd文件用map做了一个映射

每个配置文件中有一个PUBLICID和SYSTEMID 这个是我们注册的key 

而EntityResolver是一个接口 实现这个接口可以去寻找dtd文件


public class XMLMapperEntityResolver implements EntityResolver {


/**

* 用来做映射处理KEY和DTD文件的映射 可以理解为策略模式

*/

private static final Map doctypeMap = new HashMap();


private static final String IBATIS_CONFIG_PUBLIC = "-//ibatis.apache.org//DTD Config 3.0//EN"

.toUpperCase(Locale.ENGLISH);

private static final String IBATIS_CONFIG_SYSTEM = "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"

.toUpperCase(Locale.ENGLISH);


private static final String IBATIS_MAPPER_PUBLIC = "-//ibatis.apache.org//DTD Mapper 3.0//EN"

.toUpperCase(Locale.ENGLISH);

private static final String IBATIS_MAPPER_SYSTEM = "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"

.toUpperCase(Locale.ENGLISH);


private static final String MYBATIS_CONFIG_PUBLIC = "-//mybatis.org//DTD Config 3.0//EN"

.toUpperCase(Locale.ENGLISH);

private static final String MYBATIS_CONFIG_SYSTEM = "http://mybatis.org/dtd/mybatis-3-config.dtd"

.toUpperCase(Locale.ENGLISH);


private static final String MYBATIS_MAPPER_PUBLIC = "-//mybatis.org//DTD Mapper 3.0//EN"

.toUpperCase(Locale.ENGLISH);

private static final String MYBATIS_MAPPER_SYSTEM = "http://mybatis.org/dtd/mybatis-3-mapper.dtd"

.toUpperCase(Locale.ENGLISH);


private static final String MYBATIS_CONFIG_DTD = "org/mybatis/builder/xml/mybatis-3-config.dtd";

private static final String MYBATIS_MAPPER_DTD = "org/mybatis/builder/xml/mybatis-3-mapper.dtd";



static {

doctypeMap.put(IBATIS_CONFIG_SYSTEM, MYBATIS_CONFIG_DTD);

doctypeMap.put(IBATIS_CONFIG_PUBLIC, MYBATIS_CONFIG_DTD);


doctypeMap.put(IBATIS_MAPPER_SYSTEM, MYBATIS_MAPPER_DTD);

doctypeMap.put(IBATIS_MAPPER_PUBLIC, MYBATIS_MAPPER_DTD);


doctypeMap.put(MYBATIS_CONFIG_SYSTEM, MYBATIS_CONFIG_DTD);

doctypeMap.put(MYBATIS_CONFIG_PUBLIC, MYBATIS_CONFIG_DTD);


doctypeMap.put(MYBATIS_MAPPER_SYSTEM, MYBATIS_MAPPER_DTD);

doctypeMap.put(MYBATIS_MAPPER_PUBLIC, MYBATIS_MAPPER_DTD);

}

/**

* 返回PUBLICID SYSTEMID所对应的DTD文件

*/

public InputSource resolveEntity(String publicId, String systemId) throws SAXException {


if (publicId != null) {

publicId = publicId.toUpperCase(Locale.ENGLISH);

}

if (systemId != null) {

systemId = systemId.toUpperCase(Locale.ENGLISH);

}


InputSource source = null;

try {

String path = doctypeMap.get(publicId);

source = getInputSource(path, source);

if (source == null) {

path = doctypeMap.get(systemId);

source = getInputSource(path, source);

}

} catch (Exception e) {

throw new SAXException(e.toString());

}

return source;

}


/**

* SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入

* @param path

* @param source

* @return

*/

private InputSource getInputSource(String path, InputSource source) {

if (path != null) {

InputStream in;

try {

/**

* 通过自定义类加载器查找资源

*/

in = Resources.getResourceAsStream(path);

source = new InputSource(in);

} catch (IOException e) {

// ignore, null is ok

}

}

return source;

}


}



你可能感兴趣的:(mybatis-3.3.0)