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

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