1 19 package org.netbeans.lib.cvsclient.command.export; 20 21 import java.io.*; 22 import java.util.*; 23 24 import org.netbeans.lib.cvsclient.*; 25 import org.netbeans.lib.cvsclient.command.*; 26 import org.netbeans.lib.cvsclient.connection.*; 27 import org.netbeans.lib.cvsclient.event.*; 28 import org.netbeans.lib.cvsclient.request.*; 29 30 36 public class ExportCommand extends RepositoryCommand { 37 38 43 private final Set emptyDirectories = new HashSet(); 44 private boolean pruneDirectories; 45 private KeywordSubstitutionOptions keywordSubstitutionOptions; 46 47 48 private String exportByDate; 49 50 51 private String exportByRevision; 52 53 54 private String exportDirectory; 55 56 57 private boolean useHeadIfNotFound; 58 59 60 private boolean notShortenPaths; 61 62 63 private boolean notRunModuleProgram; 64 65 public ExportCommand() { 66 resetCVSCommand(); 67 } 68 69 72 public KeywordSubstitutionOptions getKeywordSubstitutionOptions() { 73 return keywordSubstitutionOptions; 74 } 75 76 79 public void setKeywordSubstitutionOptions(KeywordSubstitutionOptions keywordSubstitutionOptions) { 80 this.keywordSubstitutionOptions = keywordSubstitutionOptions; 81 } 82 83 87 public void setPruneDirectories(boolean pruneDirectories) { 88 this.pruneDirectories = pruneDirectories; 89 } 90 91 96 public boolean isPruneDirectories() { 97 return pruneDirectories; 98 } 99 100 106 protected void postExpansionExecute(ClientServices client, EventManager em) 107 throws CommandException, AuthenticationException { 108 final int FIRST_INDEX = 0; 112 final int SECOND_INDEX = 1; 113 if (!isRecursive()) { 114 requests.add(FIRST_INDEX, new ArgumentRequest("-l")); } 116 if (useHeadIfNotFound) { 117 requests.add(FIRST_INDEX, new ArgumentRequest("-f")); } 119 if (exportDirectory != null && (!exportDirectory.equals(""))) { 120 requests.add(FIRST_INDEX, new ArgumentRequest("-d")); requests.add(SECOND_INDEX, new ArgumentRequest(getExportDirectory())); 122 } 123 if (exportByDate != null && exportByDate.length() > 0) { 124 requests.add(FIRST_INDEX, new ArgumentRequest("-D")); requests.add(SECOND_INDEX, new ArgumentRequest(getExportByDate())); 126 } 127 if (exportByRevision != null && exportByRevision.length() > 0) { 128 requests.add(FIRST_INDEX, new ArgumentRequest("-r")); requests.add(SECOND_INDEX, new ArgumentRequest(getExportByRevision())); 130 } 131 if (notShortenPaths) { 132 requests.add(FIRST_INDEX, new ArgumentRequest("-N")); } 134 if (notRunModuleProgram) { 135 requests.add(FIRST_INDEX, new ArgumentRequest("-n")); } 137 if (getKeywordSubstitutionOptions() != null) { 138 requests.add(new ArgumentRequest("-k" + getKeywordSubstitutionOptions())); } 140 141 addArgumentRequests(); 142 143 requests.add(new DirectoryRequest(".", client.getRepository())); requests.add(CommandRequest.EXPORT); 145 try { 146 client.processRequests(requests); 147 if (pruneDirectories) { 148 pruneEmptyDirectories(); 149 } 150 requests.clear(); 151 152 } 153 catch (CommandException ex) { 154 throw ex; 155 } 156 catch (Exception ex) { 157 throw new CommandException(ex, ex.getLocalizedMessage()); 158 } finally { 159 removeAllCVSAdminFiles(); 160 } 161 } 162 163 private void removeAllCVSAdminFiles() { 164 File rootDirect = null; 165 if (getExportDirectory() != null) { 166 rootDirect = new File(getLocalDirectory(), getExportDirectory()); 167 deleteCVSSubDirs(rootDirect); 168 } 169 else { 170 rootDirect = new File(getLocalDirectory()); 171 Iterator mods = expandedModules.iterator(); 172 while (mods.hasNext()) { 173 String mod = mods.next().toString(); 174 File modRoot = new File(rootDirect.getAbsolutePath(), mod); 175 deleteCVSSubDirs(modRoot); 176 } 177 } 178 } 179 180 private void deleteCVSSubDirs(File root) { 181 if (root.isDirectory()) { 182 File[] subDirs = root.listFiles(); 183 if (subDirs == null) { 184 return; 185 } 186 187 for (int i = 0; i < subDirs.length; i++) { 188 if (subDirs[i].isDirectory()) { 189 if (subDirs[i].getName().equalsIgnoreCase("CVS")) { final File[] adminFiles = subDirs[i].listFiles(); 191 for (int j = 0; j < adminFiles.length; j++) { 192 adminFiles[j].delete(); 193 } 194 subDirs[i].delete(); 195 } 196 else { 197 deleteCVSSubDirs(subDirs[i]); 198 } 199 } 200 } 201 } 202 } 203 204 public String getCVSCommand() { 205 StringBuffer toReturn = new StringBuffer ("export "); toReturn.append(getCVSArguments()); 207 if (modules != null && modules.size() > 0) { 208 for (Iterator it = modules.iterator(); it.hasNext();) { 209 String module = (String )it.next(); 210 toReturn.append(module); 211 toReturn.append(' '); 212 } 213 } 214 else { 215 String localizedMsg = CommandException.getLocalMessage("ExportCommand.moduleEmpty.text"); toReturn.append(" "); toReturn.append(localizedMsg); 218 } 219 return toReturn.toString(); 220 } 221 222 public String getCVSArguments() { 223 StringBuffer toReturn = new StringBuffer (""); if (!isRecursive()) { 225 toReturn.append("-l "); } 227 if (isUseHeadIfNotFound()) { 228 toReturn.append("-f "); } 230 if (getExportByDate() != null) { 231 toReturn.append("-D "); toReturn.append(getExportByDate()); 233 toReturn.append(" "); } 235 if (getExportByRevision() != null) { 236 toReturn.append("-r "); toReturn.append(getExportByRevision()); 238 toReturn.append(" "); } 240 if (isPruneDirectories()) { 241 toReturn.append("-P "); } 243 if (isNotShortenPaths()) { 244 toReturn.append("-N "); } 246 if (isNotRunModuleProgram()) { 247 toReturn.append("-n "); } 249 if (getExportDirectory() != null) { 250 toReturn.append("-d "); toReturn.append(getExportDirectory()); 252 toReturn.append(" "); } 254 if (getKeywordSubstitutionOptions() != null) { 255 toReturn.append("-k"); toReturn.append(getKeywordSubstitutionOptions().toString()); 257 toReturn.append(" "); } 259 return toReturn.toString(); 260 } 261 262 public boolean setCVSCommand(char opt, String optArg) { 263 if (opt == 'k') { 264 setKeywordSubstitutionOptions(KeywordSubstitutionOptions.findKeywordSubstOption(optArg)); 265 } 266 else if (opt == 'r') { 267 setExportByRevision(optArg); 268 } 269 else if (opt == 'f') { 270 setUseHeadIfNotFound(true); 271 } 272 else if (opt == 'D') { 273 setExportByDate(optArg); 274 } 275 else if (opt == 'd') { 276 setExportDirectory(optArg); 277 } 278 else if (opt == 'P') { 279 setPruneDirectories(true); 280 } 281 else if (opt == 'N') { 282 setNotShortenPaths(true); 283 } 284 else if (opt == 'n') { 285 setNotRunModuleProgram(true); 286 } 287 else if (opt == 'l') { 288 setRecursive(false); 289 } 290 else if (opt == 'R') { 291 setRecursive(true); 292 } 293 else { 294 return false; 295 } 296 return true; 297 } 298 299 public void resetCVSCommand() { 300 setModules(null); 301 setKeywordSubstitutionOptions(null); 302 setPruneDirectories(false); 303 setRecursive(true); 304 setExportByDate(null); 305 setExportByRevision(null); 306 setExportDirectory(null); 307 setUseHeadIfNotFound(false); 308 setNotShortenPaths(false); 309 setNotRunModuleProgram(false); 310 } 311 312 public String getOptString() { 313 return "k:r:D:NPlRnd:f"; } 315 316 319 public Builder createBuilder(EventManager eventManager) { 320 return new ExportBuilder(eventManager, this); 321 } 322 323 329 public void messageSent(MessageEvent e) { 330 super.messageSent(e); 331 if (pruneDirectories && 334 e.getMessage().indexOf(": Exporting ") > 0) { File file = new File(getLocalDirectory(), e.getMessage().substring(21)); 336 emptyDirectories.add(file); 337 } 338 } 339 340 344 private boolean pruneEmptyDirectory(File directory) throws IOException { 345 boolean empty = true; 346 347 final File[] contents = directory.listFiles(); 348 349 if (contents != null) { 351 for (int i = 0; i < contents.length; i++) { 352 if (contents[i].isFile()) { 353 empty = false; 354 } 355 else { 356 if (!contents[i].getName().equals("CVS")) { empty = pruneEmptyDirectory(contents[i]); 358 } 359 } 360 361 if (!empty) { 362 break; 363 } 364 } 365 366 if (empty) { 367 final File entriesFile = new File(directory, "CVS/Entries"); if (entriesFile.exists()) { 371 final File adminDir = new File(directory, "CVS"); final File[] adminFiles = adminDir.listFiles(); 373 for (int i = 0; i < adminFiles.length; i++) { 374 adminFiles[i].delete(); 375 } 376 adminDir.delete(); 377 directory.delete(); 378 } 379 } 380 } 381 382 return empty; 383 } 384 385 388 private void pruneEmptyDirectories() throws IOException { 389 final Iterator it = emptyDirectories.iterator(); 390 while (it.hasNext()) { 391 final File dir = (File)it.next(); 392 if (dir.exists()) { 395 pruneEmptyDirectory(dir); 396 } 397 } 398 emptyDirectories.clear(); 399 } 400 401 404 public String getExportByDate() { 405 return this.exportByDate; 406 } 407 408 411 public void setExportByDate(String exportByDate) { 412 this.exportByDate = exportByDate; 413 } 414 415 418 public String getExportByRevision() { 419 return this.exportByRevision; 420 } 421 422 425 public void setExportByRevision(String exportByRevision) { 426 this.exportByRevision = exportByRevision; 427 } 428 429 432 public String getExportDirectory() { 433 return this.exportDirectory; 434 } 435 436 439 public void setExportDirectory(String exportDirectory) { 440 this.exportDirectory = exportDirectory; 441 } 442 443 446 public boolean isUseHeadIfNotFound() { 447 return this.useHeadIfNotFound; 448 } 449 450 453 public void setUseHeadIfNotFound(boolean useHeadIfNotFound) { 454 this.useHeadIfNotFound = useHeadIfNotFound; 455 } 456 457 461 public boolean isNotShortenPaths() { 462 return notShortenPaths; 463 } 464 465 469 public void setNotShortenPaths(boolean notShortenPaths) { 470 this.notShortenPaths = notShortenPaths; 471 } 472 473 477 public boolean isNotRunModuleProgram() { 478 return notRunModuleProgram; 479 } 480 481 485 public void setNotRunModuleProgram(boolean notRunModuleProgram) { 486 this.notRunModuleProgram = notRunModuleProgram; 487 } 488 489 } 490 | Popular Tags |