KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ccm > CCMCheck


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.ccm;
20
21
22 import java.io.File JavaDoc;
23 import java.util.Vector JavaDoc;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.DirectoryScanner;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.taskdefs.Execute;
28 import org.apache.tools.ant.types.Commandline;
29 import org.apache.tools.ant.types.FileSet;
30
31
32 /**
33  * Class common to all check commands (checkout, checkin,checkin default task);
34  * @ant.task ignore="true"
35  */

36 public class CCMCheck extends Continuus {
37
38     private File JavaDoc file = null;
39     private String JavaDoc comment = null;
40     private String JavaDoc task = null;
41
42     // CheckStyle:VisibilityModifier OFF - bc
43

44     protected Vector JavaDoc filesets = new Vector JavaDoc();
45
46     // CheckStyle:VisibilityModifier ON
47

48     /** Constructor for CCMCheck. */
49     public CCMCheck() {
50         super();
51     }
52
53     /**
54      * Get the value of file.
55      * @return value of file.
56      */

57     public File JavaDoc getFile() {
58         return file;
59     }
60
61     /**
62      * Sets the path to the file that the command will operate on.
63      * @param v Value to assign to file.
64      */

65     public void setFile(File JavaDoc v) {
66         log("working file " + v, Project.MSG_VERBOSE);
67         this.file = v;
68     }
69
70     /**
71      * Get the value of comment.
72      * @return value of comment.
73      */

74     public String JavaDoc getComment() {
75         return comment;
76     }
77
78     /**
79      * Specifies a comment.
80      * @param v Value to assign to comment.
81      */

82     public void setComment(String JavaDoc v) {
83         this.comment = v;
84     }
85
86
87     /**
88      * Get the value of task.
89      * @return value of task.
90      */

91     public String JavaDoc getTask() {
92         return task;
93     }
94
95     /**
96      * Specifies the task number used to check
97      * in the file (may use 'default').
98      * @param v Value to assign to task.
99      */

100     public void setTask(String JavaDoc v) {
101         this.task = v;
102     }
103
104
105     /**
106      * Adds a set of files to copy.
107      * @param set the set of files
108      */

109     public void addFileset(FileSet set) {
110         filesets.addElement(set);
111     }
112
113
114     /**
115      * Executes the task.
116      * <p>
117      * Builds a command line to execute ccm and then calls Exec's run method
118      * to execute the command line.
119      * </p>
120      * @throws BuildException on error
121      */

122     public void execute() throws BuildException {
123
124         if (file == null && filesets.size() == 0) {
125             throw new BuildException(
126                 "Specify at least one source - a file or a fileset.");
127         }
128
129         if (file != null && file.exists() && file.isDirectory()) {
130             throw new BuildException("CCMCheck cannot be generated for directories");
131         }
132
133         if (file != null && filesets.size() > 0) {
134             throw new BuildException("Choose between file and fileset !");
135         }
136
137         if (getFile() != null) {
138             doit();
139             return;
140         }
141
142         int sizeofFileSet = filesets.size();
143         for (int i = 0; i < sizeofFileSet; i++) {
144             FileSet fs = (FileSet) filesets.elementAt(i);
145             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
146             String JavaDoc[] srcFiles = ds.getIncludedFiles();
147             for (int j = 0; j < srcFiles.length; j++) {
148                 File JavaDoc src = new File JavaDoc(fs.getDir(getProject()), srcFiles[j]);
149                 setFile(src);
150                 doit();
151             }
152         }
153     }
154
155     /**
156      * check the file given by getFile().
157      */

158     private void doit() {
159         Commandline commandLine = new Commandline();
160
161         // build the command line from what we got the format is
162
// ccm co /t .. files
163
// as specified in the CCM.EXE help
164

165         commandLine.setExecutable(getCcmCommand());
166         commandLine.createArgument().setValue(getCcmAction());
167
168         checkOptions(commandLine);
169
170         int result = run(commandLine);
171         if (Execute.isFailure(result)) {
172             String JavaDoc msg = "Failed executing: " + commandLine.toString();
173             throw new BuildException(msg, getLocation());
174         }
175     }
176
177
178     /**
179      * Check the command line options.
180      */

181     private void checkOptions(Commandline cmd) {
182         if (getComment() != null) {
183             cmd.createArgument().setValue(FLAG_COMMENT);
184             cmd.createArgument().setValue(getComment());
185         }
186
187         if (getTask() != null) {
188             cmd.createArgument().setValue(FLAG_TASK);
189             cmd.createArgument().setValue(getTask());
190         }
191
192         if (getFile() != null) {
193             cmd.createArgument().setValue(file.getAbsolutePath());
194         }
195     }
196
197     /**
198      * -comment flag -- comment to attach to the file
199      */

200     public static final String JavaDoc FLAG_COMMENT = "/comment";
201
202     /**
203      * -task flag -- associate checkout task with task
204      */

205     public static final String JavaDoc FLAG_TASK = "/task";
206 }
207
208
Popular Tags