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.

AbstractFOPTranscoder.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.svg;
  18. import org.xml.sax.EntityResolver;
  19. import org.apache.avalon.framework.logger.ConsoleLogger;
  20. import org.apache.avalon.framework.logger.LogEnabled;
  21. import org.apache.avalon.framework.logger.Logger;
  22. import org.apache.batik.bridge.UserAgent;
  23. import org.apache.batik.dom.svg.SVGDOMImplementation;
  24. import org.apache.batik.dom.util.DocumentFactory;
  25. import org.apache.batik.transcoder.ErrorHandler;
  26. import org.apache.batik.transcoder.TranscoderException;
  27. import org.apache.batik.transcoder.TranscodingHints;
  28. import org.apache.batik.transcoder.SVGAbstractTranscoder;
  29. import org.apache.batik.transcoder.image.ImageTranscoder;
  30. import org.apache.batik.transcoder.keys.BooleanKey;
  31. import org.apache.batik.util.SVGConstants;
  32. import org.w3c.dom.DOMImplementation;
  33. /**
  34. * This is the common base class of all of FOP's transcoders.
  35. */
  36. public abstract class AbstractFOPTranscoder extends SVGAbstractTranscoder
  37. implements LogEnabled {
  38. /**
  39. * The key to specify whether to stroke text instead of using text
  40. * operations.
  41. */
  42. public static final TranscodingHints.Key KEY_STROKE_TEXT = new BooleanKey();
  43. /** The value to turn on text stroking. */
  44. public static final Boolean VALUE_FORMAT_ON = Boolean.TRUE;
  45. /** The value to turn off text stroking. */
  46. public static final Boolean VALUE_FORMAT_OFF = Boolean.FALSE;
  47. /**
  48. * The user agent dedicated to this Transcoder.
  49. */
  50. protected UserAgent userAgent = createUserAgent();
  51. private Logger logger;
  52. private EntityResolver resolver;
  53. /**
  54. * Constructs a new FOP-style transcoder.
  55. */
  56. public AbstractFOPTranscoder() {
  57. hints.put(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
  58. SVGConstants.SVG_NAMESPACE_URI);
  59. hints.put(KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG);
  60. hints.put(KEY_DOM_IMPLEMENTATION,
  61. SVGDOMImplementation.getDOMImplementation());
  62. }
  63. /**
  64. * Creates and returns the default user agent for this transcoder. Override
  65. * this method if you need non-default behaviour.
  66. * @return UserAgent the newly created user agent
  67. */
  68. protected UserAgent createUserAgent() {
  69. return new FOPTranscoderUserAgent();
  70. }
  71. /**
  72. * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(Logger)
  73. */
  74. public void enableLogging(Logger logger) {
  75. this.logger = logger;
  76. }
  77. /**
  78. * Sets the EntityResolver that should be used when building SVG documents.
  79. * @param resolver the resolver
  80. */
  81. public void setEntityResolver(EntityResolver resolver) {
  82. this.resolver = resolver;
  83. }
  84. /**
  85. * Returns the logger associated with this transcoder. It returns a
  86. * ConsoleLogger if no logger has been explicitly set.
  87. * @return Logger the logger for the transcoder.
  88. */
  89. protected final Logger getLogger() {
  90. if (this.logger == null) {
  91. this.logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
  92. }
  93. return this.logger;
  94. }
  95. /**
  96. * Creates a <tt>DocumentFactory</tt> that is used to create an SVG DOM
  97. * tree. The specified DOM Implementation is ignored and the Batik
  98. * SVG DOM Implementation is automatically used.
  99. *
  100. * @param domImpl the DOM Implementation (not used)
  101. * @param parserClassname the XML parser classname
  102. * @return the document factory
  103. */
  104. protected DocumentFactory createDocumentFactory(DOMImplementation domImpl,
  105. String parserClassname) {
  106. final FOPSAXSVGDocumentFactory factory
  107. = new FOPSAXSVGDocumentFactory(parserClassname);
  108. if (this.resolver != null) {
  109. factory.setAdditionalEntityResolver(this.resolver);
  110. }
  111. return factory;
  112. }
  113. // --------------------------------------------------------------------
  114. // FOP's default error handler (for transcoders)
  115. // --------------------------------------------------------------------
  116. /**
  117. * This is the default transcoder error handler for FOP. It logs error
  118. * to an Avalon Logger instead of to System.out. The remaining behaviour
  119. * is the same as Batik's DefaultErrorHandler.
  120. */
  121. protected class FOPErrorHandler implements ErrorHandler {
  122. /**
  123. * @see org.apache.batik.transcoder.ErrorHandler#error(TranscoderException)
  124. */
  125. public void error(TranscoderException te)
  126. throws TranscoderException {
  127. getLogger().error(te.getMessage());
  128. }
  129. /**
  130. * @see org.apache.batik.transcoder.ErrorHandler#fatalError(TranscoderException)
  131. */
  132. public void fatalError(TranscoderException te)
  133. throws TranscoderException {
  134. throw te;
  135. }
  136. /**
  137. * @see org.apache.batik.transcoder.ErrorHandler#warning(TranscoderException)
  138. */
  139. public void warning(TranscoderException te)
  140. throws TranscoderException {
  141. getLogger().warn(te.getMessage());
  142. }
  143. }
  144. // --------------------------------------------------------------------
  145. // UserAgent implementation
  146. // --------------------------------------------------------------------
  147. /**
  148. * A user agent implementation for FOP's Transcoders.
  149. */
  150. protected class FOPTranscoderUserAgent extends SVGAbstractTranscoderUserAgent {
  151. /**
  152. * Displays the specified error message using the <tt>ErrorHandler</tt>.
  153. * @param message the message to display
  154. */
  155. public void displayError(String message) {
  156. try {
  157. getErrorHandler().error(new TranscoderException(message));
  158. } catch (TranscoderException ex) {
  159. throw new RuntimeException();
  160. }
  161. }
  162. /**
  163. * Displays the specified error using the <tt>ErrorHandler</tt>.
  164. * @param e the exception to display
  165. */
  166. public void displayError(Exception e) {
  167. try {
  168. getErrorHandler().error(new TranscoderException(e));
  169. } catch (TranscoderException ex) {
  170. throw new RuntimeException();
  171. }
  172. }
  173. /**
  174. * Displays the specified message using the <tt>ErrorHandler</tt>.
  175. * @param message the message to display
  176. */
  177. public void displayMessage(String message) {
  178. getLogger().info(message);
  179. }
  180. /**
  181. * Returns the pixel to millimeter conversion factor specified in the
  182. * <tt>TranscodingHints</tt> or 0.3528 if any.
  183. * @return the pixel unit to millimeter factor
  184. */
  185. public float getPixelUnitToMillimeter() {
  186. Object key = ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER;
  187. if (getTranscodingHints().containsKey(key)) {
  188. return ((Float)getTranscodingHints().get(key)).floatValue();
  189. } else {
  190. // return 0.3528f; // 72 dpi
  191. return 0.26458333333333333333333333333333f; // 96dpi
  192. }
  193. }
  194. /**
  195. * Get the media for this transcoder. Which is always print.
  196. * @return PDF media is "print"
  197. */
  198. public String getMedia() {
  199. return "print";
  200. }
  201. }
  202. }