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 ; 9 import java.util.List ; 10 40 41 public abstract class GDOTImporter { 42 43 protected GElement graph; 44 protected float height = 0; 45 46 public GElement generateGraph(String dotFile) throws IOException { 47 48 BufferedReader br = new BufferedReader(new FileReader(dotFile)); 49 try { 50 graph = null; 51 52 58 59 StringBuffer line = new StringBuffer (); 60 int c; int pc = -1; while((c = br.read()) != -1) { 63 if(c == '\n') { 64 if(pc == '\\') { 65 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 [] parseTokens(String line) throws IOException { 89 List <String > tokens = new ArrayList <String >(); 90 91 96 97 StreamTokenizer st = new StreamTokenizer(new StringReader(line)); 98 st.parseNumbers(); 99 st.wordChars('_', '_'); 101 int token = st.nextToken(); 102 while(token != StreamTokenizer.TT_EOF) { 103 String 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 [] result = new String [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 line) throws IOException; 135 136 public abstract GElement createGraphNode(String [] tokens) throws IOException; 137 public abstract GElement createGraphEdge(String [] tokens) throws IOException; 138 139 public boolean isFloatString(String s) { 140 try { 141 Float.parseFloat(s); 142 } catch(NumberFormatException 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 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 s; 189 public int position; 190 191 public StringTokenizer(String s) { 192 this.s = s; 193 position = -1; 194 } 195 196 public String nextToken() { 197 StringBuffer token = new StringBuffer (); 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 parseString() { 230 StringBuffer string = new StringBuffer (); 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 |