KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > svg > AbstractFOPTranscoder


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: AbstractFOPTranscoder.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.svg;
21
22 import org.xml.sax.EntityResolver JavaDoc;
23
24 import org.apache.commons.logging.impl.SimpleLog;
25 import org.apache.commons.logging.Log;
26 import org.apache.batik.bridge.UserAgent;
27 import org.apache.batik.dom.svg.SVGDOMImplementation;
28 import org.apache.batik.dom.util.DocumentFactory;
29 import org.apache.batik.transcoder.ErrorHandler;
30 import org.apache.batik.transcoder.TranscoderException;
31 import org.apache.batik.transcoder.TranscodingHints;
32 import org.apache.batik.transcoder.SVGAbstractTranscoder;
33 import org.apache.batik.transcoder.image.ImageTranscoder;
34 import org.apache.batik.transcoder.keys.BooleanKey;
35 import org.apache.batik.util.SVGConstants;
36 import org.w3c.dom.DOMImplementation JavaDoc;
37
38 /**
39  * This is the common base class of all of FOP's transcoders.
40  */

41 public abstract class AbstractFOPTranscoder extends SVGAbstractTranscoder
42             {
43
44     /**
45      * The key to specify whether to stroke text instead of using text
46      * operations.
47      */

48     public static final TranscodingHints.Key KEY_STROKE_TEXT = new BooleanKey();
49
50     /** The value to turn on text stroking. */
51     public static final Boolean JavaDoc VALUE_FORMAT_ON = Boolean.TRUE;
52
53     /** The value to turn off text stroking. */
54     public static final Boolean JavaDoc VALUE_FORMAT_OFF = Boolean.FALSE;
55
56     /**
57      * The user agent dedicated to this Transcoder.
58      */

59     protected UserAgent userAgent = createUserAgent();
60
61     private Log logger;
62     private EntityResolver JavaDoc resolver;
63
64     /**
65      * Constructs a new FOP-style transcoder.
66      */

67     public AbstractFOPTranscoder() {
68         hints.put(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
69                   SVGConstants.SVG_NAMESPACE_URI);
70         hints.put(KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG);
71         hints.put(KEY_DOM_IMPLEMENTATION,
72                   SVGDOMImplementation.getDOMImplementation());
73     }
74     
75     /**
76      * Creates and returns the default user agent for this transcoder. Override
77      * this method if you need non-default behaviour.
78      * @return UserAgent the newly created user agent
79      */

80     protected UserAgent createUserAgent() {
81         return new FOPTranscoderUserAgent();
82     }
83     
84     public void setLogger(Log logger) {
85         this.logger = logger;
86     }
87
88     /**
89      * Sets the EntityResolver that should be used when building SVG documents.
90      * @param resolver the resolver
91      */

92     public void setEntityResolver(EntityResolver JavaDoc resolver) {
93         this.resolver = resolver;
94     }
95     
96     /**
97      * Returns the logger associated with this transcoder. It returns a
98      * SimpleLog if no logger has been explicitly set.
99      * @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(DOMImplementation domImpl,
119             String JavaDoc parserClassname) {
120         final FOPSAXSVGDocumentFactory factory
121                 = new FOPSAXSVGDocumentFactory(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 SVGAbstractTranscoderUserAgent {
173
174         /**
175          * Displays the specified error message using the <tt>ErrorHandler</tt>.
176          * @param message the message to display
177          */

178         public void displayError(String JavaDoc message) {
179             try {
180                 getErrorHandler().error(new TranscoderException(message));
181             } catch (TranscoderException ex) {
182                 throw new RuntimeException JavaDoc();
183             }
184         }
185
186         /**
187          * Displays the specified error using the <tt>ErrorHandler</tt>.
188          * @param e the exception to display
189          */

190         public void displayError(Exception JavaDoc e) {
191             try {
192                 getErrorHandler().error(new TranscoderException(e));
193             } catch (TranscoderException ex) {
194                 throw new RuntimeException JavaDoc();
195             }
196         }
197
198         /**
199          * Displays the specified message using the <tt>ErrorHandler</tt>.
200          * @param message the message to display
201          */

202         public void displayMessage(String JavaDoc message) {
203             getLogger().info(message);
204         }
205
206         /**
207          * Returns the pixel to millimeter conversion factor specified in the
208          * <tt>TranscodingHints</tt> or 0.3528 if any.
209          * @return the pixel unit to millimeter factor
210          */

211         public float getPixelUnitToMillimeter() {
212             Object JavaDoc key = ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER;
213             if (getTranscodingHints().containsKey(key)) {
214                 return ((Float JavaDoc)getTranscodingHints().get(key)).floatValue();
215             } else {
216                 // return 0.3528f; // 72 dpi
217
return 25.4f / 96; //96dpi = 0.2645833333333333333f;
218
}
219         }
220
221         /**
222          * Get the media for this transcoder. Which is always print.
223          * @return PDF media is "print"
224          */

225         public String JavaDoc getMedia() {
226             return "print";
227         }
228
229     }
230     
231 }
232
Popular Tags