KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > common > lib > Generator


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 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): Christophe Demarey.
23 Contributor(s): Sylvain Leblanc.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.generator.common.lib;
28
29 /**
30  ** This class uses the Velocity (http://jakarta.apache.org/Velocity)
31  ** engine to creates mapped files.
32  **/

33 public class Generator
34   implements org.objectweb.openccm.generator.common.api.Generator
35 {
36
37     // ===========================================================
38
//
39
// Internal State.
40
//
41
// ===========================================================
42

43     /**
44      ** The file ressource loader path
45      **/

46     protected String JavaDoc ressource_path_;
47
48     /**
49      ** The template path
50      **/

51     protected String JavaDoc template_path_;
52
53     /**
54      ** The velocity engine.
55      **/

56     protected org.apache.velocity.app.VelocityEngine engine_;
57
58     /**
59      ** The current context.
60      **/

61     protected org.apache.velocity.context.Context context_;
62
63     /**
64      ** A Hashtable of writers (to output mapping)
65      **/

66     protected java.util.Hashtable JavaDoc streams_;
67
68     /**
69      ** A Hashtable of file names
70      **/

71     protected java.util.Hashtable JavaDoc filenames_;
72
73     /**
74      ** The default writer
75      **/

76     protected java.io.Writer JavaDoc stream_;
77
78     /**
79      ** The log file name
80      **/

81     protected String JavaDoc log_;
82
83     /**
84      ** Is log enabled ?
85      **/

86     protected boolean log_enabled_;
87
88     /**
89      * The file indentor.
90      */

91     protected org.objectweb.openccm.generator.common.lib.Indentor ind_;
92
93     // ===========================================================
94
//
95
// Constructors.
96
//
97
// ===========================================================
98

99     public Generator()
100     {
101         engine_ = new org.apache.velocity.app.VelocityEngine();
102         ressource_path_ = ".";
103         template_path_ = "";
104         streams_ = new java.util.Hashtable JavaDoc();
105         filenames_ = new java.util.Hashtable JavaDoc();
106         log_ = "velocityError.log";
107         log_enabled_ = true;
108         ind_ = new org.objectweb.openccm.generator.common.lib.Indentor();
109     }
110
111     // ===========================================================
112
//
113
// Internal methods.
114
//
115
// ===========================================================
116

117     /**
118      * Uses velocity to map a currently registered Velocimacro.
119      *
120      * @param macro_name - The name of the macro to invoke (map)
121      * @param params - args used to invoke Velocimacro.
122      * In context key format : eg "foo","bar" (rather than "$foo","$bar")
123      * @param stream - Writer for output stream.
124      **/

125     private void
126     map(String JavaDoc macro_name, java.lang.String JavaDoc[] params, java.io.Writer JavaDoc stream)
127     {
128         try {
129             engine_.invokeVelocimacro(macro_name, log_, params, context_, stream);
130             stream.flush();
131         } catch (Exception JavaDoc e ) {
132             throw new Error JavaDoc("Problem invoking macro : " + e );
133         }
134     }
135
136     /**
137      * Uses velocity to map a template file.
138      *
139      * @param template_name - The name of the template file.
140      * @param stream - Writer for output stream.
141      **/

142     private void
143     map_file(String JavaDoc template_name, java.io.Writer JavaDoc stream)
144     {
145         try {
146             engine_.mergeTemplate(template_path_ + template_name, context_, stream);
147         } catch (java.io.FileNotFoundException JavaDoc ex) {
148             throw new Error JavaDoc("Problem with io : " + ex );
149         } catch (SecurityException JavaDoc ex) {
150             throw new Error JavaDoc("Problem with io : " + ex );
151         } catch (Exception JavaDoc e ) {
152             throw new Error JavaDoc("Problem merging template : " + e );
153         }
154     }
155
156     /**
157      * Print immediatly the text in the output stream.
158      *
159      * @param text - The text to be written.
160      * @param stream - Writer for output stream.
161      **/

162     private void
163     print(String JavaDoc text, java.io.Writer JavaDoc stream)
164     {
165         try {
166             stream.write(text);
167             stream.flush();
168         } catch (java.io.IOException JavaDoc e ) {
169             throw new Error JavaDoc("Problem with I/O : " + e );
170         }
171     }
172
173     // ===========================================================
174
//
175
// Public methods.
176
//
177
// ===========================================================
178

179     /**
180      * Sets the file ressource loader path.
181      *
182      * @param path - The path from where files will be loaded.
183      **/

184     public void
185     setRessourcePath(java.util.List JavaDoc list)
186     {
187         if ( (list!=null) && (list.size()>0) )
188         {
189             String JavaDoc path = null;
190             ressource_path_ = (String JavaDoc) list.get(0);
191
192             for (int i=1; i<list.size(); i++)
193             {
194                 path = (String JavaDoc) list.get(i);
195                 ressource_path_ += "," + path;
196             }
197         }
198     }
199
200     /**
201      * Sets the name of the local Velocimacro library template.
202      *
203      * @param name - The local Velocimacro library template path.
204      **/

205     public void
206     setLibrary(java.util.List JavaDoc list)
207     {
208         if ( (list!=null) && (list.size()>0) )
209         {
210             String JavaDoc name = (String JavaDoc) list.get(0);
211
212             for (int i=1; i<list.size(); i++)
213             {
214                 name += "," + (String JavaDoc) list.get(i);
215             }
216             try{
217                 engine_.setProperty(org.apache.velocity.app.VelocityEngine.VM_LIBRARY, name);
218             }catch(Exception JavaDoc e){
219                 e.printStackTrace();
220             };
221         }
222     }
223
224     /**
225      * Sets the current context.
226      *
227      * @param context - The context to be set.
228      **/

229     public void
230     setContext(org.apache.velocity.context.Context context)
231     {
232         context_ = context;
233     }
234
235     /**
236      * Disable the log system.
237      **/

238     public void
239     disableLog()
240     {
241         log_enabled_ = false;
242     }
243
244     /**
245      * Initialize the generator.
246      **/

247     public void
248     init()
249     {
250         // Initialize the Velocity Engine
251
try{
252             if (!log_enabled_)
253             {
254                 engine_.setProperty( org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
255                                      "org.apache.velocity.runtime.log.NullLogSystem" );
256             }
257             engine_.setProperty(org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER, "classpath");
258             engine_.setProperty("classpath." + org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER + ".class",
259                               org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader.class.getName());
260             engine_.init();
261         }catch(Exception JavaDoc e){
262             e.printStackTrace();
263         };
264
265         // Create a new context
266
setContext(new org.apache.velocity.VelocityContext());
267     }
268
269     /**
270      * Open a new generation stream.
271      *
272      * @param filename - The name of the file into generation will be done.
273      * @param id - An identifiant for this stream.
274      */

275     public void
276     open(String JavaDoc filename, String JavaDoc id)
277     {
278         // Initialize the output file
279
try {
280             java.io.File JavaDoc f = new java.io.File JavaDoc(filename);
281             java.io.FileOutputStream JavaDoc file = new java.io.FileOutputStream JavaDoc(f.getAbsolutePath());
282             // set the default stream
283
stream_ = new java.io.PrintWriter JavaDoc(file); // org.apache.velocity.io.VelocityWriter(file);
284
// add this stream to the hashtable
285
streams_.put(id, stream_);
286             // add the file name
287
filenames_.put(id, filename);
288         } catch (Exception JavaDoc e ) {
289             throw new Error JavaDoc("Problem with I/O : " + e );
290         }
291     }
292
293     /**
294      * Close a generation stream and
295      * eventually indent the generated file.
296      *
297      * @param id - The identifiant of the file to close.
298      * @param indent - True if the file must be indented, else false.
299      */

300     public void
301     close(String JavaDoc id, boolean indent)
302     {
303         java.io.PrintWriter JavaDoc stream = (java.io.PrintWriter JavaDoc) streams_.remove(id);
304         String JavaDoc name = (String JavaDoc) filenames_.remove(id);
305
306         if (stream == null)
307             return;
308
309         try {
310             stream_.close();
311             if (indent)
312                 ind_.indent(name);
313         } catch (java.io.IOException JavaDoc ex) {
314             throw new Error JavaDoc("Problem with I/O : " + ex );
315         }
316     }
317
318     /**
319      * Close a generation stream and indent the generated file.
320      *
321      * @param id - The identifiant of the file to close.
322      */

323     public void
324     close(String JavaDoc id)
325     {
326         close(id, true);
327     }
328
329     /**
330      * Switch generation output to file with identifiant id.
331      *
332      * @param id - The identifiant of the file to close.
333      */

334     public void
335     switchToFile(String JavaDoc id)
336     {
337         java.io.PrintWriter JavaDoc stream = null;
338
339         stream = (java.io.PrintWriter JavaDoc) streams_.get(id);
340         // set the default stream
341
if (stream != null)
342             stream_ = stream;
343     }
344
345     /**
346      * Gets a value from the context.
347      *
348      * @param key - The name to key the provided value with.
349      *
350      * @return The associated value.
351      **/

352     public Object JavaDoc
353     get(String JavaDoc key)
354     {
355         return context_.get(key);
356     }
357
358     /**
359      * Adds a name/value pair to the context.
360      *
361      * @param key - The name to key the provided value with.
362      * value - The corresponding value.
363      **/

364     public void
365     put(String JavaDoc key, Object JavaDoc value)
366     {
367         context_.put(key, value);
368     }
369
370     /**
371      * Removes the value associated with the specified key from the context.
372      *
373      * @param key - The name of the value to remove.
374      **/

375     public void
376     remove(Object JavaDoc key)
377     {
378         context_.remove(key);
379     }
380
381     /**
382      * Uses velocity to map a template file.
383      *
384      * @param template_name - The name of the template file.
385      **/

386     public void
387     map_file(String JavaDoc template_name)
388     {
389         if (stream_ != null)
390             map_file(template_name, stream_);
391         else
392             throw new Error JavaDoc("No default output file available.\n");
393     }
394
395     /**
396      * Uses velocity to map a template file.
397      *
398      * @param template_name - The name of the template file.
399      * @param id - The identifiant of the output file.
400      **/

401     public void
402     map_file(String JavaDoc template_name, String JavaDoc id)
403     {
404         java.io.PrintWriter JavaDoc stream = (java.io.PrintWriter JavaDoc) streams_.get(id);
405         if (stream != null)
406             map_file(template_name, stream);
407         else
408             throw new Error JavaDoc("Not a valid ID.\n");
409     }
410
411     /**
412      * Uses velocity to map a currently registered Velocimacro.
413      *
414      * @param macro_name - The name of the macro to invoke (map)
415      **/

416     public void
417     map(String JavaDoc macro_name)
418     {
419         if (stream_ != null)
420             map(macro_name, new String JavaDoc[0], stream_);
421         else
422             throw new Error JavaDoc("No default output file available.\n");
423     }
424
425     /**
426      * Uses velocity to map a currently registered Velocimacro.
427      *
428      * @param macro_name - The name of the macro to invoke (map)
429      * @param context - The context used to map the macro.
430      **/

431     public void
432     map(String JavaDoc macro_name, org.apache.velocity.context.Context context)
433     {
434         if (stream_ != null)
435         {
436             try {
437                 engine_.invokeVelocimacro(macro_name, log_, new String JavaDoc[0], context, stream_);
438                 stream_.flush();
439             } catch (Exception JavaDoc e ) {
440                 throw new Error JavaDoc("Problem invoking macro : " + e );
441             }
442         }
443         else
444         {
445             throw new Error JavaDoc("No default output file available.\n");
446         }
447     }
448
449     /**
450      * Uses velocity to map a currently registered Velocimacro.
451      *
452      * @param macro_name - The name of the macro to invoke (map)
453      * @param id - The identifiant of the output file.
454      **/

455     public void
456     map(String JavaDoc macro_name, String JavaDoc id)
457     {
458         java.io.PrintWriter JavaDoc stream = (java.io.PrintWriter JavaDoc) streams_.get(id);
459         if (stream != null)
460             map(macro_name, new String JavaDoc[0], stream);
461         else
462             throw new Error JavaDoc("Not a valid ID.\n");
463     }
464
465     /**
466      * Print immediatly the text in the output stream.
467      *
468      * @param text - The text to be written.
469      **/

470     public void
471     print(String JavaDoc text)
472     {
473         if (stream_ != null)
474             print(text, stream_);
475         else
476             throw new Error JavaDoc("No default output file available.\n");
477     }
478
479     /**
480      * Print immediatly the text in the output stream.
481      *
482      * @param text - The text to be written.
483      * @param id - The identifiant of the output file.
484      **/

485     public void
486     print(String JavaDoc text, String JavaDoc id)
487     {
488         java.io.PrintWriter JavaDoc stream = (java.io.PrintWriter JavaDoc) streams_.get(id);
489         if (stream != null)
490             print(text, stream);
491         else
492             throw new Error JavaDoc("No default output file available.\n");
493     }
494
495     /**
496      * Return a string if nb spaces.
497      *
498      * @param nb - The number of spaces to print.
499      *
500      * @return A string containing nb spaces.
501      **/

502     public String JavaDoc
503     print_spaces(int nb)
504     {
505         String JavaDoc res = "";
506
507         for (int i=0; i<nb; i++)
508             res += " ";
509         return res;
510     }
511 }
512
Popular Tags