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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.log.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. public void setLogger(Logger logger) {
  33. log = logger;
  34. }
  35. public Logger getLogger() {
  36. return log;
  37. }
  38. /**
  39. * Set the default xml handler for the given mime type.
  40. */
  41. public void setDefaultXMLHandler(String mime, XMLHandler handler) {
  42. defaults.put(mime, handler);
  43. }
  44. /**
  45. * Add an xml handler for the given mime type and xml namespace.
  46. */
  47. public void addXMLHandler(String mime, String ns, XMLHandler handler) {
  48. HashMap mh = (HashMap) handlers.get(mime);
  49. if (mh == null) {
  50. mh = new HashMap();
  51. handlers.put(mime, mh);
  52. }
  53. mh.put(ns, handler);
  54. }
  55. /**
  56. * Render the xml document with the given xml namespace.
  57. * The Render Context is by the handle to render into the current
  58. * rendering target.
  59. */
  60. public void renderXML(RendererContext ctx, Document doc,
  61. String namespace) {
  62. String mime = ctx.getMimeType();
  63. HashMap mh = (HashMap) handlers.get(mime);
  64. XMLHandler handler = null;
  65. if (mh != null) {
  66. handler = (XMLHandler) mh.get(namespace);
  67. }
  68. if (handler == null) {
  69. handler = (XMLHandler) defaults.get(mime);
  70. }
  71. if (handler != null) {
  72. try {
  73. handler.handleXML(ctx, doc, namespace);
  74. } catch (Throwable t) {
  75. // could not handle document
  76. ctx.getLogger().error("Could not render XML", t);
  77. }
  78. } else {
  79. // no handler found for document
  80. }
  81. }
  82. }