KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com4j > tlbimp > driver > AntTaskImpl


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 JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * tlbimp implemented as an Ant task.
19  *
20  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
21  */

22 public class AntTaskImpl extends Task implements ErrorListener {
23
24     private File JavaDoc destDir;
25     private File JavaDoc source;
26
27     private String JavaDoc libid;
28     private String JavaDoc libver;
29
30     private Driver driver = new Driver();
31
32
33     public void setDestDir(File JavaDoc destDir) {
34         this.destDir = destDir;
35     }
36
37     public void setPackage(String JavaDoc packageName) {
38         driver.setPackageName(packageName);
39     }
40
41     public void setFile(File JavaDoc source) {
42         this.source = source;
43     }
44
45     public void setLibid(String JavaDoc libid) {
46         this.libid = libid;
47     }
48
49     public void setLibver(String JavaDoc 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 JavaDoc 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 JavaDoc message) {
95         log(message,Project.MSG_WARN);
96     }
97 }
98
Popular Tags