1 21 22 package org.netbeans.lib.cvsclient; 23 24 import java.io.*; 25 import java.util.*; 26 import org.netbeans.lib.cvsclient.connection.Connection; 27 import org.netbeans.lib.cvsclient.connection.ConnectionFactory; 28 29 98 public class CVSRoot { 99 100 101 public static final String METHOD_LOCAL = "local"; 103 public static final String METHOD_FORK = "fork"; 105 public static final String METHOD_SERVER = "server"; 107 public static final String METHOD_PSERVER = "pserver"; 109 public static final String METHOD_EXT = "ext"; 111 private String method; 114 private String username; 116 private String password; 118 private String hostname; 120 private int port; 122 private String repository; 124 125 130 public static CVSRoot parse(String cvsroot) throws IllegalArgumentException { 131 return new CVSRoot(cvsroot); 132 } 133 134 138 public static CVSRoot parse(Properties props) throws IllegalArgumentException { 139 return new CVSRoot(props); 140 } 141 142 146 protected CVSRoot(Properties props) throws IllegalArgumentException { 147 148 String mtd = props.getProperty("method"); 149 if (mtd != null) { 150 this.method = mtd.intern(); 151 } 152 153 this.hostname = props.getProperty("hostname"); 155 156 if (this.hostname.length() == 0) 157 this.hostname = null; 158 160 if (this.hostname != null) { 161 162 this.username = props.getProperty("username"); 164 this.password = props.getProperty("password"); 165 166 try { 169 int p = Integer.parseInt(props.getProperty("port")); 170 if (p > 0) 171 this.port = p; 172 else 173 throw new IllegalArgumentException ("The port is not a positive number."); 174 } 175 catch (NumberFormatException e) { 176 throw new IllegalArgumentException ("The port is not a number: '"+props.getProperty("port")+"'."); 177 } 178 } 179 180 String r = props.getProperty("repository"); 182 if (r == null) 183 throw new IllegalArgumentException ("Repository is obligatory."); 184 else 185 this.repository = r; 186 } 187 188 207 protected CVSRoot(String cvsroot) throws IllegalArgumentException { 208 209 int colonPosition = 0; 210 boolean localFormat; 211 if (cvsroot.startsWith(":") == false) { 212 213 215 localFormat = cvsroot.startsWith("/"); 216 if (localFormat == false) { 217 if (cvsroot.indexOf(':') == 1 && cvsroot.indexOf('\\') == 2) { 218 method = METHOD_LOCAL; 220 repository = cvsroot; 221 normalize(); 222 return; 223 } 224 colonPosition = cvsroot.indexOf(':'); 225 if (colonPosition < 0) { 226 int slash = cvsroot.indexOf('/'); 228 if (slash < 0) { 229 throw new IllegalArgumentException ("CVSROOT must be an absolute pathname."); 230 } 231 method = METHOD_SERVER; 232 } else { 233 method = METHOD_EXT; 234 } 235 colonPosition = 0; 236 } else { 237 method = METHOD_LOCAL; 238 } 239 } else { 240 242 colonPosition = cvsroot.indexOf(':', 1); 243 if (colonPosition < 0) 244 throw new IllegalArgumentException ("The connection method does not end with ':'."); 245 int methodNameEnd = colonPosition; 246 int semicolonPosition = cvsroot.indexOf(";", 1); 247 248 if (semicolonPosition != -1 && semicolonPosition < colonPosition) { 249 methodNameEnd = semicolonPosition; 251 String options = cvsroot.substring(semicolonPosition +1, colonPosition); 252 StringTokenizer tokenizer = new StringTokenizer(options, "=;"); 253 while (tokenizer.hasMoreTokens()) { 254 String option = tokenizer.nextToken(); 255 if (tokenizer.hasMoreTokens() == false) { 256 throw new IllegalArgumentException ("Undefined " + option + " option value."); 257 } 258 String value = tokenizer.nextToken(); 259 if ("hostname".equals(option)) { hostname = value; 261 } else if ("username".equals(option)) { username = value; 263 } else if ("password".equals(option)) { password = value; 265 } if ("port".equals(option)) { try { 267 port = Integer.parseInt(value, 10); 268 } catch (NumberFormatException ex) { 269 throw new IllegalArgumentException ("Port option must be number."); 270 } 271 } 272 } 273 } 274 275 this.method = cvsroot.substring(1, methodNameEnd).intern(); 276 277 if ("extssh".equals(method)) { method = METHOD_EXT; 280 } 281 if ("ssh".equals(method)) { method = METHOD_EXT; 284 } 285 colonPosition++; 286 localFormat = isLocalMethod(this.method); 288 } 289 290 if (localFormat) { 291 this.repository = cvsroot.substring(colonPosition); 293 } else { 294 315 316 int startSearch = cvsroot.indexOf('@', colonPosition); 317 if (startSearch < 0) startSearch = colonPosition; 318 String userPasswdHost; 319 int pathBegin = -1; 320 int hostColon = cvsroot.indexOf(':', startSearch); 321 if (hostColon == -1) { 322 pathBegin = cvsroot.indexOf('/', startSearch); 323 if (pathBegin < 0) { 324 throw new IllegalArgumentException ("cvsroot " + cvsroot + " is malformed, host name is missing."); 325 } else { 326 userPasswdHost = cvsroot.substring(colonPosition, pathBegin); 327 } 328 } else { 329 userPasswdHost = cvsroot.substring(colonPosition, hostColon); 330 } 331 332 int at = userPasswdHost.indexOf('@'); 333 if (at == -1) { 334 if (userPasswdHost.length() > 0) { 336 this.hostname = userPasswdHost; 337 } 338 } 339 else { 340 String up = userPasswdHost.substring(0, at); 343 if (up.length() > 0) { 344 int upDivider = up.indexOf(':'); 345 if (upDivider != -1) { 346 this.username = up.substring(0, upDivider); 347 this.password = up.substring(upDivider+1); 348 } 349 else { 350 this.username = up; 351 } 352 } 353 354 this.hostname = userPasswdHost.substring(at+1); 356 } 357 358 if (hostname == null || hostname.length() == 0) { 359 throw new IllegalArgumentException ("Didn't specify hostname in CVSROOT '"+cvsroot+"'."); 360 } 361 362 366 if (hostColon > 0) { 367 String pr = cvsroot.substring(hostColon+1); 368 int index = 0; 369 int port = 0; 370 char c; 371 while (pr.length() > index && Character.isDigit(c = pr.charAt(index))) { 372 int d = Character.digit(c, 10); 373 port = port*10 + d; 374 index++; 375 } 376 this.port = port; 377 if (index > 0) pr = pr.substring(index); 378 if (pr.startsWith(":")) { pr = pr.substring(1); 380 } 381 this.repository = pr; 382 } else { 383 this.port = 0; 384 this.repository = cvsroot.substring(pathBegin); 385 } 386 } 387 normalize(); 388 } 389 390 393 private void normalize() { 394 int i = repository.length(); 395 int n = i - 1; 396 while (i > 0 && repository.charAt(--i) == '/'); 397 if (i < n) { 398 repository = repository.substring(0, i + 1); 399 } 400 } 401 402 407 public boolean isLocal() { 408 return hostname == null; 409 } 410 411 425 426 public String toString() { 427 428 if (this.hostname == null) { 429 if (this.method == null) 430 return this.repository; 431 432 return ":" + this.method + ":" + this.repository; 433 } else { 434 435 StringBuffer buf = new StringBuffer (); 436 437 if (this.method != null) { 438 buf.append(':'); 439 buf.append(this.method); 440 buf.append(':'); 441 } 442 443 if (this.username != null) { 445 buf.append(this.username); 446 buf.append('@'); 447 } 448 449 buf.append(this.hostname); 451 buf.append(':'); 452 453 if (this.port > 0) 455 buf.append(this.port); 456 457 buf.append(this.repository); 459 460 return buf.toString(); 461 } 462 } 463 464 477 public int getCompatibilityLevel(CVSRoot compared) { 478 479 if (equals(compared)) 480 return 0; 481 482 483 boolean sameRepository = isSameRepository(compared); 484 boolean sameHost = isSameHost(compared); 485 boolean sameMethod = isSameMethod(compared); 486 boolean samePort = isSamePort(compared); 487 boolean sameUser = isSameUser(compared); 488 489 if (sameRepository && sameHost && sameMethod && samePort && sameUser) 490 return 1; 491 else if (sameRepository && sameHost && sameMethod) 492 return 2; 493 else if (sameRepository && sameHost) 494 return 3; 495 else 496 return -1; 497 } 498 499 private boolean isSameRepository(CVSRoot compared) { 500 if (this.repository.equals(compared.repository)) { 501 return true; 502 } 503 try { 504 if ( 505 (new File(this.repository)).getCanonicalFile().equals( 506 new File(compared.repository).getCanonicalFile() 507 ) 508 ) 509 return true; 510 else 511 return false; 512 } 513 catch (IOException ioe) { 514 return false; 516 } 517 } 518 519 private boolean isSameHost(CVSRoot compared) { 520 String comparedHostName = compared.getHostName(); 521 if (this.hostname == comparedHostName) { 522 return true; 523 } 524 if (this.hostname != null) { 525 return this.hostname.equalsIgnoreCase(comparedHostName); 526 } else { 527 return false; 528 } 529 } 530 531 private boolean isSameMethod(CVSRoot compared) { 532 if (this.method == null) 533 if (compared.getMethod() == null) 534 return true; 535 else 536 return false; 537 else if (this.method.equals(compared.getMethod())) 538 return true; 539 else 540 return false; 541 } 542 543 private boolean isSamePort(CVSRoot compared) { 544 if (this.isLocal() == compared.isLocal()) 545 if (this.isLocal()) 546 return true; 547 else if (this.port == compared.getPort()) 548 return true; 549 else { 550 try { 551 Connection c1 = ConnectionFactory.getConnection(this); 552 Connection c2 = ConnectionFactory.getConnection(compared); 553 return c1.getPort() == c2.getPort(); 556 } catch (IllegalArgumentException iaex) { 557 return false; 558 } 559 } 560 else 561 return false; 562 } 563 564 private boolean isSameUser(CVSRoot compared) { 565 String user = compared.getUserName(); 566 if (user == getUserName()) return true; 567 if (user != null) { 568 return user.equals(getUserName()); 569 } 570 return false; 571 } 572 573 577 public boolean equals(Object o) { 578 if (!(o instanceof CVSRoot)) 580 return false; 581 582 CVSRoot compared = (CVSRoot) o; 583 584 if (toString().equals(compared.toString())) 585 return true; 586 587 return false; 588 } 589 590 public int hashCode() { 591 return toString().hashCode(); 592 } 593 594 602 603 607 public String getMethod() { 608 return method; 609 } 610 611 620 protected void setMethod(String method) { 621 if (method != null) { 622 this.method = method.intern(); 623 } else { 624 method = null; 625 } 626 if (isLocalMethod(method)) { 627 this.username = null; 628 this.password = null; 629 this.hostname = null; 630 this.port = 0; 631 } 632 else { 633 if (this.hostname == null) 634 throw new IllegalArgumentException ("Hostname must not be null when setting a remote method."); 635 } 636 } 637 638 private boolean isLocalMethod(String method) { 640 return METHOD_LOCAL == method || METHOD_FORK == method; 641 } 642 643 647 public String getUserName() { 648 return username; 649 } 650 654 protected void setUserName(String username) { 655 this.username = username; 656 } 657 658 662 public String getPassword() { 663 return this.password; 664 } 665 669 protected void setPassword(String password) { 670 this.password = password; 671 } 672 673 678 public String getHostName() { 679 return this.hostname; 680 } 681 686 protected void setHostName(String hostname) { 687 this.hostname = hostname; 688 } 689 690 694 public int getPort() { 695 return this.port; 696 } 697 701 public void setPort(int port) { 702 this.port = port; 703 } 704 705 709 public String getRepository() { 710 return repository; 711 } 712 716 protected void setRepository(String repository) { 717 if (repository == null) { 718 throw new IllegalArgumentException ("The repository must not be null."); 719 } 720 this.repository = repository; 721 } 722 723 } 724 | Popular Tags |