You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InternalResourceResolver.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /* $Id$ */
  18. package org.apache.fop.apps.io;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.net.URI;
  23. import java.net.URISyntaxException;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.stream.StreamSource;
  27. import org.apache.xmlgraphics.io.Resource;
  28. import org.apache.xmlgraphics.io.ResourceResolver;
  29. import org.apache.xmlgraphics.util.uri.DataURIResolver;
  30. /**
  31. * This object holds the base URI from which to resolve URIs against as well as the resolver for
  32. * resource acquisition. It also does some URI sanitization of common URI syntactical errors. This
  33. * class takes in a {@link org.apache.xmlgraphics.io.ResourceResolver} and delegates all relevant
  34. * URIs to it.
  35. */
  36. public class InternalResourceResolver {
  37. private final URI baseUri;
  38. private final ResourceResolver resourceResolver;
  39. private final DataURIResolver dataSchemeResolver = new DataURIResolver();
  40. /**
  41. * @param baseUri the base URI from which to resolve relative URIs
  42. * @param resourceResolver the resolver to delegate to
  43. */
  44. InternalResourceResolver(URI baseUri, ResourceResolver resourceResolver) {
  45. this.baseUri = baseUri;
  46. this.resourceResolver = resourceResolver;
  47. }
  48. /**
  49. * Returns the base URI from which to resolve all URIs against.
  50. *
  51. * @return the base URI
  52. */
  53. public URI getBaseURI() {
  54. return baseUri;
  55. }
  56. /**
  57. * Retrieve a resource given a URI in String form. This also does some syntactical sanitaion on
  58. * the URI.
  59. *
  60. * @param stringUri the URI in String form
  61. * @return the resource
  62. * @throws IOException if an I/O error occurred
  63. * @throws URISyntaxException if the URI syntax was invalid
  64. */
  65. public Resource getResource(String stringUri) throws IOException, URISyntaxException {
  66. if (stringUri.startsWith("data:")) {
  67. return new Resource(resolveDataURI(stringUri));
  68. }
  69. return getResource(cleanURI(stringUri));
  70. }
  71. /**
  72. * Retrieve a resource given a URI in String form.
  73. *
  74. * @param uri the resource URI
  75. * @return the resource
  76. * @throws IOException if an I/O error occurred
  77. */
  78. public Resource getResource(URI uri) throws IOException {
  79. if (uri.getScheme() != null && uri.getScheme().startsWith("data")) {
  80. return new Resource(resolveDataURI(uri.toASCIIString()));
  81. }
  82. return resourceResolver.getResource(resolveFromBase(uri));
  83. }
  84. /**
  85. * Returns the OutputStream for a given URI.
  86. *
  87. * @param uri the URI for the inteded stream
  88. * @return the output stream
  89. * @throws IOException if an I/O error occurrred
  90. */
  91. public OutputStream getOutputStream(URI uri) throws IOException {
  92. return resourceResolver.getOutputStream(resolveFromBase(uri));
  93. }
  94. /**
  95. * Resolves a URI against the base URI.
  96. *
  97. * @param uri the URI that requires resolution
  98. * @return the resolved URI
  99. */
  100. public URI resolveFromBase(URI uri) {
  101. return baseUri.resolve(uri);
  102. }
  103. /**
  104. * Performs some sanitation for some of the most common URI syntax mistakes.
  105. *
  106. * @param uriStr the URI in String form
  107. * @return a valid URI
  108. * @throws URISyntaxException if the given String was too erroneous to validate
  109. */
  110. public static URI cleanURI(String uriStr) throws URISyntaxException {
  111. // replace back slash with forward slash to ensure windows file:/// URLS are supported
  112. if (uriStr == null) {
  113. return null;
  114. }
  115. String fixedUri = uriStr.replace('\\', '/');
  116. fixedUri = fixedUri.replace(" ", "%20");
  117. URI baseURI = new URI(fixedUri);
  118. return baseURI;
  119. }
  120. /**
  121. * Performs some sanitation for some of the most common URI syntax mistakes but returns a
  122. * directory URI rather than a file URI.
  123. *
  124. * @param base the directory URI in String form
  125. * @return the directory URI
  126. * @throws URISyntaxException if the given String was too erroneous to validate
  127. */
  128. public static URI getBaseURI(String base) throws URISyntaxException {
  129. String path = base + (base.endsWith("/") ? "" : "/");
  130. return cleanURI(path);
  131. }
  132. private InputStream resolveDataURI(String dataURI) {
  133. try {
  134. Source src = dataSchemeResolver.resolve(dataURI, "");
  135. return src == null ? null : ((StreamSource) src).getInputStream();
  136. } catch (TransformerException e) {
  137. throw new RuntimeException(e);
  138. }
  139. }
  140. }