<<Back to http://devdoc.net
Mine with nofee-ng to get DevFee back!
View Javadoc
1   /**
2    *    Copyright 2009-2015 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    *       http://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.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   * Offline entity resolver for the MyBatis DTDs
31   * 
32   * @author Clinton Begin
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     * Converts a public DTD into a local one
69     * 
70     * @param publicId The public id that is what comes after "PUBLIC"
71     * @param systemId The system id that is what comes after the public id.
72     * @return The InputSource for the DTD
73     * 
74     * @throws org.xml.sax.SAXException If anything goes wrong
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         // ignore, null is ok
108       }
109     }
110     return source;
111   }
112 
113 }