KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > SVGAbstractTranscoder


1 /*
2
3    Copyright 2002-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.batik.transcoder;
19
20 import java.awt.Dimension JavaDoc;
21 import java.awt.geom.AffineTransform JavaDoc;
22 import java.awt.geom.Dimension2D JavaDoc;
23 import java.awt.geom.Rectangle2D JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.batik.bridge.BaseScriptingEnvironment;
31 import org.apache.batik.bridge.BridgeContext;
32 import org.apache.batik.bridge.BridgeException;
33 import org.apache.batik.bridge.DefaultScriptSecurity;
34 import org.apache.batik.bridge.GVTBuilder;
35 import org.apache.batik.bridge.NoLoadScriptSecurity;
36 import org.apache.batik.bridge.RelaxedScriptSecurity;
37 import org.apache.batik.bridge.ScriptSecurity;
38 import org.apache.batik.bridge.UserAgent;
39 import org.apache.batik.bridge.UserAgentAdapter;
40 import org.apache.batik.bridge.ViewBox;
41 import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
42 import org.apache.batik.dom.svg.SVGDOMImplementation;
43 import org.apache.batik.dom.svg.SVGOMDocument;
44 import org.apache.batik.dom.util.DocumentFactory;
45 import org.apache.batik.dom.util.DOMUtilities;
46 import org.apache.batik.gvt.CanvasGraphicsNode;
47 import org.apache.batik.gvt.CompositeGraphicsNode;
48 import org.apache.batik.gvt.GraphicsNode;
49 import org.apache.batik.transcoder.keys.BooleanKey;
50 import org.apache.batik.transcoder.keys.FloatKey;
51 import org.apache.batik.transcoder.keys.LengthKey;
52 import org.apache.batik.transcoder.keys.Rectangle2DKey;
53 import org.apache.batik.transcoder.keys.StringKey;
54 import org.apache.batik.util.ParsedURL;
55 import org.apache.batik.util.SVGConstants;
56 import org.w3c.dom.DOMImplementation JavaDoc;
57 import org.w3c.dom.Document JavaDoc;
58 import org.w3c.dom.svg.SVGSVGElement;
59
60
61 /**
62  * This class may be the base class of all transcoders which take an
63  * SVG document as input and which need to build a DOM tree. The
64  * <tt>SVGAbstractTranscoder</tt> uses several different hints that
65  * guide it's behaviour:<br/>
66  *
67  * <ul>
68  * <li><tt>KEY_WIDTH, KEY_HEIGHT</tt> can be used to specify how to scale the
69  * SVG image</li>
70  * </ul>
71  *
72  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
73  * @version $Id: SVGAbstractTranscoder.java,v 1.22 2004/11/18 01:47:02 deweese Exp $ */

74 public abstract class SVGAbstractTranscoder extends XMLAbstractTranscoder {
75     /**
76      * Value used as a default for the default font-family hint
77      */

78     public static final String JavaDoc DEFAULT_DEFAULT_FONT_FAMILY
79         = "Arial, Helvetica, sans-serif";
80
81     /**
82      * Current area of interest.
83      */

84     protected Rectangle2D JavaDoc curAOI;
85
86     /**
87      * Transform needed to render the current area of interest
88      */

89     protected AffineTransform JavaDoc curTxf;
90
91     /**
92      * Current GVT Tree, i.e., the GVT tree representing the page
93      * being printed currently.
94      */

95     protected GraphicsNode root;
96
97     /**
98      * Current bridge context
99      */

100     protected BridgeContext ctx;
101
102     /**
103      * Current gvt builder
104      */

105     protected GVTBuilder builder;
106
107     /**
108      * Image's width and height (init to 400x400).
109      */

110     protected float width=400, height=400;
111
112     /** The user agent dedicated to an SVG Transcoder. */
113     protected UserAgent userAgent;
114
115     protected SVGAbstractTranscoder() {
116         userAgent = createUserAgent();
117
118         hints.put(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
119                   SVGConstants.SVG_NAMESPACE_URI);
120         hints.put(KEY_DOCUMENT_ELEMENT,
121                   SVGConstants.SVG_SVG_TAG);
122         hints.put(KEY_DOM_IMPLEMENTATION,
123                   SVGDOMImplementation.getDOMImplementation());
124         hints.put(KEY_MEDIA,
125                   "screen");
126         hints.put(KEY_DEFAULT_FONT_FAMILY,
127                   DEFAULT_DEFAULT_FONT_FAMILY);
128         hints.put(KEY_EXECUTE_ONLOAD,
129                   Boolean.FALSE);
130         hints.put(KEY_ALLOWED_SCRIPT_TYPES,
131                   DEFAULT_ALLOWED_SCRIPT_TYPES);
132     }
133
134
135     protected UserAgent createUserAgent() {
136         return new SVGAbstractTranscoderUserAgent();
137     }
138
139     /**
140      * Creates a <tt>DocumentFactory</tt> that is used to create an SVG DOM
141      * tree. The specified DOM Implementation is ignored and the Batik
142      * SVG DOM Implementation is automatically used.
143      *
144      * @param domImpl the DOM Implementation (not used)
145      * @param parserClassname the XML parser classname
146      */

147     protected DocumentFactory createDocumentFactory(DOMImplementation domImpl,
148                                                     String JavaDoc parserClassname) {
149         return new SAXSVGDocumentFactory(parserClassname);
150     }
151
152     public void transcode(TranscoderInput input, TranscoderOutput output)
153             throws TranscoderException {
154  
155         super.transcode(input, output);
156
157         if (ctx != null)
158             ctx.dispose();
159     }
160     /**
161      * Transcodes the specified Document as an image in the specified output.
162      *
163      * @param document the document to transcode
164      * @param uri the uri of the document or null if any
165      * @param output the ouput where to transcode
166      * @exception TranscoderException if an error occured while transcoding
167      */

168     protected void transcode(Document document,
169                              String JavaDoc uri,
170                              TranscoderOutput output)
171             throws TranscoderException {
172
173         if ((document != null) &&
174             !(document.getImplementation() instanceof SVGDOMImplementation)) {
175             DOMImplementation impl;
176             impl = (DOMImplementation)hints.get(KEY_DOM_IMPLEMENTATION);
177             // impl = SVGDOMImplementation.getDOMImplementation();
178
document = DOMUtilities.deepCloneDocument(document, impl);
179             if (uri != null) {
180                 try {
181                     URL JavaDoc url = new URL JavaDoc(uri);
182                     ((SVGOMDocument)document).setURLObject(url);
183                 } catch (MalformedURLException JavaDoc mue) {
184                 }
185             }
186         }
187
188         ctx = createBridgeContext();
189         SVGOMDocument svgDoc = (SVGOMDocument)document;
190         SVGSVGElement root = svgDoc.getRootElement();
191
192         // build the GVT tree
193
builder = new GVTBuilder();
194         // flag that indicates if the document is dynamic
195
boolean isDynamic =
196             (hints.containsKey(KEY_EXECUTE_ONLOAD) &&
197              ((Boolean JavaDoc)hints.get(KEY_EXECUTE_ONLOAD)).booleanValue() &&
198              ctx.isDynamicDocument(svgDoc));
199
200         GraphicsNode gvtRoot;
201         try {
202             if (isDynamic)
203                 ctx.setDynamicState(BridgeContext.DYNAMIC);
204
205             gvtRoot = builder.build(ctx, svgDoc);
206
207             // dispatch an 'onload' event if needed
208
if (ctx.isDynamic()) {
209                 BaseScriptingEnvironment se;
210                 se = new BaseScriptingEnvironment(ctx);
211                 se.loadScripts();
212                 se.dispatchSVGLoadEvent();
213             }
214         } catch (BridgeException ex) {
215             throw new TranscoderException(ex);
216         }
217
218         // get the 'width' and 'height' attributes of the SVG document
219
float docWidth = (float)ctx.getDocumentSize().getWidth();
220         float docHeight = (float)ctx.getDocumentSize().getHeight();
221
222         setImageSize(docWidth, docHeight);
223
224         // compute the preserveAspectRatio matrix
225
AffineTransform JavaDoc Px;
226
227         // take the AOI into account if any
228
if (hints.containsKey(KEY_AOI)) {
229             Rectangle2D JavaDoc aoi = (Rectangle2D JavaDoc)hints.get(KEY_AOI);
230             // transform the AOI into the image's coordinate system
231
Px = new AffineTransform JavaDoc();
232             double sx = width / aoi.getWidth();
233             double sy = height / aoi.getHeight();
234             double scale = Math.min(sx,sy);
235             Px.scale(scale, scale);
236             double tx = -aoi.getX() + (width/scale - aoi.getWidth())/2;
237             double ty = -aoi.getY() + (height/scale -aoi.getHeight())/2;;
238             Px.translate(tx, ty);
239             // take the AOI transformation matrix into account
240
// we apply first the preserveAspectRatio matrix
241
curAOI = aoi;
242         } else {
243             String JavaDoc ref = new ParsedURL(uri).getRef();
244
245             try {
246                 Px = ViewBox.getViewTransform(ref, root, width, height);
247             } catch (BridgeException ex) {
248                 throw new TranscoderException(ex);
249             }
250
251             if (Px.isIdentity() &&
252                 (width != docWidth || height != docHeight)) {
253                 // The document has no viewBox, we need to resize it by hand.
254
// we want to keep the document size ratio
255
float xscale, yscale;
256                 xscale = width/docWidth;
257                 yscale = height/docHeight;
258                 float scale = Math.min(xscale,yscale);
259                 Px = AffineTransform.getScaleInstance(scale, scale);
260             }
261
262             curAOI = new Rectangle2D.Float JavaDoc(0, 0, width, height);
263         }
264
265         CanvasGraphicsNode cgn = getCanvasGraphicsNode(gvtRoot);
266         if (cgn != null) {
267             cgn.setViewingTransform(Px);
268             curTxf = new AffineTransform JavaDoc();
269         } else {
270             curTxf = Px;
271         }
272
273         this.root = gvtRoot;
274     }
275
276     protected CanvasGraphicsNode getCanvasGraphicsNode(GraphicsNode gn) {
277         if (!(gn instanceof CompositeGraphicsNode))
278             return null;
279         CompositeGraphicsNode cgn = (CompositeGraphicsNode)gn;
280         List JavaDoc children = cgn.getChildren();
281         if (children.size() == 0)
282             return null;
283         gn = (GraphicsNode)children.get(0);
284         if (!(gn instanceof CanvasGraphicsNode))
285             return null;
286         return (CanvasGraphicsNode)gn;
287     }
288
289     /**
290      * Factory method for constructing an configuring a
291      * BridgeContext so subclasses can insert new/modified
292      * bridges in the context.
293      */

294     protected BridgeContext createBridgeContext() {
295         return new BridgeContext(userAgent);
296     }
297
298     /**
299      * Sets document size according to the hints.
300      * Global variables width and height are modified.
301      *
302      * @param docWidth Width of the document.
303      * @param docHeight Height of the document.
304      */

305     protected void setImageSize(float docWidth, float docHeight) {
306
307         // Compute the image's width and height according the hints
308
float imgWidth = -1;
309         if (hints.containsKey(KEY_WIDTH)) {
310             imgWidth = ((Float JavaDoc)hints.get(KEY_WIDTH)).floatValue();
311         }
312         float imgHeight = -1;
313         if (hints.containsKey(KEY_HEIGHT)) {
314             imgHeight = ((Float JavaDoc)hints.get(KEY_HEIGHT)).floatValue();
315         }
316
317         if (imgWidth > 0 && imgHeight > 0) {
318             width = imgWidth;
319             height = imgHeight;
320         } else if (imgHeight > 0) {
321             width = (docWidth * imgHeight) / docHeight;
322             height = imgHeight;
323         } else if (imgWidth > 0) {
324             width = imgWidth;
325             height = (docHeight * imgWidth) / docWidth;
326         } else {
327             width = docWidth;
328             height = docHeight;
329         }
330
331         // Limit image size according to the maximuxm size hints.
332
float imgMaxWidth = -1;
333         if (hints.containsKey(KEY_MAX_WIDTH)) {
334             imgMaxWidth = ((Float JavaDoc)hints.get(KEY_MAX_WIDTH)).floatValue();
335         }
336         float imgMaxHeight = -1;
337         if (hints.containsKey(KEY_MAX_HEIGHT)) {
338             imgMaxHeight = ((Float JavaDoc)hints.get(KEY_MAX_HEIGHT)).floatValue();
339         }
340
341         if ((imgMaxHeight > 0) && (height > imgMaxHeight)) {
342             width = (docWidth * imgMaxHeight) / docHeight;
343             height = imgMaxHeight;
344         }
345         if ((imgMaxWidth > 0) && (width > imgMaxWidth)) {
346             width = imgMaxWidth;
347             height = (docHeight * imgMaxWidth) / docWidth;
348         }
349     }
350
351
352     // --------------------------------------------------------------------
353
// Keys definition
354
// --------------------------------------------------------------------
355

356     /**
357      * The image width key.
358      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
359      * <TR>
360      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
361      * <TD VALIGN="TOP">KEY_WIDTH</TD></TR>
362      * <TR>
363      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
364      * <TD VALIGN="TOP">Float</TD></TR>
365      * <TR>
366      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
367      * <TD VALIGN="TOP">The width of the top most svg element</TD></TR>
368      * <TR>
369      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
370      * <TD VALIGN="TOP">No</TD></TR>
371      * <TR>
372      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
373      * <TD VALIGN="TOP">Specify the width of the image to create.</TD></TR>
374      * </TABLE> */

375     public static final TranscodingHints.Key KEY_WIDTH
376         = new LengthKey();
377
378     /**
379      * The image height key.
380      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
381      * <TR>
382      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
383      * <TD VALIGN="TOP">KEY_HEIGHT</TD></TR>
384      * <TR>
385      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
386      * <TD VALIGN="TOP">Float</TD></TR>
387      * <TR>
388      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
389      * <TD VALIGN="TOP">The height of the top most svg element</TD></TR>
390      * <TR>
391      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
392      * <TD VALIGN="TOP">No</TD></TR>
393      * <TR>
394      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
395      * <TD VALIGN="TOP">Specify the height of the image to create.</TD></TR>
396      * </TABLE> */

397     public static final TranscodingHints.Key KEY_HEIGHT
398         = new LengthKey();
399
400     /**
401      * The maximum width of the image key.
402      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
403      * <TR>
404      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
405      * <TD VALIGN="TOP">KEY_MAX_WIDTH</TD></TR>
406      * <TR>
407      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
408      * <TD VALIGN="TOP">Float</TD></TR>
409      * <TR>
410      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
411      * <TD VALIGN="TOP">The width of the top most svg element</TD></TR>
412      * <TR>
413      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
414      * <TD VALIGN="TOP">No</TD></TR>
415      * <TR>
416      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
417      * <TD VALIGN="TOP">Specify the maximum width of the image to create.
418      * The value will set the maximum width of the image even when
419      * bigger width is specified in a document or set with KEY_WIDTH.
420      * </TD></TR>
421      * </TABLE>
422      */

423     public static final TranscodingHints.Key KEY_MAX_WIDTH
424         = new LengthKey();
425
426     /**
427      * The maximux height of the image key.
428      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
429      * <TR>
430      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
431      * <TD VALIGN="TOP">KEY_MAX_HEIGHT</TD></TR>
432      * <TR>
433      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
434      * <TD VALIGN="TOP">Float</TD></TR>
435      * <TR>
436      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
437      * <TD VALIGN="TOP">The height of the top most svg element</TD></TR>
438      * <TR>
439      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
440      * <TD VALIGN="TOP">No</TD></TR>
441      * <TR>
442      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
443      * <TD VALIGN="TOP">Specify the maximum height of the image to create.
444      * The value will set the maximum height of the image even when
445      * bigger height is specified in a document or set with KEY_HEIGHT.
446      * </TD></TR>
447      * </TABLE>
448      */

449     public static final TranscodingHints.Key KEY_MAX_HEIGHT
450         = new LengthKey();
451
452     /**
453      * The area of interest key.
454      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
455      * <TR>
456      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
457      * <TD VALIGN="TOP">KEY_AOI</TD></TR>
458      * <TR>
459      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
460      * <TD VALIGN="TOP">Rectangle2D</TD></TR>
461      * <TR>
462      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
463      * <TD VALIGN="TOP">The document's size</TD></TR>
464      * <TR>
465      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
466      * <TD VALIGN="TOP">No</TD></TR>
467      * <TR>
468      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
469      * <TD VALIGN="TOP">Specify the area of interest to render. The
470      * rectangle coordinates must be specified in pixels and in the
471      * document coordinates system.</TD></TR>
472      * </TABLE>
473      */

474     public static final TranscodingHints.Key KEY_AOI
475         = new Rectangle2DKey();
476
477     /**
478      * The language key.
479      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
480      * <TR>
481      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
482      * <TD VALIGN="TOP">KEY_LANGUAGE</TD></TR>
483      * <TR>
484      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
485      * <TD VALIGN="TOP">String</TD></TR>
486      * <TR>
487      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
488      * <TD VALIGN="TOP">"en"</TD></TR>
489      * <TR>
490      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
491      * <TD VALIGN="TOP">No</TD></TR>
492      * <TR>
493      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
494      * <TD VALIGN="TOP">Specify the preferred language of the document.
495      * </TD></TR>
496      * </TABLE>
497      */

498     public static final TranscodingHints.Key KEY_LANGUAGE
499         = new StringKey();
500
501     /**
502      * The media key.
503      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
504      * <TR>
505      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
506      * <TD VALIGN="TOP">KEY_MEDIA</TD></TR>
507      * <TR>
508      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
509      * <TD VALIGN="TOP">String</TD></TR>
510      * <TR>
511      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
512      * <TD VALIGN="TOP">"screen"</TD></TR>
513      * <TR>
514      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
515      * <TD VALIGN="TOP">No</TD></TR>
516      * <TR>
517      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
518      * <TD VALIGN="TOP">Specify the media to use with CSS.
519      * </TD></TR>
520      * </TABLE>
521      */

522     public static final TranscodingHints.Key KEY_MEDIA
523         = new StringKey();
524
525     /**
526      * The default font-family key.
527      *
528      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
529      * <TR>
530      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
531      * <TD VALIGN="TOP">KEY_DEFAULT_FONT_FAMILY</TD></TR>
532      * <TR>
533      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
534      * <TD VALIGN="TOP">String</TD></TR>
535      * <TR>
536      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
537      * <TD VALIGN="TOP">"Arial, Helvetica, sans-serif"</TD></TR>
538      * <TR>
539      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
540      * <TD VALIGN="TOP">No</TD></TR>
541      * <TR>
542      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
543      * <TD VALIGN="TOP">Controls the default
544      * value used by the CSS engine for the font-family property
545      * when that property is unspecified.Specify the media to use with CSS.
546      * </TD></TR>
547      * </TABLE>
548      */

549     public static final TranscodingHints.Key KEY_DEFAULT_FONT_FAMILY
550         = new StringKey();
551
552     /**
553      * The alternate stylesheet key.
554      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
555      * <TR>
556      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
557      * <TD VALIGN="TOP">KEY_ALTERNATE_STYLESHEET</TD></TR>
558      * <TR>
559      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
560      * <TD VALIGN="TOP">String</TD></TR>
561      * <TR>
562      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
563      * <TD VALIGN="TOP">null</TD></TR>
564      * <TR>
565      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
566      * <TD VALIGN="TOP">No</TD></TR>
567      * <TR>
568      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
569      * <TD VALIGN="TOP">Specify the alternate style sheet title.
570      * </TD></TR>
571      * </TABLE>
572      */

573     public static final TranscodingHints.Key KEY_ALTERNATE_STYLESHEET
574         = new StringKey();
575
576     /**
577      * The user stylesheet URI key.
578      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
579      * <TR>
580      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
581      * <TD VALIGN="TOP">KEY_USER_STYLESHEET_URI</TD></TR>
582      * <TR>
583      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
584      * <TD VALIGN="TOP">String</TD></TR>
585      * <TR>
586      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
587      * <TD VALIGN="TOP">null</TD></TR>
588      * <TR>
589      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
590      * <TD VALIGN="TOP">No</TD></TR>
591      * <TR>
592      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
593      * <TD VALIGN="TOP">Specify the user style sheet.</TD></TR>
594      * </TABLE>
595      */

596     public static final TranscodingHints.Key KEY_USER_STYLESHEET_URI
597         = new StringKey();
598
599     /**
600      * The number of millimeters in each pixel key.
601      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
602      * <TR>
603      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
604      * <TD VALIGN="TOP">KEY_PIXEL_UNIT_TO_MILLIMETER</TD></TR>
605      * <TR>
606      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
607      * <TD VALIGN="TOP">Float</TD></TR>
608      * <TR>
609      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
610      * <TD VALIGN="TOP">0.264583</TD></TR>
611      * <TR>
612      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
613      * <TD VALIGN="TOP">No</TD></TR>
614      * <TR>
615      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
616      * <TD VALIGN="TOP">Specify the size of a px CSS unit in millimeters.
617      * </TD></TR>
618      * </TABLE>
619      */

620     public static final TranscodingHints.Key KEY_PIXEL_UNIT_TO_MILLIMETER
621         = new FloatKey();
622
623     /**
624      * The pixel to millimeter conversion factor key.
625      * @deprecated As of Batik Version 1.5b3
626      * @see #KEY_PIXEL_UNIT_TO_MILLIMETER
627      *
628      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
629      * <TR>
630      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
631      * <TD VALIGN="TOP">KEY_PIXEL_TO_MM</TD></TR>
632      * <TR>
633      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
634      * <TD VALIGN="TOP">Float</TD></TR>
635      * <TR>
636      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
637      * <TD VALIGN="TOP">0.264583</TD></TR>
638      * <TR>
639      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
640      * <TD VALIGN="TOP">No</TD></TR>
641      * <TR>
642      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
643      * <TD VALIGN="TOP">Specify the size of a px CSS unit in millimeters.
644      * </TD></TR>
645      * </TABLE>
646      */

647     public static final TranscodingHints.Key KEY_PIXEL_TO_MM
648         = KEY_PIXEL_UNIT_TO_MILLIMETER;
649
650     /**
651      * The 'onload' execution key.
652      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
653      * <TR>
654      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
655      * <TD VALIGN="TOP">KEY_EXECUTE_ONLOAD</TD></TR>
656      * <TR>
657      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
658      * <TD VALIGN="TOP">Boolean</TD></TR>
659      * <TR>
660      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
661      * <TD VALIGN="TOP">false</TD></TR>
662      * <TR>
663      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
664      * <TD VALIGN="TOP">No</TD></TR>
665      * <TR>
666      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
667      * <TD VALIGN="TOP">Specify if scripts added on the 'onload' event
668      * attribute must be invoked.</TD></TR>
669      * </TABLE>
670      */

671     public static final TranscodingHints.Key KEY_EXECUTE_ONLOAD
672         = new BooleanKey();
673
674     /**
675      * The set of supported script languages (i.e., the set of possible
676      * values for the &lt;script&gt; tag's type attribute).
677      *
678      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
679      * <TR>
680      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
681      * <TD VALIGN="TOP">KEY_ALLOWED_SCRIPT_TYPES</TD></TR>
682      * <TR>
683      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
684      * <TD VALIGN="TOP">String (Comma separated values)</TD></TR>
685      * <TR>
686      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
687      * <TD VALIGN="TOP">text/ecmascript, application/java-archive</TD></TR>
688      * <TR>
689      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
690      * <TD VALIGN="TOP">No</TD></TR>
691      * <TR>
692      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
693      * <TD VALIGN="TOP">Specifies the allowed values for the type attribute
694      * in the &lt;script&gt; element. This is a comma separated list. The
695      * special value '*' means that all script types are allowed.
696      * </TD></TR>
697      * </TABLE>
698      */

699     public static final TranscodingHints.Key KEY_ALLOWED_SCRIPT_TYPES
700         = new StringKey();
701
702     /**
703      * Default value for the KEY_ALLOWED_SCRIPT_TYPES key
704      */

705     public static final String JavaDoc DEFAULT_ALLOWED_SCRIPT_TYPES
706         = SVGConstants.SVG_SCRIPT_TYPE_ECMASCRIPT + ", "
707         + SVGConstants.SVG_SCRIPT_TYPE_JAVA;
708
709     /**
710      * Controls whether or not scripts can only be loaded from the
711      * same location as the document which references them.
712      *
713      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
714      * <TR>
715      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
716      * <TD VALIGN="TOP">KEY_CONSTRAIN_SCRIPT_ORIGIN</TD></TR>
717      * <TR>
718      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
719      * <TD VALIGN="TOP">boolean</TD></TR>
720      * <TR>
721      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
722      * <TD VALIGN="TOP">true</TD></TR>
723      * <TR>
724      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
725      * <TD VALIGN="TOP">No</TD></TR>
726      * <TR>
727      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
728      * <TD VALIGN="TOP">When set to true, script elements referencing
729      * files from a different origin (server) than the document containing
730      * the script element will not be loaded. When set to true, script elements
731      * may reference script files from any origin.
732      * </TD></TR>
733      * </TABLE>
734      */

735     public static final TranscodingHints.Key KEY_CONSTRAIN_SCRIPT_ORIGIN
736         = new BooleanKey();
737
738
739     /**
740      * A user agent implementation for <tt>PrintTranscoder</tt>.
741      */

742     protected class SVGAbstractTranscoderUserAgent extends UserAgentAdapter {
743         /**
744          * Vector containing the allowed script types
745          */

746         protected List JavaDoc scripts;
747
748         public SVGAbstractTranscoderUserAgent() {
749             addStdFeatures();
750         }
751
752         /**
753          * Return the rendering transform.
754          */

755         public AffineTransform JavaDoc getTransform() {
756             return SVGAbstractTranscoder.this.curTxf;
757         }
758
759         /**
760          * Return the rendering transform.
761          */

762         public void setTransform(AffineTransform JavaDoc at) {
763             SVGAbstractTranscoder.this.curTxf = at;
764         }
765
766         /**
767          * Returns the default size of this user agent (400x400).
768          */

769         public Dimension2D JavaDoc getViewportSize() {
770             return new Dimension JavaDoc((int)SVGAbstractTranscoder.this.width,
771                                  (int)SVGAbstractTranscoder.this.height);
772         }
773
774         /**
775          * Displays the specified error message using the <tt>ErrorHandler</tt>.
776          */

777         public void displayError(String JavaDoc message) {
778             try {
779                 SVGAbstractTranscoder.this.handler.error
780                     (new TranscoderException(message));
781             } catch (TranscoderException ex) {
782                 throw new RuntimeException JavaDoc();
783             }
784         }
785
786         /**
787          * Displays the specified error using the <tt>ErrorHandler</tt>.
788          */

789         public void displayError(Exception JavaDoc e) {
790             try {
791                 e.printStackTrace();
792                 SVGAbstractTranscoder.this.handler.error
793                     (new TranscoderException(e));
794             } catch (TranscoderException ex) {
795                 throw new RuntimeException JavaDoc();
796             }
797         }
798
799         /**
800          * Displays the specified message using the <tt>ErrorHandler</tt>.
801          */

802         public void displayMessage(String JavaDoc message) {
803             try {
804                 SVGAbstractTranscoder.this.handler.warning
805                     (new TranscoderException(message));
806             } catch (TranscoderException ex) {
807                 throw new RuntimeException JavaDoc();
808             }
809         }
810
811         /**
812          * Returns the pixel to millimeter conversion factor specified in the
813          * <tt>TranscodingHints</tt> or 0.26458333 if not specified.
814          */

815         public float getPixelUnitToMillimeter() {
816             Object JavaDoc obj = SVGAbstractTranscoder.this.hints.get
817                 (KEY_PIXEL_UNIT_TO_MILLIMETER);
818             if (obj != null) {
819                 return ((Float JavaDoc)obj).floatValue();
820             }
821
822             return super.getPixelUnitToMillimeter();
823         }
824
825         /**
826          * Returns the user language specified in the
827          * <tt>TranscodingHints</tt> or "en" (english) if any.
828          */

829         public String JavaDoc getLanguages() {
830             if (SVGAbstractTranscoder.this.hints.containsKey(KEY_LANGUAGE)) {
831                 return (String JavaDoc)SVGAbstractTranscoder.this.hints.get
832                     (KEY_LANGUAGE);
833             }
834              
835             return super.getLanguages();
836         }
837
838         /**
839          * Returns this user agent's CSS media.
840          */

841         public String JavaDoc getMedia() {
842             String JavaDoc s = (String JavaDoc)hints.get(KEY_MEDIA);
843             if (s != null) return s;
844
845             return super.getMedia();
846         }
847
848         /**
849          * Returns the default font family.
850          */

851         public String JavaDoc getDefaultFontFamily() {
852             String JavaDoc s = (String JavaDoc)hints.get(KEY_DEFAULT_FONT_FAMILY);
853             if (s != null) return s;
854
855             return super.getDefaultFontFamily();
856         }
857
858         /**
859          * Returns this user agent's alternate style-sheet title.
860          */

861         public String JavaDoc getAlternateStyleSheet() {
862             String JavaDoc s = (String JavaDoc)hints.get(KEY_ALTERNATE_STYLESHEET);
863             if (s != null)
864                 return s;
865
866             return super.getAlternateStyleSheet();
867         }
868
869         /**
870          * Returns the user stylesheet specified in the
871          * <tt>TranscodingHints</tt> or null if any.
872          */

873         public String JavaDoc getUserStyleSheetURI() {
874             String JavaDoc s = (String JavaDoc)SVGAbstractTranscoder.this.hints.get
875                 (KEY_USER_STYLESHEET_URI);
876             if (s != null)
877                 return s;
878
879             return super.getUserStyleSheetURI();
880         }
881
882         /**
883          * Returns the XML parser to use from the TranscodingHints.
884          */

885         public String JavaDoc getXMLParserClassName() {
886             String JavaDoc s = (String JavaDoc)SVGAbstractTranscoder.this.hints.get
887                 (KEY_XML_PARSER_CLASSNAME);
888             if (s != null)
889                 return s;
890
891             return super.getXMLParserClassName();
892         }
893
894         /**
895          * Returns true if the XML parser must be in validation mode, false
896          * otherwise.
897          */

898         public boolean isXMLParserValidating() {
899             Boolean JavaDoc b = (Boolean JavaDoc)SVGAbstractTranscoder.this.hints.get
900                 (KEY_XML_PARSER_VALIDATING);
901             if (b != null)
902                 return b.booleanValue();
903
904             return super.isXMLParserValidating();
905         }
906
907         /**
908          * Returns the security settings for the given script
909          * type, script url and document url
910          *
911          * @param scriptType type of script, as found in the
912          * type attribute of the &lt;script&gt; element.
913          * @param scriptPURL url for the script, as defined in
914          * the script's xlink:href attribute. If that
915          * attribute was empty, then this parameter should
916          * be null
917          * @param docPURL url for the document into which the
918          * script was found.
919          */

920         public ScriptSecurity getScriptSecurity(String JavaDoc scriptType,
921                                                 ParsedURL scriptPURL,
922                                                 ParsedURL docPURL){
923             if (scripts == null){
924                 computeAllowedScripts();
925             }
926
927             if (!scripts.contains(scriptType)) {
928                 return new NoLoadScriptSecurity(scriptType);
929             }
930
931
932             boolean constrainOrigin = true;
933
934             if (SVGAbstractTranscoder.this.hints.containsKey
935                 (KEY_CONSTRAIN_SCRIPT_ORIGIN)) {
936                 constrainOrigin =
937                     ((Boolean JavaDoc)SVGAbstractTranscoder.this.hints.get
938                      (KEY_CONSTRAIN_SCRIPT_ORIGIN)).booleanValue();
939             }
940
941             if (constrainOrigin) {
942                 return new DefaultScriptSecurity
943                     (scriptType,scriptPURL,docPURL);
944             } else {
945                 return new RelaxedScriptSecurity
946                     (scriptType,scriptPURL,docPURL);
947             }
948         }
949
950         /**
951          * Helper method. Builds a Vector containing the allowed
952          * values for the &lt;script&gt; element's type attribute.
953          */

954         protected void computeAllowedScripts(){
955             scripts = new LinkedList JavaDoc();
956             if (!SVGAbstractTranscoder.this.hints.containsKey
957                 (KEY_ALLOWED_SCRIPT_TYPES)) {
958                 return;
959             }
960
961             String JavaDoc allowedScripts
962                 = (String JavaDoc)SVGAbstractTranscoder.this.hints.get
963                 (KEY_ALLOWED_SCRIPT_TYPES);
964                 
965             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(allowedScripts, ",");
966             while (st.hasMoreTokens()) {
967                 scripts.add(st.nextToken());
968             }
969         }
970
971     }
972 }
973
Popular Tags