Skip to content

Commit f6eda2a

Browse files
committed
Fix wrong field in DTD and more error tolerant entity resolver
1 parent 1e5ac2b commit f6eda2a

File tree

2 files changed

+18
-48
lines changed

2 files changed

+18
-48
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLMapperEntityResolver.java

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
import java.io.IOException;
1919
import java.io.InputStream;
20-
import java.util.HashMap;
2120
import java.util.Locale;
22-
import java.util.Map;
2321

2422
import org.apache.ibatis.io.Resources;
2523
import org.xml.sax.EntityResolver;
@@ -30,40 +28,18 @@
3028
* Offline entity resolver for the MyBatis DTDs
3129
*
3230
* @author Clinton Begin
31+
* @author Eduardo Macarron
3332
*/
3433
public class XMLMapperEntityResolver implements EntityResolver {
3534

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);
35+
private static final String IBATIS_CONFIG_SYSTEM = "ibatis-3-config.dtd".toUpperCase(Locale.ENGLISH);
36+
private static final String IBATIS_MAPPER_SYSTEM = "ibatis-3-mapper.dtd".toUpperCase(Locale.ENGLISH);
37+
private static final String MYBATIS_CONFIG_SYSTEM = "mybatis-3-config.dtd".toUpperCase(Locale.ENGLISH);
38+
private static final String MYBATIS_MAPPER_SYSTEM = "mybatis-3-mapper.dtd".toUpperCase(Locale.ENGLISH);
4939

5040
private static final String MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd";
5141
private static final String MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd";
5242

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-
6743
/*
6844
* Converts a public DTD into a local one
6945
*
@@ -75,34 +51,29 @@ public class XMLMapperEntityResolver implements EntityResolver {
7551
*/
7652
@Override
7753
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;
8754
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);
55+
if (systemId != null) {
56+
systemId = systemId.toUpperCase(Locale.ENGLISH);
57+
if (systemId.contains(MYBATIS_CONFIG_SYSTEM) || systemId.contains(IBATIS_CONFIG_SYSTEM)) {
58+
return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId);
59+
} else if (systemId.contains(MYBATIS_MAPPER_SYSTEM) || systemId.contains(IBATIS_MAPPER_SYSTEM)) {
60+
return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId);
61+
}
9362
}
63+
return null;
9464
} catch (Exception e) {
9565
throw new SAXException(e.toString());
9666
}
97-
return source;
9867
}
9968

100-
private InputSource getInputSource(String path, InputSource source) {
69+
private InputSource getInputSource(String path, String publicId, String systemId) {
70+
InputSource source = null;
10171
if (path != null) {
102-
InputStream in;
10372
try {
104-
in = Resources.getResourceAsStream(path);
73+
InputStream in = Resources.getResourceAsStream(path);
10574
source = new InputSource(in);
75+
source.setPublicId(publicId);
76+
source.setSystemId(systemId);
10677
} catch (IOException e) {
10778
// ignore, null is ok
10879
}

src/main/java/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
-->
1919
<!ELEMENT mapper (cache-ref | cache | resultMap* | parameterMap* | sql* | insert* | update* | delete* | select* )+>
2020
<!ATTLIST mapper
21-
xmlns:fo CDATA #IMPLIED
2221
namespace CDATA #IMPLIED
2322
>
2423

0 commit comments

Comments
 (0)