KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > edit > EditCommand


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.edit;
20
21 import java.io.*;
22 import java.util.*;
23
24 import org.netbeans.lib.cvsclient.*;
25 import org.netbeans.lib.cvsclient.admin.*;
26 import org.netbeans.lib.cvsclient.command.*;
27 import org.netbeans.lib.cvsclient.connection.*;
28 import org.netbeans.lib.cvsclient.event.*;
29 import org.netbeans.lib.cvsclient.file.*;
30 import org.netbeans.lib.cvsclient.request.*;
31
32 /**
33  * @author Thomas Singer
34  */

35 public class EditCommand extends BasicCommand {
36
37     /**
38      * Returns the file used for backup the specified file in the edit command.
39      */

40     public static File getEditBackupFile(File file) {
41         return new File(file.getParent(),
42                         "CVS/Base/" + file.getName()); // NOI18N
43
}
44
45
46     private boolean checkThatUnedited;
47     private boolean forceEvenIfEdited;
48     private Watch temporaryWatch;
49
50     private transient ClientServices clientServices;
51
52     /**
53      * Construct a new editors command.
54      */

55     public EditCommand() {
56         resetCVSCommand();
57     }
58
59     /**
60      * Executes this command.
61      *
62      * @param client the client services object that provides any necessary
63      * services to this command, including the ability to actually
64      * process all the requests.
65      */

66     public void execute(ClientServices clientServices, EventManager eventManager)
67             throws CommandException {
68         this.clientServices = clientServices;
69         try {
70             clientServices.ensureConnection();
71
72             super.execute(clientServices, eventManager);
73
74             addArgumentRequest(isCheckThatUnedited(), "-c"); // NOI18N
75
addArgumentRequest(isForceEvenIfEdited(), "-f"); // NOI18N
76

77             // now add the request that indicates the working directory for the
78
// command
79
addRequestForWorkingDirectory(clientServices);
80             addRequest(CommandRequest.NOOP);
81
82             clientServices.processRequests(requests);
83         }
84         catch (AuthenticationException ex) {
85             //TODO: handle case, where connection wasn't possible to establish
86
}
87         catch (CommandException ex) {
88             throw ex;
89         }
90         catch (EOFException ex) {
91             throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); //NOI18N
92
}
93         catch (Exception JavaDoc ex) {
94             throw new CommandException(ex, ex.getLocalizedMessage());
95         }
96         finally {
97             requests.clear();
98             this.clientServices = null;
99         }
100     }
101
102     protected void addRequestForFile(File file, Entry entry) {
103         String JavaDoc temporaryWatch = Watch.getWatchString(getTemporaryWatch());
104         requests.add(new NotifyRequest(file, "E", temporaryWatch)); // NOI18N
105

106         try {
107             editFile(clientServices, file);
108         }
109         catch (IOException ex) {
110             // ignore
111
}
112     }
113
114     /**
115      * Called when server responses with "ok" or "error", (when the command
116      * finishes).
117      */

118     public void commandTerminated(TerminationEvent e) {
119         if (builder != null) {
120             builder.outputDone();
121         }
122     }
123
124     /**
125      * This method returns how the tag command would looklike when typed on the
126      * command line.
127      */

128     public String JavaDoc getCVSCommand() {
129         StringBuffer JavaDoc cvsCommandLine = new StringBuffer JavaDoc("edit "); //NOI18N
130
cvsCommandLine.append(getCVSArguments());
131         appendFileArguments(cvsCommandLine);
132         return cvsCommandLine.toString();
133     }
134
135     /**
136      * Takes the arguments and sets the command.
137      * To be mainly used for automatic settings (like parsing the .cvsrc file)
138      * @return true if the option (switch) was recognized and set
139      */

140     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
141         if (opt == 'R') {
142             setRecursive(true);
143         }
144         else if (opt == 'l') {
145             setRecursive(false);
146         }
147         else {
148             return false;
149         }
150         return true;
151     }
152
153     /**
154      * String returned by this method defines which options are available for
155      * this command.
156      */

157     public String JavaDoc getOptString() {
158         return "Rl"; //NOI18N
159
}
160
161     /**
162      * Resets all switches in the command.
163      * After calling this method, the command should have no switches defined
164      * and should behave defaultly.
165      */

166     public void resetCVSCommand() {
167         setRecursive(true);
168         setCheckThatUnedited(false);
169         setForceEvenIfEdited(true);
170         setTemporaryWatch(null);
171     }
172
173     /**
174      * Returns the arguments of the command in the command-line style.
175      * Similar to getCVSCommand() however without the files and command's name
176      */

177     public String JavaDoc getCVSArguments() {
178         StringBuffer JavaDoc cvsArguments = new StringBuffer JavaDoc();
179         if (!isRecursive()) {
180             cvsArguments.append("-l "); //NOI18N
181
}
182         return cvsArguments.toString();
183     }
184
185     /**
186      * Returns whether to check for unedited files.
187      */

188     public boolean isCheckThatUnedited() {
189         return checkThatUnedited;
190     }
191
192     /**
193      * Sets whether to check for unedited files.
194      * This is cvs' -c option.
195      */

196     public void setCheckThatUnedited(boolean checkThatUnedited) {
197         this.checkThatUnedited = checkThatUnedited;
198     }
199
200     /**
201      * Returns whether the edit is forces even if the files are edited.
202      */

203     public boolean isForceEvenIfEdited() {
204         return forceEvenIfEdited;
205     }
206
207     /**
208      * Sets whether the edit is forces even if the files are edited.
209      * This is cvs' -f option.
210      */

211     public void setForceEvenIfEdited(boolean forceEvenIfEdited) {
212         this.forceEvenIfEdited = forceEvenIfEdited;
213     }
214
215     /**
216      * Returns the temporary watch.
217      */

218     public Watch getTemporaryWatch() {
219         return temporaryWatch;
220     }
221
222     /**
223      * Sets the temporary watch.
224      * This is cvs' -a option.
225      */

226     public void setTemporaryWatch(Watch temporaryWatch) {
227         this.temporaryWatch = temporaryWatch;
228     }
229
230     private void editFile(ClientServices clientServices, File file) throws IOException {
231         addBaserevEntry(clientServices, file);
232         FileUtils.copyFile(file, EditCommand.getEditBackupFile(file));
233         FileUtils.setFileReadOnly(file, false);
234     }
235
236     /**
237      * Create file CVS/Baserev with entries like
238      * BEntry.java/1.2/
239      */

240     private void addBaserevEntry(ClientServices clientServices, File file) throws IOException {
241         final Entry entry = clientServices.getEntry(file);
242         if (entry == null || entry.getRevision() == null || entry.isNewUserFile() || entry.isUserFileToBeRemoved()) {
243             throw new IllegalArgumentException JavaDoc("File does not have an Entry or Entry is invalid!"); // NOI18N
244
}
245
246         File baserevFile = new File(file.getParentFile(), "CVS/Baserev"); // NOI18N
247
File backupFile = new File(baserevFile.getAbsolutePath() + '~');
248         BufferedReader reader = null;
249         BufferedWriter writer = null;
250         boolean append = true;
251         boolean writeFailed = true;
252         final String JavaDoc entryStart = 'B' + file.getName() + '/';
253         try {
254             writer = new BufferedWriter(new FileWriter(backupFile));
255             writeFailed = false;
256             reader = new BufferedReader(new FileReader(baserevFile));
257
258             for (String JavaDoc line = reader.readLine();
259                  line != null;
260                  line = reader.readLine()) {
261
262                 if (line.startsWith(entryStart)) {
263                     append = false;
264                 }
265                 writeFailed = true;
266                 writer.write(line);
267                 writer.newLine();
268                 writeFailed = false;
269             }
270         }
271         catch (IOException ex) {
272             if (writeFailed) {
273                 throw ex;
274             }
275         }
276         finally {
277             if (reader != null) {
278                 try {
279                     reader.close();
280                 }
281                 catch (IOException ex) {
282                     // ignore
283
}
284             }
285             if (writer != null) {
286                 try {
287                     if (append && !writeFailed) {
288                         writer.write(entryStart + entry.getRevision() + '/');
289                         writer.newLine();
290                     }
291                 } finally {
292                     try {
293                         writer.close();
294                     }
295                     catch (IOException ex) {
296                         // ignore
297
}
298                 }
299             }
300         }
301         baserevFile.delete();
302         backupFile.renameTo(baserevFile);
303     }
304 }
305
Popular Tags