1 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 35 public class EditCommand extends BasicCommand { 36 37 40 public static File getEditBackupFile(File file) { 41 return new File(file.getParent(), 42 "CVS/Base/" + file.getName()); } 44 45 46 private boolean checkThatUnedited; 47 private boolean forceEvenIfEdited; 48 private Watch temporaryWatch; 49 50 private transient ClientServices clientServices; 51 52 55 public EditCommand() { 56 resetCVSCommand(); 57 } 58 59 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"); addArgumentRequest(isForceEvenIfEdited(), "-f"); 77 addRequestForWorkingDirectory(clientServices); 80 addRequest(CommandRequest.NOOP); 81 82 clientServices.processRequests(requests); 83 } 84 catch (AuthenticationException ex) { 85 } 87 catch (CommandException ex) { 88 throw ex; 89 } 90 catch (EOFException ex) { 91 throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); } 93 catch (Exception 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 temporaryWatch = Watch.getWatchString(getTemporaryWatch()); 104 requests.add(new NotifyRequest(file, "E", temporaryWatch)); 106 try { 107 editFile(clientServices, file); 108 } 109 catch (IOException ex) { 110 } 112 } 113 114 118 public void commandTerminated(TerminationEvent e) { 119 if (builder != null) { 120 builder.outputDone(); 121 } 122 } 123 124 128 public String getCVSCommand() { 129 StringBuffer cvsCommandLine = new StringBuffer ("edit "); cvsCommandLine.append(getCVSArguments()); 131 appendFileArguments(cvsCommandLine); 132 return cvsCommandLine.toString(); 133 } 134 135 140 public boolean setCVSCommand(char opt, String 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 157 public String getOptString() { 158 return "Rl"; } 160 161 166 public void resetCVSCommand() { 167 setRecursive(true); 168 setCheckThatUnedited(false); 169 setForceEvenIfEdited(true); 170 setTemporaryWatch(null); 171 } 172 173 177 public String getCVSArguments() { 178 StringBuffer cvsArguments = new StringBuffer (); 179 if (!isRecursive()) { 180 cvsArguments.append("-l "); } 182 return cvsArguments.toString(); 183 } 184 185 188 public boolean isCheckThatUnedited() { 189 return checkThatUnedited; 190 } 191 192 196 public void setCheckThatUnedited(boolean checkThatUnedited) { 197 this.checkThatUnedited = checkThatUnedited; 198 } 199 200 203 public boolean isForceEvenIfEdited() { 204 return forceEvenIfEdited; 205 } 206 207 211 public void setForceEvenIfEdited(boolean forceEvenIfEdited) { 212 this.forceEvenIfEdited = forceEvenIfEdited; 213 } 214 215 218 public Watch getTemporaryWatch() { 219 return temporaryWatch; 220 } 221 222 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 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 ("File does not have an Entry or Entry is invalid!"); } 245 246 File baserevFile = new File(file.getParentFile(), "CVS/Baserev"); 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 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 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 } 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 } 298 } 299 } 300 } 301 baserevFile.delete(); 302 backupFile.renameTo(baserevFile); 303 } 304 } 305 | Popular Tags |