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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. if (factory.getUserConfig() != null) {
  112. configure(factory.getUserConfig());
  113. }
  114. }
  115. /** @return the associated FopFactory instance */
  116. public FopFactory getFactory() {
  117. return this.factory;
  118. }
  119. // ---------------------------------------------- rendering-run dependent stuff
  120. /**
  121. * Sets an explicit renderer to use which overrides the one defined by the
  122. * render type setting.
  123. * @param renderer the Renderer instance to use
  124. */
  125. public void setRendererOverride(Renderer renderer) {
  126. this.rendererOverride = renderer;
  127. }
  128. /**
  129. * Returns the overriding Renderer instance, if any.
  130. * @return the overriding Renderer or null
  131. */
  132. public Renderer getRendererOverride() {
  133. return rendererOverride;
  134. }
  135. /**
  136. * Sets an explicit FOEventHandler instance which overrides the one
  137. * defined by the render type setting.
  138. * @param handler the FOEventHandler instance
  139. */
  140. public void setFOEventHandlerOverride(FOEventHandler handler) {
  141. this.foEventHandlerOverride = handler;
  142. }
  143. /**
  144. * Returns the overriding FOEventHandler instance, if any.
  145. * @return the overriding FOEventHandler or null
  146. */
  147. public FOEventHandler getFOEventHandlerOverride() {
  148. return this.foEventHandlerOverride;
  149. }
  150. /**
  151. * Sets the producer of the document.
  152. * @param producer source of document
  153. */
  154. public void setProducer(String producer) {
  155. this.producer = producer;
  156. }
  157. /**
  158. * Returns the producer of the document
  159. * @return producer name
  160. */
  161. public String getProducer() {
  162. return producer;
  163. }
  164. /**
  165. * Sets the creator of the document.
  166. * @param creator of document
  167. */
  168. public void setCreator(String creator) {
  169. this.creator = creator;
  170. }
  171. /**
  172. * Returns the creator of the document
  173. * @return creator name
  174. */
  175. public String getCreator() {
  176. return creator;
  177. }
  178. /**
  179. * Sets the creation date of the document.
  180. * @param creationDate date of document
  181. */
  182. public void setCreationDate(Date creationDate) {
  183. this.creationDate = creationDate;
  184. }
  185. /**
  186. * Returns the creation date of the document
  187. * @return creation date of document
  188. */
  189. public Date getCreationDate() {
  190. return creationDate;
  191. }
  192. /**
  193. * Sets the author of the document.
  194. * @param author of document
  195. */
  196. public void setAuthor(String author) {
  197. this.author = author;
  198. }
  199. /**
  200. * Returns the author of the document
  201. * @return author name
  202. */
  203. public String getAuthor() {
  204. return author;
  205. }
  206. /**
  207. * Sets the title of the document. This will override any title coming from
  208. * an fo:title element.
  209. * @param title of document
  210. */
  211. public void setTitle(String title) {
  212. this.title = title;
  213. }
  214. /**
  215. * Returns the title of the document
  216. * @return title name
  217. */
  218. public String getTitle() {
  219. return title;
  220. }
  221. /**
  222. * Sets the keywords for the document.
  223. * @param keywords for the document
  224. */
  225. public void setKeywords(String keywords) {
  226. this.keywords = keywords;
  227. }
  228. /**
  229. * Returns the keywords for the document
  230. * @return the keywords
  231. */
  232. public String getKeywords() {
  233. return keywords;
  234. }
  235. /**
  236. * Returns the renderer options
  237. * @return renderer options
  238. */
  239. public Map getRendererOptions() {
  240. return rendererOptions;
  241. }
  242. /**
  243. * Configures the FOUserAgent through the factory's configuration.
  244. * @see org.apache.avalon.framework.configuration.Configurable
  245. */
  246. protected void configure(Configuration cfg) {
  247. setBaseURL(FopFactory.getBaseURLfromConfig(cfg, "base"));
  248. if (cfg.getChild("target-resolution", false) != null) {
  249. this.targetResolution
  250. = cfg.getChild("target-resolution").getValueAsFloat(
  251. DEFAULT_TARGET_RESOLUTION);
  252. log.info("Target resolution set to: " + targetResolution
  253. + "dpi (px2mm=" + getTargetPixelUnitToMillimeter() + ")");
  254. }
  255. }
  256. /**
  257. * Returns the configuration subtree for a specific renderer.
  258. * @param mimeType MIME type of the renderer
  259. * @return the requested configuration subtree, null if there's no configuration
  260. */
  261. public Configuration getUserRendererConfig(String mimeType) {
  262. Configuration cfg = getFactory().getUserConfig();
  263. if (cfg == null || mimeType == null) {
  264. return null;
  265. }
  266. Configuration userRendererConfig = null;
  267. Configuration[] cfgs
  268. = cfg.getChild("renderers").getChildren("renderer");
  269. for (int i = 0; i < cfgs.length; ++i) {
  270. Configuration child = cfgs[i];
  271. try {
  272. if (child.getAttribute("mime").equals(mimeType)) {
  273. userRendererConfig = child;
  274. break;
  275. }
  276. } catch (ConfigurationException e) {
  277. // silently pass over configurations without mime type
  278. }
  279. }
  280. log.debug((userRendererConfig == null ? "No u" : "U")
  281. + "ser configuration found for MIME type " + mimeType);
  282. return userRendererConfig;
  283. }
  284. /**
  285. * Sets the base URL.
  286. * @param baseURL base URL
  287. */
  288. public void setBaseURL(String baseURL) {
  289. this.baseURL = baseURL;
  290. }
  291. /**
  292. * Returns the base URL.
  293. * @return the base URL
  294. */
  295. public String getBaseURL() {
  296. return this.baseURL;
  297. }
  298. /**
  299. * Sets the URI Resolver.
  300. * @param resolver the new URI resolver
  301. */
  302. public void setURIResolver(URIResolver resolver) {
  303. this.uriResolver = resolver;
  304. }
  305. /**
  306. * Returns the URI Resolver.
  307. * @return the URI Resolver
  308. */
  309. public URIResolver getURIResolver() {
  310. return this.uriResolver;
  311. }
  312. /**
  313. * Returns the parameters for PDF encryption.
  314. * @return the PDF encryption parameters, null if not applicable
  315. * @deprecated Use (PDFEncryptionParams)getRendererOptions().get("encryption-params")
  316. * instead.
  317. */
  318. public PDFEncryptionParams getPDFEncryptionParams() {
  319. return (PDFEncryptionParams)getRendererOptions().get(PDFRenderer.ENCRYPTION_PARAMS);
  320. }
  321. /**
  322. * Sets the parameters for PDF encryption.
  323. * @param pdfEncryptionParams the PDF encryption parameters, null to
  324. * disable PDF encryption
  325. * @deprecated Use getRendererOptions().put("encryption-params",
  326. * new PDFEncryptionParams(..)) instead or set every parameter separately:
  327. * getRendererOptions().put("noprint", Boolean.TRUE).
  328. */
  329. public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) {
  330. getRendererOptions().put(PDFRenderer.ENCRYPTION_PARAMS, pdfEncryptionParams);
  331. }
  332. /**
  333. * Attempts to resolve the given URI.
  334. * Will use the configured resolver and if not successful fall back
  335. * to the default resolver.
  336. * @param uri URI to access
  337. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  338. * cannot be resolved.
  339. * @see org.apache.fop.apps.FOURIResolver
  340. */
  341. public Source resolveURI(String uri) {
  342. return resolveURI(uri, getBaseURL());
  343. }
  344. /**
  345. * Attempts to resolve the given URI.
  346. * Will use the configured resolver and if not successful fall back
  347. * to the default resolver.
  348. * @param uri URI to access
  349. * @param base the base URI to resolve against
  350. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  351. * cannot be resolved.
  352. * @see org.apache.fop.apps.FOURIResolver
  353. */
  354. public Source resolveURI(String uri, String base) {
  355. Source source = null;
  356. //RFC 2397 data URLs don't need to be resolved, just decode them.
  357. boolean bypassURIResolution = uri.startsWith("data:");
  358. if (!bypassURIResolution && uriResolver != null) {
  359. try {
  360. source = uriResolver.resolve(uri, base);
  361. } catch (TransformerException te) {
  362. log.error("Attempt to resolve URI '" + uri + "' failed: ", te);
  363. }
  364. }
  365. if (source == null) {
  366. // URI Resolver not configured or returned null, use default resolver from the factory
  367. source = getFactory().resolveURI(uri, base);
  368. }
  369. return source;
  370. }
  371. /**
  372. * Sets the output File.
  373. * @param f the output File
  374. */
  375. public void setOutputFile(File f) {
  376. this.outputFile = f;
  377. }
  378. /**
  379. * Gets the output File.
  380. * @return the output File
  381. */
  382. public File getOutputFile() {
  383. return outputFile;
  384. }
  385. /**
  386. * Returns the conversion factor from pixel units to millimeters. This
  387. * depends on the desired target resolution.
  388. * @return float conversion factor
  389. * @see #getTargetResolution()
  390. */
  391. public float getTargetPixelUnitToMillimeter() {
  392. return 25.4f / this.targetResolution;
  393. }
  394. /** @return the resolution for resolution-dependant output */
  395. public float getTargetResolution() {
  396. return this.targetResolution;
  397. }
  398. /**
  399. * Sets the target resolution in dpi. This value defines the target resolution of
  400. * bitmap images generated by the bitmap renderers (such as the TIFF renderer) and of
  401. * bitmap images generated by filter effects in Apache Batik.
  402. * @param dpi resolution in dpi
  403. */
  404. public void setTargetResolution(int dpi) {
  405. this.targetResolution = dpi;
  406. }
  407. // ---------------------------------------------- environment-level stuff
  408. // (convenience access to FopFactory methods)
  409. /** @return the font base URL */
  410. public String getFontBaseURL() {
  411. String fontBaseURL = getFactory().getFontBaseURL();
  412. return fontBaseURL != null ? fontBaseURL : this.baseURL;
  413. }
  414. /**
  415. * Returns the conversion factor from pixel units to millimeters. This
  416. * depends on the desired source resolution.
  417. * @return float conversion factor
  418. * @see #getSourceResolution()
  419. */
  420. public float getSourcePixelUnitToMillimeter() {
  421. return getFactory().getSourcePixelUnitToMillimeter();
  422. }
  423. /** @return the resolution for resolution-dependant input */
  424. public float getSourceResolution() {
  425. return getFactory().getSourceResolution();
  426. }
  427. /**
  428. * Gets the default page-height to use as fallback,
  429. * in case page-height="auto"
  430. *
  431. * @return the page-height, as a String
  432. * @see FopFactory#getPageHeight()
  433. */
  434. public String getPageHeight() {
  435. return getFactory().getPageHeight();
  436. }
  437. /**
  438. * Gets the default page-width to use as fallback,
  439. * in case page-width="auto"
  440. *
  441. * @return the page-width, as a String
  442. * @see FopFactory#getPageWidth()
  443. */
  444. public String getPageWidth() {
  445. return getFactory().getPageWidth();
  446. }
  447. /**
  448. * Returns whether FOP is strictly validating input XSL
  449. * @return true of strict validation turned on, false otherwise
  450. * @see FopFactory#validateStrictly()
  451. */
  452. public boolean validateStrictly() {
  453. return getFactory().validateStrictly();
  454. }
  455. /**
  456. * @return true if the indent inheritance should be broken when crossing reference area
  457. * boundaries (for more info, see the javadoc for the relative member variable)
  458. * @see FopFactory#isBreakIndentInheritanceOnReferenceAreaBoundary()
  459. */
  460. public boolean isBreakIndentInheritanceOnReferenceAreaBoundary() {
  461. return getFactory().isBreakIndentInheritanceOnReferenceAreaBoundary();
  462. }
  463. /**
  464. * @return the RendererFactory
  465. */
  466. public RendererFactory getRendererFactory() {
  467. return getFactory().getRendererFactory();
  468. }
  469. /**
  470. * @return the XML handler registry
  471. */
  472. public XMLHandlerRegistry getXMLHandlerRegistry() {
  473. return getFactory().getXMLHandlerRegistry();
  474. }
  475. }