1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.builder.xml;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Locale;
21
22 import org.apache.ibatis.io.Resources;
23 import org.xml.sax.EntityResolver;
24 import org.xml.sax.InputSource;
25 import org.xml.sax.SAXException;
26
27
28
29
30
31
32
33 public class XMLMapperEntityResolver implements EntityResolver {
34
35 private static final String IBATIS_CONFIG_SYSTEM = "ibatis-3-config.dtd";
36 private static final String IBATIS_MAPPER_SYSTEM = "ibatis-3-mapper.dtd";
37 private static final String MYBATIS_CONFIG_SYSTEM = "mybatis-3-config.dtd";
38 private static final String MYBATIS_MAPPER_SYSTEM = "mybatis-3-mapper.dtd";
39
40 private static final String MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd";
41 private static final String MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd";
42
43
44
45
46
47
48
49
50
51
52
53
54
55 @Override
56 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
57 try {
58 if (systemId != null) {
59 String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH);
60 if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM) || lowerCaseSystemId.contains(IBATIS_CONFIG_SYSTEM)) {
61 return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId);
62 } else if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) || lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) {
63 return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId);
64 }
65 }
66 return null;
67 } catch (Exception e) {
68 throw new SAXException(e.toString());
69 }
70 }
71
72 private InputSource getInputSource(String path, String publicId, String systemId) {
73 InputSource source = null;
74 if (path != null) {
75 try {
76 InputStream in = Resources.getResourceAsStream(path);
77 source = new InputSource(in);
78 source.setPublicId(publicId);
79 source.setSystemId(systemId);
80 } catch (IOException e) {
81
82 }
83 }
84 return source;
85 }
86
87 }