001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.clearcase;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.taskdefs.Execute;
024: import org.apache.tools.ant.types.Commandline;
025:
026: /**
027: * Performs ClearCase mkdir.
028: *
029: * <p>
030: * The following attributes are interpreted:
031: * <table border="1">
032: * <tr>
033: * <th>Attribute</th>
034: * <th>Values</th>
035: * <th>Required</th>
036: * </tr>
037: * <tr>
038: * <td>viewpath</td>
039: * <td>Path to the ClearCase view directory that the command will operate on</td>
040: * <td>Yes</td>
041: * <tr>
042: * <tr>
043: * <td>comment</td>
044: * <td>Specify a comment. Only one of comment or cfile may be used.</td>
045: * <td>No</td>
046: * <tr>
047: * <tr>
048: * <td>commentfile</td>
049: * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
050: * <td>No</td>
051: * <tr>
052: * <tr>
053: * <td>nocheckout</td>
054: * <td>Do not checkout after element creation</td>
055: * <td>No</td>
056: * <tr>
057: * <tr>
058: * <td>failonerr</td>
059: * <td>Throw an exception if the command fails. Default is true</td>
060: * <td>No</td>
061: * <tr>
062: * </table>
063: *
064: */
065: public class CCMkdir extends ClearCase {
066: private String mComment = null;
067: private String mCfile = null;
068: private boolean mNoco = false;
069:
070: /**
071: * Executes the task.
072: * <p>
073: * Builds a command line to execute cleartool and then calls Exec's run method
074: * to execute the command line.
075: * @throws BuildException if the command fails and failonerr is set to true
076: */
077: public void execute() throws BuildException {
078: Commandline commandLine = new Commandline();
079: Project aProj = getProject();
080: int result = 0;
081:
082: // Default the viewpath to basedir if it is not specified
083: if (getViewPath() == null) {
084: setViewPath(aProj.getBaseDir().getPath());
085: }
086:
087: // build the command line from what we got. the format is
088: // cleartool mkelem [options...] [viewpath ...]
089: // as specified in the CLEARTOOL.EXE help
090: commandLine.setExecutable(getClearToolCommand());
091: commandLine.createArgument().setValue(COMMAND_MKDIR);
092:
093: checkOptions(commandLine);
094:
095: if (!getFailOnErr()) {
096: getProject().log(
097: "Ignoring any errors that occur for: "
098: + getViewPathBasename(),
099: Project.MSG_VERBOSE);
100: }
101: result = run(commandLine);
102: if (Execute.isFailure(result) && getFailOnErr()) {
103: String msg = "Failed executing: " + commandLine.toString();
104: throw new BuildException(msg, getLocation());
105: }
106: }
107:
108: /**
109: * Check the command line options.
110: */
111: private void checkOptions(Commandline cmd) {
112: if (getComment() != null) {
113: // -c
114: getCommentCommand(cmd);
115: } else {
116: if (getCommentFile() != null) {
117: // -cfile
118: getCommentFileCommand(cmd);
119: } else {
120: cmd.createArgument().setValue(FLAG_NOCOMMENT);
121: }
122: }
123: if (getNoCheckout()) {
124: // -nco
125: cmd.createArgument().setValue(FLAG_NOCHECKOUT);
126: }
127: // viewpath
128: cmd.createArgument().setValue(getViewPath());
129: }
130:
131: /**
132: * Sets the comment string.
133: *
134: * @param comment the comment string
135: */
136: public void setComment(String comment) {
137: mComment = comment;
138: }
139:
140: /**
141: * Get comment string
142: *
143: * @return String containing the comment
144: */
145: public String getComment() {
146: return mComment;
147: }
148:
149: /**
150: * Specifies a file containing a comment.
151: *
152: * @param cfile the path to the comment file
153: */
154: public void setCommentFile(String cfile) {
155: mCfile = cfile;
156: }
157:
158: /**
159: * Get comment file
160: *
161: * @return String containing the path to the comment file
162: */
163: public String getCommentFile() {
164: return mCfile;
165: }
166:
167: /**
168: * If true, do not checkout element after creation.
169: *
170: * @param co the status to set the flag to
171: */
172: public void setNoCheckout(boolean co) {
173: mNoco = co;
174: }
175:
176: /**
177: * Get no checkout flag status
178: *
179: * @return boolean containing status of noco flag
180: */
181: public boolean getNoCheckout() {
182: return mNoco;
183: }
184:
185: /**
186: * Get the 'comment' command
187: *
188: * @param cmd containing the command line string with or
189: * without the comment flag and string appended
190: */
191: private void getCommentCommand(Commandline cmd) {
192: if (getComment() != null) {
193: /* Had to make two separate commands here because if a space is
194: inserted between the flag and the value, it is treated as a
195: Windows filename with a space and it is enclosed in double
196: quotes ("). This breaks clearcase.
197: */
198: cmd.createArgument().setValue(FLAG_COMMENT);
199: cmd.createArgument().setValue(getComment());
200: }
201: }
202:
203: /**
204: * Get the 'commentfile' command
205: *
206: * @param cmd containing the command line string with or
207: * without the commentfile flag and file appended
208: */
209: private void getCommentFileCommand(Commandline cmd) {
210: if (getCommentFile() != null) {
211: /* Had to make two separate commands here because if a space is
212: inserted between the flag and the value, it is treated as a
213: Windows filename with a space and it is enclosed in double
214: quotes ("). This breaks clearcase.
215: */
216: cmd.createArgument().setValue(FLAG_COMMENTFILE);
217: cmd.createArgument().setValue(getCommentFile());
218: }
219: }
220:
221: /**
222: * -c flag -- comment to attach to the directory
223: */
224: public static final String FLAG_COMMENT = "-c";
225: /**
226: * -cfile flag -- file containing a comment to attach to the directory
227: */
228: public static final String FLAG_COMMENTFILE = "-cfile";
229: /**
230: * -nc flag -- no comment is specified
231: */
232: public static final String FLAG_NOCOMMENT = "-nc";
233: /**
234: * -nco flag -- do not checkout element after creation
235: */
236: public static final String FLAG_NOCHECKOUT = "-nco";
237: }
|