KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > visualization > SDGenerator


1 package org.antlr.works.visualization;
2
3 import org.antlr.works.ate.syntax.misc.ATEToken;
4 import org.antlr.works.grammar.EngineGrammar;
5 import org.antlr.works.prefs.AWPrefs;
6 import org.antlr.works.syntax.GrammarSyntaxEngine;
7 import org.antlr.works.visualization.graphics.*;
8 import org.antlr.works.visualization.graphics.graph.GGraph;
9 import org.antlr.works.visualization.skin.syntaxdiagram.SDSkin;
10 import org.antlr.xjlib.foundation.XJUtils;
11
12 import javax.imageio.ImageIO JavaDoc;
13 import java.awt.*;
14 import java.awt.image.BufferedImage JavaDoc;
15 import java.io.File JavaDoc;
16
17 /*
18
19 [The "BSD licence"]
20 Copyright (c) 2005-2007 Jean Bovet
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions
25 are met:
26
27 1. Redistributions of source code must retain the above copyright
28 notice, this list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright
30 notice, this list of conditions and the following disclaimer in the
31 documentation and/or other materials provided with the distribution.
32 3. The name of the author may not be used to endorse or promote products
33 derived from this software without specific prior written permission.
34
35 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
36 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
38 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
39 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
40 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
44 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
46 */

47
48 public class SDGenerator implements GContextProvider {
49
50     public EngineGrammar engineGrammar;
51     public GContext context;
52
53     public SDGenerator(EngineGrammar engineGrammar) {
54         this.engineGrammar = engineGrammar;
55
56         context = new GContext();
57         context.setSkin(new SDSkin());
58         context.setProvider(this);
59     }
60
61     public void renderRuleToEPSFile(String JavaDoc ruleName, String JavaDoc file) throws Exception JavaDoc {
62         GGraph graph = createGraph(ruleName);
63         GEnginePS engine = new GEnginePS();
64         context.setEngine(engine);
65         graph.draw();
66         XJUtils.writeStringToFile(engine.getPSText(), file);
67     }
68
69     public void renderRuleToBitmapFile(String JavaDoc ruleName, String JavaDoc imageFormat, String JavaDoc file) throws Exception JavaDoc {
70         GGraph graph = createGraph(ruleName);
71
72         int width = (int)(graph.getWidth()+1);
73         int height = (int)(graph.getHeight()+1);
74
75         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_3BYTE_BGR);
76         Graphics2D g2d = (Graphics2D)image.getGraphics();
77
78         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
79         g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
80
81         context.setEngine(new GEngineGraphics());
82         context.setGraphics2D(g2d);
83
84         g2d.setColor(Color.white);
85         g2d.fillRect(0, 0, width, height);
86         graph.draw();
87         g2d.dispose();
88
89         ImageIO.write(image, imageFormat, new File JavaDoc(file));
90     }
91
92     private GGraph createGraph(String JavaDoc ruleName) throws Exception JavaDoc {
93         GGraph graph = new GFactory().buildGraphsForRule(engineGrammar, ruleName);
94         graph.setContext(context);
95         graph.render(0,0);
96         return graph;
97     }
98
99     public Color contextGetColorForLabel(String JavaDoc label) {
100         if(label.charAt(0) == '\'' || label.charAt(0) == '"')
101             return AWPrefs.getSyntaxColor(AWPrefs.PREF_SYNTAX_STRING);
102         else {
103             if(ATEToken.isLexerName(label))
104                 return GrammarSyntaxEngine.COLOR_LEXER;
105             else
106                 return GrammarSyntaxEngine.COLOR_PARSER;
107         }
108     }
109     
110 }
111
112
113
Popular Tags