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.

AFPResourceAccessor.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.afp.util;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URI;
  22. import java.net.URISyntaxException;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.apache.fop.apps.io.InternalResourceResolver;
  26. /**
  27. * Defines an interface through which external resource objects can be accessed.
  28. */
  29. public final class AFPResourceAccessor {
  30. private static final Log log = LogFactory.getLog(AFPResourceAccessor.class);
  31. private final InternalResourceResolver resourceResolver;
  32. private final URI baseURI;
  33. private final URIResolver uriResolver;
  34. /**
  35. * Constructor for resource to be accessed via the {@link org.apache.fop.apps.FOUserAgent}. This
  36. * contructor takes a base URI for resolving font resource URIs. So, if fonts need to be
  37. * accessed, you can set the {@link org.apache.fop.fonts.FontManager}'s base URI instead of the
  38. * one on the {@link org.apache.fop.apps.FopFactory}.
  39. *
  40. * @param resourceResolver the resolver of resources
  41. * @param baseURI the custom base URI to resolve relative URIs against (may be null)
  42. */
  43. public AFPResourceAccessor(InternalResourceResolver resourceResolver, String baseURI) {
  44. this.resourceResolver = resourceResolver;
  45. URI actualBaseURI = null;
  46. URIResolver uriResolver;
  47. if (baseURI == null) {
  48. actualBaseURI = null;
  49. uriResolver = new NullBaseURIResolver();
  50. } else {
  51. try {
  52. actualBaseURI = InternalResourceResolver.getBaseURI(baseURI);
  53. uriResolver = new BaseURIResolver();
  54. } catch (URISyntaxException use) {
  55. log.error("The URI given \"" + baseURI + "\" is invalid: " + use.getMessage());
  56. actualBaseURI = null;
  57. uriResolver = new NullBaseURIResolver();
  58. }
  59. }
  60. this.baseURI = actualBaseURI;
  61. this.uriResolver = uriResolver;
  62. }
  63. /**
  64. * Constructor for resource to be accessed via the {@link org.apache.fop.apps.FOUserAgent}.
  65. *
  66. * @param resourceResolver the resolver of resources
  67. */
  68. public AFPResourceAccessor(InternalResourceResolver resourceResolver) {
  69. this(resourceResolver, null);
  70. }
  71. /**
  72. * Creates an {@link InputStream} given a URI.
  73. *
  74. * @param uri the URI of the InputStream
  75. * @return an InputStream
  76. * @throws IOException if an I/O error occurs while creating the InputStream.
  77. */
  78. public InputStream createInputStream(URI uri) throws IOException {
  79. return resourceResolver.getResource(uriResolver.resolveURI(uri));
  80. }
  81. /**
  82. * Returns the resolved URI, given the URI of a resource.
  83. *
  84. * @param uri the resource URI
  85. * @return the resolved URI
  86. */
  87. public URI resolveURI(String uri) {
  88. return uriResolver.resolveURI(uri);
  89. }
  90. private interface URIResolver {
  91. URI resolveURI(URI uri);
  92. URI resolveURI(String uri);
  93. }
  94. private static final class NullBaseURIResolver implements URIResolver {
  95. public URI resolveURI(URI uri) {
  96. return uri;
  97. }
  98. public URI resolveURI(String uri) {
  99. return URI.create("./" + uri.trim());
  100. }
  101. }
  102. private final class BaseURIResolver implements URIResolver {
  103. public URI resolveURI(URI uri) {
  104. return baseURI.resolve(uri);
  105. }
  106. public URI resolveURI(String uri) {
  107. return baseURI.resolve(uri.trim());
  108. }
  109. }
  110. }