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.

FOUserAgent.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. // Java
  20. import java.io.File;
  21. import java.util.Date;
  22. import java.util.Map;
  23. import javax.xml.transform.Source;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.URIResolver;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.xmlgraphics.image.loader.ImageContext;
  29. import org.apache.xmlgraphics.image.loader.ImageSessionContext;
  30. import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext;
  31. import org.apache.fop.Version;
  32. import org.apache.fop.events.DefaultEventBroadcaster;
  33. import org.apache.fop.events.Event;
  34. import org.apache.fop.events.EventBroadcaster;
  35. import org.apache.fop.events.EventListener;
  36. import org.apache.fop.events.FOPEventListenerProxy;
  37. import org.apache.fop.events.LoggingEventListener;
  38. import org.apache.fop.fo.FOEventHandler;
  39. import org.apache.fop.render.Renderer;
  40. import org.apache.fop.render.RendererFactory;
  41. import org.apache.fop.render.XMLHandlerRegistry;
  42. import org.apache.fop.render.intermediate.IFDocumentHandler;
  43. /**
  44. * This is the user agent for FOP.
  45. * It is the entity through which you can interact with the XSL-FO processing and is
  46. * used by the processing to obtain user configurable options.
  47. * <p>
  48. * Renderer specific extensions (that do not produce normal areas on
  49. * the output) will be done like so:
  50. * <br>
  51. * The extension will create an area, custom if necessary
  52. * <br>
  53. * this area will be added to the user agent with a key
  54. * <br>
  55. * the renderer will know keys for particular extensions
  56. * <br>
  57. * eg. bookmarks will be held in a special hierarchical area representing
  58. * the title and bookmark structure
  59. * <br>
  60. * These areas may contain resolvable areas that will be processed
  61. * with other resolvable areas
  62. */
  63. public class FOUserAgent {
  64. /** Defines the default target resolution (72dpi) for FOP */
  65. public static final float DEFAULT_TARGET_RESOLUTION
  66. = FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION;
  67. private static Log log = LogFactory.getLog("FOP");
  68. private FopFactory factory;
  69. /**
  70. * The base URL for all URL resolutions, especially for
  71. * external-graphics.
  72. */
  73. private String base = null;
  74. /** The base URL for all font URL resolutions. */
  75. private String fontBase = null;
  76. /** A user settable URI Resolver */
  77. private URIResolver uriResolver = null;
  78. private float targetResolution = FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION;
  79. private Map rendererOptions = new java.util.HashMap();
  80. private File outputFile = null;
  81. private IFDocumentHandler documentHandlerOverride = null;
  82. private Renderer rendererOverride = null;
  83. private FOEventHandler foEventHandlerOverride = null;
  84. private boolean locatorEnabled = true; // true by default (for error messages).
  85. private EventBroadcaster eventBroadcaster = new FOPEventBroadcaster();
  86. /** Producer: Metadata element for the system/software that produces
  87. * the document. (Some renderers can store this in the document.)
  88. */
  89. protected String producer = "Apache FOP Version " + Version.getVersion();
  90. /** Creator: Metadata element for the user that created the
  91. * document. (Some renderers can store this in the document.)
  92. */
  93. protected String creator = null;
  94. /** Creation Date: Override of the date the document was created.
  95. * (Some renderers can store this in the document.)
  96. */
  97. protected Date creationDate = null;
  98. /** Author of the content of the document. */
  99. protected String author = null;
  100. /** Title of the document. */
  101. protected String title = null;
  102. /** Subject of the document. */
  103. protected String subject = null;
  104. /** Set of keywords applicable to this document. */
  105. protected String keywords = null;
  106. private ImageSessionContext imageSessionContext = new AbstractImageSessionContext() {
  107. public ImageContext getParentContext() {
  108. return getFactory();
  109. }
  110. public float getTargetResolution() {
  111. return FOUserAgent.this.getTargetResolution();
  112. }
  113. public Source resolveURI(String uri) {
  114. return FOUserAgent.this.resolveURI(uri);
  115. }
  116. };
  117. /**
  118. * Main constructor. <b>This constructor should not be called directly. Please use the
  119. * methods from FopFactory to construct FOUserAgent instances!</b>
  120. * @param factory the factory that provides environment-level information
  121. * @see org.apache.fop.apps.FopFactory
  122. */
  123. public FOUserAgent(FopFactory factory) {
  124. if (factory == null) {
  125. throw new NullPointerException("The factory parameter must not be null");
  126. }
  127. this.factory = factory;
  128. setBaseURL(factory.getBaseURL());
  129. setFontBaseURL(factory.getFontManager().getFontBaseURL());
  130. setTargetResolution(factory.getTargetResolution());
  131. }
  132. /** @return the associated FopFactory instance */
  133. public FopFactory getFactory() {
  134. return this.factory;
  135. }
  136. // ---------------------------------------------- rendering-run dependent stuff
  137. /**
  138. * Sets an explicit document handler to use which overrides the one that would be
  139. * selected by default.
  140. * @param documentHandler the document handler instance to use
  141. */
  142. public void setDocumentHandlerOverride(IFDocumentHandler documentHandler) {
  143. this.documentHandlerOverride = documentHandler;
  144. }
  145. /**
  146. * Returns the overriding {@link IFDocumentHandler} instance, if any.
  147. * @return the overriding document handler or null
  148. */
  149. public IFDocumentHandler getDocumentHandlerOverride() {
  150. return this.documentHandlerOverride;
  151. }
  152. /**
  153. * Sets an explicit renderer to use which overrides the one defined by the
  154. * render type setting.
  155. * @param renderer the Renderer instance to use
  156. */
  157. public void setRendererOverride(Renderer renderer) {
  158. this.rendererOverride = renderer;
  159. }
  160. /**
  161. * Returns the overriding Renderer instance, if any.
  162. * @return the overriding Renderer or null
  163. */
  164. public Renderer getRendererOverride() {
  165. return rendererOverride;
  166. }
  167. /**
  168. * Sets an explicit FOEventHandler instance which overrides the one
  169. * defined by the render type setting.
  170. * @param handler the FOEventHandler instance
  171. */
  172. public void setFOEventHandlerOverride(FOEventHandler handler) {
  173. this.foEventHandlerOverride = handler;
  174. }
  175. /**
  176. * Returns the overriding FOEventHandler instance, if any.
  177. * @return the overriding FOEventHandler or null
  178. */
  179. public FOEventHandler getFOEventHandlerOverride() {
  180. return this.foEventHandlerOverride;
  181. }
  182. /**
  183. * Sets the producer of the document.
  184. * @param producer source of document
  185. */
  186. public void setProducer(String producer) {
  187. this.producer = producer;
  188. }
  189. /**
  190. * Returns the producer of the document
  191. * @return producer name
  192. */
  193. public String getProducer() {
  194. return producer;
  195. }
  196. /**
  197. * Sets the creator of the document.
  198. * @param creator of document
  199. */
  200. public void setCreator(String creator) {
  201. this.creator = creator;
  202. }
  203. /**
  204. * Returns the creator of the document
  205. * @return creator name
  206. */
  207. public String getCreator() {
  208. return creator;
  209. }
  210. /**
  211. * Sets the creation date of the document.
  212. * @param creationDate date of document
  213. */
  214. public void setCreationDate(Date creationDate) {
  215. this.creationDate = creationDate;
  216. }
  217. /**
  218. * Returns the creation date of the document
  219. * @return creation date of document
  220. */
  221. public Date getCreationDate() {
  222. return creationDate;
  223. }
  224. /**
  225. * Sets the author of the document.
  226. * @param author of document
  227. */
  228. public void setAuthor(String author) {
  229. this.author = author;
  230. }
  231. /**
  232. * Returns the author of the document
  233. * @return author name
  234. */
  235. public String getAuthor() {
  236. return author;
  237. }
  238. /**
  239. * Sets the title of the document. This will override any title coming from
  240. * an fo:title element.
  241. * @param title of document
  242. */
  243. public void setTitle(String title) {
  244. this.title = title;
  245. }
  246. /**
  247. * Returns the title of the document
  248. * @return title name
  249. */
  250. public String getTitle() {
  251. return title;
  252. }
  253. /**
  254. * Sets the subject of the document.
  255. * @param subject of document
  256. */
  257. public void setSubject(String subject) {
  258. this.subject = subject;
  259. }
  260. /**
  261. * Returns the subject of the document
  262. * @return the subject
  263. */
  264. public String getSubject() {
  265. return subject;
  266. }
  267. /**
  268. * Sets the keywords for the document.
  269. * @param keywords for the document
  270. */
  271. public void setKeywords(String keywords) {
  272. this.keywords = keywords;
  273. }
  274. /**
  275. * Returns the keywords for the document
  276. * @return the keywords
  277. */
  278. public String getKeywords() {
  279. return keywords;
  280. }
  281. /**
  282. * Returns the renderer options
  283. * @return renderer options
  284. */
  285. public Map getRendererOptions() {
  286. return rendererOptions;
  287. }
  288. /**
  289. * Sets the base URL.
  290. * @param baseUrl base URL
  291. */
  292. public void setBaseURL(String baseUrl) {
  293. this.base = baseUrl;
  294. }
  295. /**
  296. * sets font base URL
  297. * @param fontBaseUrl font base URL
  298. */
  299. public void setFontBaseURL(String fontBaseUrl) {
  300. this.fontBase = fontBaseUrl;
  301. }
  302. /**
  303. * Returns the base URL.
  304. * @return the base URL
  305. */
  306. public String getBaseURL() {
  307. return this.base;
  308. }
  309. /**
  310. * Sets the URI Resolver.
  311. * @param resolver the new URI resolver
  312. */
  313. public void setURIResolver(URIResolver resolver) {
  314. this.uriResolver = resolver;
  315. }
  316. /**
  317. * Returns the URI Resolver.
  318. * @return the URI Resolver
  319. */
  320. public URIResolver getURIResolver() {
  321. return this.uriResolver;
  322. }
  323. /**
  324. * Attempts to resolve the given URI.
  325. * Will use the configured resolver and if not successful fall back
  326. * to the default resolver.
  327. * @param uri URI to access
  328. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  329. * cannot be resolved.
  330. * @see org.apache.fop.apps.FOURIResolver
  331. */
  332. public Source resolveURI(String uri) {
  333. return resolveURI(uri, getBaseURL());
  334. }
  335. /**
  336. * Attempts to resolve the given URI.
  337. * Will use the configured resolver and if not successful fall back
  338. * to the default resolver.
  339. * @param href URI to access
  340. * @param base the base URI to resolve against
  341. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  342. * cannot be resolved.
  343. * @see org.apache.fop.apps.FOURIResolver
  344. */
  345. public Source resolveURI(String href, String base) {
  346. Source source = null;
  347. //RFC 2397 data URLs don't need to be resolved, just decode them through FOP's default
  348. //URIResolver.
  349. boolean bypassURIResolution = href.startsWith("data:");
  350. if (!bypassURIResolution && uriResolver != null) {
  351. try {
  352. source = uriResolver.resolve(href, base);
  353. } catch (TransformerException te) {
  354. log.error("Attempt to resolve URI '" + href + "' failed: ", te);
  355. }
  356. }
  357. if (source == null) {
  358. // URI Resolver not configured or returned null, use default resolver from the factory
  359. source = getFactory().resolveURI(href, base);
  360. }
  361. return source;
  362. }
  363. /**
  364. * Sets the output File.
  365. * @param f the output File
  366. */
  367. public void setOutputFile(File f) {
  368. this.outputFile = f;
  369. }
  370. /**
  371. * Gets the output File.
  372. * @return the output File
  373. */
  374. public File getOutputFile() {
  375. return outputFile;
  376. }
  377. /**
  378. * Returns the conversion factor from pixel units to millimeters. This
  379. * depends on the desired target resolution.
  380. * @return float conversion factor
  381. * @see #getTargetResolution()
  382. */
  383. public float getTargetPixelUnitToMillimeter() {
  384. return 25.4f / this.targetResolution;
  385. }
  386. /** @return the resolution for resolution-dependant output */
  387. public float getTargetResolution() {
  388. return this.targetResolution;
  389. }
  390. /**
  391. * Sets the target resolution in dpi. This value defines the target resolution of
  392. * bitmap images generated by the bitmap renderers (such as the TIFF renderer) and of
  393. * bitmap images generated by filter effects in Apache Batik.
  394. * @param dpi resolution in dpi
  395. */
  396. public void setTargetResolution(float dpi) {
  397. this.targetResolution = dpi;
  398. if (log.isDebugEnabled()) {
  399. log.debug("target-resolution set to: " + targetResolution
  400. + "dpi (px2mm=" + getTargetPixelUnitToMillimeter() + ")");
  401. }
  402. }
  403. /**
  404. * Sets the target resolution in dpi. This value defines the target resolution of
  405. * bitmap images generated by the bitmap renderers (such as the TIFF renderer) and of
  406. * bitmap images generated by filter effects in Apache Batik.
  407. * @param dpi resolution in dpi
  408. */
  409. public void setTargetResolution(int dpi) {
  410. setTargetResolution((float)dpi);
  411. }
  412. /**
  413. * Returns the image session context for the image package.
  414. * @return the ImageSessionContext instance for this rendering run
  415. */
  416. public ImageSessionContext getImageSessionContext() {
  417. return this.imageSessionContext;
  418. }
  419. // ---------------------------------------------- environment-level stuff
  420. // (convenience access to FopFactory methods)
  421. /** @return the font base URL */
  422. public String getFontBaseURL() {
  423. return fontBase != null ? fontBase : getBaseURL();
  424. }
  425. /**
  426. * Returns the conversion factor from pixel units to millimeters. This
  427. * depends on the desired source resolution.
  428. * @return float conversion factor
  429. * @see #getSourceResolution()
  430. */
  431. public float getSourcePixelUnitToMillimeter() {
  432. return getFactory().getSourcePixelUnitToMillimeter();
  433. }
  434. /** @return the resolution for resolution-dependant input */
  435. public float getSourceResolution() {
  436. return getFactory().getSourceResolution();
  437. }
  438. /**
  439. * Gets the default page-height to use as fallback,
  440. * in case page-height="auto"
  441. *
  442. * @return the page-height, as a String
  443. * @see FopFactory#getPageHeight()
  444. */
  445. public String getPageHeight() {
  446. return getFactory().getPageHeight();
  447. }
  448. /**
  449. * Gets the default page-width to use as fallback,
  450. * in case page-width="auto"
  451. *
  452. * @return the page-width, as a String
  453. * @see FopFactory#getPageWidth()
  454. */
  455. public String getPageWidth() {
  456. return getFactory().getPageWidth();
  457. }
  458. /**
  459. * Returns whether FOP is strictly validating input XSL
  460. * @return true of strict validation turned on, false otherwise
  461. * @see FopFactory#validateStrictly()
  462. */
  463. public boolean validateStrictly() {
  464. return getFactory().validateStrictly();
  465. }
  466. /**
  467. * @return true if the indent inheritance should be broken when crossing reference area
  468. * boundaries (for more info, see the javadoc for the relative member variable)
  469. * @see FopFactory#isBreakIndentInheritanceOnReferenceAreaBoundary()
  470. */
  471. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  472. return getFactory().isBreakIndentInheritanceOnReferenceAreaBoundary();
  473. }
  474. /**
  475. * @return the RendererFactory
  476. */
  477. public RendererFactory getRendererFactory() {
  478. return getFactory().getRendererFactory();
  479. }
  480. /**
  481. * @return the XML handler registry
  482. */
  483. public XMLHandlerRegistry getXMLHandlerRegistry() {
  484. return getFactory().getXMLHandlerRegistry();
  485. }
  486. /**
  487. * Controls the use of SAXLocators to provide location information in error
  488. * messages.
  489. *
  490. * @param enableLocator <code>false</code> if SAX Locators should be disabled
  491. */
  492. public void setLocatorEnabled(boolean enableLocator) {
  493. locatorEnabled = enableLocator;
  494. }
  495. /**
  496. * Checks if the use of Locators is enabled
  497. * @return true if context information should be stored on each node in the FO tree.
  498. */
  499. public boolean isLocatorEnabled() {
  500. return locatorEnabled;
  501. }
  502. /**
  503. * Returns the event broadcaster that control events sent inside a processing run. Clients
  504. * can register event listeners with the event broadcaster to listen for events that occur
  505. * while a document is being processed.
  506. * @return the event broadcaster.
  507. */
  508. public EventBroadcaster getEventBroadcaster() {
  509. return this.eventBroadcaster;
  510. }
  511. private class FOPEventBroadcaster extends DefaultEventBroadcaster {
  512. private EventListener rootListener;
  513. public FOPEventBroadcaster() {
  514. //Install a temporary event listener that catches the first event to
  515. //do some initialization.
  516. this.rootListener = new EventListener() {
  517. public void processEvent(Event event) {
  518. if (!listeners.hasEventListeners()) {
  519. //Backwards-compatibility: Make sure at least the LoggingEventListener is
  520. //plugged in so no events are just silently swallowed.
  521. addEventListener(
  522. new LoggingEventListener(LogFactory.getLog(FOUserAgent.class)));
  523. }
  524. //Replace with final event listener
  525. rootListener = new FOPEventListenerProxy(
  526. listeners, FOUserAgent.this);
  527. rootListener.processEvent(event);
  528. }
  529. };
  530. }
  531. /** {@inheritDoc} */
  532. public void broadcastEvent(Event event) {
  533. rootListener.processEvent(event);
  534. }
  535. }
  536. }