KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > unedit > UneditCommand


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.unedit;
20
21 import java.io.*;
22
23 import org.netbeans.lib.cvsclient.*;
24 import org.netbeans.lib.cvsclient.admin.*;
25 import org.netbeans.lib.cvsclient.command.*;
26 import org.netbeans.lib.cvsclient.command.edit.*;
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 UneditCommand extends BasicCommand {
36
37     private Watch temporaryWatch;
38
39     /**
40      * Construct a new editors command.
41      */

42     public UneditCommand() {
43         resetCVSCommand();
44     }
45
46     /**
47      * Execute the command.
48      *
49      * @param client the client services object that provides any necessary
50      * services to this command, including the ability to actually
51      * process all the requests.
52      */

53     public void execute(ClientServices clientServices, EventManager eventManager)
54             throws CommandException, AuthenticationException {
55         clientServices.ensureConnection();
56
57         try {
58             super.execute(clientServices, eventManager);
59
60             addRequestForWorkingDirectory(clientServices);
61             addRequest(CommandRequest.NOOP);
62
63             clientServices.processRequests(requests);
64         }
65         catch (CommandException ex) {
66             throw ex;
67         }
68         catch (EOFException ex) {
69             throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); //NOI18N
70
}
71         catch (Exception JavaDoc ex) {
72             throw new CommandException(ex, ex.getLocalizedMessage());
73         }
74         finally {
75             requests.clear();
76         }
77     }
78
79     protected void addRequestForFile(File file, Entry entry) {
80         String JavaDoc temporaryWatch = Watch.getWatchString(getTemporaryWatch());
81         requests.add(new NotifyRequest(file, "U", temporaryWatch)); // NOI18N
82

83         try {
84             uneditFile(file);
85         }
86         catch (IOException ex) {
87             // ignore
88
}
89     }
90
91     /**
92      * Called when server responses with "ok" or "error", (when the command
93      * finishes).
94      */

95     public void commandTerminated(TerminationEvent e) {
96         if (builder != null) {
97             builder.outputDone();
98         }
99     }
100
101     /**
102      * This method returns how the tag command would looklike when typed on the
103      * command line.
104      */

105     public String JavaDoc getCVSCommand() {
106         StringBuffer JavaDoc cvsCommandLine = new StringBuffer JavaDoc("unedit "); //NOI18N
107
cvsCommandLine.append(getCVSArguments());
108         appendFileArguments(cvsCommandLine);
109         return cvsCommandLine.toString();
110     }
111
112     /**
113      * Takes the arguments and sets the command.
114      * To be mainly used for automatic settings (like parsing the .cvsrc file)
115      * @return true if the option (switch) was recognized and set
116      */

117     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
118         if (opt == 'R') {
119             setRecursive(true);
120         }
121         else if (opt == 'l') {
122             setRecursive(false);
123         }
124         else {
125             return false;
126         }
127         return true;
128     }
129
130     /**
131      * String returned by this method defines which options are available for
132      * this command.
133      */

134     public String JavaDoc getOptString() {
135         return "Rl"; //NOI18N
136
}
137
138     /**
139      * Resets all switches in the command.
140      * After calling this method, the command should have no switches defined
141      * and should behave defaultly.
142      */

143     public void resetCVSCommand() {
144         setRecursive(true);
145     }
146
147     /**
148      * Returns the arguments of the command in the command-line style.
149      * Similar to getCVSCommand() however without the files and command's name
150      */

151     public String JavaDoc getCVSArguments() {
152         StringBuffer JavaDoc cvsArguments = new StringBuffer JavaDoc();
153         if (!isRecursive()) {
154             cvsArguments.append("-l "); //NOI18N
155
}
156         return cvsArguments.toString();
157     }
158
159     /**
160      * Returns the temporary watch.
161      */

162     public Watch getTemporaryWatch() {
163         return temporaryWatch;
164     }
165
166     /**
167      * Sets the temporary watch.
168      */

169     public void setTemporaryWatch(Watch temporaryWatch) {
170         this.temporaryWatch = temporaryWatch;
171     }
172
173     private void uneditFile(File file) throws IOException {
174         removeBaserevEntry(file);
175         EditCommand.getEditBackupFile(file).delete();
176         FileUtils.setFileReadOnly(file, true);
177     }
178
179     private void removeBaserevEntry(File file) throws IOException {
180         File baserevFile = new File(file.getParentFile(), "CVS/Baserev"); // NOI18N
181
File backupFile = new File(baserevFile.getAbsolutePath() + '~');
182
183         BufferedReader reader = null;
184         BufferedWriter writer = null;
185         final String JavaDoc entryStart = 'B' + file.getName() + '/';
186         try {
187             writer = new BufferedWriter(new FileWriter(backupFile));
188             reader = new BufferedReader(new FileReader(baserevFile));
189
190             for (String JavaDoc line = reader.readLine();
191                  line != null;
192                  line = reader.readLine()) {
193
194                 if (line.startsWith(entryStart)) {
195                     continue;
196                 }
197
198                 writer.write(line);
199                 writer.newLine();
200             }
201         }
202         catch (FileNotFoundException ex) {
203             // ignore
204
}
205         finally {
206             if (writer != null) {
207                 try {
208                     writer.close();
209                 }
210                 catch (IOException ex) {
211                     // ignore
212
}
213             }
214             if (reader != null) {
215                 try {
216                     reader.close();
217                 }
218                 catch (IOException ex) {
219                     // ignore
220
}
221             }
222         }
223         baserevFile.delete();
224         if (backupFile.length() > 0) {
225             backupFile.renameTo(baserevFile);
226         }
227         else {
228             backupFile.delete();
229         }
230     }
231
232 }
233
Popular Tags