KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > GeneratorContext


1 /*
2
3    Copyright 2001,2003 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.svggen;
19
20 import java.awt.Font JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.w3c.dom.CDATASection JavaDoc;
26 import org.w3c.dom.DOMImplementation JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29
30 import org.apache.batik.dom.svg.SVGDOMImplementation;
31 import org.apache.batik.svggen.SVGGeneratorContext.GraphicContextDefaults;
32 import org.apache.batik.util.SVGConstants;
33
34 /**
35  * Testing customization of the SVGGeneratorContext and generation of
36  * SVG Fonts.
37  *
38  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
39  * @version $Id: GeneratorContext.java,v 1.9 2004/08/18 07:16:44 vhardy Exp $
40  */

41 public class GeneratorContext extends SVGAccuracyTest implements SVGConstants {
42     public static class TestIDGenerator extends SVGIDGenerator {
43         public String JavaDoc generateID(String JavaDoc prefix) {
44             return "test"+super.generateID(prefix);
45         }
46     }
47
48     public static class TestStyleHandler extends DefaultStyleHandler {
49         private CDATASection JavaDoc styleSheet;
50         public TestStyleHandler(CDATASection JavaDoc styleSheet) {
51             this.styleSheet = styleSheet;
52         }
53         public void setStyle(Element JavaDoc element, Map JavaDoc styleMap,
54                              SVGGeneratorContext generatorContext) {
55             Iterator JavaDoc iter = styleMap.keySet().iterator();
56             // create a new class id in the style sheet
57
String JavaDoc id = generatorContext.getIDGenerator().generateID("C");
58             styleSheet.appendData("."+id+" {");
59             // append each key/value pairs
60
while (iter.hasNext()) {
61                 String JavaDoc key = (String JavaDoc)iter.next();
62                 String JavaDoc value = (String JavaDoc)styleMap.get(key);
63                 styleSheet.appendData(key+":"+value+";");
64             }
65             styleSheet.appendData("}\n");
66             // reference the class id of the style sheet on the element to be styled
67
element.setAttribute("class", id);
68         }
69     }
70
71     private Element JavaDoc topLevelGroup = null;
72
73     public GeneratorContext(Painter painter,
74                             URL JavaDoc refURL) {
75         super(painter, refURL);
76     }
77
78     protected SVGGraphics2D buildSVGGraphics2D(){
79         // Use Batik's DOM implementation to create a Document
80
DOMImplementation JavaDoc impl = SVGDOMImplementation.getDOMImplementation();
81         String JavaDoc namespaceURI = SVGDOMImplementation.SVG_NAMESPACE_URI;
82         Document JavaDoc domFactory = impl.createDocument(namespaceURI, SVG_SVG_TAG, null);
83
84         // Create a default context from our Document instance
85
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory);
86         
87         // Set ID generator
88
ctx.setIDGenerator(new TestIDGenerator());
89
90         // Extension Handler to be done
91

92         // Image Handler to be done
93
GenericImageHandler ihandler = new CachedImageHandlerBase64Encoder();
94         ctx.setGenericImageHandler(ihandler);
95
96         // Set Style handler
97
CDATASection JavaDoc styleSheet = domFactory.createCDATASection("");
98         ctx.setStyleHandler(new TestStyleHandler(styleSheet));
99
100         // Set the generator comment
101
ctx.setComment("Generated by the Batik Test Framework. Test:\u00e9j");
102
103         // Turn SVG Font embedding on.
104
ctx.setEmbeddedFontsOn(true);
105
106         // Set the default font to use
107
GraphicContextDefaults defaults
108             = new GraphicContextDefaults();
109         defaults.font = new Font JavaDoc("Arial", Font.PLAIN, 12);
110         ctx.setGraphicContextDefaults(defaults);
111
112         //
113
// Build SVGGraphics2D with our customized context
114
//
115
SVGGraphics2D g2d = new SVGGraphics2D(ctx, false);
116
117         // Append our stylesheet to the top level group.
118
topLevelGroup = g2d.getTopLevelGroup();
119         Element JavaDoc style = domFactory.createElementNS(SVG_NAMESPACE_URI, SVG_STYLE_TAG);
120         style.setAttributeNS(null, SVG_TYPE_ATTRIBUTE, "text/css");
121         style.appendChild(styleSheet);
122         topLevelGroup.appendChild(style);
123
124         return g2d;
125     }
126
127     protected void configureSVGGraphics2D(SVGGraphics2D g2d) {
128         topLevelGroup.appendChild(g2d.getTopLevelGroup());
129         g2d.setTopLevelGroup(topLevelGroup);
130     }
131 }
132
133
Popular Tags