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.

FopFactory.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.net.URI;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import org.xml.sax.SAXException;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. import org.apache.xmlgraphics.image.loader.ImageContext;
  31. import org.apache.xmlgraphics.image.loader.ImageManager;
  32. import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver;
  33. import org.apache.xmlgraphics.util.UnitConv;
  34. import org.apache.fop.apps.io.InternalResourceResolver;
  35. import org.apache.fop.apps.io.ResourceResolverFactory;
  36. import org.apache.fop.configuration.Configuration;
  37. import org.apache.fop.fo.ElementMapping;
  38. import org.apache.fop.fo.ElementMappingRegistry;
  39. import org.apache.fop.fonts.FontManager;
  40. import org.apache.fop.layoutmgr.LayoutManagerMaker;
  41. import org.apache.fop.render.ImageHandlerRegistry;
  42. import org.apache.fop.render.RendererConfig;
  43. import org.apache.fop.render.RendererConfig.RendererConfigParser;
  44. import org.apache.fop.render.RendererFactory;
  45. import org.apache.fop.render.XMLHandlerRegistry;
  46. import org.apache.fop.util.ColorSpaceCache;
  47. import org.apache.fop.util.ContentHandlerFactoryRegistry;
  48. /**
  49. * Factory class which instantiates new Fop and FOUserAgent instances. This
  50. * class also holds environmental information and configuration used by FOP.
  51. * Information that may potentially be different for each renderingq run can be
  52. * found and managed in the FOUserAgent.
  53. */
  54. public final class FopFactory implements ImageContext {
  55. /** logger instance */
  56. private static Log log = LogFactory.getLog(FopFactory.class);
  57. /** Factory for Renderers and FOEventHandlers */
  58. private final RendererFactory rendererFactory;
  59. /** Registry for XML handlers */
  60. private final XMLHandlerRegistry xmlHandlers;
  61. /** Registry for image handlers */
  62. private final ImageHandlerRegistry imageHandlers;
  63. /** The registry for ElementMapping instances */
  64. private final ElementMappingRegistry elementMappingRegistry;
  65. /** The registry for ContentHandlerFactory instance */
  66. private final ContentHandlerFactoryRegistry contentHandlerFactoryRegistry
  67. = new ContentHandlerFactoryRegistry();
  68. private final ColorSpaceCache colorSpaceCache;
  69. private final FopFactoryConfig config;
  70. private final InternalResourceResolver resolver;
  71. private final Map<String, RendererConfig> rendererConfig;
  72. private FopFactory(FopFactoryConfig config) {
  73. this.config = config;
  74. this.resolver = ResourceResolverFactory.createInternalResourceResolver(config.getBaseURI(),
  75. config.getResourceResolver());
  76. this.elementMappingRegistry = new ElementMappingRegistry(this);
  77. this.colorSpaceCache = new ColorSpaceCache(resolver);
  78. this.rendererFactory = new RendererFactory(config.preferRenderer());
  79. this.xmlHandlers = new XMLHandlerRegistry();
  80. this.imageHandlers = new ImageHandlerRegistry();
  81. rendererConfig = new HashMap<String, RendererConfig>();
  82. }
  83. /**
  84. * Map of configured names of hyphenation pattern file names: ll_CC => name
  85. */
  86. private Map<String, String> hyphPatNames;
  87. /**
  88. * FOP has the ability, for some FO's, to continue processing even if the
  89. * input XSL violates that FO's content model. This is the default
  90. * behavior for FOP. However, this flag, if set, provides the user the
  91. * ability for FOP to halt on all content model violations if desired.
  92. * Returns a new FopFactory instance that is configured using the {@link FopFactoryConfig} object.
  93. *
  94. * @param config the fop configuration
  95. * @return the requested FopFactory instance.
  96. */
  97. public static FopFactory newInstance(FopFactoryConfig config) {
  98. return new FopFactory(config);
  99. }
  100. /**
  101. * Returns a new FopFactory instance that is configured using the {@link FopFactoryConfig} object that
  102. * is created when the fopConf is parsed.
  103. *
  104. * @param fopConf the fop conf configuration file to parse
  105. * @return the requested FopFactory instance.
  106. * @throws IOException
  107. * @throws SAXException
  108. */
  109. public static FopFactory newInstance(File fopConf) throws SAXException, IOException {
  110. return new FopConfParser(fopConf).getFopFactoryBuilder().build();
  111. }
  112. /**
  113. * Returns a new FopFactory instance that is configured only by the default configuration
  114. * parameters.
  115. *
  116. * @param baseURI the base URI to resolve resource URIs against
  117. * @return the requested FopFactory instance.
  118. */
  119. public static FopFactory newInstance(URI baseURI) {
  120. return new FopFactoryBuilder(baseURI).build();
  121. }
  122. /**
  123. * Returns a new FopFactory instance that is configured using the {@link FopFactoryConfig} object that
  124. * is created when the fopConf is parsed.
  125. *
  126. * @param baseURI the base URI to resolve resource URIs against
  127. * @param confStream the fop conf configuration stream to parse
  128. * @return the requested FopFactory instance.
  129. * @throws SAXException
  130. * @throws IOException
  131. */
  132. public static FopFactory newInstance(URI baseURI, InputStream confStream) throws SAXException,
  133. IOException {
  134. return new FopConfParser(confStream, baseURI).getFopFactoryBuilder().build();
  135. }
  136. /**
  137. * Returns a new FOUserAgent instance. Use the FOUserAgent to configure special values that
  138. * are particular to a rendering run. Don't reuse instances over multiple rendering runs but
  139. * instead create a new one each time and reuse the FopFactory.
  140. * @return the newly created FOUserAgent instance initialized with default values
  141. */
  142. public FOUserAgent newFOUserAgent() {
  143. FOUserAgent userAgent = new FOUserAgent(this, resolver);
  144. return userAgent;
  145. }
  146. boolean isComplexScriptFeaturesEnabled() {
  147. return config.isComplexScriptFeaturesEnabled();
  148. }
  149. /**
  150. * Returns a new {@link Fop} instance. FOP will be configured with a default user agent
  151. * instance.
  152. * <p>
  153. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  154. * use the constants defined in {@link MimeConstants}.
  155. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  156. * @return the new Fop instance
  157. * @throws FOPException when the constructor fails
  158. */
  159. public Fop newFop(String outputFormat) throws FOPException {
  160. return newFOUserAgent().newFop(outputFormat);
  161. }
  162. /**
  163. * Returns a new {@link Fop} instance. Use this factory method if you want to configure this
  164. * very rendering run, i.e. if you want to set some metadata like the title and author of the
  165. * document you want to render. In that case, create a new {@link FOUserAgent}
  166. * instance using {@link #newFOUserAgent()}.
  167. * <p>
  168. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  169. * use the constants defined in {@link MimeConstants}.
  170. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  171. * @param userAgent the user agent that will be used to control the rendering run
  172. * @return the new Fop instance
  173. * @throws FOPException when the constructor fails
  174. */
  175. public Fop newFop(String outputFormat, FOUserAgent userAgent) throws FOPException {
  176. return userAgent.newFop(outputFormat, null);
  177. }
  178. /**
  179. * Returns a new {@link Fop} instance. FOP will be configured with a default user agent
  180. * instance. Use this factory method if your output type requires an output stream.
  181. * <p>
  182. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  183. * use the constants defined in {@link MimeConstants}.
  184. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  185. * @param stream the output stream
  186. * @return the new Fop instance
  187. * @throws FOPException when the constructor fails
  188. */
  189. public Fop newFop(String outputFormat, OutputStream stream) throws FOPException {
  190. return newFOUserAgent().newFop(outputFormat, stream);
  191. }
  192. /**
  193. * Returns a new {@link Fop} instance. Use this factory method if your output type
  194. * requires an output stream and you want to configure this very rendering run,
  195. * i.e. if you want to set some metadata like the title and author of the document
  196. * you want to render. In that case, create a new {@link FOUserAgent} instance
  197. * using {@link #newFOUserAgent()}.
  198. * <p>
  199. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  200. * use the constants defined in {@link MimeConstants}.
  201. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  202. * @param userAgent the user agent that will be used to control the rendering run
  203. * @param stream the output stream
  204. * @return the new Fop instance
  205. * @throws FOPException when the constructor fails
  206. */
  207. public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream)
  208. throws FOPException {
  209. return userAgent.newFop(outputFormat, stream);
  210. }
  211. /**
  212. * Returns a new {@link Fop} instance. Use this factory method if you want to supply your
  213. * own {@link org.apache.fop.render.Renderer Renderer} or
  214. * {@link org.apache.fop.fo.FOEventHandler FOEventHandler}
  215. * instance instead of the default ones created internally by FOP.
  216. * @param userAgent the user agent that will be used to control the rendering run
  217. * @return the new Fop instance
  218. * @throws FOPException when the constructor fails
  219. */
  220. public Fop newFop(FOUserAgent userAgent) throws FOPException {
  221. if (userAgent.getRendererOverride() == null
  222. && userAgent.getFOEventHandlerOverride() == null
  223. && userAgent.getDocumentHandlerOverride() == null) {
  224. throw new IllegalStateException("An overriding renderer,"
  225. + " FOEventHandler or IFDocumentHandler must be set on the user agent"
  226. + " when this factory method is used!");
  227. }
  228. return newFop(null, userAgent);
  229. }
  230. /** @return the RendererFactory */
  231. public RendererFactory getRendererFactory() {
  232. return this.rendererFactory;
  233. }
  234. /** @return the XML handler registry */
  235. public XMLHandlerRegistry getXMLHandlerRegistry() {
  236. return this.xmlHandlers;
  237. }
  238. /** @return the image handler registry */
  239. public ImageHandlerRegistry getImageHandlerRegistry() {
  240. return this.imageHandlers;
  241. }
  242. /** @return the element mapping registry */
  243. public ElementMappingRegistry getElementMappingRegistry() {
  244. return this.elementMappingRegistry;
  245. }
  246. /** @return the content handler factory registry */
  247. public ContentHandlerFactoryRegistry getContentHandlerFactoryRegistry() {
  248. return this.contentHandlerFactoryRegistry;
  249. }
  250. /**
  251. * Returns the renderer configuration object for a specific renderer given the parser and
  252. * configuration to read. The renderer config is cached such that the {@link Configuration} is
  253. * only parsed once per renderer, per FopFactory instance.
  254. *
  255. * @param userAgent the user agent
  256. * @param cfg the configuration to be parsed
  257. * @param configCreator the parser that creates the config object
  258. * @return the config object
  259. * @throws FOPException when an error occurs while creating the configuration object
  260. */
  261. synchronized RendererConfig getRendererConfig(FOUserAgent userAgent, Configuration cfg,
  262. RendererConfigParser configCreator) throws FOPException {
  263. RendererConfig config = rendererConfig.get(configCreator.getMimeType());
  264. if (config == null) {
  265. try {
  266. config = configCreator.build(userAgent, cfg);
  267. rendererConfig.put(configCreator.getMimeType(), config);
  268. } catch (Exception e) {
  269. throw new FOPException(e);
  270. }
  271. }
  272. return config;
  273. }
  274. /**
  275. * Add the element mapping with the given class name.
  276. * @param elementMapping the class name representing the element mapping.
  277. */
  278. public void addElementMapping(ElementMapping elementMapping) {
  279. this.elementMappingRegistry.addElementMapping(elementMapping);
  280. }
  281. /**
  282. * Returns whether accessibility is enabled.
  283. * @return true if accessibility is enabled
  284. */
  285. boolean isAccessibilityEnabled() {
  286. return config.isAccessibilityEnabled();
  287. }
  288. /** @see FopFactoryConfig#getImageManager() */
  289. public ImageManager getImageManager() {
  290. return config.getImageManager();
  291. }
  292. /** @see FopFactoryConfig#getLayoutManagerMakerOverride() */
  293. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  294. return config.getLayoutManagerMakerOverride();
  295. }
  296. /** @see FopFactoryConfig#getHyphenationPatternNames() */
  297. public Map<String, String> getHyphenationPatternNames() {
  298. return config.getHyphenationPatternNames();
  299. }
  300. /** @see FopFactoryConfig#validateStrictly() */
  301. public boolean validateStrictly() {
  302. return config.validateStrictly();
  303. }
  304. /** @see FopFactoryConfig#isBreakIndentInheritanceOnReferenceAreaBoundary() */
  305. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  306. return config.isBreakIndentInheritanceOnReferenceAreaBoundary();
  307. }
  308. /** @see FopFactoryConfig#getSourceResolution() */
  309. public float getSourceResolution() {
  310. return config.getSourceResolution();
  311. }
  312. /** @see FopFactoryConfig#getTargetResolution() */
  313. public float getTargetResolution() {
  314. return config.getTargetResolution();
  315. }
  316. public InternalResourceResolver getHyphenationResourceResolver() {
  317. return config.getHyphenationResourceResolver();
  318. }
  319. /**
  320. * Returns the conversion factor from pixel units to millimeters. This
  321. * depends on the desired source resolution.
  322. * @return float conversion factor
  323. * @see #getSourceResolution()
  324. */
  325. public float getSourcePixelUnitToMillimeter() {
  326. return UnitConv.IN2MM / getSourceResolution();
  327. }
  328. /**
  329. * Returns the conversion factor from pixel units to millimeters. This
  330. * depends on the desired target resolution.
  331. * @return float conversion factor
  332. * @see #getTargetResolution()
  333. */
  334. public float getTargetPixelUnitToMillimeter() {
  335. return 25.4f / getTargetResolution();
  336. }
  337. /** @see FopFactoryConfig#getPageHeight() */
  338. public String getPageHeight() {
  339. return config.getPageHeight();
  340. }
  341. /** @see FopFactoryConfig#getPageWidth() */
  342. public String getPageWidth() {
  343. return config.getPageWidth();
  344. }
  345. /** @see FopFactoryConfig#isNamespaceIgnored(String) */
  346. public boolean isNamespaceIgnored(String namespaceURI) {
  347. return config.isNamespaceIgnored(namespaceURI);
  348. }
  349. /** @see FopFactoryConfig#getIgnoredNamespaces() */
  350. public Set<String> getIgnoredNamespace() {
  351. return config.getIgnoredNamespaces();
  352. }
  353. /**
  354. * Get the user configuration.
  355. * @return the user configuration
  356. */
  357. public Configuration getUserConfig() {
  358. return config.getUserConfig();
  359. }
  360. /** @see FopFactoryConfig#validateUserConfigStrictly() */
  361. public boolean validateUserConfigStrictly() {
  362. return config.validateUserConfigStrictly();
  363. }
  364. /** @see FopFactoryConfig#getFontManager() */
  365. public FontManager getFontManager() {
  366. return config.getFontManager();
  367. }
  368. /** @see FopFactoryConfig#getFallbackResolver() */
  369. FallbackResolver getFallbackResolver() {
  370. return config.getFallbackResolver();
  371. }
  372. /**
  373. * Returns the color space cache for this instance.
  374. * <p>
  375. * Note: this method should not be considered as part of FOP's external API.
  376. * @return the color space cache
  377. */
  378. public ColorSpaceCache getColorSpaceCache() {
  379. return this.colorSpaceCache;
  380. }
  381. }