KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > remove > RemoveCommand


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.command.remove;
23
24 import java.io.*;
25
26 import org.netbeans.lib.cvsclient.*;
27 import org.netbeans.lib.cvsclient.admin.*;
28 import org.netbeans.lib.cvsclient.command.*;
29 import org.netbeans.lib.cvsclient.connection.*;
30 import org.netbeans.lib.cvsclient.event.*;
31 import org.netbeans.lib.cvsclient.request.*;
32 import org.netbeans.lib.cvsclient.util.*;
33
34 /**
35  * The remove command is used to remove files and directories from the
36  * repository.
37  * @author Robert Greig
38  */

39 public class RemoveCommand extends BasicCommand {
40     /**
41      * If true, will delete the file in working dir before it gets removed.
42      */

43     private boolean deleteBeforeRemove;
44
45     private boolean ignoreLocallyExistingFiles;
46
47     /**
48      * Returns true if the local files will be deleted automatically.
49      */

50     public boolean isDeleteBeforeRemove() {
51         return deleteBeforeRemove;
52     }
53
54     /**
55      * Sets whether the local files will be deleted before.
56      */

57     public void setDeleteBeforeRemove(boolean deleteBeforeRemove) {
58         this.deleteBeforeRemove = deleteBeforeRemove;
59     }
60
61     /**
62      * Returns true to indicate that locally existing files are treated as they
63      * would not exist.
64      * This is a extension to the standard cvs-behaviour!
65      * @deprecated
66      */

67     public boolean doesIgnoreLocallyExistingFiles() {
68         return ignoreLocallyExistingFiles;
69     }
70
71     /**
72      * Returns true to indicate that locally existing files are treated as they
73      * would not exist.
74      * This is a extension to the standard cvs-behaviour!
75      */

76     public boolean isIgnoreLocallyExistingFiles() {
77         return ignoreLocallyExistingFiles;
78     }
79
80     /**
81      * Sets whether locally existing files will be treated as they were deleted
82      * before.
83      * This is a extension to the standard cvs-behaviour!
84      */

85     public void setIgnoreLocallyExistingFiles(boolean ignoreLocallyExistingFiles) {
86         this.ignoreLocallyExistingFiles = ignoreLocallyExistingFiles;
87     }
88
89     /**
90      * Method that is called while the command is being executed.
91      * Descendants can override this method to return a Builder instance
92      * that will parse the server's output and create data structures.
93      */

94     public Builder createBuilder(EventManager eventMan) {
95         return new RemoveBuilder(eventMan, this);
96     }
97
98     /**
99      * Executes this command.
100      *
101      * @param client the client services object that provides any necessary
102      * services to this command, including the ability to actually
103      * process all the requests
104      */

105     public void execute(ClientServices client, EventManager em)
106             throws CommandException, AuthenticationException {
107         if (files == null || files.length == 0) {
108             throw new CommandException("No files have been specified for " + //NOI18N
109
"removal.", CommandException.getLocalMessage("RemoveCommand.noFilesSpecified", null)); //NOI18N
110
}
111
112         client.ensureConnection();
113
114         if (isDeleteBeforeRemove()) {
115             removeAll(files);
116         }
117         super.execute(client, em);
118
119         try {
120             addRequestForWorkingDirectory(client);
121             addArgumentRequests();
122             addRequest(CommandRequest.REMOVE);
123
124             client.processRequests(requests);
125         }
126         catch (CommandException ex) {
127             throw ex;
128         }
129         catch (Exception JavaDoc ex) {
130             throw new CommandException(ex, ex.getLocalizedMessage());
131         }
132         finally {
133             requests.clear();
134         }
135     }
136
137     protected void sendEntryAndModifiedRequests(Entry entry, File file) {
138         super.sendEntryAndModifiedRequests(entry,
139                                            isIgnoreLocallyExistingFiles() ? null : file);
140         if (entry.getRevision().equals("0")) {
141              // zero means a locally added file, not yet commited.
142
try {
143                 clientServices.removeEntry(file);
144             } catch (IOException exc) {
145                 BugLog.getInstance().showException(exc);
146             }
147             
148         }
149     }
150
151     /**
152      * This method returns how the command would looks like when typed on the
153      * command line.
154      * Each command is responsible for constructing this information.
155      * @returns <command's name> [<parameters>] files/dirs. Example: checkout -p CvsCommand.java
156      */

157     public String JavaDoc getCVSCommand() {
158         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc("remove "); //NOI18N
159
toReturn.append(getCVSArguments());
160         File[] files = getFiles();
161         if (files != null) {
162             for (int index = 0; index < files.length; index++) {
163                 toReturn.append(files[index].getName() + " "); //NOI18N
164
}
165         }
166         return toReturn.toString();
167     }
168
169     /**
170      * Takes the arguments and sets the command.
171      * To be mainly used for automatic settings (like parsing the .cvsrc file).
172      * @return true if the option (switch) was recognized and set
173      */

174     public boolean setCVSCommand(char opt, String JavaDoc optArg) {
175         if (opt == 'l') {
176             setRecursive(false);
177         }
178         else if (opt == 'R') {
179             setRecursive(true);
180         }
181         else if (opt == 'f') {
182             setDeleteBeforeRemove(true);
183         }
184         else {
185             return false;
186         }
187         return true;
188     }
189
190     /**
191      * Deletes all files being removed from the working directory.
192      * Doesn't delete directories.
193      * Attempts a recursive delete
194      * @throws CommandException - in case the file cannot be deleted.
195      */

196     private void removeAll(File[] filesToDel)
197             throws CommandException {
198         if (filesToDel == null) {
199             return;
200         }
201         for (int index = 0; index < filesToDel.length; index++) {
202             File file = filesToDel[index];
203             if (file.exists() && file.isFile()) {
204                 if (!file.delete()) {
205                     throw new CommandException("Cannot delete file " + file.getAbsolutePath(), //NOI18N
206
CommandException.getLocalMessage("RemoveCommand.cannotDelete", new Object JavaDoc[]{file.getAbsolutePath()})); //NOI18N
207
}
208             }
209             else {
210                 // For directories remove only it's files.
211
// Preserve the cvs structure though.
212
if (isRecursive() &&
213                         !file.getName().equalsIgnoreCase("CVS")) { //NOI18N
214
removeAll(file.listFiles());
215                 }
216             }
217         }
218     }
219
220     /**
221      * String returned by this method defines which options are available for this particular command
222      */

223     public String JavaDoc getOptString() {
224         return "flR"; //NOI18N
225
}
226
227     /**
228      * resets all switches in the command. After calling this method,
229      * the command should have no switches defined and should behave defaultly.
230      */

231     public void resetCVSCommand() {
232         setRecursive(true);
233         setDeleteBeforeRemove(false);
234     }
235
236     /**
237      * Returns the arguments of the command in the command-line style.
238      * Similar to getCVSCommand() however without the files and command's name
239      */

240     public String JavaDoc getCVSArguments() {
241         StringBuffer JavaDoc toReturn = new StringBuffer JavaDoc(""); //NOI18N
242
if (!isRecursive()) {
243             toReturn.append("-l "); //NOI18N
244
}
245         if (isDeleteBeforeRemove()) {
246             toReturn.append("-f "); //NOI18N
247
}
248         return toReturn.toString();
249     }
250 }
251
Popular Tags