001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020:
021: package org.jacorb.idl;
022:
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.DirectoryScanner;
025: import org.apache.tools.ant.taskdefs.MatchingTask;
026: import org.apache.tools.ant.types.Path;
027: import org.apache.tools.ant.util.GlobPatternMapper;
028: import org.apache.tools.ant.util.SourceFileScanner;
029:
030: import java.util.*;
031:
032: import java.io.File;
033: import java.io.IOException;
034:
035: /**
036: * This is the idl compile task for using the idl compiler
037: * from the ANT build tool.
038: *
039: * @author Wei-ju Wu
040: * @version $Id: JacIDL.java,v 1.28 2006/06/26 14:37:45 alphonse.bendt Exp $
041: */
042:
043: public class JacIDL extends MatchingTask {
044: private File _destdir;
045: private File _srcdir;
046: private Path _includepath;
047:
048: private int _debuglevel;
049: private boolean _generateir;
050: private boolean _omgprefix;
051: private boolean _generateincluded;
052: private boolean _parseonly;
053: private boolean _noskel;
054: private boolean _nostub;
055: private boolean _sloppyforward;
056: private boolean _sloppynames;
057: private boolean _nofinal;
058: private boolean _ami_callback;
059: private boolean _force_overwrite;
060: private boolean _unchecked_narrow;
061: private boolean _generateEnhanced;
062: private boolean _generatediistubs;
063:
064: private List _defines = new ArrayList();
065: private List _undefines = new ArrayList();
066: private File _compileList[] = new File[0];
067: private List _i2jpackages = new ArrayList();
068:
069: private I2JPackageTagHandler i2jHandler = new I2JPackageTagHandler();
070:
071: public JacIDL() {
072: _destdir = new File(".");
073: _srcdir = new File(".");
074: _parseonly = false;
075: _generateir = false;
076: _noskel = false;
077: _nostub = false;
078: _generateincluded = false;
079: _nofinal = false;
080: _force_overwrite = false;
081: _ami_callback = false;
082: _unchecked_narrow = false;
083: _generatediistubs = false;
084: _debuglevel = 1;
085: }
086:
087: /**
088: * Set the destination directory.
089: * @param dir the destination directory
090: */
091: public void setDestdir(File dir) {
092:
093: _destdir = dir;
094: }
095:
096: /**
097: * Set the source directory.
098: * @param dir the source directory
099: */
100: public void setSrcdir(File dir) {
101:
102: _srcdir = dir;
103: }
104:
105: /**
106: * Set the include path for the idl compiler.
107: * @param path the include path
108: */
109: public void setIncludepath(Path path) {
110:
111: _includepath = path;
112: }
113:
114: /**
115: * Set the debug level.
116: * @param level the debug level
117: */
118: public void setDebuglevel(int level) {
119: _debuglevel = level;
120: }
121:
122: // ****************************************************************
123: // **** Set the flags
124: // ******************************
125:
126: /**
127: * Set the flag to generate the interface repository files.
128: * @param flag the flag
129: */
130: public void setGenerateir(boolean flag) {
131:
132: _generateir = flag;
133: }
134:
135: /**
136: * Set the flag to use the omg package prefix
137: * @param flag the flag
138: */
139: public void setOmgprefix(boolean flag) {
140: _omgprefix = flag;
141: }
142:
143: /**
144: * Set the flag to generate all files.
145: * @param flag the flag
146: */
147: public void setAll(boolean flag) {
148:
149: _generateincluded = flag;
150: }
151:
152: /**
153: * Set the flag to parse the idl only.
154: * @param flag the flag
155: */
156: public void setParseonly(boolean flag) {
157:
158: _parseonly = flag;
159: }
160:
161: /**
162: * Set the flag to leave out skeleton generation.
163: * @param flag the flag
164: */
165: public void setNoskel(boolean flag) {
166: _noskel = flag;
167: }
168:
169: /**
170: * Set the flag to leave out stub generation.
171: * @param flag the flag
172: */
173: public void setNostub(boolean flag) {
174:
175: _nostub = flag;
176: }
177:
178: /**
179: * Set the flag to use sloppy forwards.
180: * @param flag the flag
181: */
182: public void setSloppyforward(boolean flag) {
183:
184: _sloppyforward = flag;
185: }
186:
187: /**
188: * Set the flag to use sloppy names.
189: * @param flag the flag
190: */
191: public void setSloppynames(boolean flag) {
192:
193: _sloppynames = flag;
194: }
195:
196: /**
197: * Setter for 'nofinal' property that indicates whether generated code should have
198: * a final class definition.
199: * @param nofinal <code>true</true> for definitions that are not final.
200: */
201: public void setNofinal(boolean flag) {
202: _nofinal = flag;
203: }
204:
205: /**
206: * Sets the flag to generate AMI callbacks.
207: */
208: public void setAmi_callback(boolean flag) {
209: _ami_callback = flag;
210: }
211:
212: /**
213: * Sets the flag to overwrite existing files.
214: */
215: public void setForceOverwrite(boolean flag) {
216: _force_overwrite = flag;
217: }
218:
219: /**
220: * Sets the flag to generated unchecked narrow() calls in stubs
221: */
222: public void setUncheckedNarrow(boolean flag) {
223: _unchecked_narrow = flag;
224: }
225:
226: /**
227: * Sets the flag to generated enhanced stubs
228: */
229: public void setGenerateEnhanced(boolean flag) {
230: _generateEnhanced = flag;
231: }
232:
233: public void setGenerateDIIStubs(boolean flag) {
234: _generatediistubs = flag;
235: }
236:
237: // ****************************************************************
238: // **** Nested elements
239: // ******************************
240:
241: public void addDefine(
242: org.apache.tools.ant.types.Environment.Variable def) {
243: // The variable can only be evaluated in the execute() method
244: _defines.add(def);
245: }
246:
247: public void addUndefine(
248: org.apache.tools.ant.types.Environment.Variable def) {
249: // The variable can only be evaluated in the execute() method
250: _undefines.add(def);
251: }
252:
253: /**
254: * Will be called whenever an <i2jpackage> nested PCDATA element is encountered.
255: */
256: public org.jacorb.idl.JacIDL.I2JPackageTagHandler createI2jpackage() {
257: return i2jHandler;
258: }
259:
260: /**
261: * Inner class that will read the i2jpackage tags.
262: *
263: * The format for these will be <i2jpackage names="x:y"/>.
264: * @see #createI2jpackage()
265: */
266: public class I2JPackageTagHandler {
267: /**
268: * Handle the names="packagefrom:packageto" attribute of the i2jpackage element.
269: * @param names the packagefrom:packageto value.
270: */
271: public void setNames(String names) {
272: _i2jpackages.add(names);
273: }
274: }
275:
276: // *****************************************************************
277:
278: /**
279: * The execute() method of the task.
280: * @throws BuildException
281: */
282: public void execute() throws BuildException {
283: parser myparser = null;
284:
285: parser.init();
286:
287: // set destination directory
288: if (!_destdir.exists()) {
289: _destdir.mkdirs();
290: }
291: parser.out_dir = _destdir.getPath();
292:
293: // Generate code for all IDL files, even included ones
294: parser.generateIncluded = _generateincluded;
295:
296: // generate interface repository
297: parser.generateIR = _generateir;
298:
299: // parse only
300: parser.parse_only = _parseonly;
301:
302: // no skeletons
303: parser.generate_skeletons = (!_noskel);
304:
305: // no stubs
306: parser.generate_stubs = (!_nostub);
307:
308: // sloppy forwards
309: parser.sloppy = _sloppyforward;
310:
311: // sloppy names
312: parser.strict_names = (!_sloppynames);
313:
314: // nofinal
315: parser.setGenerateFinalCode(!_nofinal);
316:
317: // AMI callback model
318: parser.generate_ami_callback = _ami_callback;
319:
320: //
321: parser.forceOverwrite = _force_overwrite;
322:
323: parser.useUncheckedNarrow = _unchecked_narrow;
324:
325: parser.generateEnhanced = _generateEnhanced;
326:
327: parser.generateDiiStubs = _generatediistubs;
328:
329: // include path
330: if (_includepath != null) {
331:
332: // Check path
333: String includeList[] = _includepath.list();
334: for (int i = 0; i < includeList.length; i++) {
335:
336: File incDir = project.resolveFile(includeList[i]);
337: if (!incDir.exists()) {
338:
339: throw new BuildException("include directory \""
340: + incDir.getPath() + "\" does not exist !",
341: location);
342: }
343: }
344: GlobalInputStream.setIncludePath(_includepath.toString());
345: }
346:
347: // omg package prefix
348: if (_omgprefix) {
349: parser.package_prefix = "org.omg";
350: }
351:
352: // Add the i2jpackage values to the parser
353: for (int i = 0; i < _i2jpackages.size(); i++) {
354: parser.addI2JPackage((String) _i2jpackages.get(i));
355: }
356:
357: // Set the logging priority
358: parser.getLogger().setPriority(
359: Environment.intToPriority(_debuglevel));
360:
361: // setup input file lists
362: resetFileLists();
363: DirectoryScanner ds = getDirectoryScanner(_srcdir);
364: String files[] = ds.getIncludedFiles();
365: scanFiles(files);
366:
367: // ***********************************
368: // **** invoke parser
369: // ***********************************
370: // invoke the parser for parsing the files that were
371: // specified in the task specification
372: try {
373: if (_compileList != null) {
374:
375: for (int i = 0; i < _compileList.length; i++) {
376:
377: // setup the parser
378: String fileName = _compileList[i].getPath();
379: log("processing idl file: " + fileName);
380:
381: GlobalInputStream.init();
382: GlobalInputStream.setInput(fileName);
383: lexer.reset();
384: NameTable.init();
385: ConstDecl.init();
386: TypeMap.init();
387: setupDefines();
388:
389: myparser = new parser();
390: myparser.parse();
391: }
392: }
393:
394: } catch (IOException ex) {
395: if (_debuglevel >= 3) {
396: ex.printStackTrace();
397: }
398: throw new BuildException();
399: } catch (ParseException ex) {
400: if (_debuglevel >= 3) {
401: ex.printStackTrace();
402: }
403: throw new BuildException();
404: } catch (Exception ex) {
405: if (_debuglevel >= 3) {
406: ex.printStackTrace();
407: }
408: throw new BuildException();
409: }
410: }
411:
412: /**
413: * Clear the list of files to be compiled and copied..
414: */
415: protected void resetFileLists() {
416: _compileList = new File[0];
417: }
418:
419: /**
420: * Scans the directory looking for source files to be compiled.
421: * The results are returned in the class variable compileList
422: */
423: protected void scanFiles(String files[]) throws BuildException {
424: File file;
425:
426: // TODO: create an own pattern mapper
427: GlobPatternMapper m = new GlobPatternMapper();
428: m.setFrom("*.idl");
429: m.setTo("*.java");
430: SourceFileScanner sfs = new SourceFileScanner(this );
431: File[] newfiles = sfs.restrictAsFiles(files, _srcdir, _destdir,
432: m);
433: _compileList = new File[newfiles.length];
434:
435: for (int i = 0; i < newfiles.length; i++) {
436: log("scan file: " + newfiles[i].getPath());
437: file = newfiles[i];
438: if (!file.exists()) {
439:
440: throw new BuildException("The input file \""
441: + file.getPath() + "\" does not exist !");
442: }
443: _compileList[i] = file;
444: }
445: }
446:
447: public File[] getFileList() {
448:
449: return _compileList;
450: }
451:
452: private void setupDefines() {
453: org.apache.tools.ant.types.Environment.Variable prop;
454: String value;
455:
456: for (int i = 0; i < _defines.size(); i++) {
457: prop = (org.apache.tools.ant.types.Environment.Variable) _defines
458: .get(i);
459: value = prop.getValue();
460: if (value == null)
461: value = "1";
462: lexer.define(prop.getKey(), value);
463: }
464: for (int i = 0; i < _undefines.size(); i++) {
465:
466: prop = (org.apache.tools.ant.types.Environment.Variable) _undefines
467: .get(i);
468: lexer.undefine(prop.getKey());
469: }
470: }
471: }
|