KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > dotnet > ImportTypelib


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.dotnet;
20
21 import org.apache.tools.ant.Task;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.util.FileUtils;
25
26 import java.io.File JavaDoc;
27
28 /**
29  * Import a COM type library into the .NET framework.
30  * <p>
31  *
32  * This task is a wrapper to .NET's tlbimport; it imports a tlb file to a NET assembly
33  * by generating a binary assembly (.dll) that contains all the binding
34  * metadata. It uses date timestamps to minimise rebuilds.
35  * <p>
36  * Example
37  * <pre>
38  * &lt;importtypelib
39  * srcfile="xerces.tlb"
40  * destfile="xerces.dll"
41  * namespace="Apache.Xerces"/&gt;
42  * </pre>
43  * @since Ant 1.6
44  * @ant.task category="dotnet"
45  */

46 public class ImportTypelib extends Task {
47
48     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
49
50     /**
51      * input file; precedes options
52      */

53     private File JavaDoc srcFile;
54
55     /**
56      * /out:file
57      */

58     private File JavaDoc destFile;
59
60     /**
61      * /namespace:[string]
62      */

63     private String JavaDoc namespace;
64
65     /**
66      * /sysarray
67      */

68     private boolean useSysArray = false;
69
70     /**
71      * /unsafe
72      */

73     private boolean unsafe = false;
74
75     /**
76      * extra commands?
77      */

78     private String JavaDoc extraOptions = null;
79
80     /**
81      * This method names the output file.
82      *
83      * This is an operation which is required to have been performed.
84      * @param destFile the output file.
85      */

86     public void setDestFile(File JavaDoc destFile) {
87         this.destFile = destFile;
88     }
89
90     /**
91      * This method sets what namespace the typelib is to be in.
92      * This is an operation which is required to have been performed.
93      * @param namespace the namespace to use.
94      */

95     public void setNamespace(String JavaDoc namespace) {
96         this.namespace = namespace;
97     }
98
99     /**
100      * This method sets which is the source .tlb file.
101      * This is an operation which is required to have been performed.
102      * @param srcFile the source file.
103      */

104     public void setSrcFile(File JavaDoc srcFile) {
105         this.srcFile = srcFile;
106     }
107
108     /**
109      * do you want unsafe code.
110      * @param unsafe a <code>boolean</code> value.
111      */

112     public void setUnsafe(boolean unsafe) {
113         this.unsafe = unsafe;
114     }
115
116     /**
117      * set this to map a COM SafeArray to the System.Array class
118      * @param useSysArray a <code>boolean</code> value.
119      */

120     public void setUseSysArray(boolean useSysArray) {
121         this.useSysArray = useSysArray;
122     }
123
124     /**
125      * set any extra options that are not yet supported by this task.
126      * @param extraOptions the options to use.
127      */

128     public void setExtraOptions(String JavaDoc extraOptions) {
129         this.extraOptions = extraOptions;
130     }
131
132     /**
133      * validation code
134      * @throws BuildException if validation failed
135      */

136     protected void validate()
137             throws BuildException {
138         if (destFile == null) {
139             throw new BuildException("destination file must be specified");
140         }
141         if (destFile.isDirectory()) {
142             throw new BuildException(
143                     "destination file is a directory");
144         }
145         if (srcFile == null || !srcFile.exists()) {
146             throw new BuildException(
147                     "source file does not exist");
148         }
149         if (srcFile.isDirectory()) {
150             throw new BuildException(
151                     "source file is a directory");
152         }
153         if (namespace == null) {
154             throw new BuildException("No namespace");
155         }
156     }
157
158     /**
159      * Test for disassembly being needed; use existence and granularity
160      * correct date stamps
161      * @return true iff a rebuild is required.
162      */

163     private boolean isExecuteNeeded() {
164         if (!destFile.exists()) {
165             log("Destination file does not exist: a build is required",
166                     Project.MSG_VERBOSE);
167             return true;
168         }
169         long sourceTime = srcFile.lastModified();
170         long destTime = destFile.lastModified();
171         if (sourceTime > (destTime + FILE_UTILS.getFileTimestampGranularity())) {
172             log("Source file is newer than the dest file: a rebuild is required",
173                     Project.MSG_VERBOSE);
174             return true;
175         } else {
176             log("The output file is up to date", Project.MSG_VERBOSE);
177             return false;
178         }
179
180     }
181
182
183     /**
184      * Create a typelib command
185      * @exception BuildException if something goes wrong with the build
186      */

187     public void execute() throws BuildException {
188         log("This task is deprecated and will be removed in a future version\n"
189             + "of Ant. It is now part of the .NET Antlib:\n"
190             + "http://ant.apache.org/antlibs/dotnet/index.html",
191             Project.MSG_WARN);
192         validate();
193         log("Importing typelib " + srcFile
194             + " to assembly " + destFile
195             + " in namespace " + namespace, Project.MSG_VERBOSE);
196         //rebuild unless the dest file is newer than the source file
197
if (!isExecuteNeeded()) {
198             return;
199         }
200
201         NetCommand command = new NetCommand(this, "ImportTypelib", "tlbimp");
202         command.setFailOnError(true);
203         command.addArgument(srcFile.toString());
204         //fill in args
205
command.addArgument("/nologo");
206         command.addArgument("/out:" + destFile);
207         command.addArgument("/namespace:", namespace);
208         if (useSysArray) {
209             command.addArgument("/sysarray");
210         }
211         if (unsafe) {
212             command.addArgument("/unsafe");
213         }
214         command.addArgument(extraOptions);
215         command.runCommand();
216     }
217 }
218
Popular Tags