DefaultEntityResolver.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.resolver;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.net.URL;
  21. import java.net.URLConnection;
  22. import java.util.HashMap;
  23. import java.util.Map;

  24. import org.xml.sax.EntityResolver;
  25. import org.xml.sax.InputSource;
  26. import org.xml.sax.SAXException;

  27. /**
  28.  * The DefaultEntityResolver used by XML Configurations.
  29.  *
  30.  * @since 1.7
  31.  */
  32. public class DefaultEntityResolver implements EntityResolver, EntityRegistry {
  33.     /** Stores a map with the registered public IDs. */
  34.     private final Map<String, URL> registeredEntities = new HashMap<>();

  35.     /**
  36.      * Gets a map with the entity IDs that have been registered using the {@code registerEntityId()} method.
  37.      *
  38.      * @return a map with the registered entity IDs
  39.      */
  40.     @Override
  41.     public Map<String, URL> getRegisteredEntities() {
  42.         return registeredEntities;
  43.     }

  44.     /**
  45.      * <p>
  46.      * Registers the specified URL for the specified public identifier.
  47.      * </p>
  48.      * <p>
  49.      * This implementation maps {@code PUBLICID}'s to URLs (from which the resource will be loaded). A common use case for
  50.      * this method is to register local URLs (possibly computed at runtime by a class loader) for DTDs and Schemas. This
  51.      * allows the performance advantage of using a local version without having to ensure every {@code SYSTEM} URI on every
  52.      * processed XML document is local. This implementation provides only basic functionality. If more sophisticated
  53.      * features are required, either calling {@code XMLConfiguration.setDocumentBuilder(DocumentBuilder)} to set a custom
  54.      * {@code DocumentBuilder} (which also can be initialized with a custom {@code EntityResolver}) or creating a custom
  55.      * entity resolver and registering it with the XMLConfiguration is recommended.
  56.      * </p>
  57.      *
  58.      * @param publicId Public identifier of the Entity to be resolved
  59.      * @param entityURL The URL to use for reading this Entity
  60.      * @throws IllegalArgumentException if the public ID is undefined
  61.      */
  62.     @Override
  63.     public void registerEntityId(final String publicId, final URL entityURL) {
  64.         if (publicId == null) {
  65.             throw new IllegalArgumentException("Public ID must not be null!");
  66.         }
  67.         getRegisteredEntities().put(publicId, entityURL);
  68.     }

  69.     /**
  70.      * Resolves the requested external entity. This is the default implementation of the {@code EntityResolver} interface.
  71.      * It checks the passed in public ID against the registered entity IDs and uses a local URL if possible.
  72.      *
  73.      * @param publicId the public identifier of the entity being referenced
  74.      * @param systemId the system identifier of the entity being referenced
  75.      * @return an input source for the specified entity
  76.      * @throws org.xml.sax.SAXException if a parsing exception occurs
  77.      */
  78.     @SuppressWarnings("resource") // The stream is managed by the InputSource returned by this method.
  79.     @Override
  80.     public InputSource resolveEntity(final String publicId, final String systemId) throws SAXException {
  81.         // Has this system identifier been registered?
  82.         URL entityURL = null;
  83.         if (publicId != null) {
  84.             entityURL = getRegisteredEntities().get(publicId);
  85.         }

  86.         if (entityURL != null) {
  87.             // Obtain an InputSource for this URL. This code is based on the
  88.             // createInputSourceFromURL() method of Commons Digester.
  89.             try {
  90.                 final URLConnection connection = entityURL.openConnection();
  91.                 connection.setUseCaches(false);
  92.                 final InputStream stream = connection.getInputStream();
  93.                 final InputSource source = new InputSource(stream);
  94.                 source.setSystemId(entityURL.toExternalForm());
  95.                 return source;
  96.             } catch (final IOException e) {
  97.                 throw new SAXException(e);
  98.             }
  99.         }
  100.         // default processing behavior
  101.         return null;
  102.     }
  103. }