001: /*****************************************************************************
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is the CVS Client Library.
027: * The Initial Developer of the Original Software is Robert Greig.
028: * Portions created by Robert Greig are Copyright (C) 2000.
029: * All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041: *
042: * Contributor(s): Robert Greig.
043: *****************************************************************************/package org.netbeans.lib.cvsclient.response;
044:
045: import java.io.*;
046: import java.util.*;
047: import java.text.*;
048:
049: import org.netbeans.lib.cvsclient.admin.*;
050: import org.netbeans.lib.cvsclient.event.*;
051: import org.netbeans.lib.cvsclient.file.*;
052: import org.netbeans.lib.cvsclient.util.*;
053:
054: /**
055: * Sends a diff of a particular file, indicating that the file currently
056: * checked-out needs to be updated by the patch sent with this response.
057: * @author Milos Kleint
058: */
059: class RcsDiffResponse implements Response {
060:
061: private static final boolean DEBUG = false;
062:
063: /**
064: * The local path of the new file.
065: */
066: private String localPath;
067:
068: /**
069: * The full repository path of the file.
070: */
071: private String repositoryPath;
072:
073: /**
074: * The entry line.
075: */
076: private String entryLine;
077:
078: /**
079: * The file mode.
080: */
081: private String mode;
082:
083: /**
084: * fullpath to the file being processed.
085: */
086: protected String localFile;
087:
088: /**
089: * The date Formatter used to parse and format dates.
090: * Format is: "EEE MMM dd HH:mm:ss yyyy"
091: */
092: private DateFormat dateFormatter;
093:
094: /**
095: * Process the data for the response.
096: * @param r the buffered reader allowing the client to read the server's
097: * response. Note that the actual response name has already been read
098: * and the reader is positioned just before the first argument, if any.
099: * @param services various services that are useful to response handlers
100: * @throws ResponseException if something goes wrong handling this response
101: */
102: public void process(LoggedDataInputStream dis,
103: ResponseServices services) throws ResponseException {
104: try {
105: localPath = dis.readLine();
106: repositoryPath = dis.readLine();
107: entryLine = dis.readLine();
108: mode = dis.readLine();
109:
110: String nextLine = dis.readLine();
111:
112: boolean useGzip = (nextLine.charAt(0) == 'z');
113:
114: int length = Integer.parseInt(useGzip ? nextLine
115: .substring(1) : nextLine);
116:
117: if (DEBUG) {
118: System.err.println("Got update response."); //NOI18N
119: System.err.println("LocalPath is : "
120: + localPath); //NOI18N
121: System.err.println("Repository path is : "
122: + repositoryPath); //NOI18N
123: System.err.println("Entries line is : "
124: + entryLine); //NOI18N
125: System.err.println("Mode is : " + mode); //NOI18N
126: System.err.println("Next line (length) is : "
127: + nextLine); //NOI18N
128: System.err.println("File length is : " + length); //NOI18N
129: }
130:
131: // now read in the file
132: final String filePath = services.convertPathname(localPath,
133: repositoryPath);
134:
135: final File newFile = new File(filePath);
136:
137: if (services.getGlobalOptions().isExcluded(newFile)) {
138: while (length > 0) {
139: length -= dis.skip(length);
140: }
141: return;
142: }
143:
144: localFile = newFile.getAbsolutePath();
145: final Entry entry = new Entry(entryLine);
146:
147: FileHandler fileHandler = useGzip ? services
148: .getGzipFileHandler() : services
149: .getUncompressedFileHandler();
150: // FileHandler fileHandler = useGzip ? getGzippedFileHandler()
151: // : getUncompressedFileHandler();
152: fileHandler.setNextFileDate(services.getNextFileDate());
153:
154: // check if the file is binary
155: if (entry.isBinary()) {
156: // should actually not happen. it's possible to send pathces for text files only..
157: //TODO add BugLog print here
158: } else {
159: fileHandler.writeRcsDiffFile(filePath, mode, dis,
160: length);
161: }
162:
163: // we set the date the file was last modified in the Entry line
164: // so that we can easily determine whether the file has been
165: // untouched
166: // for files with conflicts skip the setting of the conflict field.
167: //NOT SURE THIS IS NESSESARY HERE..
168: String conflictString = null;
169: if ((entry.getConflict() != null)
170: && (entry.getConflict().charAt(0) == Entry.HAD_CONFLICTS)) {
171: if (entry.getConflict().charAt(1) == Entry.TIMESTAMP_MATCHES_FILE) {
172: final Date d = new Date(newFile.lastModified());
173: conflictString = getEntryConflict(d, true);
174: } else {
175: conflictString = entry.getConflict().substring(1);
176: }
177: } else {
178: final Date d = new Date(newFile.lastModified());
179: conflictString = getEntryConflict(d, false);
180: }
181: entry.setConflict(conflictString);
182:
183: // update the admin files (i.e. within the CVS directory)
184: services.updateAdminData(localPath, repositoryPath, entry);
185:
186: FileUpdatedEvent e = new FileUpdatedEvent(this , filePath);
187: services.getEventManager().fireCVSEvent(e);
188: //System.err.println("Finished writing file");
189: } catch (IOException e) {
190: throw new ResponseException(e);
191: }
192: }
193:
194: /**
195: * Returns the Conflict field for the file's entry.
196: * Can be overriden by subclasses.
197: * (For example the MergedResponse that sets the "result of merge" there.)
198: * @param date the date to put in
199: * @param hadConflicts if there were conflicts (e.g after merge)
200: * @return the conflict field
201: */
202: protected String getEntryConflict(Date date, boolean hadConflicts) {
203: return getDateFormatter().format(date);
204: }
205:
206: /**
207: * Is this a terminal response, i.e. should reading of responses stop
208: * after this response. This is true for responses such as OK or
209: * an error response
210: */
211: public boolean isTerminalResponse() {
212: return false;
213: }
214:
215: /**
216: * Returns the DateFormatter instance that parses and formats date Strings.
217: * The exact format matches the one in Entry.getLastModifiedDateFormatter() method.
218: *
219: */
220: protected DateFormat getDateFormatter() {
221: if (dateFormatter == null) {
222: dateFormatter = Entry.getLastModifiedDateFormatter();
223: }
224: return dateFormatter;
225: }
226:
227: }
|