XMLMapperEntityResolver.java

  1. /*
  2.  *    Copyright 2009-2024 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. 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.    *
  45.    * @return The InputSource for the DTD
  46.    *
  47.    * @throws org.xml.sax.SAXException
  48.    *           If anything goes wrong
  49.    */
  50.   @Override
  51.   public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
  52.     try {
  53.       if (systemId != null) {
  54.         String lowerCaseSystemId = systemId.toLowerCase(Locale.ENGLISH);
  55.         if (lowerCaseSystemId.contains(MYBATIS_CONFIG_SYSTEM) || lowerCaseSystemId.contains(IBATIS_CONFIG_SYSTEM)) {
  56.           return getInputSource(MYBATIS_CONFIG_DTD, publicId, systemId);
  57.         }
  58.         if (lowerCaseSystemId.contains(MYBATIS_MAPPER_SYSTEM) || lowerCaseSystemId.contains(IBATIS_MAPPER_SYSTEM)) {
  59.           return getInputSource(MYBATIS_MAPPER_DTD, publicId, systemId);
  60.         }
  61.       }
  62.       return null;
  63.     } catch (Exception e) {
  64.       throw new SAXException(e.toString());
  65.     }
  66.   }

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

  81. }