1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.ibatis.sqlmap.engine.builder.xml;
17
18 import com.ibatis.common.resources.Resources;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.HashMap;
23 import java.util.Locale;
24 import java.util.Map;
25
26 import org.xml.sax.EntityResolver;
27 import org.xml.sax.InputSource;
28 import org.xml.sax.SAXException;
29
30
31
32
33 public class SqlMapClasspathEntityResolver implements EntityResolver {
34
35
36 private static final String SQL_MAP_CONFIG_DTD = "com/ibatis/sqlmap/engine/builder/xml/sql-map-config-2.dtd";
37
38
39 private static final String SQL_MAP_DTD = "com/ibatis/sqlmap/engine/builder/xml/sql-map-2.dtd";
40
41
42 private static final Map doctypeMap = new HashMap<>();
43
44 static {
45 doctypeMap.put("https://ibatis.apache.org/dtd/sql-map-config-2.dtd".toUpperCase(Locale.ROOT), SQL_MAP_CONFIG_DTD);
46 doctypeMap.put("-//ibatis.apache.org//DTD SQL Map Config 2.0//EN".toUpperCase(Locale.ROOT), SQL_MAP_CONFIG_DTD);
47
48 doctypeMap.put("https://ibatis.apache.org/dtd/sql-map-2.dtd".toUpperCase(Locale.ROOT), SQL_MAP_DTD);
49 doctypeMap.put("-//ibatis.apache.org//DTD SQL Map 2.0//EN".toUpperCase(Locale.ROOT), SQL_MAP_DTD);
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 @Override
66 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
67
68 if (publicId != null) {
69 publicId = publicId.toUpperCase(Locale.ROOT);
70 }
71 if (systemId != null) {
72 systemId = systemId.toUpperCase(Locale.ROOT);
73 }
74
75 InputSource source = null;
76 try {
77 String path = (String) doctypeMap.get(publicId);
78 source = getInputSource(path, source);
79 if (source == null) {
80 path = (String) doctypeMap.get(systemId);
81 source = getInputSource(path, source);
82 }
83 } catch (Exception e) {
84 throw new SAXException(e.toString());
85 }
86 return source;
87 }
88
89
90
91
92
93
94
95
96
97
98
99 private InputSource getInputSource(String path, InputSource source) {
100 if (path != null) {
101 InputStream in = null;
102 try {
103 in = Resources.getResourceAsStream(path);
104 source = new InputSource(in);
105 } catch (IOException e) {
106
107 }
108 }
109 return source;
110 }
111
112 }