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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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.avalon.framework.configuration.Configuration;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.apache.xmlgraphics.image.loader.ImageContext;
  32. import org.apache.xmlgraphics.image.loader.ImageManager;
  33. import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext.FallbackResolver;
  34. import org.apache.xmlgraphics.util.UnitConv;
  35. import org.apache.fop.apps.io.InternalResourceResolver;
  36. import org.apache.fop.apps.io.ResourceResolverFactory;
  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. * @throws FOPException
  142. */
  143. public FOUserAgent newFOUserAgent() {
  144. FOUserAgent userAgent = new FOUserAgent(this, resolver);
  145. return userAgent;
  146. }
  147. boolean isComplexScriptFeaturesEnabled() {
  148. return config.isComplexScriptFeaturesEnabled();
  149. }
  150. /**
  151. * Returns a new {@link Fop} instance. FOP will be configured with a default user agent
  152. * instance.
  153. * <p>
  154. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  155. * use the constants defined in {@link MimeConstants}.
  156. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  157. * @return the new Fop instance
  158. * @throws FOPException when the constructor fails
  159. */
  160. public Fop newFop(String outputFormat) throws FOPException {
  161. return newFOUserAgent().newFop(outputFormat);
  162. }
  163. /**
  164. * Returns a new {@link Fop} instance. Use this factory method if you want to configure this
  165. * very rendering run, i.e. if you want to set some metadata like the title and author of the
  166. * document you want to render. In that case, create a new {@link FOUserAgent}
  167. * instance using {@link #newFOUserAgent()}.
  168. * <p>
  169. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  170. * use the constants defined in {@link MimeConstants}.
  171. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  172. * @param userAgent the user agent that will be used to control the rendering run
  173. * @return the new Fop instance
  174. * @throws FOPException when the constructor fails
  175. */
  176. public Fop newFop(String outputFormat, FOUserAgent userAgent) throws FOPException {
  177. return userAgent.newFop(outputFormat, null);
  178. }
  179. /**
  180. * Returns a new {@link Fop} instance. FOP will be configured with a default user agent
  181. * instance. Use this factory method if your output type requires an output stream.
  182. * <p>
  183. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  184. * use the constants defined in {@link MimeConstants}.
  185. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  186. * @param stream the output stream
  187. * @return the new Fop instance
  188. * @throws FOPException when the constructor fails
  189. */
  190. public Fop newFop(String outputFormat, OutputStream stream) throws FOPException {
  191. return newFOUserAgent().newFop(outputFormat, stream);
  192. }
  193. /**
  194. * Returns a new {@link Fop} instance. Use this factory method if your output type
  195. * requires an output stream and you want to configure this very rendering run,
  196. * i.e. if you want to set some metadata like the title and author of the document
  197. * you want to render. In that case, create a new {@link FOUserAgent} instance
  198. * using {@link #newFOUserAgent()}.
  199. * <p>
  200. * MIME types are used to select the output format (ex. "application/pdf" for PDF). You can
  201. * use the constants defined in {@link MimeConstants}.
  202. * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
  203. * @param userAgent the user agent that will be used to control the rendering run
  204. * @param stream the output stream
  205. * @return the new Fop instance
  206. * @throws FOPException when the constructor fails
  207. */
  208. public Fop newFop(String outputFormat, FOUserAgent userAgent, OutputStream stream)
  209. throws FOPException {
  210. return userAgent.newFop(outputFormat, stream);
  211. }
  212. /**
  213. * Returns a new {@link Fop} instance. Use this factory method if you want to supply your
  214. * own {@link org.apache.fop.render.Renderer Renderer} or
  215. * {@link org.apache.fop.fo.FOEventHandler FOEventHandler}
  216. * instance instead of the default ones created internally by FOP.
  217. * @param userAgent the user agent that will be used to control the rendering run
  218. * @return the new Fop instance
  219. * @throws FOPException when the constructor fails
  220. */
  221. public Fop newFop(FOUserAgent userAgent) throws FOPException {
  222. if (userAgent.getRendererOverride() == null
  223. && userAgent.getFOEventHandlerOverride() == null
  224. && userAgent.getDocumentHandlerOverride() == null) {
  225. throw new IllegalStateException("An overriding renderer,"
  226. + " FOEventHandler or IFDocumentHandler must be set on the user agent"
  227. + " when this factory method is used!");
  228. }
  229. return newFop(null, userAgent);
  230. }
  231. /** @return the RendererFactory */
  232. public RendererFactory getRendererFactory() {
  233. return this.rendererFactory;
  234. }
  235. /** @return the XML handler registry */
  236. public XMLHandlerRegistry getXMLHandlerRegistry() {
  237. return this.xmlHandlers;
  238. }
  239. /** @return the image handler registry */
  240. public ImageHandlerRegistry getImageHandlerRegistry() {
  241. return this.imageHandlers;
  242. }
  243. /** @return the element mapping registry */
  244. public ElementMappingRegistry getElementMappingRegistry() {
  245. return this.elementMappingRegistry;
  246. }
  247. /** @return the content handler factory registry */
  248. public ContentHandlerFactoryRegistry getContentHandlerFactoryRegistry() {
  249. return this.contentHandlerFactoryRegistry;
  250. }
  251. /**
  252. * Returns the renderer configuration object for a specific renderer given the parser and
  253. * configuration to read. The renderer config is cached such that the {@link Configuration} is
  254. * only parsed once per renderer, per FopFactory instance.
  255. *
  256. * @param userAgent the user agent
  257. * @param cfg the configuration to be parsed
  258. * @param configCreator the parser that creates the config object
  259. * @return the config object
  260. * @throws FOPException when an error occurs while creating the configuration object
  261. */
  262. synchronized RendererConfig getRendererConfig(FOUserAgent userAgent, Configuration cfg,
  263. RendererConfigParser configCreator) throws FOPException {
  264. RendererConfig config = rendererConfig.get(configCreator.getMimeType());
  265. if (config == null) {
  266. try {
  267. config = configCreator.build(userAgent, cfg);
  268. rendererConfig.put(configCreator.getMimeType(), config);
  269. } catch (Exception e) {
  270. throw new FOPException(e);
  271. }
  272. }
  273. return config;
  274. }
  275. /**
  276. * Add the element mapping with the given class name.
  277. * @param elementMapping the class name representing the element mapping.
  278. */
  279. public void addElementMapping(ElementMapping elementMapping) {
  280. this.elementMappingRegistry.addElementMapping(elementMapping);
  281. }
  282. /**
  283. * Returns whether accessibility is enabled.
  284. * @return true if accessibility is enabled
  285. */
  286. boolean isAccessibilityEnabled() {
  287. return config.isAccessibilityEnabled();
  288. }
  289. /** @see FopFactoryConfig#getImageManager() */
  290. public ImageManager getImageManager() {
  291. return config.getImageManager();
  292. }
  293. /** @see FopFactoryConfig#getLayoutManagerMakerOverride() */
  294. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  295. return config.getLayoutManagerMakerOverride();
  296. }
  297. /** @see FopFactoryConfig#getHyphenationPatternNames() */
  298. public Map<String, String> getHyphenationPatternNames() {
  299. return config.getHyphenationPatternNames();
  300. }
  301. /** @see FopFactoryConfig#validateStrictly() */
  302. public boolean validateStrictly() {
  303. return config.validateStrictly();
  304. }
  305. /** @see FopFactoryConfig#isBreakIndentInheritanceOnReferenceAreaBoundary() */
  306. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  307. return config.isBreakIndentInheritanceOnReferenceAreaBoundary();
  308. }
  309. /** @see FopFactoryConfig#getSourceResolution() */
  310. public float getSourceResolution() {
  311. return config.getSourceResolution();
  312. }
  313. /** @see FopFactoryConfig#getTargetResolution() */
  314. public float getTargetResolution() {
  315. return config.getTargetResolution();
  316. }
  317. /**
  318. * Returns the conversion factor from pixel units to millimeters. This
  319. * depends on the desired source resolution.
  320. * @return float conversion factor
  321. * @see #getSourceResolution()
  322. */
  323. public float getSourcePixelUnitToMillimeter() {
  324. return UnitConv.IN2MM / getSourceResolution();
  325. }
  326. /**
  327. * Returns the conversion factor from pixel units to millimeters. This
  328. * depends on the desired target resolution.
  329. * @return float conversion factor
  330. * @see #getTargetResolution()
  331. */
  332. public float getTargetPixelUnitToMillimeter() {
  333. return 25.4f / getTargetResolution();
  334. }
  335. /** @see FopFactoryConfig#getPageHeight() */
  336. public String getPageHeight() {
  337. return config.getPageHeight();
  338. }
  339. /** @see FopFactoryConfig#getPageWidth() */
  340. public String getPageWidth() {
  341. return config.getPageWidth();
  342. }
  343. /** @see FopFactoryConfig#isNamespaceIgnored(String) */
  344. public boolean isNamespaceIgnored(String namespaceURI) {
  345. return config.isNamespaceIgnored(namespaceURI);
  346. }
  347. /** @see FopFactoryConfig#getIgnoredNamespaces() */
  348. public Set<String> getIgnoredNamespace() {
  349. return config.getIgnoredNamespaces();
  350. }
  351. /**
  352. * Get the user configuration.
  353. * @return the user configuration
  354. */
  355. public Configuration getUserConfig() {
  356. return config.getUserConfig();
  357. }
  358. /** @see FopFactoryConfig#validateUserConfigStrictly() */
  359. public boolean validateUserConfigStrictly() {
  360. return config.validateUserConfigStrictly();
  361. }
  362. /** @see FopFactoryConfig#getFontManager() */
  363. public FontManager getFontManager() {
  364. return config.getFontManager();
  365. }
  366. /** @see FopFactoryConfig#getFallbackResolver() */
  367. FallbackResolver getFallbackResolver() {
  368. return config.getFallbackResolver();
  369. }
  370. /**
  371. * Returns the color space cache for this instance.
  372. * <p>
  373. * Note: this method should not be considered as part of FOP's external API.
  374. * @return the color space cache
  375. */
  376. public ColorSpaceCache getColorSpaceCache() {
  377. return this.colorSpaceCache;
  378. }
  379. }