1. /*
  2. * Copyright 2002-2004 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.springframework.context.support;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.net.MalformedURLException;
  20. import java.net.URL;
  21. import java.net.URLDecoder;
  22. import org.xml.sax.InputSource;
  23. import org.springframework.beans.factory.xml.BeansDtdResolver;
  24. import org.springframework.context.ApplicationContext;
  25. import org.springframework.core.io.Resource;
  26. /**
  27. * EntityResolver implementation that tries to resolve entity references
  28. * relative to the resource base of the application context, if applicable.
  29. * Extends BeansDtdResolver to also provide DTD lookup in the class path.
  30. *
  31. * <p>Allows to use standard XML entities to include XML snippets into an
  32. * application context definition, for example to split a large XML file
  33. * into various modules. The include paths can be relative to the
  34. * application context's resource base as usual, instead of relative
  35. * to the JVM working directory (the XML parser's default).
  36. *
  37. * <p>Note: In addition to relative paths, every URL that specifies a
  38. * file in the current system root, i.e. the JVM working directory,
  39. * will be interpreted relative to the application context too.
  40. *
  41. * @author Juergen Hoeller
  42. * @since 31.07.2003
  43. * @see org.springframework.context.ApplicationContext#getResource
  44. */
  45. public class ResourceEntityResolver extends BeansDtdResolver {
  46. private final ApplicationContext applicationContext;
  47. public ResourceEntityResolver(ApplicationContext applicationContext) {
  48. this.applicationContext = applicationContext;
  49. }
  50. public InputSource resolveEntity(String publicId, String systemId) throws IOException {
  51. InputSource source = super.resolveEntity(publicId, systemId);
  52. if (source == null && systemId != null) {
  53. String resourcePath = null;
  54. try {
  55. String decodedSystemId = URLDecoder.decode(systemId);
  56. String givenUrl = new URL(decodedSystemId).toString();
  57. String systemRootUrl = new File("").toURL().toString();
  58. // try relative to resource base if currently in system root
  59. if (givenUrl.startsWith(systemRootUrl)) {
  60. resourcePath = givenUrl.substring(systemRootUrl.length());
  61. }
  62. }
  63. catch (MalformedURLException ex) {
  64. // no URL -> try relative to resource base
  65. resourcePath = systemId;
  66. }
  67. if (resourcePath != null) {
  68. if (logger.isDebugEnabled()) {
  69. logger.debug("Trying to locate entity [" + systemId + "] as application context resource [" +
  70. resourcePath + "]");
  71. }
  72. Resource resource = this.applicationContext.getResource(resourcePath);
  73. if (logger.isInfoEnabled()) {
  74. logger.info("Found entity [" + systemId + "] as application context resource [" + resourcePath + "]");
  75. }
  76. source = new InputSource(resource.getInputStream());
  77. source.setPublicId(publicId);
  78. source.setSystemId(systemId);
  79. }
  80. }
  81. return source;
  82. }
  83. }