1 11 package org.eclipse.team.internal.ccvs.core.util; 12 13 14 import java.io.IOException ; 15 import java.io.InterruptedIOException ; 16 import java.net.Socket ; 17 import java.net.UnknownHostException ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.eclipse.core.resources.*; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.team.internal.ccvs.core.*; 25 import org.eclipse.team.internal.ccvs.core.client.Session; 26 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 27 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 28 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo; 29 30 33 public class Util { 34 35 40 public static String getLastSegment(String path) { 41 int index = path.lastIndexOf(Session.SERVER_SEPARATOR); 42 if (index == -1) { 43 return path; 44 } 45 if (index == path.length() - 1) { 46 return getLastSegment(path.substring(0, index)); 47 } 48 return path.substring(index + 1); 49 50 } 51 52 57 public static String removeLastSegment(String path) { 58 int index = path.lastIndexOf(Session.SERVER_SEPARATOR); 59 if (index == -1) 60 return ""; else 62 return path.substring(0, index); 63 64 } 65 70 public static String asPath(String path) { 71 if (path.endsWith(Session.SERVER_SEPARATOR)) { 72 return path.substring(0, path.length() - Session.SERVER_SEPARATOR.length()); 73 } 74 return path; 75 } 76 83 public static String getRelativePath(String rootName, String resourceName) 84 throws CVSException { 85 86 if (!resourceName.startsWith(rootName) || rootName.length() > resourceName.length()) { 87 throw new CVSException(CVSMessages.Util_Internal_error__resource_does_not_start_with_root_3); 88 } 89 90 if (rootName.length() == resourceName.length()) { 93 return ""; } 95 96 String result = resourceName.substring(rootName.length()); 98 if (result.startsWith("/")) { result = result.substring(1); 100 } 101 return result; 102 } 103 104 107 public static String appendPath(String prefix, String suffix) { 108 if (prefix.length() == 0 || prefix.equals(Session.CURRENT_LOCAL_FOLDER)) { 109 return suffix; 110 } else if (prefix.endsWith(Session.SERVER_SEPARATOR)) { 111 if (suffix.startsWith(Session.SERVER_SEPARATOR)) 112 return prefix + suffix.substring(1); 113 else 114 return prefix + suffix; 115 } else if (suffix.startsWith(Session.SERVER_SEPARATOR)) 116 return prefix + suffix; 117 else 118 return prefix + Session.SERVER_SEPARATOR + suffix; 119 } 120 121 public static void logError(String message, Throwable throwable) { 122 CVSProviderPlugin.log(IStatus.ERROR, message, throwable); 123 } 124 125 130 public static String toTruncatedPath(ICVSResource resource, ICVSFolder root, int split) { 131 try { 132 String stringPath = resource.getRelativePath(root); 133 if (stringPath.equals(Session.CURRENT_LOCAL_FOLDER)) { 134 return resource.getName(); 135 } 136 String truncatedPath = toTruncatedPath(stringPath, split); 137 return truncatedPath; 138 } catch(CVSException e) { 139 return resource.getName(); 140 } 141 } 142 143 public static String toTruncatedPath(String stringPath, int split) { 144 int count = 0; 146 int index = stringPath.length(); 147 while (count++ < split && index != -1) { 148 index = stringPath.lastIndexOf(Session.SERVER_SEPARATOR, index - 1); 149 } 150 if (index == -1) { 151 return stringPath; 152 } else { 153 return NLS.bind(CVSMessages.Util_truncatedPath, new String [] { stringPath.substring(index) }); 154 } 155 } 156 157 163 public static Socket createSocket(final String host, final int port, IProgressMonitor monitor) throws UnknownHostException , IOException { 164 int timeout = CVSProviderPlugin.getPlugin().getTimeout(); 165 if (timeout == 0) timeout = CVSProviderPlugin.DEFAULT_TIMEOUT; 166 ResponsiveSocketFactory factory = new ResponsiveSocketFactory(monitor, timeout); 167 return factory.createSocket(host, port); 168 } 169 170 176 public static Process createProcess(final String [] command, IProgressMonitor monitor) throws IOException { 177 178 final Process [] process = new Process [] { null }; 180 final Exception [] exception = new Exception [] {null }; 181 final Thread thread = new Thread (new Runnable () { 182 public void run() { 183 try { 184 Process newProcess = Runtime.getRuntime().exec(command); 185 synchronized (process) { 186 if (Thread.interrupted()) { 187 newProcess.destroy(); 189 } else { 190 process[0] = newProcess; 191 } 192 } 193 } catch (IOException e) { 194 exception[0] = e; 195 } 196 } 197 }); 198 thread.start(); 199 200 int timeout = CVSProviderPlugin.getPlugin().getTimeout(); 202 if (timeout == 0) timeout = CVSProviderPlugin.DEFAULT_TIMEOUT; 203 for (int i = 0; i < timeout; i++) { 204 try { 205 thread.join(1000); 207 } catch (InterruptedException e) { 208 } 211 synchronized (process) { 212 if (monitor.isCanceled()) { 214 if (thread.isAlive()) { 215 thread.interrupt(); 216 } 217 if (process[0] != null) { 218 process[0].destroy(); 219 } 220 Policy.checkCanceled(monitor); 222 } 223 } 224 } 225 synchronized (process) { 227 if (thread.isAlive()) { 228 thread.interrupt(); 229 } 230 } 231 if (exception[0] != null) { 232 throw (IOException )exception[0]; 233 } 234 if (process[0] == null) { 235 throw new InterruptedIOException (NLS.bind(CVSMessages.Util_processTimeout, new String [] { command[0] })); 236 } 237 return process[0]; 238 } 239 240 public static String [] parseIntoSubstrings(String string, String delimiter) { 241 List result = new ArrayList (); 242 int start = 0; 243 int index = string.indexOf(delimiter); 244 String next; 245 while (index != -1) { 246 next = string.substring(start, index); 247 result.add(next); 248 start = index + 1; 249 index = string.indexOf(delimiter, start); 250 } 251 if (start >= string.length()) { 252 next = ""; } else { 254 next = string.substring(start); 255 } 256 result.add(next); 257 return (String []) result.toArray(new String [result.size()]); 258 } 259 260 270 public static String getSubstring(byte[] bytes, byte delimiter, int index, boolean includeRest) { 271 byte[] bytesForSlot = getBytesForSlot(bytes, delimiter, index, includeRest); 272 if (bytesForSlot == null) { 273 return null; 274 } 275 return new String (bytesForSlot); 276 } 277 278 286 public static int getOffsetOfDelimeter(byte[] bytes, byte delimiter, int start, int n) { 287 int count = 0; 288 for (int i = start; i < bytes.length; i++) { 289 if (bytes[i] == delimiter) count++; 290 if (count == n) return i; 291 } 292 return -1; 294 } 295 296 304 public static byte[] getBytesForSlot(byte[] bytes, byte delimiter, int index, boolean includeRest) { 305 int start; 307 if (index == 0) { 308 start = -1; 310 } else { 311 start = getOffsetOfDelimeter(bytes, delimiter, 0, index); 312 if (start == -1) return null; 313 } 314 int end = getOffsetOfDelimeter(bytes, delimiter, start + 1, 1); 316 int length; 318 if (end == -1 || includeRest) { 319 length = bytes.length - start - 1; 320 } else { 321 length = end - start - 1; 322 } 323 byte[] result = new byte[length]; 324 System.arraycopy(bytes, start + 1, result, 0, length); 325 return result; 326 } 327 328 334 public static boolean equals(byte[] syncBytes, byte[] oldBytes) { 335 if (syncBytes == null || oldBytes == null) return syncBytes == oldBytes; 336 if (syncBytes.length != oldBytes.length) return false; 337 for (int i = 0; i < oldBytes.length; i++) { 338 if (oldBytes[i] != syncBytes[i]) return false; 339 } 340 return true; 341 } 342 343 357 358 public static CVSTag getAccurateFolderTag(IResource resource, CVSTag tag) { 359 360 if (resource.getType() != IResource.FOLDER) { 362 return tag; 363 } 364 365 IResource[] members = null; 366 try { 367 members = ((IFolder) resource).members(); 368 } catch (CoreException e1) { 369 return tag; 370 } 371 372 for (int i = 0; i < members.length; i++) { 373 if (members[i].getType() == IResource.FILE) { 374 return tag; 375 } 376 } 377 378 IProject project = resource.getProject(); 381 if (project == null) { 382 return tag; 383 } 384 385 ICVSFolder projectFolder = CVSWorkspaceRoot.getCVSFolderFor(project); 386 FolderSyncInfo projectSyncInfo; 387 try { 388 projectSyncInfo = projectFolder.getFolderSyncInfo(); 389 } catch (CVSException e) { 390 return tag; 391 } 392 393 if (projectSyncInfo == null) { 394 return tag; 395 } 396 397 CVSTag projectTag = projectSyncInfo.getTag(); 398 399 if (projectTag != null && projectTag.getName().equals(tag.getName())) { 400 return projectTag; 401 } else { 402 return tag; 403 } 404 } 405 406 417 418 public static CVSTag getAccurateFileTag(ICVSResource cvsResource) throws CVSException { 419 420 CVSTag tag = null; 421 ResourceSyncInfo info = cvsResource.getSyncInfo(); 422 if(info != null) { 423 tag = info.getTag(); 424 } 425 426 FolderSyncInfo parentInfo = cvsResource.getParent().getFolderSyncInfo(); 427 CVSTag parentTag = null; 428 if(parentInfo != null) { 429 parentTag = parentInfo.getTag(); 430 } 431 432 if(tag != null) { 433 if(tag.getName().equals(info.getRevision())) { 434 tag = new CVSTag(tag.getName(), CVSTag.VERSION); 435 } else if(parentTag != null){ 436 tag = new CVSTag(tag.getName(), parentTag.getType()); 437 } 438 } 439 440 return tag; 441 } 442 443 448 public static String getFullestPath(ICVSResource resource) { 449 IResource local = resource.getIResource(); 450 if (local != null) { 451 return local.getFullPath().toString(); 452 } 453 try { 454 String remotePath = resource.getRepositoryRelativePath(); 455 if (remotePath != null) { 456 return remotePath; 457 } 458 } catch (CVSException e) { 459 } 461 return resource.getName(); 462 } 463 464 public static String getVariablePattern(String pattern, String variableName) { 465 return "(" + variableName + ":" + pattern + ":" + variableName + ")"; } 467 468 472 public static int[] convertToDigits(String localRevision) { 473 try { 474 String digitStrings[] = localRevision.split("\\."); int[] digits = new int[digitStrings.length]; 476 for (int i = 0; i < digitStrings.length; i++) { 477 String digitString = digitStrings[i]; 478 digits[i] = Integer.parseInt(digitString); 479 } 480 return digits; 481 } catch (NumberFormatException e) { 482 CVSProviderPlugin.log(CVSException.wrapException(e)); 483 return new int[0]; 484 } 485 } 486 487 public static String toTruncatedPath(ICVSStorage file, ICVSFolder localRoot, int i) { 488 if (file instanceof ICVSResource) { 489 return toTruncatedPath((ICVSResource)file, localRoot, i); 490 } 491 return file.getName(); 492 } 493 494 501 public static String removeAtticSegment(String path) { 502 int lastSeparator = path.lastIndexOf(Session.SERVER_SEPARATOR); 503 if (lastSeparator == -1) return path; 504 int secondLastSeparator = path.lastIndexOf(Session.SERVER_SEPARATOR, lastSeparator - 1); 505 if (secondLastSeparator == -1) return path; 506 String secondLastSegment = path.substring(secondLastSeparator + 1, lastSeparator); 507 if (secondLastSegment.equals("Attic")) { return path.substring(0, secondLastSeparator) + path.substring(lastSeparator); 509 } 510 return path; 511 } 512 513 516 public static String flattenText(String string) { 517 StringBuffer buffer = new StringBuffer (string.length() + 20); 518 boolean skipAdjacentLineSeparator = true; 519 for (int i = 0; i < string.length(); i++) { 520 char c = string.charAt(i); 521 if (c == '\r' || c == '\n') { 522 if (!skipAdjacentLineSeparator) 523 buffer.append(Session.SERVER_SEPARATOR); 524 skipAdjacentLineSeparator = true; 525 } else { 526 buffer.append(c); 527 skipAdjacentLineSeparator = false; 528 } 529 } 530 return buffer.toString(); 531 } 532 } 533 | Popular Tags |