KickJava   Java API By Example, From Geeks To Geeks.

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


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

40
41 public abstract class GDOTImporter {
42
43     protected GElement graph;
44     protected float height = 0;
45
46     public GElement generateGraph(String JavaDoc dotFile) throws IOException {
47
48         BufferedReader br = new BufferedReader(new FileReader(dotFile));
49         try {
50             graph = null;
51
52             /** The problem here is that DOT sometime inserts a '\' at the end of a long
53              * line so we have to skip it and continue to parse until a "real" EOL is reached.
54              * Example:
55              * statement -> compoundStatement [pos="e,3264,507 3271,2417 3293,2392 ... 3237,565 3234,560 32\
56              39,545 3243,534 3249,523 3257,514"];
57              */

58
59             StringBuffer JavaDoc line = new StringBuffer JavaDoc();
60             int c; // current character
61
int pc = -1; // previous character
62
while((c = br.read()) != -1) {
63                 if(c == '\n') {
64                     if(pc == '\\') {
65                         // Remove the last \ if it was part of the DOT wrapping character
66
line.deleteCharAt(line.length()-1);
67                     } else {
68                         GElement element = parseLine(line.toString());
69                         if(element != null) {
70                             if(graph == null)
71                                 graph = element;
72                             else
73                                 graph.addElement(element);
74                         }
75                         line.delete(0, line.length());
76                     }
77                 } else if(c != '\r') {
78                     line.append((char)c);
79                 }
80                 pc = c;
81             }
82         } finally {
83             br.close();
84         }
85         return graph;
86     }
87
88     public String JavaDoc[] parseTokens(String JavaDoc line) throws IOException {
89         List JavaDoc<String JavaDoc> tokens = new ArrayList JavaDoc<String JavaDoc>();
90
91         /*StringTokenizer st = new StringTokenizer(line);
92         String token;
93         while((token = st.nextToken()) != null) {
94             tokens.add(token);
95         } */

96
97         StreamTokenizer st = new StreamTokenizer(new StringReader(line));
98         st.parseNumbers();
99         st.wordChars('_', '_'); // A word can be THIS_IS_A_WORD
100

101         int token = st.nextToken();
102         while(token != StreamTokenizer.TT_EOF) {
103             String JavaDoc element = null;
104             switch(token) {
105                 case StreamTokenizer.TT_NUMBER:
106                     element = String.valueOf(st.nval);
107                     break;
108                 case StreamTokenizer.TT_WORD:
109                     element = st.sval;
110                     break;
111                 case '"':
112                 case '\'':
113                     element = st.sval;
114                     break;
115                 case StreamTokenizer.TT_EOL:
116                     break;
117                 case StreamTokenizer.TT_EOF:
118                     break;
119                 default:
120                     element = String.valueOf((char)st.ttype);
121                     break;
122             }
123             if(element != null)
124                 tokens.add(element);
125             token = st.nextToken();
126         }
127
128         String JavaDoc[] result = new String JavaDoc[tokens.size()];
129         for(int index=0; index<tokens.size(); index++)
130             result[index] = tokens.get(index);
131         return result;
132     }
133
134     public abstract GElement parseLine(String JavaDoc line) throws IOException;
135
136     public abstract GElement createGraphNode(String JavaDoc[] tokens) throws IOException;
137     public abstract GElement createGraphEdge(String JavaDoc[] tokens) throws IOException;
138
139     public boolean isFloatString(String JavaDoc s) {
140         try {
141             Float.parseFloat(s);
142         } catch(NumberFormatException JavaDoc e) {
143             return false;
144         }
145         return true;
146     }
147
148     public static class Node extends GElementCircle {
149
150         public boolean doublecircle;
151         public float width, height;
152
153         public Node() {
154
155         }
156         
157         public void setDouble(boolean flag) {
158             doublecircle = flag;
159         }
160
161         public void setSize(float width, float height) {
162             this.width = width;
163             this.height = height;
164         }
165
166         public void drawShape(Graphics2D g) {
167             //super.drawShape(g);
168

169             {
170                 int x = (int)(getPositionX()-width/2);
171                 int y = (int)(getPositionY()-height/2);
172
173                 g.drawOval(x, y, (int)width, (int)height);
174             }
175
176             if(doublecircle) {
177                 int x = (int)(getPositionX()-width/2);
178                 int y = (int)(getPositionY()-height/2);
179
180                 g.drawOval(x+3, y+3, (int)(width-6), (int)(height-6));
181             }
182         }
183
184     }
185
186     public static class StringTokenizer {
187
188         public String JavaDoc s;
189         public int position;
190
191         public StringTokenizer(String JavaDoc s) {
192             this.s = s;
193             position = -1;
194         }
195
196         public String JavaDoc nextToken() {
197             StringBuffer JavaDoc token = new StringBuffer JavaDoc();
198
199             while(skipChar((char)getChar(1))) {
200                 nextChar();
201             }
202
203             while(nextChar()) {
204                 char c = getChar();
205
206                 if(c == '"') {
207                     token.append(parseString());
208                     break;
209                 }
210
211                 if(isWordChar(c)) {
212                     token.append(c);
213                 } else {
214                     if(token.length() == 0)
215                         token.append(c);
216                     else
217                         position--;
218                     break;
219                 }
220
221             }
222
223             if(token.length() == 0)
224                 return null;
225             else
226                 return token.toString();
227         }
228
229         public String JavaDoc parseString() {
230             StringBuffer JavaDoc string = new StringBuffer JavaDoc();
231             boolean escaping = false;
232             while(nextChar()) {
233                 char c = getChar();
234                 if(c == '\\' && !escaping) {
235                     escaping = true;
236                     continue;
237                 }
238
239                 if(c == '"' && !escaping)
240                     break;
241                 else
242                     string.append(c);
243
244                 escaping = false;
245             }
246             return string.toString();
247         }
248
249         public boolean nextChar() {
250             position++;
251             return position < s.length();
252         }
253
254         public char getChar() {
255             return (char)getChar(0);
256         }
257
258         public int getChar(int offset) {
259             int index = position+offset;
260             if(index<s.length())
261                 return s.charAt(index);
262             else
263                 return -1;
264         }
265
266         public boolean isWordChar(char c) {
267             return Character.isLetterOrDigit(c) || c == '_' || c == '.';
268         }
269
270         public boolean skipChar(char c) {
271             return c == ' ' || c == '\t';
272         }
273     }
274
275 }
276
Popular Tags