1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.io.*; 10 11 16 public class BundleInfo implements StorageItem 17 { 18 private File file; 19 private String shortName; private String version; 21 private String fullName; private String fileName; 23 private String [] nameParts; 24 private boolean component; 25 private boolean completeComponent; 26 27 public static class InvalidBundleNameException extends SOFA.SOFAnode.SOFAException 28 { 29 public InvalidBundleNameException(String message) 30 { 31 super(message); 32 } 33 34 public InvalidBundleNameException(String message, Throwable cause) 35 { 36 super(message, cause); 37 } 38 39 public InvalidBundleNameException(Throwable cause) 40 { 41 super(cause); 42 } 43 } 44 45 46 public BundleInfo() 47 { 48 } 49 50 51 public void setFile(File file) 52 { 53 this.file = file; 54 } 55 56 public File getFile() 57 { 58 return file; 59 } 60 61 public String getShortName() 62 { 63 return shortName; 64 } 65 66 public String getVersion() 67 { 68 return version; 69 } 70 71 public String getFullName() 72 { 73 return fullName; 74 } 75 76 public String getFileName() 77 { 78 return fileName; 79 } 80 81 public String [] getNameParts() 82 { 83 return nameParts; 84 } 85 86 public boolean isComponent() 87 { 88 return component; 89 } 90 91 public boolean isCompleteComponent() 92 { 93 return completeComponent; 94 } 95 96 public boolean isSingleComponent() 97 { 98 return component && !completeComponent; 99 } 100 101 104 public void fromFilename(String name) throws InvalidBundleNameException 105 { 106 if (name.length() <= 0) throw new InvalidBundleNameException("Empty bundle name"); 107 108 fileName = name; 109 110 if (name.charAt(0) == '-') { 112 component = true; 113 name = name.substring(1); 114 if (name.length() > 0 && name.charAt(name.length() - 1) == '$') { 116 completeComponent = true; 117 name = name.substring(0, name.length() - 2); 118 } 119 else completeComponent = false; 120 } 121 else 122 { 123 component = false; 124 completeComponent = false; 125 } 126 127 int index = name.lastIndexOf('-'); 128 129 if (index == -1) throw new InvalidBundleNameException("Missing bundle version in " + fileName); 130 131 version = name.substring(index + 1); 132 name = name.substring(0, index); 133 134 if (name.length() <= 0) throw new InvalidBundleNameException("Missing bundle short name in " + fileName); 135 if (component) shortName = "::" + name.replaceAll("-", "::"); 136 else shortName = name; 137 138 nameParts = name.split("-"); 139 140 fullName = shortName + "[" + version + "]"; 141 if (completeComponent) fullName += "$"; 142 } 143 144 public void fromBundleName(String name) throws InvalidBundleNameException 145 { 146 if (name.length() <= 0) throw new InvalidBundleNameException("Empty bundle name"); 147 148 fullName = name; 149 150 if (name.startsWith("::")) 151 { 152 component = true; 153 name = name.substring(2); 154 if (name.length() <= 0) throw new InvalidBundleNameException("Invalid bundle name: " + fullName); 155 if (name.charAt(name.length() - 1) == '$') 156 { 157 name = name.substring(0, name.length() - 1); 158 completeComponent = true; 159 } 160 else completeComponent = false; 161 } 162 else 163 { 164 component = false; 165 completeComponent = false; 166 } 167 168 if (name.charAt(name.length() - 1) != ']') throw new InvalidBundleNameException("Invalid version in bundle name: " + fullName); 169 name = name.substring(0, name.length() - 1); 170 171 int index = name.lastIndexOf('['); 172 if (index == -1 || index == name.length() - 1) throw new InvalidBundleNameException("Missing version in bundle name: " + fullName); 173 version = name.substring(index + 1); 174 175 name = name.substring(0, index); 176 177 if (component) 178 { 179 shortName = "::" + name; 180 name = name.replaceAll("::", "-"); 181 } 182 else 183 { 184 shortName = ""; 185 } 186 187 nameParts = name.split("-"); 188 189 if (component) 190 { 191 fileName = "-" + name + "-" + version; 192 if (completeComponent) fileName += "-$"; 193 } 194 else 195 { 196 fileName = name + "-" + version; 197 } 198 } 199 200 292 public boolean matchPattern(String bundleNamePattern) throws InvalidBundleNameException 293 { 294 String name = bundleNamePattern.trim(); 295 if (name.compareTo("*") == 0) return true; 296 297 int index1 = name.lastIndexOf('['); 299 int index2 = name.lastIndexOf(']'); 300 if (index1 == -1 || index2 == -1 || index2 < index1) throw new InvalidBundleNameException("Cannot find version in bundle name pattern: " + bundleNamePattern); 301 302 int len; 303 char ch, ch2; 304 305 String shortNamePattern = name.substring(0, index1); 306 307 if (shortNamePattern.startsWith("::")) 309 { 310 if (!component) return false; 311 shortNamePattern = shortNamePattern.substring(2); 312 shortNamePattern = shortNamePattern.replaceAll("::", "-"); 313 } 314 else 315 { 316 if (component && shortNamePattern.compareTo("*") != 0) return false; 317 } 318 319 String [] patternParts = shortNamePattern.split("-"); 320 321 boolean asterisk = false; 322 323 for (int i = 0; i < patternParts.length; i++) 324 { 325 len = patternParts[i].length(); 326 if (len == 0) return false; 327 if (nameParts.length <= i) return false; 328 ch = patternParts[i].charAt(0); 329 if (ch == '?') 330 { 331 if (len == 2 && patternParts[i].charAt(1) == '*') 332 { 333 asterisk = true; 334 break; 335 } 336 if (len > 1) throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at short-name part"); 337 else continue; 338 } 339 int index = patternParts[i].indexOf('*'); 340 if (index != -1 && index != patternParts[i].length() - 1 || 341 patternParts[i].indexOf('?') != -1) throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at short-name part"); 342 if (index == -1) 343 { 344 if (nameParts[i].compareTo(patternParts[i]) != 0) return false; 345 } 346 else 347 { 348 if (nameParts[i].length() < index) return false; 349 if (nameParts[i].substring(0, index).compareTo(patternParts[i].substring(0, index)) != 0) return false; 350 asterisk = true; 351 break; 352 } 353 } 354 355 if (!asterisk && patternParts.length != nameParts.length) return false; 356 357 358 String versionPattern = name.substring(index1 + 1, index2); 360 len = versionPattern.length(); 361 ch = len > 0 ? versionPattern.charAt(0) : 'x'; 362 ch2 = len > 1 ? versionPattern.charAt(1) : 'x'; 363 if (len == 0 || 364 len == 1 && (ch == '<' || ch == '>') || 365 len == 2 && (ch == '<' || ch == '>') && ch2 == '=' || 366 len > 1 && (ch == '?' || ch == '*')) 367 throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at version part"); 368 369 if (len == 1) 370 { 371 if (ch != '?' && ch != '*' && version.compareTo(versionPattern) != 0) return false; 372 } 373 else { 375 if (ch == '<' && ch2 != '=') 376 { 377 if (version.compareTo(versionPattern.substring(1)) >= 0) return false; 378 } 379 else 380 if (ch == '<' && ch2 == '=') 381 { 382 if (version.compareTo(versionPattern.substring(2)) > 0) return false; 383 } 384 else 385 if (ch == '>' && ch2 != '=') 386 { 387 if (version.compareTo(versionPattern.substring(1)) <= 0) return false; 388 } 389 else 390 if (ch == '>' && ch2 == '=') 391 { 392 if (version.compareTo(versionPattern.substring(2)) < 0) return false; 393 } 394 else 395 { 396 if (version.compareTo(versionPattern) != 0) return false; 397 } 398 } 399 400 String afterVersionPattern = name.substring(index2 + 1); 402 len = afterVersionPattern.length(); 403 ch = len > 0 ? afterVersionPattern.charAt(0) : 'x'; 404 if (len > 1 || len == 1 && ch != '$' && ch != '*' && ch != '?') throw new InvalidBundleNameException("Error in bundle name pattern \"" + bundleNamePattern + "\" at complete-component part"); 405 406 if (len == 0 && completeComponent) return false; 407 if (len == 1 && ch == '$' && (!component || !completeComponent)) return false; 408 409 return true; 410 } 411 412 public boolean isComponentBundleOf(String componentFullName) 413 { 414 return component && componentFullName.compareTo(shortName + "[" + version + "]") == 0; 415 } 416 417 public boolean isLoaded() 418 { 419 return false; 420 } 421 public void setLoaded(boolean loaded) 422 { 423 } 424 425 public boolean toBeDeleted() 426 { 427 return true; 428 } 429 430 public void loadFromStorage() 431 { 432 } 433 434 public void saveToStorage() 435 { 436 } 437 438 public String getName() 439 { 440 return fullName; 441 } 442 443 public void deleteFromStorage() 444 { 445 } 446 447 public static String fileNameToBundleName(String fileName) 448 { 449 BundleInfo bundleInfo = new BundleInfo(); 450 try 451 { 452 bundleInfo.fromFilename(fileName); 453 } 454 catch (BundleInfo.InvalidBundleNameException e) 455 { 456 return null; 457 } 458 return bundleInfo.getFullName(); 459 } 460 461 public static String bundleNameToFileName(String bundleName) 462 { 463 BundleInfo bundleInfo = new BundleInfo(); 464 try 465 { 466 bundleInfo.fromBundleName(bundleName); 467 } 468 catch (BundleInfo.InvalidBundleNameException e) 469 { 470 return null; 471 } 472 return bundleInfo.getFileName(); 473 } 474 475 public static String makeComponentBundleName(String nameOfComponent, String version, boolean isCompleteComponent) 476 { 477 if (isCompleteComponent) return nameOfComponent + "[" + version + "]$"; 478 else return nameOfComponent + "[" + version + "]"; 479 } 480 481 public static String makeUserBundleName(String name, String version) 482 { 483 return name + "[" + version + "]"; 484 } 485 486 public static boolean matchPattern(String bundleName, String bundleNamePattern) throws InvalidBundleNameException 487 { 488 BundleInfo bundleInfo = new BundleInfo(); 489 bundleInfo.fromBundleName(bundleName); 490 return bundleInfo.matchPattern(bundleNamePattern); 491 } 492 493 } 494 | Popular Tags |