1 22 package org.netbeans.lib.cvsclient.command.add; 23 24 import java.io.*; 25 import java.util.*; 26 27 import org.netbeans.lib.cvsclient.*; 28 import org.netbeans.lib.cvsclient.util.SimpleStringPattern; 29 import org.netbeans.lib.cvsclient.admin.*; 30 import org.netbeans.lib.cvsclient.command.*; 31 import org.netbeans.lib.cvsclient.connection.*; 32 import org.netbeans.lib.cvsclient.event.*; 33 import org.netbeans.lib.cvsclient.request.*; 34 35 39 public class AddCommand extends BuildableCommand { 40 44 private static final String DIR_ADDED = " added to the repository"; private static final String DIRECTORY = "Directory "; 47 50 private List requests; 51 52 56 private final List argumentRequests = new LinkedList(); 57 58 61 64 private final List newDirList = new LinkedList(); 65 66 69 private ClientServices clientServices; 70 71 74 private File[] files; 75 76 79 private String message; 80 81 84 private KeywordSubstitutionOptions keywordSubst; 85 86 87 88 93 private Map wrapperMap; 94 95 99 private HashMap dir2WrapperMap = new HashMap(16); 100 101 private static final Map EMPTYWRAPPER = new HashMap(1); 102 105 public AddCommand() { 106 resetCVSCommand(); 107 } 108 109 116 public void setFiles(File[] files) { 117 this.files = files; 118 if (files == null) { 119 return; 120 } 121 122 this.files = new File[files.length]; 124 int dirCount = 0; 125 int fileCount = 0; 126 int totalCount = files.length; 127 for (int index = 0; index < totalCount; index++) { 128 File currentFile = files[index]; 129 if (currentFile.isDirectory()) { 130 this.files[dirCount] = currentFile; 131 dirCount++; 132 } 133 else { 134 this.files[totalCount - (1 + fileCount)] = currentFile; 135 fileCount++; 136 } 137 } 138 } 139 140 145 public File[] getFiles() { 146 return files; 147 } 148 149 152 public File getFileEndingWith(String ending) { 153 String locEnding = ending.replace('\\', '/'); 154 String localDir = getLocalDirectory().replace('\\','/'); 155 int index = 0; 156 for (index = 0; index < files.length; index++) { 157 String path = files[index].getAbsolutePath(); 158 String parentPath = files[index].getParentFile().getAbsolutePath().replace('\\', '/'); 159 path = path.replace('\\', '/'); 160 if ((path.endsWith(locEnding) && locEnding.indexOf('/') >= 0) || 161 (files[index].getName().equals(locEnding) && parentPath.equals(localDir))) { 162 return files[index]; 163 } 164 } 165 return null; 166 } 167 168 172 public String getMessage() { 173 return message; 174 } 175 176 180 public void setMessage(String message) { 181 this.message = message; 182 } 183 184 188 public KeywordSubstitutionOptions getKeywordSubst() { 189 return keywordSubst; 190 } 191 192 196 public void setKeywordSubst(KeywordSubstitutionOptions keywordSubst) { 197 this.keywordSubst = keywordSubst; 198 } 199 200 203 protected void addRequests(File file) 204 throws IOException, CommandException { 205 if (file.isDirectory()) { 206 addRequestsForDirectory(file, false); 207 } 208 else { 209 addRequestsForFile(file); 210 } 211 } 212 213 220 private void addRequestsForDirectory(File directory, boolean recursion) 221 throws IOException { 222 223 File parentDirectory = directory.getParentFile(); 224 String dir = recursion 225 ? getRelativeToLocalPathInUnixStyle(directory) 226 : getRelativeToLocalPathInUnixStyle(parentDirectory); 227 228 String partPath; 229 if (dir.equals(".")) { partPath = directory.getName(); 231 } 232 else { 233 partPath = dir + "/" + directory.getName(); addRequestsForDirectory(parentDirectory, true); 241 } 242 243 if (recursion) { 244 partPath = dir; 245 } 246 247 String repository; 252 String tag; 253 254 if (recursion) { 255 repository = clientServices.getRepositoryForDirectory( 256 directory.getAbsolutePath()); 257 tag = clientServices.getStickyTagForDirectory(directory); 258 } 259 else { 260 repository = clientServices.getRepositoryForDirectory( 261 parentDirectory.getAbsolutePath()); 262 if (repository.endsWith(".")) { 263 repository = repository.substring(0, repository.length() - 1) + directory.getName(); 264 } else { 265 repository = repository + "/" + directory.getName(); } 267 tag = clientServices.getStickyTagForDirectory(parentDirectory); 268 } 269 270 requests.add(new DirectoryRequest(partPath, repository)); 271 if (tag != null) { 272 requests.add(new StickyRequest(tag)); 273 } 274 275 if (!recursion) { 276 argumentRequests.add(new ArgumentRequest(partPath)); 277 280 newDirList.add(new Paths(partPath, repository)); 281 } 282 } 284 285 288 protected void addRequestsForFile(File file) 289 throws IOException, CommandException { 290 File directory = file.getParentFile(); 291 String dir = getRelativeToLocalPathInUnixStyle(directory); 292 293 String repository = clientServices.getRepositoryForDirectory( 294 directory.getAbsolutePath()); 295 requests.add(new DirectoryRequest(dir, repository)); 296 String tag = clientServices.getStickyTagForDirectory(directory); 297 if (tag != null) { 298 requests.add(new StickyRequest(tag)); 299 } 300 301 Entry entry = clientServices.getEntry(file); 302 303 if (entry != null) { 304 requests.add(new EntryRequest(entry)); 305 } 306 else { 307 308 Map directoryLevelWrapper = (Map) dir2WrapperMap.get(dir); 309 if (directoryLevelWrapper == null) { 310 311 314 File wrapperFile = new File(directory, ".cvswrappers"); if (wrapperFile.exists()) { 316 directoryLevelWrapper = new HashMap(5); 317 WrapperUtils.readWrappersFromFile(wrapperFile, directoryLevelWrapper); 318 } 319 else { 320 directoryLevelWrapper = EMPTYWRAPPER; 321 } 322 323 dir2WrapperMap.put(dir, directoryLevelWrapper); 325 } 326 327 boolean isBinary = isBinary(clientServices, file.getName(), directoryLevelWrapper); 328 329 if (isBinary) { 330 requests.add(new KoptRequest("-kb")); } 332 requests.add(new IsModifiedRequest(file)); 333 } 334 335 if (dir.equals(".")) { argumentRequests.add(new ArgumentRequest(file.getName())); 337 } 338 else { 339 argumentRequests.add(new ArgumentRequest(dir + "/" + file.getName())); } 341 } 342 343 344 350 private boolean isBinary(ClientServices client, String filename, Map directoryLevelWrappers) throws CommandException { 351 KeywordSubstitutionOptions keywordSubstitutionOptions = getKeywordSubst(); 352 353 if (keywordSubstitutionOptions == KeywordSubstitutionOptions.BINARY) { 354 return true; 355 } 356 357 362 boolean wrapperFound = false; 363 364 if (wrapperMap == null) { 365 wrapperMap = WrapperUtils.mergeWrapperMap(client); 367 } 368 369 370 for (Iterator it = wrapperMap.keySet().iterator(); it.hasNext();) { 371 SimpleStringPattern pattern = (SimpleStringPattern)it.next(); 372 if (pattern.doesMatch(filename)) { 373 keywordSubstitutionOptions = (KeywordSubstitutionOptions)wrapperMap.get(pattern); 374 wrapperFound = true; 375 break; 376 } 377 } 378 379 if (!wrapperFound && (directoryLevelWrappers != null) && (directoryLevelWrappers!=EMPTYWRAPPER)) { 382 for (Iterator it = directoryLevelWrappers.keySet().iterator(); it.hasNext();) { 383 SimpleStringPattern pattern = (SimpleStringPattern)it.next(); 384 if (pattern.doesMatch(filename)) { 385 keywordSubstitutionOptions = (KeywordSubstitutionOptions)directoryLevelWrappers.get(pattern); 386 wrapperFound = true; 387 break; 388 } 389 } 390 } 391 392 return keywordSubstitutionOptions == KeywordSubstitutionOptions.BINARY; 393 } 394 395 401 public void execute(ClientServices client, EventManager em) 402 throws CommandException, AuthenticationException { 403 if (files == null || files.length == 0) { 404 throw new CommandException("No files have been specified for " + "adding.", CommandException.getLocalMessage("AddCommand.noFilesSpecified", null)); } 407 408 client.ensureConnection(); 409 410 clientServices = client; 411 setLocalDirectory(client.getLocalPath()); 412 413 String directory = client.getLocalPath(); 414 File cvsfolder = new File(directory, "CVS"); 415 if (!cvsfolder.isDirectory()) { 416 MessageEvent event = new MessageEvent(this, "cvs [add aborted]: there is no version here; do 'cvs checkout' first", true); 418 messageSent(event); 419 em.fireCVSEvent(event); 420 return ; 421 } 422 425 newDirList.clear(); 426 427 super.execute(client, em); 428 429 requests = new LinkedList(); 430 431 if (client.isFirstCommand()) { 432 requests.add(new RootRequest(client.getRepository())); 433 } 434 435 String message = getMessage(); 437 if (message != null) { 438 message = message.trim(); 439 } 440 if (message != null 441 && message.length() > 0) { 442 addMessageRequest(message); 443 } 444 445 if (getKeywordSubst() != null && !getKeywordSubst().equals("")) { requests.add(new ArgumentRequest("-k" + getKeywordSubst())); } 448 449 try { 450 for (int i = 0; i < files.length; i++) { 452 addRequests(files[i]); 453 } 454 455 requests.add(new DirectoryRequest(".", client.getRepositoryForDirectory(getLocalDirectory()))); 459 460 requests.addAll(argumentRequests); 461 argumentRequests.clear(); requests.add(CommandRequest.ADD); 463 client.processRequests(requests); 464 } 465 catch (CommandException ex) { 466 throw ex; 467 } 468 catch (Exception ex) { 469 throw new CommandException(ex, ex.getLocalizedMessage()); 470 } 471 finally { 472 requests.clear(); 473 } 474 } 475 476 private void addMessageRequest(String message) { 477 requests.add(new ArgumentRequest("-m")); StringTokenizer token = new StringTokenizer(message, "\n", false); boolean first = true; 480 while (token.hasMoreTokens()) { 481 if (first) { 482 requests.add(new ArgumentRequest(token.nextToken())); 483 first = false; 484 } 485 else { 486 requests.add(new ArgumentxRequest(token.nextToken())); 487 } 488 } 489 } 490 491 497 public String getCVSCommand() { 498 StringBuffer toReturn = new StringBuffer ("add "); toReturn.append(getCVSArguments()); 500 File[] files = getFiles(); 501 if (files != null) { 502 for (int index = 0; index < files.length; index++) { 503 toReturn.append(files[index].getName()); 504 toReturn.append(' '); 505 } 506 } 507 return toReturn.toString(); 508 } 509 510 515 public Builder createBuilder(EventManager eventManager) { 516 return new AddBuilder(eventManager, this); 517 } 518 519 524 public boolean setCVSCommand(char opt, String optArg) { 525 if (opt == 'm') { 526 setMessage(optArg); 527 } 528 else if (opt == 'k') { 529 KeywordSubstitutionOptions keywordSubst = 530 KeywordSubstitutionOptions.findKeywordSubstOption(optArg); 531 setKeywordSubst(keywordSubst); 532 } 533 else { 534 return false; 535 } 536 return true; 537 } 538 539 542 public String getOptString() { 543 return "m:k:"; } 545 546 550 public void messageSent(MessageEvent e) { 551 String str = e.getMessage(); 552 if (str.endsWith(DIR_ADDED)) { 553 str = str.substring(DIRECTORY.length(), str.indexOf(DIR_ADDED)).trim(); 554 createCvsFiles(str); 555 } 556 super.messageSent(e); 557 } 558 559 563 private void createCvsFiles(String newDirInRepository) { 564 String repository = newDirInRepository; 565 String dirName = repository; 566 if (dirName.lastIndexOf('/') >= 0) { 567 dirName = dirName.substring(dirName.lastIndexOf('/') + 1, 568 dirName.length()); 569 } 570 571 if (newDirList.size() == 0) { 572 System.err.println("JavaCVS: Bug in AddCommand|createCvsFiles"); System.err.println(" newDirInRepository = " + newDirInRepository); return; 575 } 576 577 Paths paths = null; 578 for (Iterator i = newDirList.iterator(); i.hasNext();) { 579 paths = (Paths) i.next(); 580 if (paths.getRepositoryPath().equals(newDirInRepository)) { 581 i.remove(); 582 break; 583 } 584 } 585 586 String local = paths.getPartPath(); 587 String part = paths.getRepositoryPath(); 588 repository = paths.getRepositoryPath(); 589 590 String tempDirName = part; 591 if (part.lastIndexOf('/') >= 0) { 592 tempDirName = part.substring(part.lastIndexOf('/') + 1, 593 part.length()); 594 } 595 596 if (!tempDirName.equalsIgnoreCase(dirName)) { 597 System.err.println("JavaCVS: Bug in AddCommand|createCvsFiles"); System.err.println(" newDirInRepository = " + newDirInRepository); System.err.println(" tempDirName = " + tempDirName); System.err.println(" dirName = " + dirName); return; 602 } 603 604 try { 605 if (repository.startsWith(".")) { repository = repository.substring(1); 607 } 608 clientServices.updateAdminData(local, repository, null); 609 createCvsTagFile(local, repository); 610 } 611 catch (IOException ex) { 612 System.err.println("TODO: couldn't create/update Cvs admin files"); } 614 643 } 644 645 private void createCvsTagFile(String local, String repository) throws IOException { 646 File current = new File(getLocalDirectory(), local); 647 File parent = current.getParentFile(); 648 String tag = clientServices.getStickyTagForDirectory(parent); 649 if (tag != null) { 650 File tagFile = new File(current, "CVS/Tag"); tagFile.createNewFile(); 652 PrintWriter w = new PrintWriter(new BufferedWriter(new FileWriter(tagFile))); 653 w.println(tag); 654 w.close(); 655 } 656 } 657 658 662 public void resetCVSCommand() { 663 setMessage(null); 664 setKeywordSubst(null); 665 } 666 667 671 public String getCVSArguments() { 672 StringBuffer toReturn = new StringBuffer (); 673 if (getMessage() != null) { 674 toReturn.append("-m \""); toReturn.append(getMessage()); 676 toReturn.append("\" "); } 678 if (getKeywordSubst() != null) { 679 toReturn.append("-k"); toReturn.append(getKeywordSubst().toString()); 681 toReturn.append(" "); } 683 return toReturn.toString(); 684 } 685 686 private static class Paths { 687 private final String partPath; 688 private final String repositoryPath; 689 690 public Paths(String partPath, String repositoryPath) { 691 this.partPath = partPath; 692 this.repositoryPath = repositoryPath; 693 } 694 695 public String getPartPath() { 696 return partPath; 697 } 698 699 public String getRepositoryPath() { 700 return repositoryPath; 701 } 702 } 703 } 704 | Popular Tags |