KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > utils > GDOTImporterDOT


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

41
42 public class GDOTImporterDOT extends GDOTImporter {
43
44     public static final int INCH_TO_PIXEL = 72;
45
46     /** Example of DOT output:
47      digraph {
48      node [label="\N"];
49      graph [bb="0,0,229,324"];
50      compilationUnit [pos="162,306", width="1.64", height="0.50"];
51      packageDefinition [pos="70,234", width="1.78", height="0.50"];
52      identifier [pos="83,162", width="1.06", height="0.50"];
53      IDENT [pos="144,90", width="0.94", height="0.50"];
54      DOT [pos="65,90", width="0.75", height="0.50"];
55      A [pos="65,18", width="0.75", height="0.50"];
56      compilationUnit -> packageDefinition [pos="e,92,251 140,289 128,280 113,268 100,258"];
57      packageDefinition -> identifier [pos="e,80,180 73,216 74,208 76,199 78,190"];
58      identifier -> IDENT [pos="e,126,105 92,144 99,134 109,123 119,112"];
59      identifier -> DOT [pos="e,69,108 78,144 76,136 74,127 72,118"];
60      identifier -> IDENT [pos="e,135,108 102,146 111,137 120,126 129,116"];
61      IDENT -> compilationUnit [pos="e,160,288 145,108 149,146 156,233 159,278"];
62      DOT -> A [pos="e,65,36 65,72 65,64 65,55 65,46"];
63      A -> compilationUnit [pos="e,171,288 92,21 121,26 166,40 187,72 229,135 196,232 175,279"];
64      A -> packageDefinition [pos="e,56,216 51,34 43,44 33,58 29,72 15,118 20,134 36,180 39,190 45,199 50,208"];
65      }
66      */

67
68     public GElement parseLine(String JavaDoc line) throws IOException JavaDoc {
69         String JavaDoc[] tokens = parseTokens(line);
70         if(tokens[0].equals("graph"))
71             parseGraphHeight(tokens);
72         else if(tokens.length >= 2 && !tokens[0].equals("node") && tokens[1].equals("["))
73             return createGraphNode(tokens);
74         else if(tokens.length >= 3 && tokens[1].equals("-") && tokens[2].equals(">"))
75             return createGraphEdge(tokens);
76
77         return null;
78     }
79
80     public void parseGraphHeight(String JavaDoc[] tokens) throws IOException JavaDoc {
81         // graph [bb="0,0,229,324"];
82
if(tokens[2].equals("bb")) {
83             String JavaDoc[] t = parseTokens(tokens[4]);
84             height = Float.parseFloat(t[6]);
85         }
86     }
87
88     public GElement createGraphNode(String JavaDoc[] tokens) throws IOException JavaDoc {
89         // identifier [pos="83,162", width="1.06", height="0.50"];
90
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
91
// "s2=>2" [shape=doublecircle, pos="35,35", width="0.97", height="0.97"];
92

93         float x = 0;
94         float y = 0;
95         float w = 0;
96         float h = 0;
97         boolean doublecircle = false;
98
99         int index = 0;
100         while(index < tokens.length-1) {
101             index++;
102             if(tokens[index].equals("pos")) {
103                 String JavaDoc[] posTokens = parseTokens(tokens[index+=2]);
104                 x = Float.parseFloat(posTokens[0]);
105                 y = (height - Float.parseFloat(posTokens[2]));
106             } else if(tokens[index].equals("width")) {
107                 w = Float.parseFloat(tokens[index+=2])*INCH_TO_PIXEL;
108             } else if(tokens[index].equals("height")) {
109                 h = Float.parseFloat(tokens[index+=2])*INCH_TO_PIXEL;
110             } else if(tokens[index].equals("shape")) {
111                 doublecircle = tokens[index+=2].equals("doublecircle");
112             }
113         }
114
115         Node node = new Node();
116         node.setDraggable(true);
117         node.setPosition(x, y);
118         node.setSize(w, h);
119         node.setRadius(w/2);
120         node.setLabel(tokens[0]);
121         node.setDouble(doublecircle);
122
123         return node;
124     }
125
126     public GElement createGraphEdge(String JavaDoc[] tokens) throws IOException JavaDoc {
127         // DOT -> foo_bar [pos="e,65,36 65,72 65,64 65,55 65,46"];
128
// 0 12 3 4 5 6 7 8 9
129
// DOT -> A [label=foo, pos="e,153,33 119,88 124,78 129,65 136,54 139,49 142,45 146,40", lp="146,62"];
130
// 0 12 3 4 5 6 7 8 9 10 11
131

132         String JavaDoc sourceName = tokens[0];
133         String JavaDoc targetName = tokens[3];
134
135         String JavaDoc labelName = null;
136         Vector2D labelPosition = null;
137         Vector2D points[] = null;
138
139         int index = 4;
140         while(index < tokens.length-1) {
141             index++;
142
143             if(tokens[index].equals("label")) {
144                 // Label name
145
labelName = tokens[index+=2];
146             } else if(tokens[index].equals("lp")) {
147                 // Label position
148
String JavaDoc[] lpos = parseTokens(tokens[index+=2]);
149                 labelPosition = new Vector2D(Float.parseFloat(lpos[0]), height-Float.parseFloat(lpos[2]));
150             } else if(tokens[index].equals("pos")) {
151                 // Edge control points
152
points = parseControlPoints(tokens[index+=2]);
153             } else if(tokens[index].equals(";"))
154                 break;
155         }
156
157         GElement source = graph.findElementWithLabel(sourceName);
158         GElement target = graph.findElementWithLabel(targetName);
159
160         GLink link = new GLink(source, GElementCircle.ANCHOR_CENTER,
161                 target, GElementCircle.ANCHOR_CENTER,
162                 GLink.SHAPE_BEZIER, labelName, 0);
163
164         if(points == null) {
165             System.err.println("No points for "+sourceName+", "+targetName+", "+tokens.length);
166             for (int i = 0; i < tokens.length; i++) {
167                 String JavaDoc token = tokens[i];
168                 System.out.println(token);
169             }
170         }
171
172         link.setBezierControlPoints(points);
173         link.setBezierLabelPosition(labelPosition);
174
175         return link;
176     }
177
178     public Vector2D[] parseControlPoints(String JavaDoc s) throws IOException JavaDoc {
179         // e,56,216 51,34 43,44 33,58 29,72 15,118 20,134 36,180 39,190 45,199 50,208
180
List JavaDoc<Vector2D> points = new ArrayList JavaDoc<Vector2D>();
181         Vector2D endPoint = null;
182
183         String JavaDoc[] pts = parseTokens(s);
184         int index = -1;
185         while(index < pts.length-1) {
186             index++;
187
188             if(pts[index].equals("e")) {
189                 // Arrow at the end
190
String JavaDoc x = pts[index+=2];
191                 String JavaDoc y = pts[index+=2];
192                 endPoint = new Vector2D(Float.parseFloat(x), height-Float.parseFloat(y));
193             } else if(isFloatString(pts[index]) && isFloatString(pts[index+2])) {
194                 // Assume pair of numbers
195
String JavaDoc x = pts[index];
196                 String JavaDoc y = pts[index+=2];
197                 points.add(new Vector2D(Float.parseFloat(x), height-Float.parseFloat(y)));
198             }
199         }
200
201         if(endPoint != null)
202             points.add(endPoint);
203
204         Vector2D p[] = new Vector2D[points.size()];
205         for(int i=0; i<points.size(); i++) {
206             p[i] = points.get(i);
207         }
208         return p;
209     }
210 }
211
Popular Tags