KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > update > UpdateBuilder


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.update;
20
21 import java.io.*;
22
23 import org.netbeans.lib.cvsclient.command.*;
24 import org.netbeans.lib.cvsclient.event.*;
25
26 /**
27  * Handles the building of update information object and the firing of
28  * events when complete objects are built.
29  *
30  * @author Milos Kleint, Thomas Singer
31  */

32 public class UpdateBuilder
33         implements Builder {
34
35     /**
36      * Parsing constants..
37      */

38     public static final String JavaDoc UNKNOWN = ": nothing known about"; //NOI18N
39
public static final String JavaDoc EXAM_DIR = ": Updating"; //NOI18N
40
public static final String JavaDoc TO_ADD = ": use `cvs add' to create an entry for"; //NOI18N
41
public static final String JavaDoc STATES = "U P A R M C ? "; //NOI18N
42
public static final String JavaDoc WARNING = ": warning: "; //NOI18N
43
public static final String JavaDoc SERVER = "server: "; //NOI18N
44
public static final String JavaDoc PERTINENT = "is not (any longer) pertinent"; //NOI18N
45
public static final String JavaDoc REMOVAL = "for removal"; //NOI18N
46
public static final String JavaDoc SERVER_SCHEDULING = "server: scheduling"; //NOI18N
47
public static final String JavaDoc CONFLICTS = "rcsmerge: warning: conflicts during merge"; //NOI18N
48
public static final String JavaDoc NOT_IN_REPOSITORY = "is no longer in the repository"; //NOI18N;
49
//cotacao/src/client/net/riobranco/common/client/gui/BaseDialogThinlet.java already contains the differences between 1.17 and 1.18
50
private static final String JavaDoc MERGE_SAME = " already contains the differences between";
51     private static final String JavaDoc MERGED = "Merging differences between"; //NOI18N;
52

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

56     private DefaultFileInfoContainer fileInfoContainer;
57
58     /**
59      * The event manager to use.
60      */

61     private EventManager eventManager;
62
63     /**
64      * The local path the command run in.
65      */

66     private final String JavaDoc localPath;
67
68     private String JavaDoc diagnostics;
69     
70     /**
71      * Holds 'G' or 'C' if the current file was merged or conflicted, respectively.
72      */

73     private String JavaDoc fileMergedOrConflict;
74
75
76     public UpdateBuilder(EventManager eventManager, String JavaDoc localPath) {
77         this.eventManager = eventManager;
78         this.localPath = localPath;
79     }
80
81     public void outputDone() {
82         fileMergedOrConflict = null;
83         if (fileInfoContainer != null) {
84             if (fileInfoContainer.getFile() == null) {
85                 System.err.println("#65387 CVS: firing invalid event while processing: " + diagnostics);
86             }
87             eventManager.fireCVSEvent(new FileInfoEvent(this, fileInfoContainer));
88             fileInfoContainer = null;
89         }
90     }
91
92     public void parseLine(String JavaDoc line, boolean isErrorMessage) {
93         diagnostics = line;
94         if (line.indexOf(UNKNOWN) >= 0) {
95             processUnknownFile(line, line.indexOf(UNKNOWN) + UNKNOWN.length());
96         }
97         else if (line.indexOf(TO_ADD) >= 0) {
98             processUnknownFile(line, line.indexOf(TO_ADD) + TO_ADD.length());
99         }
100         else if (line.indexOf(EXAM_DIR) >= 0) { // never comes with :local; connection method
101
return;
102         }
103         else if (line.startsWith(CONFLICTS)) {
104             if (fileInfoContainer != null) {
105                 fileInfoContainer.setType("C"); //NOI18N
106
// fire from Merged response which follows
107
}
108             fileMergedOrConflict = "C";
109         }
110         else if (line.indexOf(WARNING) >= 0) {
111             if (line.indexOf(PERTINENT) > 0) {
112                 String JavaDoc filename = line.substring(line.indexOf(WARNING) + WARNING.length(),
113                                                  line.indexOf(PERTINENT)).trim();
114                 processNotPertinent(filename);
115             }
116         }
117         else if (line.indexOf(SERVER_SCHEDULING) >= 0) {
118             if (line.indexOf(REMOVAL) > 0) {
119                 String JavaDoc filename = line.substring(line.indexOf(SERVER_SCHEDULING) + SERVER_SCHEDULING.length(),
120                                                  line.indexOf(REMOVAL)).trim();
121                 processNotPertinent(filename);
122             }
123         }
124         else if (line.indexOf(MERGE_SAME) >= 0) { // not covered by parseEnhancedMessage
125
ensureExistingFileInfoContainer();
126             fileInfoContainer.setType(DefaultFileInfoContainer.MERGED_FILE);
127             String JavaDoc path = line.substring(0, line.indexOf(MERGE_SAME));
128             fileInfoContainer.setFile(createFile(path));
129             outputDone();
130         }
131         else if (line.startsWith(MERGED)) { // not covered by parseEnhancedMessage
132
outputDone();
133             fileMergedOrConflict = "G";
134         }
135         else if (line.indexOf(NOT_IN_REPOSITORY) > 0) {
136             String JavaDoc filename = line.substring(line.indexOf(SERVER) + SERVER.length(),
137                                              line.indexOf(NOT_IN_REPOSITORY)).trim();
138             processNotPertinent(filename);
139             return;
140         }
141         else {
142             // otherwise
143
if (line.length() > 2) {
144                 String JavaDoc firstChar = line.substring(0, 2);
145                 if (STATES.indexOf(firstChar) >= 0) {
146                     processFile(line);
147                     return;
148                 }
149             }
150         }
151     }
152
153     private File createFile(String JavaDoc fileName) {
154         return new File(localPath, fileName);
155     }
156
157     private void ensureExistingFileInfoContainer() {
158         if (fileInfoContainer != null) {
159             return;
160         }
161         fileInfoContainer = new DefaultFileInfoContainer();
162     }
163
164     private void processUnknownFile(String JavaDoc line, int index) {
165         outputDone();
166         fileInfoContainer = new DefaultFileInfoContainer();
167         fileInfoContainer.setType("?"); //NOI18N
168
String JavaDoc fileName = (line.substring(index)).trim();
169         fileInfoContainer.setFile(createFile(fileName));
170     }
171
172     private void processFile(String JavaDoc line) {
173         String JavaDoc fileName = line.substring(2).trim();
174
175         if (fileName.startsWith("no file")) { //NOI18N
176
fileName = fileName.substring(8);
177         }
178
179         if (fileName.startsWith("./")) { //NOI18N
180
fileName = fileName.substring(2);
181         }
182
183         File file = createFile(fileName);
184         if (fileInfoContainer != null) {
185             // sometimes (when locally modified.. the merged response is followed by mesage M <file> or C <file>..
186
// check the file.. if equals.. it's the same one.. don't send again.. the prior type has preference
187
if (fileInfoContainer.getFile() == null) {
188                 // is null in case the global switch -n is used - then no Enhanced message is sent, and no
189
// file is assigned the merged file..
190
fileInfoContainer.setFile(file);
191             }
192             if (file.equals(fileInfoContainer.getFile())) {
193                 // if the previous information does not say anything, prefer newer one
194
if (fileInfoContainer.getType().equals("?")) {
195                     fileInfoContainer = null;
196                 } else {
197                     outputDone();
198                     return;
199                 }
200             }
201         }
202
203         if (fileMergedOrConflict != null && line.charAt(0) == 'M') {
204             line = fileMergedOrConflict; // can be done this way, see below
205
}
206         
207         outputDone();
208         ensureExistingFileInfoContainer();
209
210         fileInfoContainer.setType(line.substring(0, 1));
211         fileInfoContainer.setFile(file);
212     }
213
214     private void processLog(String JavaDoc line) {
215         ensureExistingFileInfoContainer();
216     }
217
218     private void processNotPertinent(String JavaDoc fileName) {
219         outputDone();
220         File fileToDelete = createFile(fileName);
221
222         ensureExistingFileInfoContainer();
223
224         // HACK - will create a non-cvs status in order to be able to have consistent info format
225
fileInfoContainer.setType(DefaultFileInfoContainer.PERTINENT_STATE);
226         fileInfoContainer.setFile(fileToDelete);
227     }
228
229     /** <tt>Merged</tt> response handler. */
230     public void parseEnhancedMessage(String JavaDoc key, Object JavaDoc value) {
231         if (key.equals(EnhancedMessageEvent.MERGED_PATH)) {
232             ensureExistingFileInfoContainer();
233             String JavaDoc path = value.toString();
234             File newFile = new File(path);
235             // #70106 Merged responce must not rewrite CONFLICTS
236
if (newFile.equals(fileInfoContainer.getFile()) == false) {
237                 fileInfoContainer.setFile(newFile);
238                 fileInfoContainer.setType(DefaultFileInfoContainer.MERGED_FILE);
239                 if (fileMergedOrConflict != null) {
240                     fileInfoContainer.setType(fileMergedOrConflict);
241                 }
242             }
243             outputDone();
244         }
245     }
246 }
247
248
Popular Tags