KickJava   Java API By Example, From Geeks To Geeks.

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


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
26 /**
27  * Performs ClearCase mkelem.
28  *
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>comment</td>
44  * <td>Specify a comment. Only one of comment or cfile may be used.</td>
45  * <td>No</td>
46  * <tr>
47  * <tr>
48  * <td>commentfile</td>
49  * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
50  * <td>No</td>
51  * <tr>
52  * <tr>
53  * <td>nowarn</td>
54  * <td>Suppress warning messages</td>
55  * <td>No</td>
56  * <tr>
57  * <tr>
58  * <td>nocheckout</td>
59  * <td>Do not checkout after element creation</td>
60  * <td>No</td>
61  * <tr>
62  * <tr>
63  * <td>checkin</td>
64  * <td>Checkin element after creation</td>
65  * <td>No</td>
66  * <tr>
67  * <tr>
68  * <td>preservetime</td>
69  * <td>Preserve the modification time (for checkin)</td>
70  * <td>No</td>
71  * <tr>
72  * <tr>
73  * <td>master</td>
74  * <td>Assign mastership of the main branch to the current site</td>
75  * <td>No</td>
76  * <tr>
77  * <tr>
78  * <td>eltype</td>
79  * <td>Element type to use during element creation</td>
80  * <td>No</td>
81  * <tr>
82  * <tr>
83  * <td>failonerr</td>
84  * <td>Throw an exception if the command fails. Default is true</td>
85  * <td>No</td>
86  * <tr>
87  * </table>
88  *
89  */

90 public class CCMkelem extends ClearCase {
91     private String JavaDoc mComment = null;
92     private String JavaDoc mCfile = null;
93     private boolean mNwarn = false;
94     private boolean mPtime = false;
95     private boolean mNoco = false;
96     private boolean mCheckin = false;
97     private boolean mMaster = false;
98     private String JavaDoc mEltype = null;
99
100     /**
101      * Executes the task.
102      * <p>
103      * Builds a command line to execute cleartool and then calls Exec's run method
104      * to execute the command line.
105      * @throws BuildException if the command fails and failonerr is set to true
106      */

107     public void execute() throws BuildException {
108         Commandline commandLine = new Commandline();
109         Project aProj = getProject();
110         int result = 0;
111
112         // Default the viewpath to basedir if it is not specified
113
if (getViewPath() == null) {
114             setViewPath(aProj.getBaseDir().getPath());
115         }
116
117         // build the command line from what we got. the format is
118
// cleartool mkelem [options...] [viewpath ...]
119
// as specified in the CLEARTOOL.EXE help
120
commandLine.setExecutable(getClearToolCommand());
121         commandLine.createArgument().setValue(COMMAND_MKELEM);
122
123         checkOptions(commandLine);
124
125         if (!getFailOnErr()) {
126             getProject().log("Ignoring any errors that occur for: "
127                     + getViewPathBasename(), Project.MSG_VERBOSE);
128         }
129         result = run(commandLine);
130         if (Execute.isFailure(result) && getFailOnErr()) {
131             String JavaDoc msg = "Failed executing: " + commandLine.toString();
132             throw new BuildException(msg, getLocation());
133         }
134     }
135
136
137     /**
138      * Check the command line options.
139      */

140     private void checkOptions(Commandline cmd) {
141         if (getComment() != null) {
142             // -c
143
getCommentCommand(cmd);
144         } else {
145             if (getCommentFile() != null) {
146                 // -cfile
147
getCommentFileCommand(cmd);
148             } else {
149                 cmd.createArgument().setValue(FLAG_NOCOMMENT);
150             }
151         }
152
153         if (getNoWarn()) {
154             // -nwarn
155
cmd.createArgument().setValue(FLAG_NOWARN);
156         }
157         /*
158          * Should choose either -ci or -nco.
159          */

160         if (getNoCheckout() && getCheckin()) {
161             throw new BuildException("Should choose either [nocheckout | checkin]");
162         }
163         if (getNoCheckout()) {
164             // -nco
165
cmd.createArgument().setValue(FLAG_NOCHECKOUT);
166         }
167         if (getCheckin()) {
168             // -ci
169
cmd.createArgument().setValue(FLAG_CHECKIN);
170             if (getPreserveTime()) {
171                 // -ptime
172
cmd.createArgument().setValue(FLAG_PRESERVETIME);
173             }
174         }
175         if (getMaster()) {
176             // -master
177
cmd.createArgument().setValue(FLAG_MASTER);
178         }
179         if (getEltype() != null) {
180             // -eltype
181
getEltypeCommand(cmd);
182         }
183         // viewpath
184
cmd.createArgument().setValue(getViewPath());
185     }
186
187     /**
188      * Sets the comment string.
189      *
190      * @param comment the comment string
191      */

192     public void setComment(String JavaDoc comment) {
193         mComment = comment;
194     }
195
196     /**
197      * Get comment string
198      *
199      * @return String containing the comment
200      */

201     public String JavaDoc getComment() {
202         return mComment;
203     }
204
205     /**
206      * Specifies a file containing a comment.
207      *
208      * @param cfile the path to the comment file
209      */

210     public void setCommentFile(String JavaDoc cfile) {
211         mCfile = cfile;
212     }
213
214     /**
215      * Get comment file
216      *
217      * @return String containing the path to the comment file
218      */

219     public String JavaDoc getCommentFile() {
220         return mCfile;
221     }
222
223     /**
224      * If true, suppress warning messages.
225      *
226      * @param nwarn the status to set the flag to
227      */

228     public void setNoWarn(boolean nwarn) {
229         mNwarn = nwarn;
230     }
231
232     /**
233      * Get nowarn flag status
234      *
235      * @return boolean containing status of nwarn flag
236      */

237     public boolean getNoWarn() {
238         return mNwarn;
239     }
240
241     /**
242      * If true, preserve the modification time.
243      *
244      * @param ptime the status to set the flag to
245      */

246     public void setPreserveTime(boolean ptime) {
247         mPtime = ptime;
248     }
249
250     /**
251      * Get preservetime flag status
252      *
253      * @return boolean containing status of preservetime flag
254      */

255     public boolean getPreserveTime() {
256         return mPtime;
257     }
258
259     /**
260      * If true, do not checkout element after creation.
261      *
262      * @param co the status to set the flag to
263      */

264     public void setNoCheckout(boolean co) {
265         mNoco = co;
266     }
267
268     /**
269      * Get no checkout flag status
270      *
271      * @return boolean containing status of noco flag
272      */

273     public boolean getNoCheckout() {
274         return mNoco;
275     }
276
277     /**
278      * If true, checkin the element after creation
279      *
280      * @param ci the status to set the flag to
281      */

282     public void setCheckin(boolean ci) {
283         mCheckin = ci;
284     }
285
286     /**
287      * Get ci flag status
288      *
289      * @return boolean containing status of ci flag
290      */

291     public boolean getCheckin() {
292         return mCheckin;
293     }
294
295     /**
296      * If true, changes mastership of the main branch
297      * to the current site
298      *
299      * @param master the status to set the flag to
300      */

301     public void setMaster(boolean master) {
302         mMaster = master;
303     }
304
305     /**
306      * Get master flag status
307      *
308      * @return boolean containing status of master flag
309      */

310     public boolean getMaster() {
311         return mMaster;
312     }
313
314     /**
315      * Specifies the element type to use.
316      *
317      * @param eltype to create element
318      */

319     public void setEltype(String JavaDoc eltype) {
320         mEltype = eltype;
321     }
322
323     /**
324      * Get element type
325      *
326      * @return String containing the element type
327      */

328     public String JavaDoc getEltype() {
329         return mEltype;
330     }
331
332
333     /**
334      * Get the 'comment' command
335      *
336      * @param cmd containing the command line string with or
337      * without the comment flag and string appended
338      */

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

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

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

364             cmd.createArgument().setValue(FLAG_COMMENTFILE);
365             cmd.createArgument().setValue(getCommentFile());
366         }
367     }
368
369     /**
370      * Get the 'element type' command
371      *
372      * @param cmd containing the command line string with or
373      * without the comment flag and string appended
374      */

375     private void getEltypeCommand(Commandline cmd) {
376         if (getEltype() != null) {
377             /* Had to make two separate commands here because if a space is
378                inserted between the flag and the value, it is treated as a
379                Windows filename with a space and it is enclosed in double
380                quotes ("). This breaks clearcase.
381             */

382             cmd.createArgument().setValue(FLAG_ELTYPE);
383             cmd.createArgument().setValue(getEltype());
384         }
385     }
386
387     /**
388      * -c flag -- comment to attach to the file
389      */

390     public static final String JavaDoc FLAG_COMMENT = "-c";
391     /**
392      * -cfile flag -- file containing a comment to attach to the file
393      */

394     public static final String JavaDoc FLAG_COMMENTFILE = "-cfile";
395     /**
396      * -nc flag -- no comment is specified
397      */

398     public static final String JavaDoc FLAG_NOCOMMENT = "-nc";
399     /**
400      * -nwarn flag -- suppresses warning messages
401      */

402     public static final String JavaDoc FLAG_NOWARN = "-nwarn";
403     /**
404      * -ptime flag -- preserves the modification time on checkin
405      */

406     public static final String JavaDoc FLAG_PRESERVETIME = "-ptime";
407     /**
408      * -nco flag -- do not checkout element after creation
409      */

410     public static final String JavaDoc FLAG_NOCHECKOUT = "-nco";
411     /**
412      * -ci flag -- checkin element after creation
413      */

414     public static final String JavaDoc FLAG_CHECKIN = "-ci";
415     /**
416      * -master flag -- change mastership of main branch to current site
417      */

418     public static final String JavaDoc FLAG_MASTER = "-master";
419     /**
420      * -eltype flag -- element type to use during creation
421      */

422     public static final String JavaDoc FLAG_ELTYPE = "-eltype";
423 }
424
425
Popular Tags