| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DefaultEntityResolver |
|
| 3.3333333333333335;3,333 |
| 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.configuration.resolver; | |
| 18 | ||
| 19 | import java.io.IOException; | |
| 20 | import java.io.InputStream; | |
| 21 | import java.net.URL; | |
| 22 | import java.net.URLConnection; | |
| 23 | import java.util.HashMap; | |
| 24 | import java.util.Map; | |
| 25 | ||
| 26 | import org.xml.sax.EntityResolver; | |
| 27 | import org.xml.sax.InputSource; | |
| 28 | import org.xml.sax.SAXException; | |
| 29 | ||
| 30 | /** | |
| 31 | * The DefaultEntityResolver used by XML Configurations. | |
| 32 | * @author <a | |
| 33 | * href="http://commons.apache.org/configuration/team-list.html">Commons | |
| 34 | * Configuration team</a> | |
| 35 | * @since 1.7 | |
| 36 | * @version $Id: DefaultEntityResolver.java 1301991 2012-03-17 20:18:02Z sebb $ | |
| 37 | */ | |
| 38 | 752 | public class DefaultEntityResolver implements EntityResolver, EntityRegistry |
| 39 | { | |
| 40 | /** Stores a map with the registered public IDs.*/ | |
| 41 | 752 | private Map<String, URL> registeredEntities = new HashMap<String, URL>(); |
| 42 | ||
| 43 | /** | |
| 44 | * <p> | |
| 45 | * Registers the specified URL for the specified public identifier. | |
| 46 | * </p> | |
| 47 | * <p> | |
| 48 | * This implementation maps {@code PUBLICID}'s to URLs (from which | |
| 49 | * the resource will be loaded). A common use case for this method is to | |
| 50 | * register local URLs (possibly computed at runtime by a class loader) for | |
| 51 | * DTDs and Schemas. This allows the performance advantage of using a local | |
| 52 | * version without having to ensure every {@code SYSTEM} URI on every | |
| 53 | * processed XML document is local. This implementation provides only basic | |
| 54 | * functionality. If more sophisticated features are required, either calling | |
| 55 | * {@code XMLConfiguration.setDocumentBuilder(DocumentBuilder)} to set a custom | |
| 56 | * {@code DocumentBuilder} (which also can be initialized with a | |
| 57 | * custom {@code EntityResolver}) or creating a custom entity resolver | |
| 58 | * and registering it with the XMLConfiguration is recommended. | |
| 59 | * </p> | |
| 60 | * | |
| 61 | * @param publicId Public identifier of the Entity to be resolved | |
| 62 | * @param entityURL The URL to use for reading this Entity | |
| 63 | * @throws IllegalArgumentException if the public ID is undefined | |
| 64 | */ | |
| 65 | public void registerEntityId(String publicId, URL entityURL) | |
| 66 | { | |
| 67 | 4 | if (publicId == null) |
| 68 | { | |
| 69 | 1 | throw new IllegalArgumentException("Public ID must not be null!"); |
| 70 | } | |
| 71 | 3 | getRegisteredEntities().put(publicId, entityURL); |
| 72 | 3 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Resolves the requested external entity. This is the default | |
| 76 | * implementation of the {@code EntityResolver} interface. It checks | |
| 77 | * the passed in public ID against the registered entity IDs and uses a | |
| 78 | * local URL if possible. | |
| 79 | * | |
| 80 | * @param publicId the public identifier of the entity being referenced | |
| 81 | * @param systemId the system identifier of the entity being referenced | |
| 82 | * @return an input source for the specified entity | |
| 83 | * @throws org.xml.sax.SAXException if a parsing exception occurs | |
| 84 | */ | |
| 85 | public InputSource resolveEntity(String publicId, String systemId) | |
| 86 | throws SAXException | |
| 87 | { | |
| 88 | // Has this system identifier been registered? | |
| 89 | 5 | URL entityURL = null; |
| 90 | 5 | if (publicId != null) |
| 91 | { | |
| 92 | 4 | entityURL = getRegisteredEntities().get(publicId); |
| 93 | } | |
| 94 | ||
| 95 | 5 | if (entityURL != null) |
| 96 | { | |
| 97 | // Obtain an InputSource for this URL. This code is based on the | |
| 98 | // createInputSourceFromURL() method of Commons Digester. | |
| 99 | try | |
| 100 | { | |
| 101 | 3 | URLConnection connection = entityURL.openConnection(); |
| 102 | 3 | connection.setUseCaches(false); |
| 103 | 3 | InputStream stream = connection.getInputStream(); |
| 104 | 3 | InputSource source = new InputSource(stream); |
| 105 | 3 | source.setSystemId(entityURL.toExternalForm()); |
| 106 | 3 | return source; |
| 107 | } | |
| 108 | 0 | catch (IOException e) |
| 109 | { | |
| 110 | 0 | throw new SAXException(e); |
| 111 | } | |
| 112 | } | |
| 113 | else | |
| 114 | { | |
| 115 | // default processing behavior | |
| 116 | 2 | return null; |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Returns a map with the entity IDs that have been registered using the | |
| 122 | * {@code registerEntityId()} method. | |
| 123 | * | |
| 124 | * @return a map with the registered entity IDs | |
| 125 | */ | |
| 126 | public Map<String, URL> getRegisteredEntities() | |
| 127 | { | |
| 128 | 223 | return registeredEntities; |
| 129 | } | |
| 130 | } |