選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EnvironmentalProfileFactory.java 5.1KB

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