1 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 ; 33 import java.io.IOException ; 34 35 42 43 public class JacIDL 44 extends MatchingTask 45 { 46 private File _destdir; 47 private File _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 _compileList[] = new File [ 0 ]; 68 private List _i2jpackages = new ArrayList(); 69 70 private I2JPackageTagHandler i2jHandler = new I2JPackageTagHandler(); 71 72 public JacIDL() 73 { 74 _destdir = new File ("."); 75 _srcdir = new File ("."); 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 92 public void setDestdir(File dir) 93 { 94 95 _destdir = dir; 96 } 97 98 102 public void setSrcdir(File dir) 103 { 104 105 _srcdir = dir; 106 } 107 108 112 public void setIncludepath(Path path) 113 { 114 115 _includepath = path; 116 } 117 118 119 123 public void setDebuglevel(int level) 124 { 125 _debuglevel = level; 126 } 127 128 132 136 public void setGenerateir(boolean flag) 137 { 138 139 _generateir = flag; 140 } 141 142 146 public void setOmgprefix(boolean flag) 147 { 148 _omgprefix = flag; 149 } 150 151 155 public void setAll(boolean flag) 156 { 157 158 _generateincluded = flag; 159 } 160 161 165 public void setParseonly(boolean flag) 166 { 167 168 _parseonly = flag; 169 } 170 171 172 176 public void setNoskel(boolean flag) 177 { 178 _noskel = flag; 179 } 180 181 185 public void setNostub(boolean flag) 186 { 187 188 _nostub = flag; 189 } 190 191 195 public void setSloppyforward(boolean flag) 196 { 197 198 _sloppyforward = flag; 199 } 200 201 205 public void setSloppynames(boolean flag) 206 { 207 208 _sloppynames = flag; 209 } 210 211 216 public void setNofinal(boolean flag) 217 { 218 _nofinal = flag; 219 } 220 221 224 public void setAmi_callback(boolean flag) 225 { 226 _ami_callback = flag; 227 } 228 229 232 public void setForceOverwrite(boolean flag) 233 { 234 _force_overwrite = flag; 235 } 236 237 240 public void setUncheckedNarrow(boolean flag) 241 { 242 _unchecked_narrow = flag; 243 } 244 245 249 public void addDefine(org.apache.tools.ant.types.Environment.Variable 250 def) 251 { 252 _defines.add(def); 254 } 255 256 public void addUndefine(org.apache.tools.ant.types.Environment.Variable 257 def) 258 { 259 _undefines.add(def); 261 } 262 263 266 public org.jacorb.idl.JacIDL.I2JPackageTagHandler createI2jpackage() 267 { 268 return i2jHandler; 269 } 270 271 277 public class I2JPackageTagHandler 278 { 279 283 public void setNames(String names) 284 { 285 _i2jpackages.add(names); 286 } 287 } 288 289 291 295 public void execute() throws BuildException 296 { 297 parser myparser = null; 298 299 parser.init (); 300 301 if (! _destdir.exists ()) 303 { 304 _destdir.mkdirs (); 305 } 306 parser.out_dir = _destdir.getPath(); 307 308 parser.generateIncluded = _generateincluded; 310 311 parser.generateIR = _generateir; 313 314 parser.parse_only = _parseonly; 316 317 parser.generate_skeletons = (!_noskel); 319 320 parser.generate_stubs = (!_nostub); 322 323 parser.sloppy = _sloppyforward; 325 326 parser.strict_names = (!_sloppynames); 328 329 parser.setGenerateFinalCode(!_nofinal); 331 332 parser.generate_ami_callback = _ami_callback; 334 335 parser.forceOverwrite = _force_overwrite; 337 338 parser.useUncheckedNarrow = _unchecked_narrow; 339 340 if (_includepath != null) 342 { 343 344 String includeList[] = _includepath.list(); 346 for (int i = 0; i < includeList.length; i++) 347 { 348 349 File 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 if (_omgprefix) 362 { 363 parser.package_prefix = "org.omg"; 364 } 365 366 for (int i = 0; i < _i2jpackages.size(); i++) 368 { 369 parser.addI2JPackage((String ) _i2jpackages.get(i)); 370 } 371 372 parser.getLogger().setPriority(Environment.intToPriority(_debuglevel)); 374 375 resetFileLists(); 377 DirectoryScanner ds = getDirectoryScanner(_srcdir); 378 String files[] = ds.getIncludedFiles(); 379 scanFiles(files); 380 381 try 387 { 388 if (_compileList != null) 389 { 390 391 for (int i = 0; i < _compileList.length; i++) 392 { 393 394 String 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 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 ex) 423 { 424 System.err.println(ex); 425 throw new BuildException(); 426 } 427 } 428 429 432 protected void resetFileLists() 433 { 434 _compileList = new File [ 0 ]; 435 } 436 437 441 protected void scanFiles(String files[]) throws BuildException 442 { 443 File file; 444 445 GlobPatternMapper m = new GlobPatternMapper(); 447 m.setFrom("*.idl"); 448 m.setTo("*.java"); 449 SourceFileScanner sfs = new SourceFileScanner(this); 450 File [] newfiles = sfs.restrictAsFiles(files, _srcdir, _destdir, m); 451 _compileList = new File [ 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 [] getFileList() 468 { 469 470 return _compileList; 471 } 472 473 private static boolean fileExists(String filename) 474 { 475 476 if (filename == null || filename.length() == 0) return false; 477 File file = new File (filename); 478 return (file.exists() && file.isFile()); 479 } 480 481 private static boolean dirExists(File 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 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 |