選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FOUserAgent.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * $Id: FOUserAgent.java,v 1.15 2003/03/05 21:48:02 jeremias Exp $
  3. * ============================================================================
  4. * The Apache Software License, Version 1.1
  5. * ============================================================================
  6. *
  7. * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modifica-
  10. * tion, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if any, must
  20. * include the following acknowledgment: "This product includes software
  21. * developed by the Apache Software Foundation (http://www.apache.org/)."
  22. * Alternately, this acknowledgment may appear in the software itself, if
  23. * and wherever such third-party acknowledgments normally appear.
  24. *
  25. * 4. The names "FOP" and "Apache Software Foundation" must not be used to
  26. * endorse or promote products derived from this software without prior
  27. * written permission. For written permission, please contact
  28. * apache@apache.org.
  29. *
  30. * 5. Products derived from this software may not be called "Apache", nor may
  31. * "Apache" appear in their name, without prior written permission of the
  32. * Apache Software Foundation.
  33. *
  34. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  35. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  36. * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  37. * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  38. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  39. * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  40. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  43. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. * ============================================================================
  45. *
  46. * This software consists of voluntary contributions made by many individuals
  47. * on behalf of the Apache Software Foundation and was originally created by
  48. * James Tauber <jtauber@jtauber.com>. For more information on the Apache
  49. * Software Foundation, please see <http://www.apache.org/>.
  50. */
  51. package org.apache.fop.fo;
  52. // Java
  53. import java.util.Map;
  54. import java.io.IOException;
  55. import java.io.InputStream;
  56. // XML
  57. import org.w3c.dom.Document;
  58. // Avalon
  59. import org.apache.avalon.framework.logger.LogEnabled;
  60. import org.apache.avalon.framework.logger.Logger;
  61. // FOP
  62. import org.apache.fop.render.XMLHandler;
  63. import org.apache.fop.render.RendererContext;
  64. /**
  65. * The User Agent for fo.
  66. * This user agent is used by the processing to obtain user configurable
  67. * options.
  68. * <p>
  69. * Renderer specific extensions (that do not produce normal areas on
  70. * the output) will be done like so:
  71. * <br>
  72. * The extension will create an area, custom if necessary
  73. * <br>
  74. * this area will be added to the user agent with a key
  75. * <br>
  76. * the renderer will know keys for particular extensions
  77. * <br>
  78. * eg. bookmarks will be held in a special hierarchical area representing
  79. * the title and bookmark structure
  80. * <br>
  81. * These areas may contain resolveable areas that will be processed
  82. * with other resolveable areas
  83. */
  84. public class FOUserAgent implements LogEnabled {
  85. private Logger log;
  86. private Map defaults = new java.util.HashMap();
  87. private Map handlers = new java.util.HashMap();
  88. private String baseURL;
  89. /**
  90. * Sets the logger.
  91. * @param log Logger to use
  92. * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(Logger)
  93. */
  94. public void enableLogging(Logger log) {
  95. this.log = log;
  96. }
  97. /**
  98. * Returns the logger to use.
  99. * @see org.apache.avalon.framework.logger.AbstractLogEnabled#getLogger()
  100. * @todo This breaks IoC/SoC. Should be improved.
  101. */
  102. public Logger getLogger() {
  103. return this.log;
  104. }
  105. /**
  106. * Sets the base URL.
  107. * @param baseURL base URL
  108. */
  109. public void setBaseURL(String baseURL) {
  110. this.baseURL = baseURL;
  111. }
  112. /**
  113. * Returns the base URL.
  114. * @return the base URL
  115. */
  116. public String getBaseURL() {
  117. if ((this.baseURL == null) || (this.baseURL.trim().equals(""))) {
  118. return "file:.";
  119. } else {
  120. return this.baseURL;
  121. }
  122. }
  123. /**
  124. * Get an input stream for a reference.
  125. * Temporary solution until API better.
  126. * @param uri URI to access
  127. * @return InputStream for accessing the resource.
  128. * @throws IOException in case of an I/O problem
  129. */
  130. public InputStream getStream(String uri) throws IOException {
  131. return null;
  132. }
  133. /**
  134. * Returns the conversion factor from pixel units to millimeters. This
  135. * depends on the desired reolution.
  136. * @return float conversion factor
  137. */
  138. public float getPixelUnitToMillimeter() {
  139. return 0.35277777777777777778f;
  140. }
  141. /**
  142. * If to create hot links to footnotes and before floats.
  143. * @return True if hot links dhould be created
  144. */
  145. public boolean linkToFootnotes() {
  146. return true;
  147. }
  148. /**
  149. * Set the default xml handler for the given mime type.
  150. * @param mime MIME type
  151. * @param handler XMLHandler to use
  152. */
  153. public void setDefaultXMLHandler(String mime, XMLHandler handler) {
  154. defaults.put(mime, handler);
  155. }
  156. /**
  157. * Add an xml handler for the given mime type and xml namespace.
  158. * @param mime MIME type
  159. * @param ns Namespace URI
  160. * @param handler XMLHandler to use
  161. */
  162. public void addXMLHandler(String mime, String ns, XMLHandler handler) {
  163. Map mh = (Map) handlers.get(mime);
  164. if (mh == null) {
  165. mh = new java.util.HashMap();
  166. handlers.put(mime, mh);
  167. }
  168. mh.put(ns, handler);
  169. }
  170. /**
  171. * Render the xml document with the given xml namespace.
  172. * The Render Context is by the handle to render into the current
  173. * rendering target.
  174. * @param ctx rendering context
  175. * @param doc DOM Document containing the source document
  176. * @param namespace Namespace URI of the document
  177. */
  178. public void renderXML(RendererContext ctx, Document doc,
  179. String namespace) {
  180. String mime = ctx.getMimeType();
  181. Map mh = (Map) handlers.get(mime);
  182. XMLHandler handler = null;
  183. if (mh != null) {
  184. handler = (XMLHandler) mh.get(namespace);
  185. }
  186. if (handler == null) {
  187. handler = (XMLHandler) defaults.get(mime);
  188. }
  189. if (handler != null) {
  190. try {
  191. handler.handleXML(ctx, doc, namespace);
  192. } catch (Throwable t) {
  193. // could not handle document
  194. getLogger().error("Some XML content will be ignored. "
  195. + "Could not render XML", t);
  196. }
  197. } else {
  198. // no handler found for document
  199. getLogger().warn("Some XML content will be ignored. "
  200. + "No handler defined for XML: " + namespace);
  201. }
  202. }
  203. }