KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > response > RcsDiffResponse


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 the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.response;
23
24 import java.io.*;
25 import java.util.*;
26 import java.text.*;
27
28 import org.netbeans.lib.cvsclient.admin.*;
29 import org.netbeans.lib.cvsclient.event.*;
30 import org.netbeans.lib.cvsclient.file.*;
31 import org.netbeans.lib.cvsclient.util.*;
32
33 /**
34  * Sends a diff of a particular file, indicating that the file currently
35  * checked-out needs to be updated by the patch sent with this response.
36  * @author Milos Kleint
37  */

38 class RcsDiffResponse implements Response {
39
40     private static final boolean DEBUG = false;
41
42     /**
43      * The local path of the new file.
44      */

45     private String JavaDoc localPath;
46
47     /**
48      * The full repository path of the file.
49      */

50     private String JavaDoc repositoryPath;
51
52     /**
53      * The entry line.
54      */

55     private String JavaDoc entryLine;
56
57     /**
58      * The file mode.
59      */

60     private String JavaDoc mode;
61
62     /**
63      * fullpath to the file being processed.
64      */

65     protected String JavaDoc localFile;
66     
67     /**
68      * The date Formatter used to parse and format dates.
69      * Format is: "EEE MMM dd HH:mm:ss yyyy"
70      */

71     private DateFormat dateFormatter;
72
73     /**
74      * Process the data for the response.
75      * @param r the buffered reader allowing the client to read the server's
76      * response. Note that the actual response name has already been read
77      * and the reader is positioned just before the first argument, if any.
78      * @param services various services that are useful to response handlers
79      * @throws ResponseException if something goes wrong handling this response
80      */

81     public void process(LoggedDataInputStream dis, ResponseServices services)
82             throws ResponseException {
83         try {
84             localPath = dis.readLine();
85             repositoryPath = dis.readLine();
86             entryLine = dis.readLine();
87             mode = dis.readLine();
88
89             String JavaDoc nextLine = dis.readLine();
90
91             boolean useGzip = (nextLine.charAt(0) == 'z');
92
93             int length = Integer.parseInt(useGzip ? nextLine.substring(1)
94                                           : nextLine);
95
96             if (DEBUG) {
97                 System.err.println("Got update response."); //NOI18N
98
System.err.println("LocalPath is : " + localPath); //NOI18N
99
System.err.println("Repository path is : " + repositoryPath); //NOI18N
100
System.err.println("Entries line is : " + entryLine); //NOI18N
101
System.err.println("Mode is : " + mode); //NOI18N
102
System.err.println("Next line (length) is : " + nextLine); //NOI18N
103
System.err.println("File length is : " + length); //NOI18N
104
}
105
106             // now read in the file
107
final String JavaDoc filePath = services.convertPathname(localPath,
108                                                              repositoryPath);
109
110             final File newFile = new File(filePath);
111             
112             if (services.getGlobalOptions().isExcluded(newFile)) {
113                 while (length > 0) {
114                     length -= dis.skip(length);
115                 }
116                 return;
117             }
118             
119             localFile = newFile.getAbsolutePath();
120             final Entry entry = new Entry(entryLine);
121
122             FileHandler fileHandler = useGzip ? services.getGzipFileHandler()
123                     : services.getUncompressedFileHandler();
124             // FileHandler fileHandler = useGzip ? getGzippedFileHandler()
125
// : getUncompressedFileHandler();
126
fileHandler.setNextFileDate(services.getNextFileDate());
127
128             // check if the file is binary
129
if (entry.isBinary()) {
130                 // should actually not happen. it's possible to send pathces for text files only..
131
//TODO add BugLog print here
132
}
133             else {
134                 fileHandler.writeRcsDiffFile(filePath, mode, dis, length);
135             }
136
137             // we set the date the file was last modified in the Entry line
138
// so that we can easily determine whether the file has been
139
// untouched
140
// for files with conflicts skip the setting of the conflict field.
141
//NOT SURE THIS IS NESSESARY HERE..
142
String JavaDoc conflictString = null;
143             if ((entry.getConflict() != null) &&
144                     (entry.getConflict().charAt(0) == Entry.HAD_CONFLICTS)) {
145                 if (entry.getConflict().charAt(1) ==
146                         Entry.TIMESTAMP_MATCHES_FILE) {
147                     final Date d = new Date(newFile.lastModified());
148                     conflictString = getEntryConflict(d, true);
149                 }
150                 else {
151                     conflictString = entry.getConflict().substring(1);
152                 }
153             }
154             else {
155                 final Date d = new Date(newFile.lastModified());
156                 conflictString = getEntryConflict(d, false);
157             }
158             entry.setConflict(conflictString);
159
160             // update the admin files (i.e. within the CVS directory)
161
services.updateAdminData(localPath, repositoryPath, entry);
162
163             FileUpdatedEvent e = new FileUpdatedEvent(this, filePath);
164             services.getEventManager().fireCVSEvent(e);
165             //System.err.println("Finished writing file");
166
}
167         catch (IOException e) {
168             throw new ResponseException(e);
169         }
170     }
171
172     /**
173      * Returns the Conflict field for the file's entry.
174      * Can be overriden by subclasses.
175      * (For example the MergedResponse that sets the "result of merge" there.)
176      * @param date the date to put in
177      * @param hadConflicts if there were conflicts (e.g after merge)
178      * @return the conflict field
179      */

180     protected String JavaDoc getEntryConflict(Date date, boolean hadConflicts) {
181         return getDateFormatter().format(date);
182     }
183
184     /**
185      * Is this a terminal response, i.e. should reading of responses stop
186      * after this response. This is true for responses such as OK or
187      * an error response
188      */

189     public boolean isTerminalResponse() {
190         return false;
191     }
192     
193     /**
194      * Returns the DateFormatter instance that parses and formats date Strings.
195      * The exact format matches the one in Entry.getLastModifiedDateFormatter() method.
196      *
197      */

198     protected DateFormat getDateFormatter() {
199         if (dateFormatter == null) {
200             dateFormatter = new SimpleDateFormat(Entry.getLastModifiedDateFormatter().toPattern(), Locale.US);
201             dateFormatter.setTimeZone(Entry.getTimeZone());
202         }
203         return dateFormatter;
204     }
205     
206 }
207
Popular Tags