17
17
18
18
import java .io .IOException ;
19
19
import java .io .InputStream ;
20
- import java .util .HashMap ;
21
20
import java .util .Locale ;
22
- import java .util .Map ;
23
21
24
22
import org .apache .ibatis .io .Resources ;
25
23
import org .xml .sax .EntityResolver ;
30
28
* Offline entity resolver for the MyBatis DTDs
31
29
*
32
30
* @author Clinton Begin
31
+ * @author Eduardo Macarron
33
32
*/
34
33
public class XMLMapperEntityResolver implements EntityResolver {
35
34
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 );
49
39
50
40
private static final String MYBATIS_CONFIG_DTD = "org/apache/ibatis/builder/xml/mybatis-3-config.dtd" ;
51
41
private static final String MYBATIS_MAPPER_DTD = "org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd" ;
52
42
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
43
/*
68
44
* Converts a public DTD into a local one
69
45
*
@@ -75,34 +51,29 @@ public class XMLMapperEntityResolver implements EntityResolver {
75
51
*/
76
52
@ Override
77
53
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
54
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
+ }
93
62
}
63
+ return null ;
94
64
} catch (Exception e ) {
95
65
throw new SAXException (e .toString ());
96
66
}
97
- return source ;
98
67
}
99
68
100
- private InputSource getInputSource (String path , InputSource source ) {
69
+ private InputSource getInputSource (String path , String publicId , String systemId ) {
70
+ InputSource source = null ;
101
71
if (path != null ) {
102
- InputStream in ;
103
72
try {
104
- in = Resources .getResourceAsStream (path );
73
+ InputStream in = Resources .getResourceAsStream (path );
105
74
source = new InputSource (in );
75
+ source .setPublicId (publicId );
76
+ source .setSystemId (systemId );
106
77
} catch (IOException e ) {
107
78
// ignore, null is ok
108
79
}
0 commit comments