View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * Offline entity resolver for the MyBatis DTDs.
29   *
30   * @author Clinton Begin
31   * @author Eduardo Macarron
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     * Converts a public DTD into a local one.
45     *
46     * @param publicId
47     *          The public id that is what comes after "PUBLIC"
48     * @param systemId
49     *          The system id that is what comes after the public id.
50     *
51     * @return The InputSource for the DTD
52     *
53     * @throws org.xml.sax.SAXException
54     *           If anything goes wrong
55     */
56    @Override
57    public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
58      try {
59        if (systemId != null) {
60          String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH);
61          if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM) || lowerCaseSystemId.contains(IBATIS_CONFIG_SYSTEM)) {
62            return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId);
63          }
64          if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) || lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) {
65            return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId);
66          }
67        }
68        return null;
69      } catch (Exception e) {
70        throw new SAXException(e.toString());
71      }
72    }
73  
74    private InputSource getInputSource(String path, String publicId, String systemId) {
75      InputSource source = null;
76      if (path != null) {
77        try {
78          InputStream in = Resources.getResourceAsStream(path);
79          source = new InputSource(in);
80          source.setPublicId(publicId);
81          source.setSystemId(systemId);
82        } catch (IOException e) {
83          // ignore, null is ok
84        }
85      }
86      return source;
87    }
88  
89  }