1 package com4j.tlbimp.driver; 2 3 import com4j.COM4J; 4 import com4j.ComException; 5 import com4j.tlbimp.BindingException; 6 import com4j.tlbimp.ErrorListener; 7 import com4j.tlbimp.FileCodeWriter; 8 import com4j.tlbimp.TypeLibInfo; 9 import com4j.tlbimp.def.IWTypeLib; 10 import org.apache.tools.ant.BuildException; 11 import org.apache.tools.ant.Project; 12 import org.apache.tools.ant.Task; 13 14 import java.io.File ; 15 import java.io.IOException ; 16 17 22 public class AntTaskImpl extends Task implements ErrorListener { 23 24 private File destDir; 25 private File source; 26 27 private String libid; 28 private String libver; 29 30 private Driver driver = new Driver(); 31 32 33 public void setDestDir(File destDir) { 34 this.destDir = destDir; 35 } 36 37 public void setPackage(String packageName) { 38 driver.setPackageName(packageName); 39 } 40 41 public void setFile(File source) { 42 this.source = source; 43 } 44 45 public void setLibid(String libid) { 46 this.libid = libid; 47 } 48 49 public void setLibver(String libver) { 50 this.libver = libver; 51 } 52 53 public void addConfiguredRef( Ref r ) { 54 r.validate(); 55 driver.addRef(r); 56 } 57 58 public void execute() throws BuildException { 59 if( destDir==null ) 60 throw new BuildException("@destDir is missing"); 61 if( source==null ) { 62 if( libid==null ) 63 throw new BuildException("both @file and @libid is missing"); 64 try { 65 TypeLibInfo tli = TypeLibInfo.locate(libid,libver); 66 log("The type library is "+tli.libName+" <"+tli.version+">", Project.MSG_INFO); 67 source = tli.typeLibrary; 68 } catch (BindingException e) { 69 error(e); 70 return; 71 } 72 } 73 74 try { 75 log("Generating definitions from "+source, Project.MSG_INFO); 76 77 driver.run( 78 COM4J.loadTypeLibrary(source).queryInterface(IWTypeLib.class), 79 new FileCodeWriter(destDir), 80 this); 81 } catch( ComException e ) { 82 throw new BuildException(e); 83 } catch( IOException e ) { 84 throw new BuildException(e); 85 } catch( BindingException e ) { 86 throw new BuildException(e); 87 } 88 } 89 90 public void error(BindingException e) { 91 log(e.getMessage(),Project.MSG_ERR); 92 } 93 94 public void warning(String message) { 95 log(message,Project.MSG_WARN); 96 } 97 } 98 | Popular Tags |