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.ccm;
020:
021: import java.io.File;
022: import java.util.Vector;
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.DirectoryScanner;
025: import org.apache.tools.ant.Project;
026: import org.apache.tools.ant.taskdefs.Execute;
027: import org.apache.tools.ant.types.Commandline;
028: import org.apache.tools.ant.types.FileSet;
029:
030: /**
031: * Class common to all check commands (checkout, checkin,checkin default task);
032: * @ant.task ignore="true"
033: */
034: public class CCMCheck extends Continuus {
035:
036: private File file = null;
037: private String comment = null;
038: private String task = null;
039:
040: // CheckStyle:VisibilityModifier OFF - bc
041:
042: protected Vector filesets = new Vector();
043:
044: // CheckStyle:VisibilityModifier ON
045:
046: /** Constructor for CCMCheck. */
047: public CCMCheck() {
048: super ();
049: }
050:
051: /**
052: * Get the value of file.
053: * @return value of file.
054: */
055: public File getFile() {
056: return file;
057: }
058:
059: /**
060: * Sets the path to the file that the command will operate on.
061: * @param v Value to assign to file.
062: */
063: public void setFile(File v) {
064: log("working file " + v, Project.MSG_VERBOSE);
065: this .file = v;
066: }
067:
068: /**
069: * Get the value of comment.
070: * @return value of comment.
071: */
072: public String getComment() {
073: return comment;
074: }
075:
076: /**
077: * Specifies a comment.
078: * @param v Value to assign to comment.
079: */
080: public void setComment(String v) {
081: this .comment = v;
082: }
083:
084: /**
085: * Get the value of task.
086: * @return value of task.
087: */
088: public String getTask() {
089: return task;
090: }
091:
092: /**
093: * Specifies the task number used to check
094: * in the file (may use 'default').
095: * @param v Value to assign to task.
096: */
097: public void setTask(String v) {
098: this .task = v;
099: }
100:
101: /**
102: * Adds a set of files to copy.
103: * @param set the set of files
104: */
105: public void addFileset(FileSet set) {
106: filesets.addElement(set);
107: }
108:
109: /**
110: * Executes the task.
111: * <p>
112: * Builds a command line to execute ccm and then calls Exec's run method
113: * to execute the command line.
114: * </p>
115: * @throws BuildException on error
116: */
117: public void execute() throws BuildException {
118:
119: if (file == null && filesets.size() == 0) {
120: throw new BuildException(
121: "Specify at least one source - a file or a fileset.");
122: }
123:
124: if (file != null && file.exists() && file.isDirectory()) {
125: throw new BuildException(
126: "CCMCheck cannot be generated for directories");
127: }
128:
129: if (file != null && filesets.size() > 0) {
130: throw new BuildException(
131: "Choose between file and fileset !");
132: }
133:
134: if (getFile() != null) {
135: doit();
136: return;
137: }
138:
139: int sizeofFileSet = filesets.size();
140: for (int i = 0; i < sizeofFileSet; i++) {
141: FileSet fs = (FileSet) filesets.elementAt(i);
142: DirectoryScanner ds = fs.getDirectoryScanner(getProject());
143: String[] srcFiles = ds.getIncludedFiles();
144: for (int j = 0; j < srcFiles.length; j++) {
145: File src = new File(fs.getDir(getProject()),
146: srcFiles[j]);
147: setFile(src);
148: doit();
149: }
150: }
151: }
152:
153: /**
154: * check the file given by getFile().
155: */
156: private void doit() {
157: Commandline commandLine = new Commandline();
158:
159: // build the command line from what we got the format is
160: // ccm co /t .. files
161: // as specified in the CCM.EXE help
162:
163: commandLine.setExecutable(getCcmCommand());
164: commandLine.createArgument().setValue(getCcmAction());
165:
166: checkOptions(commandLine);
167:
168: int result = run(commandLine);
169: if (Execute.isFailure(result)) {
170: String msg = "Failed executing: " + commandLine.toString();
171: throw new BuildException(msg, getLocation());
172: }
173: }
174:
175: /**
176: * Check the command line options.
177: */
178: private void checkOptions(Commandline cmd) {
179: if (getComment() != null) {
180: cmd.createArgument().setValue(FLAG_COMMENT);
181: cmd.createArgument().setValue(getComment());
182: }
183:
184: if (getTask() != null) {
185: cmd.createArgument().setValue(FLAG_TASK);
186: cmd.createArgument().setValue(getTask());
187: }
188:
189: if (getFile() != null) {
190: cmd.createArgument().setValue(file.getAbsolutePath());
191: }
192: }
193:
194: /**
195: * -comment flag -- comment to attach to the file
196: */
197: public static final String FLAG_COMMENT = "/comment";
198:
199: /**
200: * -task flag -- associate checkout task with task
201: */
202: public static final String FLAG_TASK = "/task";
203: }
|