Source code: com/memoire/dja/DjaLoader.java
1 /**
2 * @modification $Date: 2002/12/16 18:56:25 $
3 * @statut unstable
4 * @file DjaLoader.java
5 * @version 0.36
6 * @author Guillaume Desnoix
7 * @email guillaume@desnoix.com
8 * @license GNU General Public License 2 (GPL2)
9 * @copyright 1998-2001 Guillaume Desnoix
10 */
11
12 package com.memoire.dja;
13
14 import com.memoire.fu.*;
15 import com.memoire.dnd.*;
16 import com.memoire.bu.*;
17 import com.memoire.dja.*;
18 import com.memoire.mst.*;
19
20
21 import java.awt.*;
22 import java.io.*;
23 import java.util.*;
24
25 public class DjaLoader
26 {
27 /*
28 public static DjaGrid loadAsSer(InputStream _in)
29 throws IOException, ClassNotFoundException
30 {
31 return (DjaGrid)new ObjectInputStream(_in).readObject();
32 }
33 */
34
35 private static final Color color(String _s)
36 {
37 Color r=null;
38 if(!"null".equals(_s))
39 r=new Color((int)Long.parseLong(_s,16));
40 return r;
41 }
42
43 private static final Font font(String _p, int _line)
44 {
45 Font r=null;
46
47 if(!"null".equals(_p))
48 {
49 try
50 {
51 String n=DjaCoder.decode(_p);
52 int i=n.indexOf(",");
53 int j=n.lastIndexOf(",");
54
55 String ff=n.substring(0,i);
56 int ft=Integer.parseInt(n.substring(j+1));
57
58 int fs=Font.PLAIN;
59 if(n.indexOf(",gras,") >=0) fs|=Font.BOLD;
60 if(n.indexOf(",italique,")>=0) fs|=Font.ITALIC;
61
62 r=new Font(ff,fs,ft);
63 }
64 catch(Exception ex)
65 {
66 System.err.println("Unreconized font: "+_p+" [line "+_line+"]");
67 r=DjaOptions.defaultPlainFont;
68 }
69 }
70
71 return r;
72 }
73
74 private static final String text(String _t)
75 {
76 String r=null;
77 if(!"null".equals(_t)) r=DjaCoder.decode(_t);
78 return r;
79 }
80
81 private static final DjaObject unid(Hashtable _tbl, String _id)
82 {
83 // if(_id==null) { System.err.println("id=null"); System.exit(0); }
84 return ("null".equals(_id) ? null : (DjaObject)_tbl.get(_id));
85 }
86
87 /*
88 public static DjaGrid loadGridAsText(InputStream _in)
89 { return loadGridAsText(_in,false); }
90 */
91
92 /*
93 public static DjaGrid loadGridAsText(InputStream _in, boolean _interactive)
94 {
95 DjaGrid r;
96
97 if(_interactive) r=new DjaGridInteractive();
98 else r=new DjaGrid();
99
100 r.setObjects(loadAsText(_in));
101 return r;
102 }
103 */
104
105 public static DjaVector loadAsText(InputStream _in)
106 {
107 DjaVector r=null;
108
109 TextReader lin=new TextReader(_in);
110
111 try
112 {
113 String t=lin.get();
114 while(!t.equals(""))
115 {
116 if(t.equals("grille")) r=parseGrid(lin);
117 else throw new Exception("Syntax Error: "+t);
118
119 t=lin.get();
120 }
121 }
122 catch(Throwable ex)
123 {
124 System.err.println("Unreconized format [line "+lin.line()+"]");
125 System.err.println("DjaLoader: loadAsText(): "+ex.getMessage());
126 }
127
128 return r;
129 }
130
131 private static void checkToken(TextReader lin, String s)
132 {
133 String t=lin.get();
134 if(!s.equals(t)) throw new RuntimeException("Syntax Error: "+t+" instead of "+s);
135 }
136
137 public static void parseBlock(TextReader lin)
138 {
139 int n=0;
140 String t;
141
142 while(true)
143 {
144 t=lin.get();
145 if("" .equals(t)) break;
146
147 if("{".equals(t)) n++;
148 else if("}".equals(t)) { n--; if(n==0) break; }
149 // else System.err.println(" "+t);
150 }
151 }
152
153 public static void parseRegistry(TextReader lin)
154 {
155 checkToken(lin,"{");
156
157 String t;
158
159 while(true)
160 {
161 t=lin.get();
162 if("}".equals(t)) break;
163 if("" .equals(t)) break;
164
165 String n=text(lin.get());
166 // System.err.println("parse registry: "+t+" -> "+n);
167 DjaRegistry.register(t,n);
168 }
169 }
170
171 public static void parseForm(TextReader lin, DjaForm o, Hashtable tbl)
172 {
173 checkToken(lin,"{");
174
175 String t;
176 while(true)
177 {
178 t=lin.get();
179 if("}".equals(t)) break;
180 if("" .equals(t)) break;
181
182 // System.err.println(" "+t);
183 if("id".equals(t)) tbl.put(lin.get(),o);
184 else if("x" .equals(t)) o.setX(Integer.parseInt(lin.get()));
185 else if("y" .equals(t)) o.setY(Integer.parseInt(lin.get()));
186 else if("largeur" .equals(t)) o.setWidth (Integer.parseInt(lin.get()));
187 else if("hauteur" .equals(t)) o.setHeight(Integer.parseInt(lin.get()));
188 else if("contour" .equals(t)) parseForeground(lin,o);
189 else if("surface" .equals(t)) parseBackground(lin,o);
190 else if("texte" .equals(t)) parseText(lin,o);
191 else if("proprietes".equals(t)) parseProperties(lin,o);
192 else parseBlock(lin);
193 }
194 }
195
196 public static void parseArrow(TextReader lin, DjaLink o, Hashtable tbl)
197 {
198 checkToken(lin,"{");
199
200 String t;
201 int x=0,y=0;
202
203 while(true)
204 {
205 t=lin.get();
206 if("}".equals(t)) break;
207 if("" .equals(t)) break;
208
209 // System.err.println(" "+t);
210 if("id".equals(t)) tbl.put(lin.get(),o);
211 else if("x" .equals(t)) o.setBeginX(x=Integer.parseInt(lin.get()));
212 else if("y" .equals(t)) o.setBeginY(y=Integer.parseInt(lin.get()));
213 else if("largeur" .equals(t)) o.setEndX(x+Integer.parseInt(lin.get()));
214 else if("hauteur" .equals(t)) o.setEndY(y+Integer.parseInt(lin.get()));
215 else if("contour" .equals(t)) parseForeground(lin,o);
216 else if("surface" .equals(t)) parseBackground(lin,o);
217 else if("texte" .equals(t)) parseText(lin,o);
218 else if("proprietes".equals(t)) parseProperties(lin,o);
219 else parseBlock(lin);
220 }
221 }
222
223 public static void parseForeground(TextReader lin, DjaObject o)
224 {
225 checkToken(lin,"{");
226
227 String t;
228
229 while(true)
230 {
231 t=lin.get();
232 if("}".equals(t)) break;
233 if("" .equals(t)) break;
234
235 // System.err.println(" "+t);
236 if("couleur" .equals(t)) o.setForeground(color(lin.get()));
237 }
238 }
239
240 public static void parseBackground(TextReader lin, DjaObject o)
241 {
242 checkToken(lin,"{");
243
244 String t;
245
246 while(true)
247 {
248 t=lin.get();
249 if("}".equals(t)) break;
250 if("" .equals(t)) break;
251
252 // System.err.println(" "+t);
253 if("couleur" .equals(t)) o.setBackground(color(lin.get()));
254 }
255 }
256
257 public static void parseText(TextReader lin, DjaObject o)
258 {
259 checkToken(lin,"{");
260
261 String t;
262 DjaText r=new DjaText(o,0,null);
263
264 while(true)
265 {
266 t=lin.get();
267 if("}".equals(t)) break;
268 if("" .equals(t)) break;
269
270 // System.err.println(" "+t);
271 if("valeur" .equals(t)) r.setText (text(lin.get()));
272 else if("avant" .equals(t)) r.setBefore (text(lin.get()));
273 else if("apres" .equals(t)) r.setAfter (text(lin.get()));
274 else if("numero" .equals(t)) r.setNum (Integer.parseInt(lin.get()));
275 else if("x" .equals(t)) r.setX (Integer.parseInt(lin.get()));
276 else if("y" .equals(t)) r.setY (Integer.parseInt(lin.get()));
277 else if("largeur" .equals(t)) r.setW (Integer.parseInt(lin.get()));
278 else if("hauteur" .equals(t)) r.setH (Integer.parseInt(lin.get()));
279 else if("position" .equals(t)) r.setPosition (Integer.parseInt(lin.get()));
280 else if("reference" .equals(t)) r.setReference (Integer.parseInt(lin.get()));
281 else if("alignement" .equals(t)) r.setAlignment (Integer.parseInt(lin.get()));
282 else if("couleur" .equals(t)) r.setColor (color(lin.get()));
283 else if("police" .equals(t)) r.setFont (font(lin.get(),lin.line()));
284 else if("multiligne" .equals(t)) r.setMultiline ("vrai".equals(lin.get()));
285 else if("souligne" .equals(t)) r.setUnderlined("vrai".equals(lin.get()));
286 else if("selectionne".equals(t)) r.setSelected ("vrai".equals(lin.get()));
287 }
288
289 /*
290 DjaText[] texts=o.getTextArray();
291 int n =r.getNum();
292 if((n>=0)&&(n<texts.length)) texts[n]=r;
293 else
294 */
295 o.addText(r);
296 }
297
298 public static void parseProperties(TextReader lin, DjaObject o)
299 {
300 checkToken(lin,"{");
301
302 String k,v;
303
304 while(true)
305 {
306 k=lin.get();
307 if("}".equals(k)) break;
308 if("" .equals(k)) break;
309
310 // System.err.println(" "+k);
311 v=lin.get().replace('§','"');
312 o.putProperty(k,v);
313 }
314 }
315
316 public static void parseLink(TextReader lin, Hashtable tbl)
317 {
318 checkToken(lin,"{");
319
320 String t;
321 DjaLink a=null;
322
323 while(true)
324 {
325 t=lin.get();
326 if("}".equals(t)) break;
327 if("" .equals(t)) break;
328
329 // System.err.println(" "+t);
330 if("id" .equals(t)) a=(DjaLink)unid(tbl,lin.get());
331 else if("debut".equals(t)&&(a!=null)) parseBracket(lin,a,false,tbl);
332 else if("fin" .equals(t)&&(a!=null)) parseBracket(lin,a,true,tbl);
333 else parseBlock(lin);
334 }
335 }
336
337 public static void parseBracket(TextReader lin, DjaLink o,
338 boolean end, Hashtable tbl)
339 {
340 checkToken(lin,"{");
341
342 String t;
343 while(true)
344 {
345 t=lin.get();
346 if("}".equals(t)) break;
347 if("" .equals(t)) break;
348
349 // System.err.println(" "+t);
350
351 if(!end)
352 {
353 if("type" .equals(t)) o.setBeginType(Integer.parseInt(lin.get()));
354 else if("x" .equals(t)) o.setBeginX(Integer.parseInt(lin.get()));
355 else if("y" .equals(t)) o.setBeginY(Integer.parseInt(lin.get()));
356 else if("o" .equals(t)) o.setBeginO(Integer.parseInt(lin.get()));
357 else if("id" .equals(t)) o.setBeginObject(unid(tbl,lin.get()));
358 else if("position".equals(t)) o.setBeginPosition(Integer.parseInt(lin.get()));
359 }
360 else
361 {
362 if("type" .equals(t)) o.setEndType(Integer.parseInt(lin.get()));
363 else if("x" .equals(t)) o.setEndX(Integer.parseInt(lin.get()));
364 else if("y" .equals(t)) o.setEndY(Integer.parseInt(lin.get()));
365 else if("o" .equals(t)) o.setEndO(Integer.parseInt(lin.get()));
366 else if("id" .equals(t)) o.setEndObject(unid(tbl,lin.get()));
367 else if("position".equals(t)) o.setEndPosition(Integer.parseInt(lin.get()));
368 }
369 }
370 }
371
372 public static DjaVector parseGrid(TextReader lin)
373 {
374 DjaVector r=new DjaVector();
375
376 checkToken(lin,"{");
377
378 Hashtable tbl=new Hashtable();
379
380 while(true)
381 {
382 String t=lin.get();
383 if("}".equals(t)) break;
384 if("" .equals(t)) break;
385 // System.err.println(t);
386
387
388 if("registre".equals(t))
389 parseRegistry(lin);
390 else
391 if("lien".equals(t))
392 parseLink(lin,tbl);
393 else
394 if("ordre".equals(t)||"position".equals(t))
395 parseBlock(lin);
396 else
397 {
398 DjaObject o=DjaRegistry.getInstance(t);
399
400 if(o instanceof DjaForm)
401 { parseForm(lin,(DjaForm)o,tbl); r.addElement(o); }
402 else
403 if(o instanceof DjaLink)
404 { parseArrow(lin,(DjaLink)o,tbl); r.addElement(o); }
405 else
406 parseBlock(lin);
407
408 if(o!=null) o.afterLoading();
409 }
410 }
411
412 return r;
413 }
414
415 public static final class TextReader
416 {
417 StreamTokenizer st;
418 Stack ss;
419
420 public TextReader(InputStream is)
421 {
422 st=new StreamTokenizer
423 (new InputStreamReader(new BufferedInputStream(is, 1024)));
424 st.resetSyntax();
425 st.eolIsSignificant(false);
426 st.quoteChar('"');
427 st.quoteChar('\'');
428 st.slashSlashComments(true);
429 st.slashStarComments(true);
430 st.wordChars('a','z');
431 st.wordChars('A','Z');
432 st.wordChars('0','9');
433 st.wordChars('_','_');
434 st.wordChars('.','.');
435 st.wordChars('+','+');
436 st.wordChars('-','-');
437 st.wordChars('[','[');
438 st.wordChars(']',']');
439 st.whitespaceChars(' ',' ');
440 st.whitespaceChars('\t','\t');
441 st.whitespaceChars('\n','\n');
442 st.whitespaceChars('\r','\r');
443
444 ss=new Stack();
445 }
446
447 String get()
448 {
449 String r="";
450
451 if(ss.empty())
452 {
453 try
454 {
455 st.nextToken();
456 // System.err.println(">>>"+st.ttype+" "+st.sval);
457
458 switch(st.ttype)
459 {
460 case StreamTokenizer.TT_EOF : r=""; break;
461 case StreamTokenizer.TT_WORD : r=st.sval; break;
462 case StreamTokenizer.TT_NUMBER: r=""+st.nval; break;
463 case '"' : r=st.sval; break;
464 case '\'' : r=""+st.sval; break;
465 default : r=""+(char)st.ttype; break;
466 }
467 }
468 catch(Exception ex) { System.err.println("Error: "+ex); }
469 }
470 else r=(String)ss.pop();
471
472 return r;
473 }
474
475 void put(String s)
476 {
477 ss.push(s);
478 }
479
480 int line()
481 {
482 return st.lineno();
483 }
484 }
485 }