XMLMapperEntityResolver.java

  1. /*
  2.  *    Copyright 2009-2021 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. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.Locale;

  20. import org.apache.ibatis.io.Resources;
  21. import org.xml.sax.EntityResolver;
  22. import org.xml.sax.InputSource;
  23. import org.xml.sax.SAXException;

  24. /**
  25.  * Offline entity resolver for the MyBatis DTDs.
  26.  *
  27.  * @author Clinton Begin
  28.  * @author Eduardo Macarron
  29.  */
  30. public class XMLMapperEntityResolver implements EntityResolver {

  31.   private static final String IBATIS_CONFIG_SYSTEM = "ibatis-3-config.dtd";
  32.   private static final String IBATIS_MAPPER_SYSTEM = "ibatis-3-mapper.dtd";
  33.   private static final String MYBATIS_CONFIG_SYSTEM = "mybatis-3-config.dtd";
  34.   private static final String MYBATIS_MAPPER_SYSTEM = "mybatis-3-mapper.dtd";

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

  37.   /**
  38.    * Converts a public DTD into a local one.
  39.    *
  40.    * @param publicId
  41.    *          The public id that is what comes after "PUBLIC"
  42.    * @param systemId
  43.    *          The system id that is what comes after the public id.
  44.    * @return The InputSource for the DTD
  45.    *
  46.    * @throws org.xml.sax.SAXException
  47.    *           If anything goes wrong
  48.    */
  49.   @Override
  50.   public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
  51.     try {
  52.       if (systemId != null) {
  53.         String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH);
  54.         if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM) || lowerCaseSystemId.contains(IBATIS_CONFIG_SYSTEM)) {
  55.           return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId);
  56.         } else if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) || lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) {
  57.           return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId);
  58.         }
  59.       }
  60.       return null;
  61.     } catch (Exception e) {
  62.       throw new SAXException(e.toString());
  63.     }
  64.   }

  65.   private InputSource getInputSource(String path, String publicId, String systemId) {
  66.     InputSource source = null;
  67.     if (path != null) {
  68.       try {
  69.         InputStream in = Resources.getResourceAsStream(path);
  70.         source = new InputSource(in);
  71.         source.setPublicId(publicId);
  72.         source.setSystemId(systemId);
  73.       } catch (IOException e) {
  74.         // ignore, null is ok
  75.       }
  76.     }
  77.     return source;
  78.   }

  79. }