KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > status > StatusBuilder


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.cvsclient.command.status;
20
21 import java.io.*;
22
23 import org.netbeans.lib.cvsclient.command.*;
24 import org.netbeans.lib.cvsclient.event.*;
25 import org.netbeans.lib.cvsclient.file.*;
26
27 /**
28  * Handles the building of a status information object and the firing of
29  * events when complete objects are built.
30  *
31  * @author Milos Kleint
32  * @author Thomas Singer
33  */

34 public class StatusBuilder implements Builder {
35     private static final String JavaDoc UNKNOWN = ": nothing known about"; //NOI18N
36
private static final String JavaDoc EXAM_DIR = ": Examining"; //NOI18N
37
private static final String JavaDoc NOT_IN_REPOSITORY = "No revision control file"; //NOI18N
38

39     private static final String JavaDoc FILE = "File: "; //NOI18N
40
private static final String JavaDoc STATUS = "Status:"; //NOI18N
41
private static final String JavaDoc NO_FILE_FILENAME = "no file"; //NOI18N
42
private static final String JavaDoc WORK_REV = " Working revision:"; //NOI18N
43
private static final String JavaDoc REP_REV = " Repository revision:"; //NOI18N
44
private static final String JavaDoc TAG = " Sticky Tag:"; //NOI18N
45
private static final String JavaDoc DATE = " Sticky Date:"; //NOI18N
46
private static final String JavaDoc OPTIONS = " Sticky Options:"; //NOI18N
47
private static final String JavaDoc EXISTING_TAGS = " Existing Tags:"; //NOI18N
48
private static final String JavaDoc EMPTY_BEFORE_TAGS = " "; //NOI18N
49
private static final String JavaDoc NO_TAGS = " No Tags Exist"; //NOI18N
50
private static final String JavaDoc UNKNOWN_FILE = "? "; //NOI18N
51

52     /**
53      * The status object that is currently being built.
54      */

55     private StatusInformation statusInformation;
56
57     /**
58      * The event manager to use.
59      */

60     private EventManager eventManager;
61
62     private final StatusCommand statusCommand;
63     private String JavaDoc relativeDirectory;
64     private final String JavaDoc localPath;
65
66     private boolean beginning;
67
68     private boolean readingTags;
69
70     private final File[] fileArray;
71
72     /**
73      * Creates a StatusBuilder.
74      */

75     public StatusBuilder(EventManager eventManager,
76                          StatusCommand statusCommand) {
77         this.eventManager = eventManager;
78         this.statusCommand = statusCommand;
79
80         File[] fileArray = statusCommand.getFiles();
81         if (fileArray != null) {
82             this.fileArray = new File[fileArray.length];
83             System.arraycopy(fileArray, 0, this.fileArray, 0, fileArray.length);
84         }
85         else {
86             this.fileArray = null;
87         }
88
89         this.localPath = statusCommand.getLocalDirectory();
90
91         this.beginning = true;
92     }
93
94     public void outputDone() {
95         if (statusInformation != null) {
96             eventManager.fireCVSEvent(new FileInfoEvent(this, statusInformation));
97             statusInformation = null;
98             readingTags = false;
99         }
100     }
101
102     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
103         if (readingTags) {
104             if (line.startsWith(NO_TAGS)) {
105                 outputDone();
106                 return;
107             }
108
109             int bracket = line.indexOf("\t(");
110             if (bracket > 0) {
111                 // it's another tag..
112
String JavaDoc tag = line.substring(0, bracket).trim();
113                 String JavaDoc rev = line.substring(bracket + 2, line.length() - 1);
114
115                 if (statusInformation == null) {
116                     statusInformation = new StatusInformation();
117                 }
118                 statusInformation.addExistingTag(tag, rev);
119             }
120             else {
121                 outputDone();
122                 return;
123             }
124         }
125
126         if (line.startsWith(UNKNOWN_FILE) && beginning) {
127             File file = new File(localPath, line.substring(UNKNOWN_FILE.length()));
128             statusInformation = new StatusInformation();
129             statusInformation.setFile(file);
130             statusInformation.setStatusString(FileStatus.UNKNOWN.toString());
131             outputDone();
132         }
133
134         if (line.startsWith(UNKNOWN)) {
135             outputDone();
136             beginning = false;
137         }
138         else if (line.indexOf(EXAM_DIR) >= 0) {
139             relativeDirectory = line.substring(line.indexOf(EXAM_DIR) + EXAM_DIR.length()).trim();
140             beginning = false;
141         }
142         else if (line.startsWith(FILE)) {
143             outputDone();
144             statusInformation = new StatusInformation();
145             processFileAndStatusLine(line.substring(FILE.length()));
146             beginning = false;
147         }
148         else if (line.startsWith(WORK_REV)) {
149             processWorkRev(line.substring(WORK_REV.length()));
150         }
151         else if (line.startsWith(REP_REV)) {
152             processRepRev(line.substring(REP_REV.length()));
153 /* if (statusInformation.getRepositoryRevision().startsWith(NOT_IN_REPOSITORY))
154             {
155                 outputDone();
156             }
157  */

158         }
159         else if (line.startsWith(TAG)) {
160             processTag(line.substring(TAG.length()));
161         }
162         else if (line.startsWith(DATE)) {
163             processDate(line.substring(DATE.length()));
164         }
165         else if (line.startsWith(OPTIONS)) {
166             processOptions(line.substring(OPTIONS.length()));
167             if (!statusCommand.isIncludeTags()) {
168                 outputDone();
169             }
170         }
171         else if (line.startsWith(EXISTING_TAGS)) {
172             readingTags = true;
173         }
174     }
175
176     private File createFile(String JavaDoc fileName) {
177         File file = null;
178
179         if (relativeDirectory != null) {
180             if (relativeDirectory.trim().equals(".")) { //NOI18N
181
file = new File(localPath, fileName);
182             }
183             else {
184                 file = new File(localPath, relativeDirectory + '/' + fileName);
185             }
186         }
187         else if (fileArray != null) {
188             for (int i = 0; i < fileArray.length; i++) {
189                 File currentFile = fileArray[i];
190                 if (currentFile == null || currentFile.isDirectory()) {
191                     continue;
192                 }
193
194                 String JavaDoc currentFileName = currentFile.getName();
195                 if (fileName.equals(currentFileName)) {
196                     fileArray[i] = null;
197                     file = currentFile;
198                     break;
199                 }
200             }
201         }
202
203         if (file == null) {
204             System.err.println("JAVACVS ERROR!! wrong algorithm for assigning path to single files(1)!!");
205         }
206
207         return file;
208     }
209
210     private void processFileAndStatusLine(String JavaDoc line) {
211         int statusIndex = line.lastIndexOf(STATUS);
212         String JavaDoc fileName = line.substring(0, statusIndex).trim();
213         if (fileName.startsWith(NO_FILE_FILENAME)) {
214             fileName = fileName.substring(8);
215         }
216
217         statusInformation.setFile(createFile(fileName));
218
219         String JavaDoc status = new String JavaDoc(line.substring(statusIndex + 8).trim());
220         statusInformation.setStatusString(status);
221     }
222
223     private boolean assertNotNull() {
224         if (statusInformation == null) {
225             System.err.println("Bug: statusInformation must not be null!");
226             return false;
227         }
228
229         return true;
230     }
231
232     private void processWorkRev(String JavaDoc line) {
233         if (!assertNotNull()) {
234             return;
235         }
236         statusInformation.setWorkingRevision(line.trim().intern());
237     }
238
239     private void processRepRev(String JavaDoc line) {
240         if (!assertNotNull()) {
241             return;
242         }
243         line = line.trim();
244         if (line.startsWith(NOT_IN_REPOSITORY)) {
245             statusInformation.setRepositoryRevision(line.trim().intern());
246             return;
247         }
248
249         int firstSpace = line.indexOf('\t');
250         if (firstSpace > 0) {
251             statusInformation.setRepositoryRevision(
252                     line.substring(0, firstSpace).trim().intern());
253             statusInformation.setRepositoryFileName(
254                     new String JavaDoc(line.substring(firstSpace).trim()));
255         }
256         else {
257             statusInformation.setRepositoryRevision(""); //NOI18N
258
statusInformation.setRepositoryFileName(""); //NOI18N
259
}
260     }
261
262     private void processTag(String JavaDoc line) {
263         if (!assertNotNull()) {
264             return;
265         }
266         statusInformation.setStickyTag(line.trim().intern());
267     }
268
269     private void processDate(String JavaDoc line) {
270         if (!assertNotNull()) {
271             return;
272         }
273         statusInformation.setStickyDate(line.trim().intern());
274     }
275
276     private void processOptions(String JavaDoc line) {
277         if (!assertNotNull()) {
278             return;
279         }
280         statusInformation.setStickyOptions(line.trim().intern());
281     }
282
283     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
284     }
285
286 }
287
Popular Tags