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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.w3c.dom.*;
  11. import java.util.HashMap;
  12. /**
  13. * The User Agent for fo.
  14. * This user agent is used by the processing to obtain user configurable
  15. * options.
  16. *
  17. * Renderer specific extensions (that do not produce normal areas on
  18. * the output) will be done like so:
  19. * The extension will create an area, custom if necessary
  20. * this area will be added to the user agent with a key
  21. * the renderer will know keys for particular extensions
  22. * eg. bookmarks will be held in a special hierarchical area representing
  23. * the title and bookmark structure
  24. * These areas may contain resolveable areas that will be processed
  25. * with other resolveable areas
  26. */
  27. public class FOUserAgent {
  28. HashMap defaults = new HashMap();
  29. HashMap handlers = new HashMap();
  30. /**
  31. * Set the default xml handler for the given mime type.
  32. */
  33. public void setDefaultXMLHandler(String mime, XMLHandler handler) {
  34. defaults.put(mime, handler);
  35. }
  36. /**
  37. * Add an xml handler for the given mime type and xml namespace.
  38. */
  39. public void addXMLHandler(String mime, String ns, XMLHandler handler) {
  40. HashMap mh = (HashMap) handlers.get(mime);
  41. if (mh == null) {
  42. mh = new HashMap();
  43. handlers.put(mime, mh);
  44. }
  45. mh.put(ns, handler);
  46. }
  47. /**
  48. * Render the xml document with the given xml namespace.
  49. * The Render Context is by the handle to render into the current
  50. * rendering target.
  51. */
  52. public void renderXML(RendererContext ctx, Document doc,
  53. String namespace) {
  54. String mime = ctx.getMimeType();
  55. HashMap mh = (HashMap) handlers.get(mime);
  56. XMLHandler handler = null;
  57. if (mh != null) {
  58. handler = (XMLHandler) mh.get(namespace);
  59. }
  60. if (handler == null) {
  61. handler = (XMLHandler) defaults.get(mime);
  62. }
  63. if (handler != null) {
  64. try {
  65. handler.handleXML(ctx, doc, namespace);
  66. } catch (Throwable t) {
  67. // could not handle document
  68. //t.printStackTrace();
  69. }
  70. } else {
  71. // no handler found for document
  72. }
  73. }
  74. }