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.

DefaultFOPResourceAccessor.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.net.URI;
  23. import java.net.URL;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.fop.apps.FOUserAgent;
  28. /**
  29. * Default implementation of the {@link ResourceAccessor} interface for use inside FOP.
  30. */
  31. public class DefaultFOPResourceAccessor extends SimpleResourceAccessor {
  32. private FOUserAgent userAgent;
  33. private String categoryBaseURI;
  34. /**
  35. * Constructor for resource to be accessed via the {@link FOUserAgent}. This contructor
  36. * can take two base URIs: the category base URI is the one to use when differentiating between
  37. * normal resources (ex. images) and font resources. So, if fonts need to be accessed, you can
  38. * set the {@link org.apache.fop.fonts.FontManager}'s base URI instead of the one on the
  39. * {@link org.apache.fop.apps.FopFactory}.
  40. * @param userAgent the FO user agent
  41. * @param categoryBaseURI the category base URI (may be null)
  42. * @param baseURI the custom base URI to resolve relative URIs against (may be null)
  43. */
  44. public DefaultFOPResourceAccessor(FOUserAgent userAgent, String categoryBaseURI, URI baseURI) {
  45. super(baseURI);
  46. this.userAgent = userAgent;
  47. this.categoryBaseURI = categoryBaseURI;
  48. }
  49. /** {@inheritDoc} */
  50. public InputStream createInputStream(URI uri) throws IOException {
  51. //Step 1: resolve against local base URI --> URI
  52. URI resolved = resolveAgainstBase(uri);
  53. //Step 2: resolve against the user agent --> stream
  54. String base = (this.categoryBaseURI != null
  55. ? this.categoryBaseURI
  56. : this.userAgent.getBaseURL());
  57. Source src = userAgent.resolveURI(resolved.toASCIIString(), base);
  58. if (src == null) {
  59. throw new FileNotFoundException("Resource not found: " + uri.toASCIIString());
  60. } else if (src instanceof StreamSource) {
  61. StreamSource ss = (StreamSource)src;
  62. InputStream in = ss.getInputStream();
  63. if (in != null) {
  64. return in;
  65. }
  66. if (ss.getReader() != null) {
  67. //Don't support reader, retry using system ID below
  68. IOUtils.closeQuietly(ss.getReader());
  69. }
  70. }
  71. URL url = new URL(src.getSystemId());
  72. return url.openStream();
  73. }
  74. }