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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright 1999-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.apps;
  18. // Java
  19. import java.io.File;
  20. import java.util.Date;
  21. import java.util.Map;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.TransformerException;
  24. import javax.xml.transform.URIResolver;
  25. // avalon configuration
  26. import org.apache.avalon.framework.configuration.Configuration;
  27. import org.apache.avalon.framework.configuration.ConfigurationException;
  28. // commons logging
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. // FOP
  32. import org.apache.fop.Version;
  33. import org.apache.fop.fo.FOEventHandler;
  34. import org.apache.fop.pdf.PDFEncryptionParams;
  35. import org.apache.fop.render.Renderer;
  36. import org.apache.fop.render.RendererFactory;
  37. import org.apache.fop.render.XMLHandlerRegistry;
  38. import org.apache.fop.render.pdf.PDFRenderer;
  39. /**
  40. * This is the user agent for FOP.
  41. * It is the entity through which you can interact with the XSL-FO processing and is
  42. * used by the processing to obtain user configurable options.
  43. * <p>
  44. * Renderer specific extensions (that do not produce normal areas on
  45. * the output) will be done like so:
  46. * <br>
  47. * The extension will create an area, custom if necessary
  48. * <br>
  49. * this area will be added to the user agent with a key
  50. * <br>
  51. * the renderer will know keys for particular extensions
  52. * <br>
  53. * eg. bookmarks will be held in a special hierarchical area representing
  54. * the title and bookmark structure
  55. * <br>
  56. * These areas may contain resolvable areas that will be processed
  57. * with other resolvable areas
  58. */
  59. public class FOUserAgent {
  60. /** Defines the default target resolution (72dpi) for FOP */
  61. public static final float DEFAULT_TARGET_RESOLUTION = 72.0f; //dpi
  62. private static Log log = LogFactory.getLog("FOP");
  63. private FopFactory factory;
  64. /** The base URL for all URL resolutions, especially for external-graphics */
  65. private String baseURL;
  66. /** A user settable URI Resolver */
  67. private URIResolver uriResolver = null;
  68. private float targetResolution = DEFAULT_TARGET_RESOLUTION;
  69. private Map rendererOptions = new java.util.HashMap();
  70. private File outputFile = null;
  71. private Renderer rendererOverride = null;
  72. private FOEventHandler foEventHandlerOverride = null;
  73. /** Producer: Metadata element for the system/software that produces
  74. * the document. (Some renderers can store this in the document.)
  75. */
  76. protected String producer = "Apache FOP Version " + Version.getVersion();
  77. /** Creator: Metadata element for the user that created the
  78. * document. (Some renderers can store this in the document.)
  79. */
  80. protected String creator = null;
  81. /** Creation Date: Override of the date the document was created.
  82. * (Some renderers can store this in the document.)
  83. */
  84. protected Date creationDate = null;
  85. /** Author of the content of the document. */
  86. protected String author = null;
  87. /** Title of the document. */
  88. protected String title = null;
  89. /** Set of keywords applicable to this document. */
  90. protected String keywords = null;
  91. /**
  92. * Default constructor
  93. * @see org.apache.fop.apps.FopFactory
  94. * @deprecated Provided for compatibility only. Please use the methods from
  95. * FopFactory to construct FOUserAgent instances!
  96. */
  97. public FOUserAgent() {
  98. this(FopFactory.newInstance());
  99. }
  100. /**
  101. * Main constructor. <b>This constructor should not be called directly. Please use the
  102. * methods from FopFactory to construct FOUserAgent instances!</b>
  103. * @param factory the factory that provides environment-level information
  104. * @see org.apache.fop.apps.FopFactory
  105. */
  106. public FOUserAgent(FopFactory factory) {
  107. if (factory == null) {
  108. throw new NullPointerException("The factory parameter must not be null");
  109. }
  110. this.factory = factory;
  111. try {
  112. if (factory.getUserConfig() != null) {
  113. configure(factory.getUserConfig());
  114. }
  115. } catch (ConfigurationException cfge) {
  116. log.error("Error while initializing the user asgent: "
  117. + cfge.getMessage());
  118. }
  119. }
  120. /** @return the associated FopFactory instance */
  121. public FopFactory getFactory() {
  122. return this.factory;
  123. }
  124. // ---------------------------------------------- rendering-run dependent stuff
  125. /**
  126. * Sets an explicit renderer to use which overrides the one defined by the
  127. * render type setting.
  128. * @param renderer the Renderer instance to use
  129. */
  130. public void setRendererOverride(Renderer renderer) {
  131. this.rendererOverride = renderer;
  132. }
  133. /**
  134. * Returns the overriding Renderer instance, if any.
  135. * @return the overriding Renderer or null
  136. */
  137. public Renderer getRendererOverride() {
  138. return rendererOverride;
  139. }
  140. /**
  141. * Sets an explicit FOEventHandler instance which overrides the one
  142. * defined by the render type setting.
  143. * @param handler the FOEventHandler instance
  144. */
  145. public void setFOEventHandlerOverride(FOEventHandler handler) {
  146. this.foEventHandlerOverride = handler;
  147. }
  148. /**
  149. * Returns the overriding FOEventHandler instance, if any.
  150. * @return the overriding FOEventHandler or null
  151. */
  152. public FOEventHandler getFOEventHandlerOverride() {
  153. return this.foEventHandlerOverride;
  154. }
  155. /**
  156. * Sets the producer of the document.
  157. * @param producer source of document
  158. */
  159. public void setProducer(String producer) {
  160. this.producer = producer;
  161. }
  162. /**
  163. * Returns the producer of the document
  164. * @return producer name
  165. */
  166. public String getProducer() {
  167. return producer;
  168. }
  169. /**
  170. * Sets the creator of the document.
  171. * @param creator of document
  172. */
  173. public void setCreator(String creator) {
  174. this.creator = creator;
  175. }
  176. /**
  177. * Returns the creator of the document
  178. * @return creator name
  179. */
  180. public String getCreator() {
  181. return creator;
  182. }
  183. /**
  184. * Sets the creation date of the document.
  185. * @param creationDate date of document
  186. */
  187. public void setCreationDate(Date creationDate) {
  188. this.creationDate = creationDate;
  189. }
  190. /**
  191. * Returns the creation date of the document
  192. * @return creation date of document
  193. */
  194. public Date getCreationDate() {
  195. return creationDate;
  196. }
  197. /**
  198. * Sets the author of the document.
  199. * @param author of document
  200. */
  201. public void setAuthor(String author) {
  202. this.author = author;
  203. }
  204. /**
  205. * Returns the author of the document
  206. * @return author name
  207. */
  208. public String getAuthor() {
  209. return author;
  210. }
  211. /**
  212. * Sets the title of the document. This will override any title coming from
  213. * an fo:title element.
  214. * @param title of document
  215. */
  216. public void setTitle(String title) {
  217. this.title = title;
  218. }
  219. /**
  220. * Returns the title of the document
  221. * @return title name
  222. */
  223. public String getTitle() {
  224. return title;
  225. }
  226. /**
  227. * Sets the keywords for the document.
  228. * @param keywords for the document
  229. */
  230. public void setKeywords(String keywords) {
  231. this.keywords = keywords;
  232. }
  233. /**
  234. * Returns the keywords for the document
  235. * @return the keywords
  236. */
  237. public String getKeywords() {
  238. return keywords;
  239. }
  240. /**
  241. * Returns the renderer options
  242. * @return renderer options
  243. */
  244. public Map getRendererOptions() {
  245. return rendererOptions;
  246. }
  247. /**
  248. * Configures the FOUserAgent through the factory's configuration.
  249. * @see org.apache.avalon.framework.configuration.Configurable
  250. */
  251. protected void configure(Configuration cfg) throws ConfigurationException {
  252. setBaseURL(FopFactory.getBaseURLfromConfig(cfg, "base"));
  253. if (cfg.getChild("target-resolution", false) != null) {
  254. this.targetResolution
  255. = cfg.getChild("target-resolution").getValueAsFloat(
  256. DEFAULT_TARGET_RESOLUTION);
  257. log.info("Target resolution set to: " + targetResolution
  258. + "dpi (px2mm=" + getTargetPixelUnitToMillimeter() + ")");
  259. }
  260. }
  261. /**
  262. * Returns the configuration subtree for a specific renderer.
  263. * @param mimeType MIME type of the renderer
  264. * @return the requested configuration subtree, null if there's no configuration
  265. */
  266. public Configuration getUserRendererConfig(String mimeType) {
  267. Configuration cfg = getFactory().getUserConfig();
  268. if (cfg == null || mimeType == null) {
  269. return null;
  270. }
  271. Configuration userRendererConfig = null;
  272. Configuration[] cfgs
  273. = cfg.getChild("renderers").getChildren("renderer");
  274. for (int i = 0; i < cfgs.length; ++i) {
  275. Configuration child = cfgs[i];
  276. try {
  277. if (child.getAttribute("mime").equals(mimeType)) {
  278. userRendererConfig = child;
  279. break;
  280. }
  281. } catch (ConfigurationException e) {
  282. // silently pass over configurations without mime type
  283. }
  284. }
  285. log.debug((userRendererConfig == null ? "No u" : "U")
  286. + "ser configuration found for MIME type " + mimeType);
  287. return userRendererConfig;
  288. }
  289. /**
  290. * Sets the base URL.
  291. * @param baseURL base URL
  292. */
  293. public void setBaseURL(String baseURL) {
  294. this.baseURL = baseURL;
  295. }
  296. /**
  297. * Returns the base URL.
  298. * @return the base URL
  299. */
  300. public String getBaseURL() {
  301. return this.baseURL;
  302. }
  303. /**
  304. * Sets the URI Resolver.
  305. * @param resolver the new URI resolver
  306. */
  307. public void setURIResolver(URIResolver resolver) {
  308. this.uriResolver = resolver;
  309. }
  310. /**
  311. * Returns the URI Resolver.
  312. * @return the URI Resolver
  313. */
  314. public URIResolver getURIResolver() {
  315. return this.uriResolver;
  316. }
  317. /**
  318. * Returns the parameters for PDF encryption.
  319. * @return the PDF encryption parameters, null if not applicable
  320. * @deprecated Use (PDFEncryptionParams)getRendererOptions().get("encryption-params")
  321. * instead.
  322. */
  323. public PDFEncryptionParams getPDFEncryptionParams() {
  324. return (PDFEncryptionParams)getRendererOptions().get(PDFRenderer.ENCRYPTION_PARAMS);
  325. }
  326. /**
  327. * Sets the parameters for PDF encryption.
  328. * @param pdfEncryptionParams the PDF encryption parameters, null to
  329. * disable PDF encryption
  330. * @deprecated Use getRendererOptions().put("encryption-params",
  331. * new PDFEncryptionParams(..)) instead or set every parameter separately:
  332. * getRendererOptions().put("noprint", Boolean.TRUE).
  333. */
  334. public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) {
  335. getRendererOptions().put(PDFRenderer.ENCRYPTION_PARAMS, pdfEncryptionParams);
  336. }
  337. /**
  338. * Attempts to resolve the given URI.
  339. * Will use the configured resolver and if not successful fall back
  340. * to the default resolver.
  341. * @param uri URI to access
  342. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  343. * cannot be resolved.
  344. * @see org.apache.fop.apps.FOURIResolver
  345. */
  346. public Source resolveURI(String uri) {
  347. return resolveURI(uri, getBaseURL());
  348. }
  349. /**
  350. * Attempts to resolve the given URI.
  351. * Will use the configured resolver and if not successful fall back
  352. * to the default resolver.
  353. * @param uri URI to access
  354. * @param base the base URI to resolve against
  355. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  356. * cannot be resolved.
  357. * @see org.apache.fop.apps.FOURIResolver
  358. */
  359. public Source resolveURI(String uri, String base) {
  360. Source source = null;
  361. if (uriResolver != null) {
  362. try {
  363. source = uriResolver.resolve(uri, base);
  364. } catch (TransformerException te) {
  365. log.error("Attempt to resolve URI '" + uri + "' failed: ", te);
  366. }
  367. }
  368. if (source == null) {
  369. // URI Resolver not configured or returned null, use default resolver from the factory
  370. source = getFactory().resolveURI(uri, base);
  371. }
  372. return source;
  373. }
  374. /**
  375. * Sets the output File.
  376. * @param f the output File
  377. */
  378. public void setOutputFile(File f) {
  379. this.outputFile = f;
  380. }
  381. /**
  382. * Gets the output File.
  383. * @return the output File
  384. */
  385. public File getOutputFile() {
  386. return outputFile;
  387. }
  388. /**
  389. * Returns the conversion factor from pixel units to millimeters. This
  390. * depends on the desired target resolution.
  391. * @return float conversion factor
  392. * @see #getTargetResolution()
  393. */
  394. public float getTargetPixelUnitToMillimeter() {
  395. return 25.4f / this.targetResolution;
  396. }
  397. /** @return the resolution for resolution-dependant output */
  398. public float getTargetResolution() {
  399. return this.targetResolution;
  400. }
  401. /**
  402. * Sets the target resolution in dpi. This value defines the target resolution of
  403. * bitmap images generated by the bitmap renderers (such as the TIFF renderer) and of
  404. * bitmap images generated by filter effects in Apache Batik.
  405. * @param dpi resolution in dpi
  406. */
  407. public void setTargetResolution(int dpi) {
  408. this.targetResolution = dpi;
  409. }
  410. // ---------------------------------------------- environment-level stuff
  411. // (convenience access to FopFactory methods)
  412. /** @return the font base URL */
  413. public String getFontBaseURL() {
  414. String fontBaseURL = getFactory().getFontBaseURL();
  415. return fontBaseURL != null ? fontBaseURL : this.baseURL;
  416. }
  417. /**
  418. * Returns the conversion factor from pixel units to millimeters. This
  419. * depends on the desired source resolution.
  420. * @return float conversion factor
  421. * @see #getSourceResolution()
  422. */
  423. public float getSourcePixelUnitToMillimeter() {
  424. return getFactory().getSourcePixelUnitToMillimeter();
  425. }
  426. /** @return the resolution for resolution-dependant input */
  427. public float getSourceResolution() {
  428. return getFactory().getSourceResolution();
  429. }
  430. /**
  431. * Gets the default page-height to use as fallback,
  432. * in case page-height="auto"
  433. *
  434. * @return the page-height, as a String
  435. * @see FopFactory#getPageHeight()
  436. */
  437. public String getPageHeight() {
  438. return getFactory().getPageHeight();
  439. }
  440. /**
  441. * Gets the default page-width to use as fallback,
  442. * in case page-width="auto"
  443. *
  444. * @return the page-width, as a String
  445. * @see FopFactory#getPageWidth()
  446. */
  447. public String getPageWidth() {
  448. return getFactory().getPageWidth();
  449. }
  450. /**
  451. * Returns whether FOP is strictly validating input XSL
  452. * @return true of strict validation turned on, false otherwise
  453. * @see FopFactory#validateStrictly()
  454. */
  455. public boolean validateStrictly() {
  456. return getFactory().validateStrictly();
  457. }
  458. /**
  459. * @return true if the indent inheritance should be broken when crossing reference area
  460. * boundaries (for more info, see the javadoc for the relative member variable)
  461. * @see FopFactory#isBreakIndentInheritanceOnReferenceAreaBoundary()
  462. */
  463. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  464. return getFactory().isBreakIndentInheritanceOnReferenceAreaBoundary();
  465. }
  466. /**
  467. * @return the RendererFactory
  468. */
  469. public RendererFactory getRendererFactory() {
  470. return getFactory().getRendererFactory();
  471. }
  472. /**
  473. * @return the XML handler registry
  474. */
  475. public XMLHandlerRegistry getXMLHandlerRegistry() {
  476. return getFactory().getXMLHandlerRegistry();
  477. }
  478. }