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

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