KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > perforce > P4Fstat


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  * Portions of this software are based upon public domain software
20  * originally written at the National Center for Supercomputing Applications,
21  * University of Illinois, Urbana-Champaign.
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.perforce;
25
26
27 import java.io.File JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.ArrayList JavaDoc;
30
31 import org.apache.tools.ant.Project;
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.DirectoryScanner;
34 import org.apache.tools.ant.types.FileSet;
35
36 /**
37  * P4Fstat--find out which files are under Perforce control and which are not.
38  *
39  * <br><b>Example Usage:</b><br>
40  * <pre>
41  * &lt;project name=&quot;p4fstat&quot; default=&quot;p4fstat&quot;
42  * basedir=&quot;C:\dev\gnu&quot;&gt;
43  * &lt;target name=&quot;p4fstat&quot; &gt;
44  * &lt;p4fstat showfilter=&quot;all&quot;&gt;
45  * &lt;fileset dir=&quot;depot&quot; includes=&quot;**\/*&quot;/&gt;
46  * &lt;/p4fstat&gt;
47  * &lt;/target&gt;
48  * &lt;/project&gt;
49  * </pre>
50  *
51  * @ant.task category="scm"
52  */

53 public class P4Fstat extends P4Base {
54
55     private int changelist;
56     private String JavaDoc addCmd = "";
57     private Vector JavaDoc filesets = new Vector JavaDoc();
58     private static final int DEFAULT_CMD_LENGTH = 300;
59     private int cmdLength = DEFAULT_CMD_LENGTH;
60     private static final int SHOW_ALL = 0;
61     private static final int SHOW_EXISTING = 1;
62     private static final int SHOW_NON_EXISTING = 2;
63     private int show = SHOW_NON_EXISTING;
64     private FStatP4OutputHandler handler;
65     private StringBuffer JavaDoc filelist;
66     private int fileNum = 0;
67     private int doneFileNum = 0;
68     private boolean debug = false;
69
70     private static final String JavaDoc EXISTING_HEADER
71         = "Following files exist in perforce";
72     private static final String JavaDoc NONEXISTING_HEADER
73         = "Following files do not exist in perforce";
74
75     /**
76      * Sets the filter that one wants applied.
77      * <table>
78      * <tr><th>Option</th><th>Meaning</th></tr>
79      * <tr><td>all</td><td>all files under Perforce control or not</td></tr>
80      * <tr><td>existing</td><td>only files under Perforce control</td></tr>
81      * <tr><td>non-existing</td><td>only files not under Perforce control or not</td></tr>
82      * </table>
83      * @param filter should be one of all|existing|non-existing.
84      */

85     public void setShowFilter(String JavaDoc filter) {
86         if (filter.equalsIgnoreCase("all")) {
87             show = SHOW_ALL;
88         } else if (filter.equalsIgnoreCase("existing")) {
89             show = SHOW_EXISTING;
90         } else if (filter.equalsIgnoreCase("non-existing")) {
91             show = SHOW_NON_EXISTING;
92         } else {
93             throw new BuildException("P4Fstat: ShowFilter should be one of: "
94                 + "all, existing, non-existing");
95         }
96     }
97
98     /**
99      * Sets optionally a change list number.
100      * @param changelist change list that one wants information about.
101      * @throws BuildException if the change list number is negative.
102      */

103     public void setChangelist(int changelist) throws BuildException {
104         if (changelist <= 0) {
105             throw new BuildException("P4FStat: Changelist# should be a "
106                 + "positive number");
107         }
108         this.changelist = changelist;
109     }
110
111     /**
112      * Adds a fileset to be examined by p4fstat.
113      * @param set the fileset to add.
114      */

115     public void addFileset(FileSet set) {
116         filesets.addElement(set);
117     }
118
119     /**
120      * Executes the p4fstat task.
121      * @throws BuildException if no files are specified.
122      */

123     public void execute() throws BuildException {
124         handler = new FStatP4OutputHandler(this);
125         if (P4View != null) {
126             addCmd = P4View;
127         }
128         P4CmdOpts = (changelist > 0) ? ("-c " + changelist) : "";
129
130         filelist = new StringBuffer JavaDoc();
131
132         for (int i = 0; i < filesets.size(); i++) {
133             FileSet fs = (FileSet) filesets.elementAt(i);
134             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
135
136             String JavaDoc[] srcFiles = ds.getIncludedFiles();
137             fileNum = srcFiles.length;
138
139             if (srcFiles != null) {
140                 for (int j = 0; j < srcFiles.length; j++) {
141                     File JavaDoc f = new File JavaDoc(ds.getBasedir(), srcFiles[j]);
142                     filelist.append(" ").append('"').append(f.getAbsolutePath()).append('"');
143                     doneFileNum++;
144                     if (filelist.length() > cmdLength) {
145
146                         execP4Fstat(filelist);
147                         filelist = new StringBuffer JavaDoc();
148                     }
149                 }
150                 if (filelist.length() > 0) {
151                     execP4Fstat(filelist);
152                 }
153             } else {
154                 log("No files specified to query status on!", Project.MSG_WARN);
155             }
156         }
157         if (show == SHOW_ALL || show == SHOW_EXISTING) {
158             printRes(handler.getExisting(), EXISTING_HEADER);
159         }
160         if (show == SHOW_ALL || show == SHOW_NON_EXISTING) {
161             printRes(handler.getNonExisting(), NONEXISTING_HEADER);
162         }
163     }
164
165     /**
166      * Return the number of files seen.
167      * @return the number of files seen.
168      */

169     public int getLengthOfTask() {
170         return fileNum;
171     }
172
173     /**
174      * Return the number of passes to make.
175      * IS THIS BEING USED?
176      * @return number of passes (how many filesets).
177      */

178     int getPasses() {
179         return filesets.size();
180     }
181
182     private void printRes(ArrayList JavaDoc ar, String JavaDoc header) {
183         log(header, Project.MSG_INFO);
184         for (int i = 0; i < ar.size(); i++) {
185             log((String JavaDoc) ar.get(i), Project.MSG_INFO);
186         }
187     }
188
189     private void execP4Fstat(StringBuffer JavaDoc list) {
190         String JavaDoc l = list.substring(0);
191         if (debug) {
192             log("Executing fstat " + P4CmdOpts + " " + addCmd + l + "\n",
193                 Project.MSG_INFO);
194         }
195         execP4Command("fstat " + P4CmdOpts + " " + addCmd + l, handler);
196     }
197
198 }
199
Popular Tags