KickJava   Java API By Example, From Geeks To Geeks.

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


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.taskdefs.MatchingTask;
22 import org.apache.tools.ant.taskdefs.condition.Os;
23 import org.apache.tools.ant.types.FileSet;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.DirectoryScanner;
26
27 import java.io.File JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Enumeration JavaDoc;
31
32 /**
33  * refactoring of some stuff so that different things (like ILASM)
34  * can use shared code.
35  */

36 public class DotnetBaseMatchingTask extends MatchingTask {
37     // CheckStyle:VisibilityModifier OFF - bc
38
/**
39      * output file. If not supplied this is derived from the source file
40      */

41     protected File JavaDoc outputFile;
42     /**
43      * filesets of file to compile
44      */

45     protected Vector JavaDoc filesets = new Vector JavaDoc();
46
47     /**
48      * source directory upon which the search pattern is applied
49      */

50     protected File JavaDoc srcDir;
51
52     /**
53      * Are we running on Windows?
54      *
55      * @since Ant 1.6.3
56      */

57     // CheckStyle:ConstantNameCheck OFF - bc
58
protected static final boolean isWindows = Os.isFamily("windows");
59
60     // CheckStyle:ConstantNameCheck ON
61
// CheckStyle:VisibilityModifier ON
62

63     /**
64     * Overridden because we need to be able to set the srcDir.
65     * @return the source directory.
66     */

67     public File JavaDoc getSrcDir() {
68         return this.srcDir;
69     }
70
71     /**
72      * Set the source directory of the files to be compiled.
73      *
74      *@param srcDirName The new SrcDir value
75      */

76     public void setSrcDir(File JavaDoc srcDirName) {
77         this.srcDir = srcDirName;
78     }
79
80     /**
81      * Set the name of exe/library to create.
82      *
83      *@param file The new outputFile value
84      */

85     public void setDestFile(File JavaDoc file) {
86         outputFile = file;
87     }
88
89     /**
90      * add a new source directory to the compile
91      * @param src a fileset.
92      */

93     public void addSrc(FileSet src) {
94         filesets.add(src);
95     }
96
97     /**
98      * get the destination file
99      * @return the dest file or null for not assigned
100      */

101     public File JavaDoc getDestFile() {
102         return outputFile;
103     }
104
105     /**
106      * create the list of files
107      * @param command the command to create the files for.
108      * @param filesToBuild vector to add files to
109      * @param outputTimestamp timestamp to compare against
110      * @return number of files out of date
111      */

112     protected int buildFileList(NetCommand command, Hashtable JavaDoc filesToBuild, long outputTimestamp) {
113         int filesOutOfDate = 0;
114         boolean scanImplicitFileset
115             = getSrcDir() != null || filesets.size() == 0;
116         if (scanImplicitFileset) {
117             //scan for an implicit fileset if there was a srcdir set
118
//or there was no srcDir set but there was no contained classes
119
if (getSrcDir() == null) {
120                 //if there is no src dir here, set it
121
setSrcDir(getProject().resolveFile("."));
122             }
123             log("working from source directory " + getSrcDir(),
124                     Project.MSG_VERBOSE);
125             //get dependencies list.
126
DirectoryScanner scanner = getDirectoryScanner(getSrcDir());
127             filesOutOfDate = command.scanOneFileset(scanner,
128                     filesToBuild, outputTimestamp);
129         }
130         //get any included source directories
131
for (int i = 0; i < filesets.size(); i++) {
132             FileSet fs = (FileSet) filesets.elementAt(i);
133             filesOutOfDate += command.scanOneFileset(
134                     fs.getDirectoryScanner(getProject()),
135                     filesToBuild,
136                     outputTimestamp);
137         }
138
139         return filesOutOfDate;
140     }
141
142     /**
143      * add the list of files to a command
144      * @param filesToBuild vector of files
145      * @param command the command to append to
146      */

147     protected void addFilesToCommand(Hashtable JavaDoc filesToBuild, NetCommand command) {
148         int count = filesToBuild.size();
149         log("compiling " + count + " file" + ((count == 1) ? "" : "s"),
150                 Project.MSG_VERBOSE);
151         Enumeration JavaDoc files = filesToBuild.elements();
152         while (files.hasMoreElements()) {
153             File JavaDoc file = (File JavaDoc) files.nextElement();
154             command.addArgument(file.toString());
155         }
156     }
157
158     /**
159      * determine the timestamp of the output file
160      * @return a timestamp or 0 for no output file known/exists
161      */

162     protected long getOutputFileTimestamp() {
163         long outputTimestamp;
164         if (getDestFile() != null && getDestFile().exists()) {
165             outputTimestamp = getDestFile().lastModified();
166         } else {
167             outputTimestamp = 0;
168         }
169         return outputTimestamp;
170     }
171
172     /**
173      * finish off the command by adding all dependent files, execute
174      * @param command the command to update.
175      * @param ignoreTimestamps not used.
176      */

177     protected void addFilesAndExecute(NetCommand command, boolean ignoreTimestamps) {
178         long outputTimestamp = getOutputFileTimestamp();
179         Hashtable JavaDoc filesToBuild = new Hashtable JavaDoc();
180         int filesOutOfDate = buildFileList(command, filesToBuild, outputTimestamp);
181
182         //now run the command of exe + settings + files
183
if (filesOutOfDate > 0) {
184             //add the files to the command
185
addFilesToCommand(filesToBuild, command);
186             command.runCommand();
187         } else {
188             log("output file is up to date", Project.MSG_VERBOSE);
189         }
190     }
191
192
193
194 }
195
Popular Tags