KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > visitorIDL3 > common > Printer


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2005 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Mathieu Vadet.
23 Contributor(s): Philippe Merle.
24
25 ====================================================================
26 $Id: Printer.java,v 1.3 2005/06/17 15:58:27 merle Exp $
27 ====================================================================*/

28
29 package org.objectweb.ccm.visitorIDL3.common;
30
31 /**
32  * .
33  *
34  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
35  *
36  * @version 0.3
37  */

38
39 public class Printer
40        extends fr.lifl.cim.jidlscript.JOSInterpreter
41 {
42     // ==================================================================
43
//
44
// Internal state.
45
//
46
// ==================================================================
47

48     /**
49      **
50      **/

51     private PrintStreamManager the_stream_manager_;
52
53     /**
54      **
55      **/

56     private PrintStream stream_;
57
58     /**
59      **
60      **/

61     private org.objectweb.ccm.util.Table string_mappings_;
62
63     /**
64      **
65      **/

66     private org.objectweb.ccm.util.Vector file_mappings_;
67
68     /**
69      **
70      **/

71     private java.lang.String JavaDoc remaining_text_;
72
73     /**
74      **
75      **/

76     private int char_to_skip_;
77
78     /**
79      **
80      **/

81     private java.lang.String JavaDoc indentation_;
82
83     /**
84      **
85      **/

86     private char call_char_;
87
88     /**
89      **
90      **/

91     private char comment_char_;
92
93     /**
94      **
95      **/

96     private Generator generator_;
97
98     // ==================================================================
99
//
100
// Constructor.
101
//
102
// ==================================================================
103

104     /**
105      ** The default constructor.
106      **/

107     public
108     Printer(Generator gen,
109             char call_char,
110             char comment_char,
111             java.lang.String JavaDoc indentation)
112     throws fr.lifl.cim.jidlscript.lang.JISError
113     {
114         super(new java.lang.String JavaDoc[0]);
115
116         // init internal state.
117
the_stream_manager_ = new PrintStreamManager();
118         string_mappings_ = new org.objectweb.ccm.util.Table();
119         file_mappings_ = new org.objectweb.ccm.util.Vector();
120         remaining_text_ = "";
121         char_to_skip_ = 0;
122         stream_ = the_stream_manager_.create("stdout", indentation);
123         setGenerator(gen);
124         indentation_ = indentation;
125         call_char_ = call_char;
126         comment_char_ = comment_char;
127     }
128
129     // ==================================================================
130
//
131
// Internal methods.
132
//
133
// ==================================================================
134

135     /**
136      **
137      **/

138     private void
139     print(java.lang.String JavaDoc text)
140     {
141         if (stream_==null)
142             return ;
143
144         int idx = text.indexOf("\\n");
145         if (idx==-1)
146             remaining_text_ = remaining_text_ + text;
147         else
148         {
149             stream_.print(remaining_text_ + text.substring(0, idx) + '\n');
150             remaining_text_ = "";
151             if (idx+2<text.length())
152                 print(text.substring(idx+2, text.length()));
153         }
154     }
155
156     /**
157      **
158      **/

159     private void
160     analyze(java.lang.String JavaDoc id,
161             java.lang.String JavaDoc mapping)
162     {
163         org.objectweb.ccm.util.Vector mappings = new org.objectweb.ccm.util.Vector();
164         org.objectweb.ccm.util.Vector to_eval = new org.objectweb.ccm.util.Vector();
165         MapInfo info = null;
166         java.lang.String JavaDoc tmp = "";
167
168         if (mapping.length()==0)
169         {
170             info = new MapInfo(mappings, to_eval);
171             string_mappings_.put(id.toUpperCase(), info);
172             return ;
173         }
174
175         int i = 0;
176         int j = mapping.indexOf(call_char_, i);
177         while (j!=-1)
178         {
179             if (i!=j)
180             {
181                 tmp = mapping.substring(i, j);
182                 mappings.add(tmp);
183                 to_eval.add(new java.lang.Boolean JavaDoc(false));
184             }
185
186             i = j+1;
187             if (i>=mapping.length())
188             {
189                 info = new MapInfo(mappings, to_eval);
190                 string_mappings_.put(id.toUpperCase(), info);
191                 return ;
192             }
193             j = mapping.indexOf(call_char_, i);
194             if (j==-1)
195             {
196                 java.lang.String JavaDoc msg =
197                     "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+
198                     " operation : printFromString\n"+
199                     " message : Missing " + call_char_ + " character after : "+
200                     mapping.substring(i)+" in : "+id;
201                 // throw new Error(msg);
202
System.err.println(msg);
203             }
204
205             if (i!=j)
206             {
207                 tmp = mapping.substring(i, j);
208                 mappings.add(tmp);
209                 to_eval.add(new java.lang.Boolean JavaDoc(true));
210             }
211
212             i = j+1;
213             if (i>=mapping.length())
214             {
215                 info = new MapInfo(mappings, to_eval);
216                 string_mappings_.put(id.toUpperCase(), info);
217                 return ;
218             }
219             j = mapping.indexOf(call_char_, i);
220         }
221
222         tmp = mapping.substring(i, mapping.length());
223         mappings.add(tmp);
224         to_eval.add(new java.lang.Boolean JavaDoc(false));
225
226         info = new MapInfo(mappings, to_eval);
227         string_mappings_.put(id.toUpperCase(), info);
228     }
229
230     /**
231      **
232      **/

233     private void
234     evaluate(MapInfo info,
235              java.lang.String JavaDoc[] aliases,
236              java.lang.Object JavaDoc[] objs)
237     {
238         java.lang.String JavaDoc[] mappings =
239             (java.lang.String JavaDoc[])info.mappings.toArray(new java.lang.String JavaDoc[0]);
240         java.lang.Boolean JavaDoc[] to_eval =
241             (java.lang.Boolean JavaDoc[])info.to_eval.toArray(new java.lang.Boolean JavaDoc[0]);
242
243         for (int i=0;i<mappings.length;i++)
244         {
245             if (to_eval[i].booleanValue())
246                 print(joshEval(aliases, objs, mappings[i]));
247             else if (char_to_skip_!=0)
248             {
249                 print(mappings[i].substring(char_to_skip_));
250                 char_to_skip_ = 0;
251             }
252             else
253                 print(mappings[i]);
254         }
255     }
256
257     /**
258      **
259      **/

260     private java.lang.String JavaDoc
261     joshEval(java.lang.String JavaDoc[] aliases,
262               java.lang.Object JavaDoc[] objs,
263               java.lang.String JavaDoc mapping)
264     {
265         fr.lifl.cim.jidlscript.lang.JISObject result = null;
266         try
267         {
268             for (int i=0;i<aliases.length;i++)
269                 setAlias(aliases[i], objs[i]);
270
271             result = super.eval(mapping);
272             return ((fr.lifl.cim.jidlscript.java.JISJavaString)result).getValue();
273         }
274         catch(java.lang.ClassCastException JavaDoc ex)
275         {
276             java.lang.String JavaDoc msg =
277                 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+
278                 " operation : joshEval\n"+
279                 " message : result of eval is not a string : "+ex.getMessage()+"\n"+
280                 " while visiting the following template :\n"+
281                 " "+mapping+"\n";
282             // throw new Error("");
283
System.err.println(msg);
284             return "";
285         }
286         catch(fr.lifl.cim.jidlscript.lang.JISError ex)
287         {
288             java.lang.String JavaDoc msg =
289                 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+
290                 " operation : joshEval\n"+
291                 " message : an error occured while evaluating the following template :\n"+
292                 " "+mapping+"\n"+
293                 " reason : "+ex.exception.toString();
294             // throw new Error("");
295
System.err.println(msg);
296             ex.printStackTrace();
297         return "";
298         }
299     }
300
301     // ==================================================================
302
//
303
// Final public methods.
304
//
305
// ==================================================================
306

307     /**
308      **
309      **/

310     public final char
311     getCallChar()
312     {
313         return call_char_;
314     }
315
316     /**
317      **
318      **/

319     public final char
320     getCommentChar()
321     {
322         return comment_char_;
323     }
324
325     /**
326      **
327      **/

328     public final java.lang.String JavaDoc
329     getIndentation()
330     {
331         return indentation_;
332     }
333
334     /**
335      **
336      **/

337     public final Generator
338     getGenerator()
339     {
340         return generator_;
341     }
342
343     /**
344      **
345      **/

346     public final void
347     delete(int n)
348     {
349         if (remaining_text_.length()>n)
350             remaining_text_ = remaining_text_.substring(0, remaining_text_.length()-n);
351         else
352             remaining_text_ = "";
353     }
354
355     /**
356      **
357      **/

358     public final void
359     skip(int n)
360     {
361         char_to_skip_ = n;
362     }
363
364     /**
365      **
366      **/

367     public final void
368     insert(int n)
369     {
370         int length = remaining_text_.length();
371         java.lang.String JavaDoc tmp1 = remaining_text_.substring(0, length-n);
372         java.lang.String JavaDoc tmp2 = remaining_text_.substring(length-n, length);
373         remaining_text_ = tmp1 + "\n" + stream_.getTab() + tmp2;
374     }
375
376     /**
377      **
378      **/

379     public final void
380     loadFile(java.lang.String JavaDoc filename)
381     {
382         boolean already_parsed = file_mappings_.contains(filename);
383         if (already_parsed)
384             return ;
385
386         java.lang.String JavaDoc mapping = "";
387         java.lang.String JavaDoc tmp = "";
388         java.lang.String JavaDoc curr_id = "";
389         try
390         {
391             java.io.BufferedReader JavaDoc file = new java.io.BufferedReader JavaDoc(
392                 new java.io.FileReader JavaDoc(generator_.getTemplateLocation()+"/"+filename));
393             tmp = file.readLine();
394             while (tmp!=null)
395             {
396                 if ((tmp.length()>0) && (tmp.charAt(0)==comment_char_))
397                 {
398                     if ((tmp.length()>2) && (tmp.substring(1, 3).toLowerCase().equals("id")))
399                     {
400                         if (!mapping.equals(""))
401                             analyze(curr_id, mapping);
402
403                         mapping = "";
404                         curr_id = tmp.substring(4);
405                         curr_id = curr_id.trim();
406                     }
407                 }
408                 else
409                     mapping = mapping+modifyCurrentLine(tmp);
410
411                 tmp = file.readLine();
412             }
413
414             analyze(curr_id, mapping);
415         }
416         catch(java.io.FileNotFoundException JavaDoc ex)
417         {
418             java.lang.String JavaDoc msg =
419                 "exception in class org.objectweb.ccm.visitorIDL3.Printer\n"+
420                 " operation : mapFromFile\n"+
421                 " message : "+ex.getMessage();
422             // throw new Error(msg);
423
System.err.println(msg);
424         }
425         catch(java.io.IOException JavaDoc ex)
426         {
427             java.lang.String JavaDoc msg =
428                 "exception in class org.objectweb.ccm.visitorIDL3.Printer\n"+
429                 " operation : mapFromFile\n"+
430                 " message : "+ex.getMessage();
431             // throw new Error(msg);
432
System.err.println(msg);
433         }
434
435         file_mappings_.add(filename);
436     }
437
438     // ==================================================================
439
//
440
// Internal methods that can be overrided by subclasses.
441
//
442
// ==================================================================
443

444     /**
445      **
446      **/

447     public void
448     setDirectory(java.lang.String JavaDoc dirname)
449     {
450         the_stream_manager_.setDirectory(dirname);
451     }
452
453     /**
454      **
455      **/

456     public java.lang.String JavaDoc
457     open(java.lang.String JavaDoc filename)
458     {
459         stream_ = the_stream_manager_.create(filename, indentation_);
460         return "";
461     }
462
463     /**
464      **
465      **/

466     public java.lang.String JavaDoc
467     close()
468     {
469         the_stream_manager_.close(stream_.getName());
470         stream_ = null;
471         return "";
472     }
473
474     /**
475      **
476      **/

477     public java.lang.String JavaDoc
478     inc()
479     {
480         stream_.incDepth();
481         return "";
482     }
483
484     /**
485      **
486      **/

487     public java.lang.String JavaDoc
488     dec()
489     {
490         stream_.decDepth();
491         return "";
492     }
493
494     /**
495      **
496      **/

497     public java.lang.String JavaDoc
498     modifyCurrentLine(java.lang.String JavaDoc str)
499     {
500         return str;
501     }
502
503     // ==================================================================
504
//
505
// Public methods.
506
//
507
// ==================================================================
508

509     /**
510      **
511      **/

512     public void
513     setAlias(java.lang.String JavaDoc alias,
514              java.lang.Object JavaDoc obj)
515     {
516         try
517         {
518             fr.lifl.cim.jidlscript.java.JISJavaClass jc = null;
519             fr.lifl.cim.jidlscript.java.JISJavaInstance ji = null;
520             java.lang.String JavaDoc itfname = obj.getClass().getName();
521
522             if (itfname.equals("java.lang.String"))
523                 ji = new fr.lifl.cim.jidlscript.java.JISJavaString((java.lang.String JavaDoc)obj);
524             else
525                 {
526                     jc = fr.lifl.cim.jidlscript.java.JISJavaClass.importClass(
527                         new fr.lifl.cim.jidlscript.lang.JISString(itfname));
528                     ji = new fr.lifl.cim.jidlscript.java.JISJavaInstance(jc, obj);
529                 }
530
531             fr.lifl.cim.jidlscript.lang.JISRootScope.root().insertScoped(
532                 new fr.lifl.cim.jidlscript.lang.JISAlias(alias, ji));
533         }
534         catch(fr.lifl.cim.jidlscript.lang.JISError ex)
535         {
536             java.lang.String JavaDoc msg =
537                 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+
538                 " operation : setAlias\n"+
539                 " message : "+ex.exception.toString();
540             // throw new Error(msg);
541
System.err.println(msg);
542         }
543     }
544
545     /**
546      **
547      **/

548     public void
549     setGenerator(Generator gen)
550     {
551         generator_ = gen;
552
553         // set the "generator" alias in root scope on the Generator class instance.
554
setAlias("generator", gen);
555     }
556
557     /**
558      **
559      **/

560     public void
561     map(java.lang.String JavaDoc template_id,
562         java.lang.String JavaDoc[] aliases,
563         java.lang.Object JavaDoc[] objs)
564     {
565         java.lang.Object JavaDoc obj = string_mappings_.get(template_id);
566         if (obj==null)
567         {
568                 java.lang.String JavaDoc msg =
569                     "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+
570                     " operation : map\n"+
571                     " message : Unknown mapping identifier : "+template_id+"\n";
572                 // throw new Error(msg);
573
System.err.println(msg);
574         }
575
576         evaluate((MapInfo)obj, aliases, objs);
577     }
578
579     /**
580      **
581      **/

582     public void
583     printImmediately(java.lang.String JavaDoc text)
584     {
585         print(text);
586     }
587 }
588
589
590 /**
591  ** Nested class MapInfo
592  **/

593 class MapInfo
594 {
595     // ==================================================================
596
//
597
// Internal state.
598
//
599
// ==================================================================
600

601     /**
602      **
603      **/

604     public org.objectweb.ccm.util.Vector mappings;
605
606     /**
607      **
608      **/

609     public org.objectweb.ccm.util.Vector to_eval;
610
611     // ==================================================================
612
//
613
// Constructor.
614
//
615
// ==================================================================
616

617     /**
618      ** The default constructor.
619      **/

620     protected
621     MapInfo(org.objectweb.ccm.util.Vector maps,
622             org.objectweb.ccm.util.Vector bools)
623     {
624         mappings = maps;
625         to_eval = bools;
626     }
627 }
628
Popular Tags