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.2KB

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