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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.util.UnitConv;
  34. import org.apache.fop.apps.io.InternalResourceResolver;
  35. import org.apache.fop.apps.io.ResourceResolverFactory;
  36. import org.apache.fop.fo.ElementMapping;
  37. import org.apache.fop.fo.ElementMappingRegistry;
  38. import org.apache.fop.fonts.FontManager;
  39. import org.apache.fop.layoutmgr.LayoutManagerMaker;
  40. import org.apache.fop.render.ImageHandlerRegistry;
  41. import org.apache.fop.render.RendererConfig;
  42. import org.apache.fop.render.RendererConfig.RendererConfigParser;
  43. import org.apache.fop.render.RendererFactory;
  44. import org.apache.fop.render.XMLHandlerRegistry;
  45. import org.apache.fop.util.ColorSpaceCache;
  46. import org.apache.fop.util.ContentHandlerFactoryRegistry;
  47. /**
  48. * Factory class which instantiates new Fop and FOUserAgent instances. This
  49. * class also holds environmental information and configuration used by FOP.
  50. * Information that may potentially be different for each renderingq run can be
  51. * found and managed in the FOUserAgent.
  52. */
  53. public final class FopFactory implements ImageContext {
  54. /** logger instance */
  55. private static Log log = LogFactory.getLog(FopFactory.class);
  56. /** Factory for Renderers and FOEventHandlers */
  57. private final RendererFactory rendererFactory;
  58. /** Registry for XML handlers */
  59. private final XMLHandlerRegistry xmlHandlers;
  60. /** Registry for image handlers */
  61. private final ImageHandlerRegistry imageHandlers;
  62. /** The registry for ElementMapping instances */
  63. private final ElementMappingRegistry elementMappingRegistry;
  64. /** The registry for ContentHandlerFactory instance */
  65. private final ContentHandlerFactoryRegistry contentHandlerFactoryRegistry
  66. = new ContentHandlerFactoryRegistry();
  67. private final ColorSpaceCache colorSpaceCache;
  68. private final FopFactoryConfig config;
  69. private final InternalResourceResolver resolver;
  70. private final Map<String, RendererConfig> rendererConfig;
  71. private FopFactory(FopFactoryConfig config) {
  72. this.config = config;
  73. this.resolver = ResourceResolverFactory.createInternalResourceResolver(config.getBaseURI(),
  74. config.getResourceResolver());
  75. this.elementMappingRegistry = new ElementMappingRegistry(this);
  76. this.colorSpaceCache = new ColorSpaceCache(resolver);
  77. this.rendererFactory = new RendererFactory(config.preferRenderer());
  78. this.xmlHandlers = new XMLHandlerRegistry();
  79. this.imageHandlers = new ImageHandlerRegistry();
  80. rendererConfig = new HashMap<String, RendererConfig>();
  81. }
  82. /**
  83. * Map of configured names of hyphenation pattern file names: ll_CC => name
  84. */
  85. private Map/*<String,String>*/ hyphPatNames = null;
  86. /**
  87. * FOP has the ability, for some FO's, to continue processing even if the
  88. * input XSL violates that FO's content model. This is the default
  89. * behavior for FOP. However, this flag, if set, provides the user the
  90. * ability for FOP to halt on all content model violations if desired.
  91. * Returns a new FopFactory instance that is configured using the {@link FopFactoryConfig} object.
  92. *
  93. * @param config the fop configuration
  94. * @return the requested FopFactory instance.
  95. */
  96. public static FopFactory newInstance(FopFactoryConfig config) {
  97. return new FopFactory(config);
  98. }
  99. /**
  100. * Returns a new FopFactory instance that is configured using the {@link FopFactoryConfig} object that
  101. * is created when the fopConf is parsed.
  102. *
  103. * @param fopConf the fop conf configuration file to parse
  104. * @return the requested FopFactory instance.
  105. * @throws IOException
  106. * @throws SAXException
  107. */
  108. public static FopFactory newInstance(File fopConf) throws SAXException, IOException {
  109. return new FopConfParser(fopConf).getFopFactoryBuilder().build();
  110. }
  111. /**
  112. * Returns a new FopFactory instance that is configured only by the default configuration
  113. * parameters.
  114. *
  115. * @param baseURI the base URI to resolve resource URIs against
  116. * @return the requested FopFactory instance.
  117. */
  118. public static FopFactory newInstance(URI baseURI) {
  119. return new FopFactoryBuilder(baseURI).build();
  120. }
  121. /**
  122. * Returns a new FopFactory instance that is configured using the {@link FopFactoryConfig} object that
  123. * is created when the fopConf is parsed.
  124. *
  125. * @param baseURI the base URI to resolve resource URIs against
  126. * @param confStream the fop conf configuration stream to parse
  127. * @return the requested FopFactory instance.
  128. * @throws SAXException
  129. * @throws IOException
  130. */
  131. public static FopFactory newInstance(URI baseURI, InputStream confStream) throws SAXException,
  132. IOException {
  133. return new FopConfParser(confStream, baseURI).getFopFactoryBuilder().build();
  134. }
  135. /**
  136. * Returns a new FOUserAgent instance. Use the FOUserAgent to configure special values that
  137. * are particular to a rendering run. Don't reuse instances over multiple rendering runs but
  138. * instead create a new one each time and reuse the FopFactory.
  139. * @return the newly created FOUserAgent instance initialized with default values
  140. * @throws FOPException
  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. 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. /**
  289. * Returns the image manager.
  290. * @return the image manager
  291. */
  292. public ImageManager getImageManager() {
  293. return config.getImageManager();
  294. }
  295. /**
  296. * Returns the overriding LayoutManagerMaker instance, if any.
  297. * @return the overriding LayoutManagerMaker or null
  298. */
  299. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  300. return config.getLayoutManagerMakerOverride();
  301. }
  302. /** @return the hyphenation pattern names */
  303. public Map<String, String> getHyphenationPatternNames() {
  304. return config.getHyphenationPatternNames();
  305. }
  306. /**
  307. * Returns whether FOP is strictly validating input XSL
  308. * @return true of strict validation turned on, false otherwise
  309. */
  310. public boolean validateStrictly() {
  311. return config.validateStrictly();
  312. }
  313. /**
  314. * @return true if the indent inheritance should be broken when crossing reference area
  315. * boundaries (for more info, see the javadoc for the relative member variable)
  316. */
  317. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  318. return config.isBreakIndentInheritanceOnReferenceAreaBoundary();
  319. }
  320. /** @return the resolution for resolution-dependent input */
  321. public float getSourceResolution() {
  322. return config.getSourceResolution();
  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. /** @return the resolution for resolution-dependant output */
  334. public float getTargetResolution() {
  335. return config.getTargetResolution();
  336. }
  337. /**
  338. * Returns the conversion factor from pixel units to millimeters. This
  339. * depends on the desired target resolution.
  340. * @return float conversion factor
  341. * @see #getTargetResolution()
  342. */
  343. public float getTargetPixelUnitToMillimeter() {
  344. return 25.4f / getTargetResolution();
  345. }
  346. /**
  347. * Gets the default page-height to use as fallback,
  348. * in case page-height="auto"
  349. *
  350. * @return the page-height, as a String
  351. */
  352. public String getPageHeight() {
  353. return config.getPageHeight();
  354. }
  355. /**
  356. * Gets the default page-width to use as fallback,
  357. * in case page-width="auto"
  358. *
  359. * @return the page-width, as a String
  360. */
  361. public String getPageWidth() {
  362. return config.getPageWidth();
  363. }
  364. /**
  365. * Indicates whether a namespace URI is on the ignored list.
  366. * @param namespaceURI the namespace URI
  367. * @return true if the namespace is ignored by FOP
  368. */
  369. public boolean isNamespaceIgnored(String namespaceURI) {
  370. return config.isNamespaceIgnored(namespaceURI);
  371. }
  372. /** @return the set of namespaces that are ignored by FOP */
  373. public Set<String> getIgnoredNamespace() {
  374. return config.getIgnoredNamespaces();
  375. }
  376. //------------------------------------------- Configuration stuff
  377. /**
  378. * Get the user configuration.
  379. * @return the user configuration
  380. */
  381. public Configuration getUserConfig() {
  382. return config.getUserConfig();
  383. }
  384. /**
  385. * Is the user configuration to be validated?
  386. * @return if the user configuration should be validated
  387. */
  388. public boolean validateUserConfigStrictly() {
  389. return config.validateUserConfigStrictly();
  390. }
  391. //------------------------------------------- Font related stuff
  392. /**
  393. * Returns the font manager.
  394. * @return the font manager
  395. */
  396. public FontManager getFontManager() {
  397. return config.getFontManager();
  398. }
  399. /**
  400. * Returns the color space cache for this instance.
  401. * <p>
  402. * Note: this method should not be considered as part of FOP's external API.
  403. * @return the color space cache
  404. */
  405. public ColorSpaceCache getColorSpaceCache() {
  406. return this.colorSpaceCache;
  407. }
  408. }