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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.fo;
  8. import org.apache.fop.render.XMLHandler;
  9. import org.apache.fop.render.RendererContext;
  10. import org.apache.avalon.framework.logger.Logger;
  11. import org.w3c.dom.*;
  12. import java.util.HashMap;
  13. /**
  14. * The User Agent for fo.
  15. * This user agent is used by the processing to obtain user configurable
  16. * options.
  17. *
  18. * Renderer specific extensions (that do not produce normal areas on
  19. * the output) will be done like so:
  20. * The extension will create an area, custom if necessary
  21. * this area will be added to the user agent with a key
  22. * the renderer will know keys for particular extensions
  23. * eg. bookmarks will be held in a special hierarchical area representing
  24. * the title and bookmark structure
  25. * These areas may contain resolveable areas that will be processed
  26. * with other resolveable areas
  27. */
  28. public class FOUserAgent {
  29. HashMap defaults = new HashMap();
  30. HashMap handlers = new HashMap();
  31. Logger log;
  32. String base;
  33. public void setLogger(Logger logger) {
  34. log = logger;
  35. }
  36. public Logger getLogger() {
  37. return log;
  38. }
  39. public void setBaseURL(String b) {
  40. base = b;
  41. }
  42. public String getBaseURL() {
  43. return base;
  44. }
  45. public float getPixelToMM() {
  46. return 0.35277777777777777778f;
  47. }
  48. /**
  49. * If to create hot links to footnotes and before floats.
  50. */
  51. public boolean linkToFootnotes() {
  52. return true;
  53. }
  54. /**
  55. * Set the default xml handler for the given mime type.
  56. */
  57. public void setDefaultXMLHandler(String mime, XMLHandler handler) {
  58. defaults.put(mime, handler);
  59. }
  60. /**
  61. * Add an xml handler for the given mime type and xml namespace.
  62. */
  63. public void addXMLHandler(String mime, String ns, XMLHandler handler) {
  64. HashMap mh = (HashMap) handlers.get(mime);
  65. if (mh == null) {
  66. mh = new HashMap();
  67. handlers.put(mime, mh);
  68. }
  69. mh.put(ns, handler);
  70. }
  71. /**
  72. * Render the xml document with the given xml namespace.
  73. * The Render Context is by the handle to render into the current
  74. * rendering target.
  75. */
  76. public void renderXML(RendererContext ctx, Document doc,
  77. String namespace) {
  78. String mime = ctx.getMimeType();
  79. HashMap mh = (HashMap) handlers.get(mime);
  80. XMLHandler handler = null;
  81. if (mh != null) {
  82. handler = (XMLHandler) mh.get(namespace);
  83. }
  84. if (handler == null) {
  85. handler = (XMLHandler) defaults.get(mime);
  86. }
  87. if (handler != null) {
  88. try {
  89. handler.handleXML(ctx, doc, namespace);
  90. } catch (Throwable t) {
  91. // could not handle document
  92. log.error("Could not render XML", t);
  93. }
  94. } else {
  95. // no handler found for document
  96. log.debug("No handler defined for XML: " + namespace);
  97. }
  98. }
  99. }