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.4KB

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