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.2KB

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