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.HashMap;
21 import java.util.Locale;
22 import java.util.Map;
23
24 import org.apache.ibatis.io.Resources;
25 import org.xml.sax.EntityResolver;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
28
29
30
31
32
33
34 public class XMLMapperEntityResolver implements EntityResolver {
35
36 private static final Map<String, String> doctypeMap = new HashMap<String, String>();
37
38 private static final String IBATIS_CONFIG_PUBLIC = "-//ibatis.apache.org//DTD Config 3.0//EN".toUpperCase(Locale.ENGLISH);
39 private static final String IBATIS_CONFIG_SYSTEM = "http://ibatis.apache.org/dtd/ibatis-3-config.dtd".toUpperCase(Locale.ENGLISH);
40
41 private static final String IBATIS_MAPPER_PUBLIC = "-//ibatis.apache.org//DTD Mapper 3.0//EN".toUpperCase(Locale.ENGLISH);
42 private static final String IBATIS_MAPPER_SYSTEM = "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd".toUpperCase(Locale.ENGLISH);
43
44 private static final String MYBATIS_CONFIG_PUBLIC = "-//mybatis.org//DTD Config 3.0//EN".toUpperCase(Locale.ENGLISH);
45 private static final String MYBATIS_CONFIG_SYSTEM = "http://mybatis.org/dtd/mybatis-3-config.dtd".toUpperCase(Locale.ENGLISH);
46
47 private static final String MYBATIS_MAPPER_PUBLIC = "-//mybatis.org//DTD Mapper 3.0//EN".toUpperCase(Locale.ENGLISH);
48 private static final String MYBATIS_MAPPER_SYSTEM = "http://mybatis.org/dtd/mybatis-3-mapper.dtd".toUpperCase(Locale.ENGLISH);
49
50 private static final String MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd";
51 private static final String MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd";
52
53 static {
54 doctypeMap.put(IBATIS_CONFIG_SYSTEM, MYBATIS_CONFIG_DTD);
55 doctypeMap.put(IBATIS_CONFIG_PUBLIC, MYBATIS_CONFIG_DTD);
56
57 doctypeMap.put(IBATIS_MAPPER_SYSTEM, MYBATIS_MAPPER_DTD);
58 doctypeMap.put(IBATIS_MAPPER_PUBLIC, MYBATIS_MAPPER_DTD);
59
60 doctypeMap.put(MYBATIS_CONFIG_SYSTEM, MYBATIS_CONFIG_DTD);
61 doctypeMap.put(MYBATIS_CONFIG_PUBLIC, MYBATIS_CONFIG_DTD);
62
63 doctypeMap.put(MYBATIS_MAPPER_SYSTEM, MYBATIS_MAPPER_DTD);
64 doctypeMap.put(MYBATIS_MAPPER_PUBLIC, MYBATIS_MAPPER_DTD);
65 }
66
67
68
69
70
71
72
73
74
75
76 @Override
77 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
78
79 if (publicId != null) {
80 publicId = publicId.toUpperCase(Locale.ENGLISH);
81 }
82 if (systemId != null) {
83 systemId = systemId.toUpperCase(Locale.ENGLISH);
84 }
85
86 InputSource source = null;
87 try {
88 String path = doctypeMap.get(publicId);
89 source = getInputSource(path, source);
90 if (source == null) {
91 path = doctypeMap.get(systemId);
92 source = getInputSource(path, source);
93 }
94 } catch (Exception e) {
95 throw new SAXException(e.toString());
96 }
97 return source;
98 }
99
100 private InputSource getInputSource(String path, InputSource source) {
101 if (path != null) {
102 InputStream in;
103 try {
104 in = Resources.getResourceAsStream(path);
105 source = new InputSource(in);
106 } catch (IOException e) {
107
108 }
109 }
110 return source;
111 }
112
113 }