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

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