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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright 1999-2005 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.List;
  22. import java.util.Map;
  23. import javax.xml.transform.Source;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.URIResolver;
  26. // avalon configuration
  27. import org.apache.avalon.framework.configuration.Configuration;
  28. import org.apache.avalon.framework.configuration.ConfigurationException;
  29. // commons logging
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. // FOP
  33. import org.apache.fop.fo.ElementMapping;
  34. import org.apache.fop.fo.FOEventHandler;
  35. import org.apache.fop.layoutmgr.LayoutManagerMaker;
  36. import org.apache.fop.pdf.PDFEncryptionParams;
  37. import org.apache.fop.render.Renderer;
  38. import org.apache.fop.render.XMLHandlerRegistry;
  39. /**
  40. * The User Agent for fo.
  41. * This user agent is used by the processing to obtain user configurable
  42. * 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 resolution (72dpi) for FOP */
  61. public static final float DEFAULT_PX2MM = (25.4f / 72); //dpi (=25.4/dpi)
  62. /** Defines the default page-height */
  63. public static final String DEFAULT_PAGE_HEIGHT = "11in";
  64. /** Defines the default page-width */
  65. public static final String DEFAULT_PAGE_WIDTH = "8.26in";
  66. /** Registry for XML handlers */
  67. private XMLHandlerRegistry xmlHandlers = new XMLHandlerRegistry();
  68. private String baseURL;
  69. /** A user settable URI Resolver */
  70. private URIResolver uriResolver = null;
  71. /** Our default resolver if none is set */
  72. private URIResolver foURIResolver = new FOURIResolver();
  73. private PDFEncryptionParams pdfEncryptionParams;
  74. private float px2mm = DEFAULT_PX2MM;
  75. private String pageHeight = DEFAULT_PAGE_HEIGHT;
  76. private String pageWidth = DEFAULT_PAGE_WIDTH;
  77. private Map rendererOptions = new java.util.HashMap();
  78. private File outputFile = null;
  79. private Renderer rendererOverride = null;
  80. private FOEventHandler foEventHandlerOverride = null;
  81. private LayoutManagerMaker lmMakerOverride = null;
  82. /* user configuration */
  83. private Configuration userConfig = null;
  84. private Log log = LogFactory.getLog("FOP");
  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. */
  90. private boolean strictValidation = true;
  91. /* Additional fo.ElementMapping subclasses set by user */
  92. private List additionalElementMappings = null;
  93. /** Producer: Metadata element for the system/software that produces
  94. * the document. (Some renderers can store this in the document.)
  95. */
  96. protected String producer = "Apache FOP Version " + Fop.getVersion();
  97. /** Creator: Metadata element for the user that created the
  98. * document. (Some renderers can store this in the document.)
  99. */
  100. protected String creator = null;
  101. /** Creation Date: Override of the date the document was created.
  102. * (Some renderers can store this in the document.)
  103. */
  104. protected Date creationDate = null;
  105. /** Author of the content of the document. */
  106. protected String author = null;
  107. /** Title of the document. */
  108. protected String title = null;
  109. /** Set of keywords applicable to this document. */
  110. protected String keywords = null;
  111. /**
  112. * Add the element mapping with the given class name.
  113. * @param elementMapping the class name representing the element mapping.
  114. */
  115. public void addElementMapping(ElementMapping elementMapping) {
  116. if (additionalElementMappings == null) {
  117. additionalElementMappings = new java.util.ArrayList();
  118. }
  119. additionalElementMappings.add(elementMapping);
  120. }
  121. /**
  122. * Returns the List of user-added ElementMapping class names
  123. * @return List of Strings holding ElementMapping names.
  124. */
  125. public List getAdditionalElementMappings() {
  126. return additionalElementMappings;
  127. }
  128. /**
  129. * Sets an explicit renderer to use which overrides the one defined by the
  130. * render type setting.
  131. * @param renderer the Renderer instance to use
  132. */
  133. public void setRendererOverride(Renderer renderer) {
  134. this.rendererOverride = renderer;
  135. }
  136. /**
  137. * Returns the overriding Renderer instance, if any.
  138. * @return the overriding Renderer or null
  139. */
  140. public Renderer getRendererOverride() {
  141. return rendererOverride;
  142. }
  143. /**
  144. * Sets an explicit FOEventHandler instance which overrides the one
  145. * defined by the render type setting.
  146. * @param handler the FOEventHandler instance
  147. */
  148. public void setFOEventHandlerOverride(FOEventHandler handler) {
  149. this.foEventHandlerOverride = handler;
  150. }
  151. /**
  152. * Returns the overriding FOEventHandler instance, if any.
  153. * @return the overriding FOEventHandler or null
  154. */
  155. public FOEventHandler getFOEventHandlerOverride() {
  156. return this.foEventHandlerOverride;
  157. }
  158. /**
  159. * Activates strict XSL content model validation for FOP
  160. * Default is false (FOP will continue processing where it can)
  161. * @param validateStrictly true to turn on strict validation
  162. */
  163. public void setStrictValidation(boolean validateStrictly) {
  164. this.strictValidation = validateStrictly;
  165. }
  166. /**
  167. * Returns whether FOP is strictly validating input XSL
  168. * @return true of strict validation turned on, false otherwise
  169. */
  170. public boolean validateStrictly() {
  171. return strictValidation;
  172. }
  173. /**
  174. * Sets an explicit LayoutManagerMaker instance which overrides the one
  175. * defined by the AreaTreeHandler.
  176. * @param lmMaker the LayoutManagerMaker instance
  177. */
  178. public void setLayoutManagerMakerOverride(LayoutManagerMaker lmMaker) {
  179. this.lmMakerOverride = lmMaker;
  180. }
  181. /**
  182. * Returns the overriding LayoutManagerMaker instance, if any.
  183. * @return the overriding LayoutManagerMaker or null
  184. */
  185. public LayoutManagerMaker getLayoutManagerMakerOverride() {
  186. return this.lmMakerOverride;
  187. }
  188. /**
  189. * Sets the producer of the document.
  190. * @param producer source of document
  191. */
  192. public void setProducer(String producer) {
  193. this.producer = producer;
  194. }
  195. /**
  196. * Returns the producer of the document
  197. * @return producer name
  198. */
  199. public String getProducer() {
  200. return producer;
  201. }
  202. /**
  203. * Sets the creator of the document.
  204. * @param creator of document
  205. */
  206. public void setCreator(String creator) {
  207. this.creator = creator;
  208. }
  209. /**
  210. * Returns the creator of the document
  211. * @return creator name
  212. */
  213. public String getCreator() {
  214. return creator;
  215. }
  216. /**
  217. * Sets the creation date of the document.
  218. * @param creationDate date of document
  219. */
  220. public void setCreationDate(Date creationDate) {
  221. this.creationDate = creationDate;
  222. }
  223. /**
  224. * Returns the creation date of the document
  225. * @return creation date of document
  226. */
  227. public Date getCreationDate() {
  228. return creationDate;
  229. }
  230. /**
  231. * Sets the author of the document.
  232. * @param author of document
  233. */
  234. public void setAuthor(String author) {
  235. this.author = author;
  236. }
  237. /**
  238. * Returns the author of the document
  239. * @return author name
  240. */
  241. public String getAuthor() {
  242. return author;
  243. }
  244. /**
  245. * Sets the title of the document. This will override any title coming from
  246. * an fo:title element.
  247. * @param title of document
  248. */
  249. public void setTitle(String title) {
  250. this.title = title;
  251. }
  252. /**
  253. * Returns the title of the document
  254. * @return title name
  255. */
  256. public String getTitle() {
  257. return title;
  258. }
  259. /**
  260. * Sets the keywords for the document.
  261. * @param keywords for the document
  262. */
  263. public void setKeywords(String keywords) {
  264. this.keywords = keywords;
  265. }
  266. /**
  267. * Returns the keywords for the document
  268. * @return the keywords
  269. */
  270. public String getKeywords() {
  271. return keywords;
  272. }
  273. /**
  274. * Returns the renderer options
  275. * @return renderer options
  276. */
  277. public Map getRendererOptions() {
  278. return rendererOptions;
  279. }
  280. /**
  281. * Set the user configuration.
  282. * @param userConfig configuration
  283. */
  284. public void setUserConfig(Configuration userConfig) {
  285. this.userConfig = userConfig;
  286. }
  287. /**
  288. * Get the user configuration.
  289. * @return the user configuration
  290. */
  291. public Configuration getUserConfig() {
  292. return userConfig;
  293. }
  294. /**
  295. * Returns the configuration subtree for a specific renderer.
  296. * @param mimeType MIME type of the renderer
  297. * @return the requested configuration subtree, null if there's no configuration
  298. */
  299. public Configuration getUserRendererConfig (String mimeType) {
  300. if (userConfig == null || mimeType == null) {
  301. return null;
  302. }
  303. Configuration userRendererConfig = null;
  304. Configuration[] cfgs
  305. = userConfig.getChild("renderers").getChildren("renderer");
  306. for (int i = 0; i < cfgs.length; ++i) {
  307. Configuration cfg = cfgs[i];
  308. try {
  309. if (cfg.getAttribute("mime").equals(mimeType)) {
  310. userRendererConfig = cfg;
  311. break;
  312. }
  313. } catch (ConfigurationException e) {
  314. // silently pass over configurations without mime type
  315. }
  316. }
  317. log.debug((userRendererConfig == null ? "No u" : "U")
  318. + "ser configuration found for MIME type " + mimeType);
  319. return userRendererConfig;
  320. }
  321. /**
  322. * Sets the base URL.
  323. * @param baseURL base URL
  324. */
  325. public void setBaseURL(String baseURL) {
  326. this.baseURL = baseURL;
  327. }
  328. /**
  329. * Returns the base URL.
  330. * @return the base URL
  331. */
  332. public String getBaseURL() {
  333. return this.baseURL;
  334. }
  335. /**
  336. * Sets the URI Resolver.
  337. * @param uriResolver the new URI resolver
  338. */
  339. public void setURIResolver(URIResolver uriResolver) {
  340. this.uriResolver = uriResolver;
  341. }
  342. /**
  343. * Returns the URI Resolver.
  344. * @return the URI Resolver
  345. */
  346. public URIResolver getURIResolver() {
  347. return this.uriResolver;
  348. }
  349. /**
  350. * Returns the parameters for PDF encryption.
  351. * @return the PDF encryption parameters, null if not applicable
  352. */
  353. public PDFEncryptionParams getPDFEncryptionParams() {
  354. return pdfEncryptionParams;
  355. }
  356. /**
  357. * Sets the parameters for PDF encryption.
  358. * @param pdfEncryptionParams the PDF encryption parameters, null to
  359. * disable PDF encryption
  360. */
  361. public void setPDFEncryptionParams(PDFEncryptionParams pdfEncryptionParams) {
  362. this.pdfEncryptionParams = pdfEncryptionParams;
  363. }
  364. /**
  365. * Attempts to resolve the given URI.
  366. * Will use the configured resolver and if not successful fall back
  367. * to the default resolver.
  368. * @param uri URI to access
  369. * @return A {@link javax.xml.transform.Source} object, or null if the URI
  370. * cannot be resolved.
  371. * @see org.apache.fop.apps.FOURIResolver
  372. */
  373. public Source resolveURI(String uri) {
  374. Source source = null;
  375. if (uriResolver != null) {
  376. try {
  377. source = uriResolver.resolve(uri, getBaseURL());
  378. } catch (TransformerException te) {
  379. log.error("Attempt to resolve URI '" + uri + "' failed: ", te);
  380. }
  381. }
  382. if (source == null) {
  383. // URI Resolver not configured or returned null, use default resolver
  384. try {
  385. source = foURIResolver.resolve(uri, getBaseURL());
  386. } catch (TransformerException te) {
  387. log.error("Attempt to resolve URI '" + uri + "' failed: ", te);
  388. }
  389. }
  390. return source;
  391. }
  392. /**
  393. * Sets the output File.
  394. * @param f the output File
  395. */
  396. public void setOutputFile(File f) {
  397. this.outputFile = f;
  398. }
  399. /**
  400. * Gets the output File.
  401. * @return the output File
  402. */
  403. public File getOutputFile() {
  404. return outputFile;
  405. }
  406. /**
  407. * Returns the conversion factor from pixel units to millimeters. This
  408. * depends on the desired reolution.
  409. * @return float conversion factor
  410. */
  411. public float getPixelUnitToMillimeter() {
  412. return this.px2mm;
  413. }
  414. /**
  415. * Sets the resolution in dpi.
  416. * @param dpi resolution in dpi
  417. */
  418. public void setResolution(int dpi) {
  419. this.px2mm = (float)(25.4 / dpi);
  420. }
  421. /**
  422. * Gets the default page-height to use as fallback,
  423. * in case page-height="auto"
  424. *
  425. * @return the page-height, as a String
  426. */
  427. public String getPageHeight() {
  428. return this.pageHeight;
  429. }
  430. /**
  431. * Sets the page-height to use as fallback, in case
  432. * page-height="auto"
  433. *
  434. * @param pageHeight page-height as a String
  435. */
  436. public void setPageHeight(String pageHeight) {
  437. this.pageHeight = pageHeight;
  438. }
  439. /**
  440. * Gets the default page-width to use as fallback,
  441. * in case page-width="auto"
  442. *
  443. * @return the page-width, as a String
  444. */
  445. public String getPageWidth() {
  446. return this.pageWidth;
  447. }
  448. /**
  449. * Sets the page-width to use as fallback, in case
  450. * page-width="auto"
  451. *
  452. * @param pageWidth page-width as a String
  453. */
  454. public void setPageWidth(String pageWidth) {
  455. this.pageWidth = pageWidth;
  456. }
  457. /**
  458. * If to create hot links to footnotes and before floats.
  459. * @return True if hot links should be created
  460. */
  461. public boolean linkToFootnotes() {
  462. return true;
  463. }
  464. /**
  465. * @return the XML handler registry
  466. */
  467. public XMLHandlerRegistry getXMLHandlerRegistry() {
  468. return this.xmlHandlers;
  469. }
  470. }