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.

URIResolverWrapper.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. public class URIResolverWrapper {
  29. private final URI baseUri;
  30. private final ResourceResolver uriResolver;
  31. private final DataURIResolver dataSchemeResolver = new DataURIResolver();
  32. public URIResolverWrapper(URI baseUri, ResourceResolver uriResolver) {
  33. this.baseUri = baseUri;
  34. this.uriResolver = uriResolver;
  35. }
  36. public URI getBaseURI() {
  37. return baseUri;
  38. }
  39. public InputStream resolveIn(String stringUri) throws IOException, URISyntaxException {
  40. if (stringUri.startsWith("data:")) {
  41. return resolveDataURI(stringUri);
  42. }
  43. return resolveIn(cleanURI(stringUri));
  44. }
  45. public InputStream resolveIn(URI uri) throws IOException {
  46. if (uri.getScheme() != null && uri.getScheme().startsWith("data")) {
  47. return resolveDataURI(uri.toASCIIString());
  48. }
  49. return uriResolver.getResource(resolveFromBase(uri));
  50. }
  51. public OutputStream resolveOut(URI uri) throws IOException {
  52. return uriResolver.getOutputStream(resolveFromBase(uri));
  53. }
  54. private URI resolveFromBase(URI uri) {
  55. return baseUri.resolve(uri);
  56. }
  57. public static URI cleanURI(String uriStr) throws URISyntaxException {
  58. // replace back slash with forward slash to ensure windows file:/// URLS are supported
  59. if (uriStr == null) {
  60. return null;
  61. }
  62. String fixedUri = uriStr.replace('\\', '/');
  63. fixedUri = fixedUri.replace(" ", "%20");
  64. URI baseURI = new URI(fixedUri);
  65. return baseURI;
  66. }
  67. public static URI getBaseURI(String base) throws URISyntaxException {
  68. String path = base + (base.endsWith("/") ? "" : "/");
  69. return cleanURI(path);
  70. }
  71. private InputStream resolveDataURI(String dataURI) {
  72. try {
  73. Source src = dataSchemeResolver.resolve(dataURI, "");
  74. return src == null ? null : ((StreamSource) src).getInputStream();
  75. } catch (TransformerException e) {
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. }