001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: AbstractFOPTranscoder.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.svg;
021:
022: import org.xml.sax.EntityResolver;
023:
024: import org.apache.commons.logging.impl.SimpleLog;
025: import org.apache.commons.logging.Log;
026: import org.apache.batik.bridge.UserAgent;
027: import org.apache.batik.dom.svg.SVGDOMImplementation;
028: import org.apache.batik.dom.util.DocumentFactory;
029: import org.apache.batik.transcoder.ErrorHandler;
030: import org.apache.batik.transcoder.TranscoderException;
031: import org.apache.batik.transcoder.TranscodingHints;
032: import org.apache.batik.transcoder.SVGAbstractTranscoder;
033: import org.apache.batik.transcoder.image.ImageTranscoder;
034: import org.apache.batik.transcoder.keys.BooleanKey;
035: import org.apache.batik.util.SVGConstants;
036: import org.w3c.dom.DOMImplementation;
037:
038: /**
039: * This is the common base class of all of FOP's transcoders.
040: */
041: public abstract class AbstractFOPTranscoder extends
042: SVGAbstractTranscoder {
043:
044: /**
045: * The key to specify whether to stroke text instead of using text
046: * operations.
047: */
048: public static final TranscodingHints.Key KEY_STROKE_TEXT = new BooleanKey();
049:
050: /** The value to turn on text stroking. */
051: public static final Boolean VALUE_FORMAT_ON = Boolean.TRUE;
052:
053: /** The value to turn off text stroking. */
054: public static final Boolean VALUE_FORMAT_OFF = Boolean.FALSE;
055:
056: /**
057: * The user agent dedicated to this Transcoder.
058: */
059: protected UserAgent userAgent = createUserAgent();
060:
061: private Log logger;
062: private EntityResolver resolver;
063:
064: /**
065: * Constructs a new FOP-style transcoder.
066: */
067: public AbstractFOPTranscoder() {
068: hints.put(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
069: SVGConstants.SVG_NAMESPACE_URI);
070: hints.put(KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG);
071: hints.put(KEY_DOM_IMPLEMENTATION, SVGDOMImplementation
072: .getDOMImplementation());
073: }
074:
075: /**
076: * Creates and returns the default user agent for this transcoder. Override
077: * this method if you need non-default behaviour.
078: * @return UserAgent the newly created user agent
079: */
080: protected UserAgent createUserAgent() {
081: return new FOPTranscoderUserAgent();
082: }
083:
084: public void setLogger(Log logger) {
085: this .logger = logger;
086: }
087:
088: /**
089: * Sets the EntityResolver that should be used when building SVG documents.
090: * @param resolver the resolver
091: */
092: public void setEntityResolver(EntityResolver resolver) {
093: this .resolver = resolver;
094: }
095:
096: /**
097: * Returns the logger associated with this transcoder. It returns a
098: * SimpleLog if no logger has been explicitly set.
099: * @return Logger the logger for the transcoder.
100: */
101: protected final Log getLogger() {
102: if (this .logger == null) {
103: this .logger = new SimpleLog("FOP/Transcoder");
104: ((SimpleLog) logger).setLevel(SimpleLog.LOG_LEVEL_INFO);
105: }
106: return this .logger;
107: }
108:
109: /**
110: * Creates a <tt>DocumentFactory</tt> that is used to create an SVG DOM
111: * tree. The specified DOM Implementation is ignored and the Batik
112: * SVG DOM Implementation is automatically used.
113: *
114: * @param domImpl the DOM Implementation (not used)
115: * @param parserClassname the XML parser classname
116: * @return the document factory
117: */
118: protected DocumentFactory createDocumentFactory(
119: DOMImplementation domImpl, String parserClassname) {
120: final FOPSAXSVGDocumentFactory factory = new FOPSAXSVGDocumentFactory(
121: parserClassname);
122: if (this .resolver != null) {
123: factory.setAdditionalEntityResolver(this .resolver);
124: }
125: return factory;
126: }
127:
128: // --------------------------------------------------------------------
129: // FOP's default error handler (for transcoders)
130: // --------------------------------------------------------------------
131:
132: /**
133: * This is the default transcoder error handler for FOP. It logs error
134: * to an Commons Logger instead of to System.out. The remaining behaviour
135: * is the same as Batik's DefaultErrorHandler.
136: */
137: protected class FOPErrorHandler implements ErrorHandler {
138:
139: /**
140: * @see org.apache.batik.transcoder.ErrorHandler#error(TranscoderException)
141: */
142: public void error(TranscoderException te)
143: throws TranscoderException {
144: getLogger().error(te.getMessage());
145: }
146:
147: /**
148: * @see org.apache.batik.transcoder.ErrorHandler#fatalError(TranscoderException)
149: */
150: public void fatalError(TranscoderException te)
151: throws TranscoderException {
152: throw te;
153: }
154:
155: /**
156: * @see org.apache.batik.transcoder.ErrorHandler#warning(TranscoderException)
157: */
158: public void warning(TranscoderException te)
159: throws TranscoderException {
160: getLogger().warn(te.getMessage());
161: }
162:
163: }
164:
165: // --------------------------------------------------------------------
166: // UserAgent implementation
167: // --------------------------------------------------------------------
168:
169: /**
170: * A user agent implementation for FOP's Transcoders.
171: */
172: protected class FOPTranscoderUserAgent extends
173: SVGAbstractTranscoderUserAgent {
174:
175: /**
176: * Displays the specified error message using the <tt>ErrorHandler</tt>.
177: * @param message the message to display
178: */
179: public void displayError(String message) {
180: try {
181: getErrorHandler().error(
182: new TranscoderException(message));
183: } catch (TranscoderException ex) {
184: throw new RuntimeException();
185: }
186: }
187:
188: /**
189: * Displays the specified error using the <tt>ErrorHandler</tt>.
190: * @param e the exception to display
191: */
192: public void displayError(Exception e) {
193: try {
194: getErrorHandler().error(new TranscoderException(e));
195: } catch (TranscoderException ex) {
196: throw new RuntimeException();
197: }
198: }
199:
200: /**
201: * Displays the specified message using the <tt>ErrorHandler</tt>.
202: * @param message the message to display
203: */
204: public void displayMessage(String message) {
205: getLogger().info(message);
206: }
207:
208: /**
209: * Returns the pixel to millimeter conversion factor specified in the
210: * <tt>TranscodingHints</tt> or 0.3528 if any.
211: * @return the pixel unit to millimeter factor
212: */
213: public float getPixelUnitToMillimeter() {
214: Object key = ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER;
215: if (getTranscodingHints().containsKey(key)) {
216: return ((Float) getTranscodingHints().get(key))
217: .floatValue();
218: } else {
219: // return 0.3528f; // 72 dpi
220: return 25.4f / 96; //96dpi = 0.2645833333333333333f;
221: }
222: }
223:
224: /**
225: * Get the media for this transcoder. Which is always print.
226: * @return PDF media is "print"
227: */
228: public String getMedia() {
229: return "print";
230: }
231:
232: }
233:
234: }
|