KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > JacIDL


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.idl;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.DirectoryScanner;
25 import org.apache.tools.ant.taskdefs.MatchingTask;
26 import org.apache.tools.ant.types.Path;
27 import org.apache.tools.ant.util.GlobPatternMapper;
28 import org.apache.tools.ant.util.SourceFileScanner;
29
30 import java.util.*;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 /**
36  * This is the idl compile task for using the idl compiler
37  * from the ANT build tool.
38  *
39  * @author Wei-ju Wu
40  * @version $Id: JacIDL.java,v 1.24 2004/05/06 12:39:58 nicolas Exp $
41  */

42
43 public class JacIDL
44     extends MatchingTask
45 {
46     private File JavaDoc _destdir;
47     private File JavaDoc _srcdir;
48     private Path _includepath;
49
50     private int _debuglevel;
51     private boolean _generateir;
52     private boolean _omgprefix;
53     private boolean _generateincluded;
54     private boolean _parseonly;
55     private boolean _noskel;
56     private boolean _nostub;
57     private boolean _sloppyforward;
58     private boolean _sloppynames;
59     private boolean _includestate;
60     private boolean _nofinal;
61     private boolean _ami_callback;
62     private boolean _force_overwrite;
63     private boolean _unchecked_narrow;
64
65     private List _defines = new ArrayList();
66     private List _undefines = new ArrayList();
67     private File JavaDoc _compileList[] = new File JavaDoc[ 0 ];
68     private List _i2jpackages = new ArrayList();
69
70     private I2JPackageTagHandler i2jHandler = new I2JPackageTagHandler();
71
72     public JacIDL()
73     {
74         _destdir = new File JavaDoc(".");
75         _srcdir = new File JavaDoc(".");
76         _parseonly = false;
77         _generateir = false;
78         _noskel = false;
79         _nostub = false;
80         _generateincluded = false;
81         _nofinal = false;
82         _force_overwrite = false;
83         _ami_callback = false;
84         _unchecked_narrow = false;
85         _debuglevel = 1;
86     }
87
88     /**
89      * Set the destination directory.
90      * @param dir the destination directory
91      */

92     public void setDestdir(File JavaDoc dir)
93     {
94
95         _destdir = dir;
96     }
97
98     /**
99      * Set the source directory.
100      * @param dir the source directory
101      */

102     public void setSrcdir(File JavaDoc dir)
103     {
104
105         _srcdir = dir;
106     }
107
108     /**
109      * Set the include path for the idl compiler.
110      * @param path the include path
111      */

112     public void setIncludepath(Path path)
113     {
114
115         _includepath = path;
116     }
117
118
119     /**
120      * Set the debug level.
121      * @param level the debug level
122      */

123     public void setDebuglevel(int level)
124     {
125         _debuglevel = level;
126     }
127
128     // ****************************************************************
129
// **** Set the flags
130
// ******************************
131

132     /**
133      * Set the flag to generate the interface repository files.
134      * @param flag the flag
135      */

136     public void setGenerateir(boolean flag)
137     {
138
139         _generateir = flag;
140     }
141
142     /**
143      * Set the flag to use the omg package prefix
144      * @param flag the flag
145      */

146     public void setOmgprefix(boolean flag)
147     {
148         _omgprefix = flag;
149     }
150
151     /**
152      * Set the flag to generate all files.
153      * @param flag the flag
154      */

155     public void setAll(boolean flag)
156     {
157
158         _generateincluded = flag;
159     }
160
161     /**
162      * Set the flag to parse the idl only.
163      * @param flag the flag
164      */

165     public void setParseonly(boolean flag)
166     {
167
168         _parseonly = flag;
169     }
170
171
172     /**
173      * Set the flag to leave out skeleton generation.
174      * @param flag the flag
175      */

176     public void setNoskel(boolean flag)
177     {
178         _noskel = flag;
179     }
180
181     /**
182      * Set the flag to leave out stub generation.
183      * @param flag the flag
184      */

185     public void setNostub(boolean flag)
186     {
187
188         _nostub = flag;
189     }
190
191     /**
192      * Set the flag to use sloppy forwards.
193      * @param flag the flag
194      */

195     public void setSloppyforward(boolean flag)
196     {
197
198         _sloppyforward = flag;
199     }
200
201     /**
202      * Set the flag to use sloppy names.
203      * @param flag the flag
204      */

205     public void setSloppynames(boolean flag)
206     {
207
208         _sloppynames = flag;
209     }
210
211     /**
212      * Setter for 'nofinal' property that indicates whether generated code should have
213      * a final class definition.
214      * @param nofinal <code>true</true> for definitions that are not final.
215      */

216     public void setNofinal(boolean flag)
217     {
218         _nofinal = flag;
219     }
220
221     /**
222      * Sets the flag to generate AMI callbacks.
223      */

224     public void setAmi_callback(boolean flag)
225     {
226         _ami_callback = flag;
227     }
228
229     /**
230      * Sets the flag to overwrite existing files.
231      */

232     public void setForceOverwrite(boolean flag)
233     {
234         _force_overwrite = flag;
235     }
236
237     /**
238      * Sets the flag to generated unchecked narrow() calls in stubs
239      */

240     public void setUncheckedNarrow(boolean flag)
241     {
242         _unchecked_narrow = flag;
243     }
244
245     // ****************************************************************
246
// **** Nested elements
247
// ******************************
248

249     public void addDefine(org.apache.tools.ant.types.Environment.Variable
250                            def)
251     {
252         // The variable can only be evaluated in the execute() method
253
_defines.add(def);
254     }
255
256     public void addUndefine(org.apache.tools.ant.types.Environment.Variable
257                              def)
258     {
259         // The variable can only be evaluated in the execute() method
260
_undefines.add(def);
261     }
262
263     /**
264     * Will be called whenever an <i2jpackage> nested PCDATA element is encountered.
265     */

266     public org.jacorb.idl.JacIDL.I2JPackageTagHandler createI2jpackage()
267     {
268         return i2jHandler;
269     }
270
271     /**
272     * Inner class that will read the i2jpackage tags.
273     *
274     * The format for these will be <i2jpackage names="x:y"/>.
275     * @see #createI2jpackage()
276     */

277     public class I2JPackageTagHandler
278     {
279         /**
280         * Handle the names="packagefrom:packageto" attribute of the i2jpackage element.
281         * @param names the packagefrom:packageto value.
282         */

283         public void setNames(String JavaDoc names)
284         {
285             _i2jpackages.add(names);
286         }
287     }
288
289     // *****************************************************************
290

291     /**
292      * The execute() method of the task.
293      * @throws BuildException
294      */

295     public void execute() throws BuildException
296     {
297         parser myparser = null;
298
299         parser.init ();
300
301         // set destination directory
302
if (! _destdir.exists ())
303         {
304             _destdir.mkdirs ();
305         }
306         parser.out_dir = _destdir.getPath();
307
308         // Generate code for all IDL files, even included ones
309
parser.generateIncluded = _generateincluded;
310
311         // generate interface repository
312
parser.generateIR = _generateir;
313
314         // parse only
315
parser.parse_only = _parseonly;
316
317         // no skeletons
318
parser.generate_skeletons = (!_noskel);
319
320         // no stubs
321
parser.generate_stubs = (!_nostub);
322
323         // sloppy forwards
324
parser.sloppy = _sloppyforward;
325
326         // sloppy names
327
parser.strict_names = (!_sloppynames);
328
329         // nofinal
330
parser.setGenerateFinalCode(!_nofinal);
331
332         // AMI callback model
333
parser.generate_ami_callback = _ami_callback;
334
335         //
336
parser.forceOverwrite = _force_overwrite;
337
338         parser.useUncheckedNarrow = _unchecked_narrow;
339
340         // include path
341
if (_includepath != null)
342         {
343
344             // Check path
345
String JavaDoc includeList[] = _includepath.list();
346             for (int i = 0; i < includeList.length; i++)
347             {
348
349                 File JavaDoc incDir = project.resolveFile(includeList[ i ]);
350                 if (!incDir.exists())
351                 {
352
353                     throw new BuildException("include directory \"" +
354                                               incDir.getPath() + "\" does not exist !", location);
355                 }
356             }
357             GlobalInputStream.setIncludePath(_includepath.toString());
358         }
359
360         // omg package prefix
361
if (_omgprefix)
362         {
363             parser.package_prefix = "org.omg";
364         }
365
366         // Add the i2jpackage values to the parser
367
for (int i = 0; i < _i2jpackages.size(); i++)
368         {
369             parser.addI2JPackage((String JavaDoc) _i2jpackages.get(i));
370         }
371
372         // Set the logging priority
373
parser.getLogger().setPriority(Environment.intToPriority(_debuglevel));
374
375         // setup input file lists
376
resetFileLists();
377         DirectoryScanner ds = getDirectoryScanner(_srcdir);
378         String JavaDoc files[] = ds.getIncludedFiles();
379         scanFiles(files);
380
381         // ***********************************
382
// **** invoke parser
383
// ***********************************
384
// invoke the parser for parsing the files that were
385
// specified in the task specification
386
try
387         {
388             if (_compileList != null)
389             {
390
391                 for (int i = 0; i < _compileList.length; i++)
392                 {
393
394                     // setup the parser
395
String JavaDoc fileName = _compileList[ i ].getPath();
396                     log("processing idl file: " + fileName);
397
398                     GlobalInputStream.init();
399                     GlobalInputStream.setInput(fileName);
400                     lexer.reset();
401                     NameTable.init();
402                     ConstDecl.init();
403                     TypeMap.init();
404                     setupDefines();
405
406                     myparser = new parser();
407                     myparser.parse();
408                 }
409             }
410
411         }
412         catch (IOException JavaDoc ex)
413         {
414             System.err.println(ex);
415             throw new BuildException();
416         }
417         catch (ParseException ex)
418         {
419             System.err.println(ex);
420             throw new BuildException();
421         }
422         catch (Exception JavaDoc ex)
423         {
424             System.err.println(ex);
425             throw new BuildException();
426         }
427     }
428
429     /**
430      * Clear the list of files to be compiled and copied..
431      */

432     protected void resetFileLists()
433     {
434         _compileList = new File JavaDoc[ 0 ];
435     }
436
437     /**
438      * Scans the directory looking for source files to be compiled.
439      * The results are returned in the class variable compileList
440      */

441     protected void scanFiles(String JavaDoc files[]) throws BuildException
442     {
443         File JavaDoc file;
444
445         // TODO: create an own pattern mapper
446
GlobPatternMapper m = new GlobPatternMapper();
447         m.setFrom("*.idl");
448         m.setTo("*.java");
449         SourceFileScanner sfs = new SourceFileScanner(this);
450         File JavaDoc[] newfiles = sfs.restrictAsFiles(files, _srcdir, _destdir, m);
451         _compileList = new File JavaDoc[ newfiles.length ];
452
453         for (int i = 0; i < newfiles.length; i++)
454         {
455             log("scan file: " + newfiles[ i ].getPath());
456             file = newfiles[ i ];
457             if (!file.exists())
458             {
459
460                 throw new BuildException("The input file \"" + file.getPath() +
461                                           "\" does not exist !");
462             }
463             _compileList[ i ] = file;
464         }
465     }
466
467     public File JavaDoc[] getFileList()
468     {
469
470         return _compileList;
471     }
472
473     private static boolean fileExists(String JavaDoc filename)
474     {
475
476         if (filename == null || filename.length() == 0) return false;
477         File JavaDoc file = new File JavaDoc(filename);
478         return (file.exists() && file.isFile());
479     }
480
481     private static boolean dirExists(File JavaDoc dir)
482     {
483
484         return (dir.exists() && dir.isDirectory());
485     }
486
487     private void setupDefines()
488     {
489         org.apache.tools.ant.types.Environment.Variable prop;
490         String JavaDoc value;
491
492         for (int i = 0; i < _defines.size(); i++)
493         {
494             prop = (org.apache.tools.ant.types.Environment.Variable)
495                 _defines.get(i);
496             value = prop.getValue();
497             if (value == null)
498                 value = "1";
499             lexer.define(prop.getKey(), value);
500         }
501         for (int i = 0; i < _undefines.size(); i++)
502         {
503
504             prop = (org.apache.tools.ant.types.Environment.Variable)
505                 _undefines.get(i);
506             lexer.undefine(prop.getKey());
507         }
508     }
509 }
510
Popular Tags