KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > clearcase > CCMkattr


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.clearcase;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.taskdefs.Execute;
24 import org.apache.tools.ant.types.Commandline;
25 import org.apache.tools.ant.taskdefs.condition.Os;
26
27 /**
28  * Task to perform mkattr command to ClearCase.
29  * <p>
30  * The following attributes are interpreted:
31  * <table border="1">
32  * <tr>
33  * <th>Attribute</th>
34  * <th>Values</th>
35  * <th>Required</th>
36  * </tr>
37  * <tr>
38  * <td>viewpath</td>
39  * <td>Path to the ClearCase view file or directory that the command will operate on</td>
40  * <td>Yes</td>
41  * <tr>
42  * <tr>
43  * <td>replace</td>
44  * <td>Replace the value of the attribute if it already exists</td>
45  * <td>No</td>
46  * <tr>
47  * <tr>
48  * <td>recurse</td>
49  * <td>Process each subdirectory under viewpath</td>
50  * <td>No</td>
51  * <tr>
52  * <tr>
53  * <td>version</td>
54  * <td>Identify a specific version to attach the attribute to</td>
55  * <td>No</td>
56  * <tr>
57  * <tr>
58  * <td>typename</td>
59  * <td>Name of the attribute type</td>
60  * <td>Yes</td>
61  * <tr>
62  * <tr>
63  * <td>typevalue</td>
64  * <td>Value to attach to the attribute type</td>
65  * <td>Yes</td>
66  * <tr>
67  * <tr>
68  * <td>comment</td>
69  * <td>Specify a comment. Only one of comment or cfile may be used.</td>
70  * <td>No</td>
71  * <tr>
72  * <tr>
73  * <td>commentfile</td>
74  * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
75  * <td>No</td>
76  * <tr>
77  * <tr>
78  * <td>failonerr</td>
79  * <td>Throw an exception if the command fails. Default is true</td>
80  * <td>No</td>
81  * <tr>
82  * </table>
83  *
84  */

85 public class CCMkattr extends ClearCase {
86     private boolean mReplace = false;
87     private boolean mRecurse = false;
88     private String JavaDoc mVersion = null;
89     private String JavaDoc mTypeName = null;
90     private String JavaDoc mTypeValue = null;
91     private String JavaDoc mComment = null;
92     private String JavaDoc mCfile = null;
93
94     /**
95      * Executes the task.
96      * <p>
97      * Builds a command line to execute cleartool and then calls Exec's run method
98      * to execute the command line.
99      * @throws BuildException if the command fails and failonerr is set to true
100      */

101     public void execute() throws BuildException {
102         Commandline commandLine = new Commandline();
103         Project aProj = getProject();
104         int result = 0;
105
106         // Check for required attributes
107
if (getTypeName() == null) {
108             throw new BuildException("Required attribute TypeName not specified");
109         }
110         if (getTypeValue() == null) {
111             throw new BuildException("Required attribute TypeValue not specified");
112         }
113         // Default the viewpath to basedir if it is not specified
114
if (getViewPath() == null) {
115             setViewPath(aProj.getBaseDir().getPath());
116         }
117
118         // build the command line from what we got. the format is
119
// cleartool mkattr [options...] [viewpath ...]
120
// as specified in the CLEARTOOL help
121
commandLine.setExecutable(getClearToolCommand());
122         commandLine.createArgument().setValue(COMMAND_MKATTR);
123
124         checkOptions(commandLine);
125
126         if (!getFailOnErr()) {
127             getProject().log("Ignoring any errors that occur for: "
128                     + getViewPathBasename(), Project.MSG_VERBOSE);
129         }
130
131         // For debugging
132
// System.out.println(commandLine.toString());
133

134         result = run(commandLine);
135         if (Execute.isFailure(result) && getFailOnErr()) {
136             String JavaDoc msg = "Failed executing: " + commandLine.toString();
137             throw new BuildException(msg, getLocation());
138         }
139     }
140
141
142     /**
143      * Check the command line options.
144      */

145     private void checkOptions(Commandline cmd) {
146         if (getReplace()) {
147             // -replace
148
cmd.createArgument().setValue(FLAG_REPLACE);
149         }
150
151         if (getRecurse()) {
152             // -recurse
153
cmd.createArgument().setValue(FLAG_RECURSE);
154         }
155
156         if (getVersion() != null) {
157             // -version
158
getVersionCommand(cmd);
159         }
160
161         if (getComment() != null) {
162             // -c
163
getCommentCommand(cmd);
164         } else {
165             if (getCommentFile() != null) {
166                 // -cfile
167
getCommentFileCommand(cmd);
168             } else {
169                 cmd.createArgument().setValue(FLAG_NOCOMMENT);
170             }
171         }
172
173         if (getTypeName() != null) {
174             // type
175
getTypeCommand(cmd);
176         }
177         if (getTypeValue() != null) {
178             // type value
179
getTypeValueCommand(cmd);
180         }
181         // viewpath
182
cmd.createArgument().setValue(getViewPath());
183     }
184
185
186     /**
187      * Set the replace flag
188      *
189      * @param replace the status to set the flag to
190      */

191     public void setReplace(boolean replace) {
192         mReplace = replace;
193     }
194
195     /**
196      * Get replace flag status
197      *
198      * @return boolean containing status of replace flag
199      */

200     public boolean getReplace() {
201         return mReplace;
202     }
203
204     /**
205      * Set recurse flag
206      *
207      * @param recurse the status to set the flag to
208      */

209     public void setRecurse(boolean recurse) {
210         mRecurse = recurse;
211     }
212
213     /**
214      * Get recurse flag status
215      *
216      * @return boolean containing status of recurse flag
217      */

218     public boolean getRecurse() {
219         return mRecurse;
220     }
221
222     /**
223      * Set the version flag
224      *
225      * @param version the status to set the flag to
226      */

227     public void setVersion(String JavaDoc version) {
228         mVersion = version;
229     }
230
231     /**
232      * Get version flag status
233      *
234      * @return boolean containing status of version flag
235      */

236     public String JavaDoc getVersion() {
237         return mVersion;
238     }
239
240     /**
241      * Set comment string
242      *
243      * @param comment the comment string
244      */

245     public void setComment(String JavaDoc comment) {
246         mComment = comment;
247     }
248
249     /**
250      * Get comment string
251      *
252      * @return String containing the comment
253      */

254     public String JavaDoc getComment() {
255         return mComment;
256     }
257
258     /**
259      * Set comment file
260      *
261      * @param cfile the path to the comment file
262      */

263     public void setCommentFile(String JavaDoc cfile) {
264         mCfile = cfile;
265     }
266
267     /**
268      * Get comment file
269      *
270      * @return String containing the path to the comment file
271      */

272     public String JavaDoc getCommentFile() {
273         return mCfile;
274     }
275
276     /**
277      * Set the attribute type-name
278      *
279      * @param tn the type name
280      */

281     public void setTypeName(String JavaDoc tn) {
282         mTypeName = tn;
283     }
284
285     /**
286      * Get attribute type-name
287      *
288      * @return String containing type name
289      */

290     public String JavaDoc getTypeName() {
291         return mTypeName;
292     }
293
294     /**
295      * Set the attribute type-value
296      *
297      * @param tv the type value
298      */

299     public void setTypeValue(String JavaDoc tv) {
300         mTypeValue = tv;
301     }
302
303     /**
304      * Get the attribute type-value
305      *
306      * @return String containing type value
307      */

308     public String JavaDoc getTypeValue() {
309         return mTypeValue;
310     }
311
312
313     /**
314      * Get the 'version' command
315      *
316      * @param cmd CommandLine containing the command line string with or
317      * without the version flag and string appended
318      */

319     private void getVersionCommand(Commandline cmd) {
320         if (getVersion() != null) {
321             /* Had to make two separate commands here because if a space is
322                inserted between the flag and the value, it is treated as a
323                Windows filename with a space and it is enclosed in double
324                quotes ("). This breaks clearcase.
325             */

326             cmd.createArgument().setValue(FLAG_VERSION);
327             cmd.createArgument().setValue(getVersion());
328         }
329     }
330
331     /**
332      * Get the 'comment' command
333      *
334      * @param cmd containing the command line string with or
335      * without the comment flag and string appended
336      */

337     private void getCommentCommand(Commandline cmd) {
338         if (getComment() != null) {
339             /* Had to make two separate commands here because if a space is
340                inserted between the flag and the value, it is treated as a
341                Windows filename with a space and it is enclosed in double
342                quotes ("). This breaks clearcase.
343             */

344             cmd.createArgument().setValue(FLAG_COMMENT);
345             cmd.createArgument().setValue(getComment());
346         }
347     }
348
349     /**
350      * Get the 'commentfile' command
351      *
352      * @param cmd containing the command line string with or
353      * without the commentfile flag and file appended
354      */

355     private void getCommentFileCommand(Commandline cmd) {
356         if (getCommentFile() != null) {
357             /* Had to make two separate commands here because if a space is
358                inserted between the flag and the value, it is treated as a
359                Windows filename with a space and it is enclosed in double
360                quotes ("). This breaks clearcase.
361             */

362             cmd.createArgument().setValue(FLAG_COMMENTFILE);
363             cmd.createArgument().setValue(getCommentFile());
364         }
365     }
366
367     /**
368      * Get the attribute type-name
369      *
370      * @param cmd containing the command line string with or
371      * without the type-name
372      */

373     private void getTypeCommand(Commandline cmd) {
374         String JavaDoc typenm = getTypeName();
375
376         if (typenm != null) {
377             cmd.createArgument().setValue(typenm);
378         }
379     }
380
381     /**
382      * Get the attribute type-value
383      *
384      * @param cmd containing the command line string with or
385      * without the type-value
386      */

387     private void getTypeValueCommand(Commandline cmd) {
388         String JavaDoc typevl = getTypeValue();
389
390         if (typevl != null) {
391             if (Os.isFamily("windows")) {
392                 typevl = "\\\"" + typevl + "\\\""; // Windows quoting of the value
393
} else {
394                 typevl = "\"" + typevl + "\"";
395             }
396             cmd.createArgument().setValue(typevl);
397         }
398     }
399
400     /**
401      * -replace flag -- replace the existing value of the attribute
402      */

403     public static final String JavaDoc FLAG_REPLACE = "-replace";
404     /**
405      * -recurse flag -- process all subdirectories
406      */

407     public static final String JavaDoc FLAG_RECURSE = "-recurse";
408     /**
409      * -version flag -- attach attribute to specified version
410      */

411     public static final String JavaDoc FLAG_VERSION = "-version";
412     /**
413      * -c flag -- comment to attach to the element
414      */

415     public static final String JavaDoc FLAG_COMMENT = "-c";
416     /**
417      * -cfile flag -- file containing a comment to attach to the file
418      */

419     public static final String JavaDoc FLAG_COMMENTFILE = "-cfile";
420     /**
421      * -nc flag -- no comment is specified
422      */

423     public static final String JavaDoc FLAG_NOCOMMENT = "-nc";
424 }
425
426
Popular Tags