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.

EnvironmentalProfileFactory.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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;
  19. import java.net.URI;
  20. import org.apache.fop.apps.io.ResourceResolver;
  21. import org.apache.fop.apps.io.InternalResourceResolver;
  22. import org.apache.fop.fonts.FontCacheManager;
  23. import org.apache.fop.fonts.FontCacheManagerFactory;
  24. import org.apache.fop.fonts.FontDetector;
  25. import org.apache.fop.fonts.FontDetectorFactory;
  26. import org.apache.fop.fonts.FontManager;
  27. /**
  28. * Creates an {@link EnvironmentProfile} that sets the environment in which a FOP instance is run.
  29. */
  30. public final class EnvironmentalProfileFactory {
  31. private EnvironmentalProfileFactory() {
  32. };
  33. /**
  34. * Creates the default environment that FOP is invoked in. This default profile has no
  35. * operational restrictions for FOP.
  36. *
  37. * @param defaultBaseUri the default base URI for resolving resource URIs
  38. * @param resourceResolver the resource resolver
  39. * @return the environment profile
  40. */
  41. public static EnvironmentProfile createDefault(URI defaultBaseUri,
  42. ResourceResolver resourceResolver) {
  43. return new Profile(defaultBaseUri, resourceResolver,
  44. createFontManager(defaultBaseUri, resourceResolver,
  45. FontDetectorFactory.createDefault(),
  46. FontCacheManagerFactory.createDefault()));
  47. }
  48. /**
  49. * Creates an IO-restricted environment for FOP by disabling some of the environment-specific
  50. * functionality within FOP.
  51. *
  52. * @param defaultBaseUri the default base URI for resolving resource URIs
  53. * @param resourceResolver the resource resolver
  54. * @return the environment profile
  55. */
  56. public static EnvironmentProfile createRestrictedIO(URI defaultBaseUri,
  57. ResourceResolver resourceResolver) {
  58. return new Profile(defaultBaseUri, resourceResolver,
  59. createFontManager(defaultBaseUri, resourceResolver,
  60. FontDetectorFactory.createDisabled(),
  61. FontCacheManagerFactory.createDisabled()));
  62. }
  63. private static final class Profile implements EnvironmentProfile {
  64. private final ResourceResolver resourceResolver;
  65. private final FontManager fontManager;
  66. private final URI defaultBaseURI;
  67. private Profile(URI defaultBaseURI, ResourceResolver resourceResolver,
  68. FontManager fontManager) {
  69. if (defaultBaseURI == null) {
  70. throw new IllegalArgumentException("Default base URI must not be null");
  71. }
  72. if (resourceResolver == null) {
  73. throw new IllegalArgumentException("URI Resolver must not be null");
  74. }
  75. this.defaultBaseURI = defaultBaseURI;
  76. this.resourceResolver = resourceResolver;
  77. this.fontManager = fontManager;
  78. }
  79. public ResourceResolver getResourceResolver() {
  80. return resourceResolver;
  81. }
  82. public FontManager getFontManager() {
  83. return fontManager;
  84. }
  85. public URI getDefaultBaseURI() {
  86. return defaultBaseURI;
  87. }
  88. }
  89. private static FontManager createFontManager(URI defaultBaseUri, ResourceResolver resourceResolver,
  90. FontDetector fontDetector, FontCacheManager fontCacheManager) {
  91. return new FontManager(new InternalResourceResolver(defaultBaseUri, resourceResolver), fontDetector,
  92. fontCacheManager);
  93. }
  94. }