KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > starteam > StarTeamList


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 package org.apache.tools.ant.taskdefs.optional.starteam;
19
20 import com.starbase.starteam.File;
21 import com.starbase.starteam.Folder;
22 import com.starbase.starteam.Item;
23 import com.starbase.starteam.Status;
24 import com.starbase.starteam.View;
25 import com.starbase.starteam.ViewConfiguration;
26 import java.io.IOException JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Project;
31
32 /**
33  * Produces a listing of the contents of the StarTeam repository
34  * at the specified view and StarTeamFolder.
35  *
36  * Created: Tue Dec 25 06:51:14 2001
37  *
38  * @version 1.0
39  *
40  * @ant.task name="stlist" category="scm"
41  */

42
43 public class StarTeamList extends TreeBasedTask {
44     private boolean listUncontrolled = true;
45     /**
46      * List files, dates, and statuses as of this label; optional.
47      * The label must exist in starteam or an exception will be thrown.
48      * If not specified, the most recent version of each file will be listed.
49      *
50      * @param label the label to be listed
51      */

52     public void setLabel(String JavaDoc label) {
53         _setLabel(label);
54     }
55
56     /**
57      * List files, dates, and statuses as of this date; optional.
58      * If not specified, the most recent version of each file will be listed.
59      *
60      * @param asOfDateParam the date as of which the listing to be made
61      * @since Ant 1.6
62      */

63     public void setAsOfDate(String JavaDoc asOfDateParam) {
64         _setAsOfDate(asOfDateParam);
65     }
66
67     /**
68      * Date Format with which asOfDate parameter to be parsed; optional.
69      * Must be a SimpleDateFormat compatible string.
70      * If not specified, and asOfDateParam is specified, parse will use ISO8601
71      * datetime and date formats.
72      *
73      * @param asOfDateFormat the SimpleDateFormat-compatible format string
74      * @since Ant 1.6
75      */

76     public void setAsOfDateFormat(String JavaDoc asOfDateFormat) {
77         _setAsOfDateFormat(asOfDateFormat);
78     }
79
80
81     /**
82      * Override of base-class abstract function creates an
83      * appropriately configured view for checkoutlists - either
84      * the current view or a view from this.label.
85      *
86      * @param raw the unconfigured <code>View</code>
87      * @return the snapshot <code>View</code> appropriately configured.
88      */

89     protected View createSnapshotView(View raw) {
90
91         int labelID = getLabelID(raw);
92
93         // if a label has been supplied, use it to configure the view
94
// otherwise use current view
95
if (labelID >= 0) {
96             return new View(raw, ViewConfiguration.createFromLabel(labelID));
97         }
98         // if a date has been supplied use a view configured to the date.
99
View view = getViewConfiguredByDate(raw);
100         if (view != null) {
101             return view;
102         // otherwise, use this view configured as the tip.
103
} else {
104             return new View(raw, ViewConfiguration.createTip());
105         }
106     }
107
108     /**
109      * Required base-class abstract function implementation checks for
110      * incompatible parameters.
111      *
112      * @exception BuildException thrown on incompatible params specified
113      */

114     protected void testPreconditions() throws BuildException {
115         if (null != getLabel() && null != getAsOfDate()) {
116             throw new BuildException(
117                 "Both label and asOfDate specified. "
118                 + "Unable to process request.");
119         }
120     }
121
122     /**
123      * extenders should emit to the log an entry describing the parameters
124      * that will be used by this operation.
125      *
126      * @param starteamrootFolder
127      * root folder in StarTeam for the operation
128      * @param targetrootFolder
129      * root local folder for the operation (whether specified by the user or not.
130      */

131     protected void logOperationDescription(Folder starteamrootFolder,
132                                            java.io.File JavaDoc targetrootFolder) {
133         log((this.isRecursive() ? "Recursive" : "Non-recursive")
134             + " Listing of: " + starteamrootFolder.getFolderHierarchy());
135
136         log("Listing against local folder"
137             + (null == getRootLocalFolder() ? " (default): " : ": ")
138             + targetrootFolder.getAbsolutePath(),
139                     Project.MSG_INFO);
140         logLabel();
141         logAsOfDate();
142         logIncludes();
143         logExcludes();
144
145
146     }
147     /**
148      * Implements base-class abstract function to perform the checkout
149      * operation on the files in each folder of the tree.
150      *
151      * @param starteamFolder the StarTeam folder from which files to be
152      * checked out
153      * @param targetFolder the local mapping of rootStarteamFolder
154      * @throws BuildException on error
155      */

156     protected void visit(Folder starteamFolder, java.io.File JavaDoc targetFolder)
157             throws BuildException {
158         try {
159             if (null != getRootLocalFolder()) {
160                 starteamFolder.setAlternatePathFragment(
161                     targetFolder.getAbsolutePath());
162
163             }
164             Folder[] subFolders = starteamFolder.getSubFolders();
165             Item[] files = starteamFolder.getItems(getTypeNames().FILE);
166
167             UnmatchedFileMap ufm =
168                 new UnmatchedListingMap().init(
169                     targetFolder.getAbsoluteFile(), starteamFolder);
170
171             log("");
172             log("Listing StarTeam folder "
173                 + starteamFolder.getFolderHierarchy());
174             log(" against local folder " + targetFolder.getAbsolutePath());
175
176
177             // For all Files in this folder, we need to check
178
// if there have been modifications.
179

180             for (int i = 0; i < files.length; i++) {
181                 File eachFile = (File) files[i];
182                 String JavaDoc filename = eachFile.getName();
183                 java.io.File JavaDoc localFile =
184                         new java.io.File JavaDoc(targetFolder, filename);
185
186                 ufm.removeControlledItem(localFile);
187
188                 // If the file doesn't pass the include/exclude tests, skip it.
189
if (!shouldProcess(filename)) {
190                     continue;
191                 }
192
193                 list(eachFile, localFile);
194             }
195
196
197             // Now we recursively call this method on all sub folders in this
198
// folder unless recursive attribute is off.
199
for (int i = 0; i < subFolders.length; i++) {
200                 java.io.File JavaDoc targetSubfolder =
201                         new java.io.File JavaDoc(targetFolder, subFolders[i].getName());
202                 ufm.removeControlledItem(targetSubfolder);
203                 if (isRecursive()) {
204                     visit(subFolders[i], targetSubfolder);
205                 }
206             }
207             if (this.listUncontrolled) {
208                 ufm.processUncontrolledItems();
209             }
210
211         } catch (IOException JavaDoc e) {
212             throw new BuildException(e);
213         }
214     }
215
216     private static final SimpleDateFormat JavaDoc SDF =
217         new SimpleDateFormat JavaDoc("yyyy-MM-dd hh:mm:ss zzz");
218
219     /**
220      * Log a repositary file and it's corresponding local file.
221      * @param reposFile the repositary file to log
222      * @param localFile the corresponding local file
223      * @throws IOException on error getting information from files
224      */

225     protected void list(File reposFile, java.io.File JavaDoc localFile)
226             throws IOException JavaDoc {
227         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
228         int status = reposFile.getStatus();
229         java.util.Date JavaDoc displayDate = null;
230         if (status == Status.NEW) {
231             displayDate = new java.util.Date JavaDoc(localFile.lastModified());
232         } else {
233             displayDate = reposFile.getModifiedTime().createDate();
234         }
235         b.append(pad(Status.name(status), 12)).append(' ');
236         b.append(pad(getUserName(reposFile.getLocker()), 20))
237                 .append(' ')
238                 .append(SDF.format(displayDate))
239                 .append(rpad(String.valueOf(reposFile.getSize()), 9))
240                 .append(' ')
241                 .append(reposFile.getName());
242
243         log(b.toString());
244     }
245
246     private static final String JavaDoc BLANK_STRING = blanks(30);
247
248     private static String JavaDoc blanks(int len) {
249         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
250         for (int i = 0; i < len; i++) {
251             b.append(' ');
252         }
253         return b.toString();
254     }
255
256     /**
257      * Return a padded string.
258      * @param s the string to pad
259      * @param padlen the size of the padded string
260      * @return the padded string
261      */

262     protected static String JavaDoc pad(String JavaDoc s, int padlen) {
263         return (s + BLANK_STRING).substring(0, padlen);
264     }
265
266     /**
267      * Return a right padded string.
268      * @param s the string to pad
269      * @param padlen the size of the padded string
270      * @return the padded string
271      */

272     protected static String JavaDoc rpad(String JavaDoc s, int padlen) {
273         s = BLANK_STRING + s;
274         return s.substring(s.length() - padlen);
275     }
276
277     /**
278      * handles the list of uncontrolled items
279      */

280     private class UnmatchedListingMap extends UnmatchedFileMap {
281
282         protected boolean isActive() {
283             return StarTeamList.this.listUncontrolled;
284         }
285
286         /**
287          * lists uncontrolled items from the local tree. It is assumed
288          * that this method will not be called until all the items in the
289          * corresponding folder have been processed, and that the internal map
290          * will contain only uncontrolled items.
291          */

292         void processUncontrolledItems() throws BuildException {
293             if (this.isActive()) {
294                 Enumeration JavaDoc e = this.keys();
295
296                 // handle the files so they appear first
297
while (e.hasMoreElements()) {
298                     java.io.File JavaDoc local = (java.io.File JavaDoc) e.nextElement();
299                     Item remoteItem = (Item) this.get(local);
300
301                     // once we find a folder that isn't in the repository,
302
// we know we can add it.
303
if (local.isFile()) {
304                         com.starbase.starteam.File remoteFile =
305                             (com.starbase.starteam.File) remoteItem;
306                         try {
307                             list(remoteFile, local);
308                         } catch (IOException JavaDoc ie) {
309                             throw new BuildException("IOError in stlist", ie);
310                         }
311                     }
312                 }
313                 // now do it again for the directories so they appear last.
314
e = this.keys();
315                 while (e.hasMoreElements()) {
316                     java.io.File JavaDoc local = (java.io.File JavaDoc) e.nextElement();
317                     Item remoteItem = (Item) this.get(local);
318
319                     // once we find a folder that isn't in the repository,
320
// we know we can add it.
321
if (local.isDirectory()) {
322                         Folder folder = (Folder) remoteItem;
323                         if (isRecursive()) {
324                             log("Listing uncontrolled folder "
325                                 + folder.getFolderHierarchy()
326                                 + " from " + local.getAbsoluteFile());
327                             UnmatchedFileMap submap =
328                                 new UnmatchedListingMap().init(local, folder);
329                             submap.processUncontrolledItems();
330                         }
331                     }
332                 }
333             }
334         }
335     }
336 }
337
338
339
Popular Tags