1 19 package org.netbeans.lib.cvsclient.command.importcmd; 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 import org.netbeans.lib.cvsclient.response.WrapperSendResponse; 30 import org.netbeans.lib.cvsclient.util.*; 31 32 37 public class ImportCommand extends BuildableCommand { 38 private Map wrapperMap = new HashMap(); 39 private String logMessage; 40 private String module; 41 private String releaseTag; 42 private String vendorBranch; 43 private String vendorTag; 44 private String importDirectory; 45 private KeywordSubstitutionOptions keywordSubstitutionOptions; 46 private boolean useFileModifTime; 47 private List ignoreList = new LinkedList(); 48 49 public ImportCommand() { 50 resetCVSCommand(); 51 } 52 53 public void addWrapper(String filenamePattern, KeywordSubstitutionOptions keywordSubstitutionOptions) { 54 if (keywordSubstitutionOptions == null) { 55 throw new IllegalArgumentException ("keywordSubstitutionOptions must not be null"); 56 } 57 58 wrapperMap.put(new SimpleStringPattern(filenamePattern), keywordSubstitutionOptions); 59 } 60 61 public void addWrapper(StringPattern filenamePattern, KeywordSubstitutionOptions keywordSubstitutionOptions) { 62 if (keywordSubstitutionOptions == null) { 63 throw new IllegalArgumentException ("keywordSubstitutionOptions must not be null"); 64 } 65 66 wrapperMap.put(filenamePattern, keywordSubstitutionOptions); 67 } 68 69 75 public void setWrappers(Map wrapperMap) { 76 this.wrapperMap = wrapperMap; 77 } 78 79 83 public Map getWrappers() { 84 return wrapperMap; 85 } 86 87 90 public KeywordSubstitutionOptions getKeywordSubstitutionOptions() { 91 return keywordSubstitutionOptions; 92 } 93 94 97 public void setKeywordSubstitutionOptions(KeywordSubstitutionOptions keywordSubstitutionOptions) { 98 this.keywordSubstitutionOptions = keywordSubstitutionOptions; 99 } 100 101 104 public String getReleaseTag() { 105 return releaseTag; 106 } 107 108 111 public void setReleaseTag(String releaseTag) { 112 this.releaseTag = getTrimmedString(releaseTag); 113 } 114 115 118 public String getLogMessage() { 119 return logMessage; 120 } 121 122 125 public void setLogMessage(String logMessage) { 126 this.logMessage = getTrimmedString(logMessage); 127 } 128 129 133 public String getModule() { 134 return module; 135 } 136 137 141 public void setModule(String module) { 142 this.module = getTrimmedString(module); 143 } 144 145 148 public void setImportDirectory(String directory) { 149 importDirectory = directory; 150 } 151 152 public String getImportDirectory() { 153 return importDirectory; 154 } 155 156 159 public String getVendorBranch() { 160 return vendorBranch; 161 } 162 163 167 private String getVendorBranchNotNull() { 168 if (vendorBranch == null) { 169 return "1.1.1"; } 171 172 return vendorBranch; 173 } 174 175 179 public void setVendorBranch(String vendorBranch) { 180 this.vendorBranch = getTrimmedString(vendorBranch); 181 } 182 183 186 public String getVendorTag() { 187 return vendorTag; 188 } 189 190 193 public void setVendorTag(String vendorTag) { 194 this.vendorTag = getTrimmedString(vendorTag); 195 } 196 197 200 public boolean isUseFileModifTime() { 201 return useFileModifTime; 202 } 203 204 207 public void setUseFileModifTime(boolean useFileModifTime) { 208 this.useFileModifTime = useFileModifTime; 209 } 210 211 214 public List getIgnoreFiles() { 215 return Collections.unmodifiableList(ignoreList); 216 } 217 218 221 public void addIgnoredFile(String ignoredFileName) { 222 ignoreList.add(ignoredFileName); 223 } 224 225 228 public void execute(ClientServices client, EventManager eventManager) 229 throws CommandException, AuthenticationException { 230 if (getLogMessage() == null) { 232 String localizedMsg = CommandException.getLocalMessage("ImportCommand.messageEmpty"); throw new CommandException("message may not be null nor empty", localizedMsg); 235 } 236 if (getModule() == null) { 237 String localizedMsg = CommandException.getLocalMessage("ImportCommand.moduleEmpty"); throw new CommandException("module may not be null nor empty", localizedMsg); 240 } 241 if (getReleaseTag() == null) { 242 String localizedMsg = CommandException.getLocalMessage("ImportCommand.releaseTagEmpty"); throw new CommandException("release tag may not be null nor empty", localizedMsg); 245 } 246 if (getVendorTag() == null) { 247 String localizedMsg = CommandException.getLocalMessage("ImportCommand.vendorTagEmpty"); throw new CommandException("vendor tag may not be null nor empty", localizedMsg); 250 } 251 252 client.ensureConnection(); 253 254 Map allWrappersMap = new HashMap(client.getWrappersMap()); 256 allWrappersMap.putAll(getWrappers()); 257 setWrappers(allWrappersMap); 258 259 super.execute(client, eventManager); 261 assert getLocalDirectory() != null : "local directory may not be null"; 262 263 List requestList = new ArrayList(); 264 265 try { 266 requestList.add(new ArgumentRequest("-b")); requestList.add(new ArgumentRequest(getVendorBranchNotNull())); 269 270 if (getKeywordSubstitutionOptions() != null) { 271 requestList.add(new ArgumentRequest("-k")); requestList.add(new ArgumentRequest(getKeywordSubstitutionOptions().toString())); 273 } 274 275 addMessageRequests(requestList, getLogMessage()); 276 277 addWrapperRequests(requestList, this.wrapperMap); 278 279 if (isUseFileModifTime()) { 280 requestList.add(new ArgumentRequest("-d")); } 282 283 for (int i = 0; i < ignoreList.size(); i++) { 284 requestList.add(new ArgumentRequest("-I")); requestList.add(new ArgumentRequest((String ) ignoreList.get(i))); 286 } 287 288 requestList.add(new ArgumentRequest(getModule())); 289 requestList.add(new ArgumentRequest(getVendorTag())); 290 requestList.add(new ArgumentRequest(getReleaseTag())); 291 292 addFileRequests(new File(getLocalDirectory()), 293 requestList, client); 294 295 requestList.add(new DirectoryRequest(".", getRepositoryRoot(client))); 297 requestList.add(CommandRequest.IMPORT); 298 299 client.processRequests(requestList); 301 } 302 catch (CommandException ex) { 303 throw ex; 304 } 305 catch (EOFException ex) { 306 String localizedMsg = CommandException.getLocalMessage("CommandException.EndOfFile", null); throw new CommandException(ex, localizedMsg); 308 } 309 catch (Exception ex) { 310 throw new CommandException(ex, ex.getLocalizedMessage()); 311 } 312 } 313 314 public String getCVSCommand() { 315 StringBuffer toReturn = new StringBuffer ("import "); toReturn.append(getCVSArguments()); 317 if (getModule() != null) { 318 toReturn.append(" "); toReturn.append(getModule()); 320 } 321 else { 322 String localizedMsg = CommandException.getLocalMessage("ImportCommand.moduleEmpty.text"); toReturn.append(" "); toReturn.append(localizedMsg); 325 } 326 if (getVendorTag() != null) { 327 toReturn.append(" "); toReturn.append(getVendorTag()); 329 } 330 else { 331 String localizedMsg = CommandException.getLocalMessage("ImportCommand.vendorTagEmpty.text"); toReturn.append(" "); toReturn.append(localizedMsg); 334 } 335 if (getReleaseTag() != null) { 336 toReturn.append(" "); toReturn.append(getReleaseTag()); 338 } 339 else { 340 String localizedMsg = CommandException.getLocalMessage("ImportCommand.releaseTagEmpty.text"); toReturn.append(" "); toReturn.append(localizedMsg); 343 } 344 return toReturn.toString(); 345 } 346 347 public String getCVSArguments() { 348 StringBuffer toReturn = new StringBuffer (""); if (getLogMessage() != null) { 350 toReturn.append("-m \""); toReturn.append(getLogMessage()); 352 toReturn.append("\" "); } 354 if (getKeywordSubstitutionOptions() != null) { 355 toReturn.append("-k"); toReturn.append(getKeywordSubstitutionOptions().toString()); 357 toReturn.append(" "); } 359 if (getVendorBranch() != null) { 360 toReturn.append("-b "); toReturn.append(getVendorBranch()); 362 toReturn.append(" "); } 364 if (isUseFileModifTime()) { 365 toReturn.append("-d "); } 367 if (wrapperMap.size() > 0) { 368 Iterator it = wrapperMap.keySet().iterator(); 369 while (it.hasNext()) { 370 StringPattern pattern = (StringPattern)it.next(); 371 KeywordSubstitutionOptions keywordSubstitutionOptions = (KeywordSubstitutionOptions)wrapperMap.get(pattern); 372 toReturn.append("-W "); toReturn.append(pattern.toString()); 374 toReturn.append(" -k '"); toReturn.append(keywordSubstitutionOptions.toString()); 376 toReturn.append("' "); } 378 } 379 for (Iterator it = ignoreList.iterator(); it.hasNext(); ) { 380 toReturn.append("-I "); toReturn.append((String ) it.next()); 382 toReturn.append(" "); } 384 return toReturn.toString(); 385 } 386 387 public boolean setCVSCommand(char opt, String optArg) { 388 if (opt == 'b') { 389 setVendorBranch(optArg); 390 } 391 else if (opt == 'm') { 392 setLogMessage(optArg); 393 } 394 else if (opt == 'k') { 395 setKeywordSubstitutionOptions(KeywordSubstitutionOptions.findKeywordSubstOption(optArg)); 396 } 397 else if (opt == 'W') { 398 Map wrappers = WrapperSendResponse.parseWrappers(optArg); 399 for (Iterator it = wrappers.keySet().iterator(); it.hasNext(); ) { 400 StringPattern pattern = (StringPattern) it.next(); 401 KeywordSubstitutionOptions keywordOption = (KeywordSubstitutionOptions) wrappers.get(pattern); 402 addWrapper(pattern, keywordOption); 403 } 404 } 405 else if (opt == 'd') { 406 setUseFileModifTime(true); 407 } 408 else if (opt == 'I') { 409 addIgnoredFile(optArg); 410 } 411 else { 412 return false; 413 } 414 return true; 415 } 416 417 public void resetCVSCommand() { 418 setLogMessage(null); 419 setModule(null); 420 setReleaseTag(null); 421 setVendorTag(null); 422 setVendorBranch(null); 423 setUseFileModifTime(false); 424 ignoreList.clear(); 425 wrapperMap.clear(); 426 } 427 428 public String getOptString() { 429 return "m:W:b:k:dI:"; } 431 432 435 private void addMessageRequests(List requestList, String logMessage) { 436 requestList.add(new ArgumentRequest("-m")); 438 StringTokenizer token = new StringTokenizer(logMessage, "\n", false); boolean first = true; 440 while (token.hasMoreTokens()) { 441 if (first) { 442 requestList.add(new ArgumentRequest(token.nextToken())); 443 first = false; 444 } 445 else { 446 requestList.add(new ArgumentxRequest(token.nextToken())); 447 } 448 } 449 } 450 451 454 private void addWrapperRequests(List requestList, Map wrapperMap) { 455 for (Iterator it = wrapperMap.keySet().iterator(); it.hasNext();) { 456 StringPattern pattern = (StringPattern) it.next(); 457 KeywordSubstitutionOptions keywordSubstitutionOptions = (KeywordSubstitutionOptions)wrapperMap.get(pattern); 458 459 StringBuffer buffer = new StringBuffer (); 460 buffer.append(pattern.toString()); 461 buffer.append(" -k '"); buffer.append(keywordSubstitutionOptions.toString()); 463 buffer.append("'"); 465 requestList.add(new ArgumentRequest("-W")); requestList.add(new ArgumentRequest(buffer.toString())); 467 } 468 } 469 470 474 private void addFileRequests(File directory, 475 List requestList, 476 ClientServices clientServices) 477 throws IOException { 478 String relativePath = getRelativeToLocalPathInUnixStyle(directory); 479 String repository = getRepositoryRoot(clientServices); 480 if (!relativePath.equals(".")) { repository += '/' + relativePath; 482 } 483 requestList.add(new DirectoryRequest(relativePath, repository)); 484 485 File[] files = directory.listFiles(); 486 if (files == null) { 487 return; 488 } 489 490 List subdirectories = null; 491 492 for (int i = 0; i < files.length; i++) { 493 File file = files[i]; 494 String filename = file.getName(); 495 496 if (clientServices.shouldBeIgnored(directory, filename)) { 497 continue; 498 } 499 500 if (file.isDirectory()) { 501 if (subdirectories == null) { 502 subdirectories = new LinkedList(); 503 } 504 subdirectories.add(file); 505 } 506 else { 507 boolean isBinary = isBinary(filename); 508 requestList.add(new ModifiedRequest(file, isBinary)); 509 } 510 } 511 512 if (subdirectories != null) { 513 for (Iterator it = subdirectories.iterator(); it.hasNext();) { 514 File subdirectory = (File)it.next(); 515 addFileRequests(subdirectory, requestList, clientServices); 516 } 517 } 518 } 519 520 525 private String getRepositoryRoot(ClientServices clientServices) { 526 String repository = clientServices.getRepository() + '/' + getModule(); 527 return repository; 528 } 529 530 536 private boolean isBinary(String filename) { 537 KeywordSubstitutionOptions keywordSubstitutionOptions = getKeywordSubstitutionOptions(); 538 539 for (Iterator it = wrapperMap.keySet().iterator(); it.hasNext();) { 540 StringPattern pattern = (StringPattern)it.next(); 541 if (pattern.doesMatch(filename)) { 542 keywordSubstitutionOptions = (KeywordSubstitutionOptions)wrapperMap.get(pattern); 543 break; 544 } 545 } 546 547 return keywordSubstitutionOptions == KeywordSubstitutionOptions.BINARY; 548 } 549 550 553 public Builder createBuilder(EventManager eventManager) { 554 return new ImportBuilder(eventManager, this); 555 } 556 } 557 | Popular Tags |