Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DefaultFOPResourceAccessor.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.fop.apps.FOUserAgent;
  24. import org.apache.fop.apps.FopFactory;
  25. import org.apache.fop.apps.io.URIResolverWrapper;
  26. /**
  27. * Default implementation of the {@link ResourceAccessor} interface for use inside FOP.
  28. */
  29. public class DefaultFOPResourceAccessor extends SimpleResourceAccessor {
  30. private final URIResolverWrapper resolver;
  31. private final String baseURI;
  32. /**
  33. * Constructor for resource to be accessed via the {@link FOUserAgent}. This contructor
  34. * can take two base URIs: the category base URI is the one to use when differentiating between
  35. * normal resources (ex. images) and font resources. So, if fonts need to be accessed, you can
  36. * set the {@link FontManager}'s base URI instead of the one on the {@link FopFactory}.
  37. * @param userAgent the FO user agent
  38. * @param categoryBaseURI the category base URI (may be null)
  39. * @param baseURI the custom base URI to resolve relative URIs against (may be null)
  40. */
  41. public DefaultFOPResourceAccessor(URIResolverWrapper resolver, String baseURI) {
  42. super(resolver.getBaseURI());
  43. this.resolver = resolver;
  44. this.baseURI = baseURI;
  45. }
  46. public DefaultFOPResourceAccessor(URIResolverWrapper resolver) {
  47. super(resolver.getBaseURI());
  48. this.resolver = resolver;
  49. this.baseURI = null;
  50. }
  51. private URI getResourceURI(URI uri) {
  52. if (baseURI == null) {
  53. return uri;
  54. }
  55. try {
  56. URI baseURI = URIResolverWrapper.getBaseURI(this.baseURI);
  57. return baseURI.resolve(uri);
  58. } catch (URISyntaxException use) {
  59. return uri;
  60. }
  61. }
  62. /** {@inheritDoc} */
  63. public InputStream createInputStream(URI uri) throws IOException {
  64. return resolver.resolveIn(getResourceURI(uri));
  65. }
  66. }