1 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 35 public class UneditCommand extends BasicCommand { 36 37 private Watch temporaryWatch; 38 39 42 public UneditCommand() { 43 resetCVSCommand(); 44 } 45 46 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)); } 71 catch (Exception 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 temporaryWatch = Watch.getWatchString(getTemporaryWatch()); 81 requests.add(new NotifyRequest(file, "U", temporaryWatch)); 83 try { 84 uneditFile(file); 85 } 86 catch (IOException ex) { 87 } 89 } 90 91 95 public void commandTerminated(TerminationEvent e) { 96 if (builder != null) { 97 builder.outputDone(); 98 } 99 } 100 101 105 public String getCVSCommand() { 106 StringBuffer cvsCommandLine = new StringBuffer ("unedit "); cvsCommandLine.append(getCVSArguments()); 108 appendFileArguments(cvsCommandLine); 109 return cvsCommandLine.toString(); 110 } 111 112 117 public boolean setCVSCommand(char opt, String 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 134 public String getOptString() { 135 return "Rl"; } 137 138 143 public void resetCVSCommand() { 144 setRecursive(true); 145 } 146 147 151 public String getCVSArguments() { 152 StringBuffer cvsArguments = new StringBuffer (); 153 if (!isRecursive()) { 154 cvsArguments.append("-l "); } 156 return cvsArguments.toString(); 157 } 158 159 162 public Watch getTemporaryWatch() { 163 return temporaryWatch; 164 } 165 166 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"); File backupFile = new File(baserevFile.getAbsolutePath() + '~'); 182 183 BufferedReader reader = null; 184 BufferedWriter writer = null; 185 final String entryStart = 'B' + file.getName() + '/'; 186 try { 187 writer = new BufferedWriter(new FileWriter(backupFile)); 188 reader = new BufferedReader(new FileReader(baserevFile)); 189 190 for (String 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 } 205 finally { 206 if (writer != null) { 207 try { 208 writer.close(); 209 } 210 catch (IOException ex) { 211 } 213 } 214 if (reader != null) { 215 try { 216 reader.close(); 217 } 218 catch (IOException ex) { 219 } 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 |