1 3 package jodd.io; 4 5 import java.io.File ; 6 7 60 public class FileNameUtil { 61 62 65 private static final char EXTENSION_SEPARATOR = '.'; 66 67 70 private static final char UNIX_SEPARATOR = '/'; 71 72 75 private static final char WINDOWS_SEPARATOR = '\\'; 76 77 80 private static final char SYSTEM_SEPARATOR = File.separatorChar; 81 82 85 private static final char OTHER_SEPARATOR; 86 static { 87 if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) { 88 OTHER_SEPARATOR = UNIX_SEPARATOR; 89 } else { 90 OTHER_SEPARATOR = WINDOWS_SEPARATOR; 91 } 92 } 93 94 97 private static boolean isSeparator(char ch) { 98 return (ch == UNIX_SEPARATOR) || (ch == WINDOWS_SEPARATOR); 99 } 100 101 142 public static String normalize(String filename) { 143 return doNormalize(filename, true); 144 } 145 146 147 148 189 public static String normalizeNoEndSeparator(String filename) { 190 return doNormalize(filename, false); 191 } 192 193 200 private static String doNormalize(String filename, boolean keepSeparator) { 201 if (filename == null) { 202 return null; 203 } 204 int size = filename.length(); 205 if (size == 0) { 206 return filename; 207 } 208 int prefix = getPrefixLength(filename); 209 if (prefix < 0) { 210 return null; 211 } 212 213 char[] array = new char[size + 2]; filename.getChars(0, filename.length(), array, 0); 215 216 for (int i = 0; i < array.length; i++) { 218 if (array[i] == OTHER_SEPARATOR) { 219 array[i] = SYSTEM_SEPARATOR; 220 } 221 } 222 223 boolean lastIsDirectory = true; 225 if (array[size - 1] != SYSTEM_SEPARATOR) { 226 array[size] = SYSTEM_SEPARATOR; 227 size++; 228 lastIsDirectory = false; 229 } 230 231 for (int i = prefix + 1; i < size; i++) { 233 if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == SYSTEM_SEPARATOR) { 234 System.arraycopy(array, i, array, i - 1, size - i); 235 size--; 236 i--; 237 } 238 } 239 240 for (int i = prefix + 1; i < size; i++) { 242 if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '.' && 243 (i == prefix + 1 || array[i - 2] == SYSTEM_SEPARATOR)) { 244 if (i == size - 1) { 245 lastIsDirectory = true; 246 } 247 System.arraycopy(array, i + 1, array, i - 1, size - i); 248 size -=2; 249 i--; 250 } 251 } 252 253 outer: 255 for (int i = prefix + 2; i < size; i++) { 256 if (array[i] == SYSTEM_SEPARATOR && array[i - 1] == '.' && array[i - 2] == '.' && 257 (i == prefix + 2 || array[i - 3] == SYSTEM_SEPARATOR)) { 258 if (i == prefix + 2) { 259 return null; 260 } 261 if (i == size - 1) { 262 lastIsDirectory = true; 263 } 264 int j; 265 for (j = i - 4 ; j >= prefix; j--) { 266 if (array[j] == SYSTEM_SEPARATOR) { 267 System.arraycopy(array, i + 1, array, j + 1, size - i); 269 size -= (i - j); 270 i = j + 1; 271 continue outer; 272 } 273 } 274 System.arraycopy(array, i + 1, array, prefix, size - i); 276 size -= (i + 1 - prefix); 277 i = prefix + 1; 278 } 279 } 280 281 if (size <= 0) { return ""; 283 } 284 if (size <= prefix) { return new String (array, 0, size); 286 } 287 if (lastIsDirectory && keepSeparator) { 288 return new String (array, 0, size); } 290 return new String (array, 0, size - 1); } 292 293 334 public static String concat(String basePath, String fullFilenameToAdd) { 335 int prefix = getPrefixLength(fullFilenameToAdd); 336 if (prefix < 0) { 337 return null; 338 } 339 if (prefix > 0) { 340 return normalize(fullFilenameToAdd); 341 } 342 if (basePath == null) { 343 return null; 344 } 345 int len = basePath.length(); 346 if (len == 0) { 347 return normalize(fullFilenameToAdd); 348 } 349 char ch = basePath.charAt(len - 1); 350 if (isSeparator(ch)) { 351 return normalize(basePath + fullFilenameToAdd); 352 } else { 353 return normalize(basePath + '/' + fullFilenameToAdd); 354 } 355 } 356 357 359 365 public static String separatorsToUnix(String path) { 366 if (path == null || path.indexOf(WINDOWS_SEPARATOR) == -1) { 367 return path; 368 } 369 return path.replace(WINDOWS_SEPARATOR, UNIX_SEPARATOR); 370 } 371 372 378 public static String separatorsToWindows(String path) { 379 if (path == null || path.indexOf(UNIX_SEPARATOR) == -1) { 380 return path; 381 } 382 return path.replace(UNIX_SEPARATOR, WINDOWS_SEPARATOR); 383 } 384 385 391 public static String separatorsToSystem(String path) { 392 if (path == null) { 393 return null; 394 } 395 if (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR) { 396 return separatorsToWindows(path); 397 } else { 398 return separatorsToUnix(path); 399 } 400 } 401 402 434 public static int getPrefixLength(String filename) { 435 if (filename == null) { 436 return -1; 437 } 438 int len = filename.length(); 439 if (len == 0) { 440 return 0; 441 } 442 char ch0 = filename.charAt(0); 443 if (ch0 == ':') { 444 return -1; 445 } 446 if (len == 1) { 447 if (ch0 == '~') { 448 return 2; } 450 return (isSeparator(ch0) ? 1 : 0); 451 } else { 452 if (ch0 == '~') { 453 int posUnix = filename.indexOf(UNIX_SEPARATOR, 1); 454 int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1); 455 if (posUnix == -1 && posWin == -1) { 456 return len + 1; } 458 posUnix = (posUnix == -1 ? posWin : posUnix); 459 posWin = (posWin == -1 ? posUnix : posWin); 460 return Math.min(posUnix, posWin) + 1; 461 } 462 char ch1 = filename.charAt(1); 463 if (ch1 == ':') { 464 ch0 = Character.toUpperCase(ch0); 465 if (ch0 >= 'A' && ch0 <= 'Z') { 466 if (len == 2 || isSeparator(filename.charAt(2)) == false) { 467 return 2; 468 } 469 return 3; 470 } 471 return -1; 472 473 } else if (isSeparator(ch0) && isSeparator(ch1)) { 474 int posUnix = filename.indexOf(UNIX_SEPARATOR, 2); 475 int posWin = filename.indexOf(WINDOWS_SEPARATOR, 2); 476 if ((posUnix == -1 && posWin == -1) || posUnix == 2 || posWin == 2) { 477 return -1; 478 } 479 posUnix = (posUnix == -1 ? posWin : posUnix); 480 posWin = (posWin == -1 ? posUnix : posWin); 481 return Math.min(posUnix, posWin) + 1; 482 } else { 483 return (isSeparator(ch0) ? 1 : 0); 484 } 485 } 486 } 487 488 499 public static int indexOfLastSeparator(String filename) { 500 if (filename == null) { 501 return -1; 502 } 503 int lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR); 504 int lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR); 505 return Math.max(lastUnixPos, lastWindowsPos); 506 } 507 508 521 public static int indexOfExtension(String filename) { 522 if (filename == null) { 523 return -1; 524 } 525 int extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR); 526 int lastSeparator = indexOfLastSeparator(filename); 527 return (lastSeparator > extensionPos ? -1 : extensionPos); 528 } 529 530 532 561 public static String getPrefix(String filename) { 562 if (filename == null) { 563 return null; 564 } 565 int len = getPrefixLength(filename); 566 if (len < 0) { 567 return null; 568 } 569 if (len > filename.length()) { 570 return filename + UNIX_SEPARATOR; } 572 return filename.substring(0, len); 573 } 574 575 597 public static String getPath(String filename) { 598 return doGetPath(filename, 1); 599 } 600 601 624 public static String getPathNoEndSeparator(String filename) { 625 return doGetPath(filename, 0); 626 } 627 628 635 private static String doGetPath(String filename, int separatorAdd) { 636 if (filename == null) { 637 return null; 638 } 639 int prefix = getPrefixLength(filename); 640 if (prefix < 0) { 641 return null; 642 } 643 int index = indexOfLastSeparator(filename); 644 if (prefix >= filename.length() || index < 0) { 645 return ""; 646 } 647 return filename.substring(prefix, index + separatorAdd); 648 } 649 650 675 public static String getFullPath(String filename) { 676 return doGetFullPath(filename, true); 677 } 678 679 705 public static String getFullPathNoEndSeparator(String filename) { 706 return doGetFullPath(filename, false); 707 } 708 709 716 private static String doGetFullPath(String filename, boolean includeSeparator) { 717 if (filename == null) { 718 return null; 719 } 720 int prefix = getPrefixLength(filename); 721 if (prefix < 0) { 722 return null; 723 } 724 if (prefix >= filename.length()) { 725 if (includeSeparator) { 726 return getPrefix(filename); } else { 728 return filename; 729 } 730 } 731 int index = indexOfLastSeparator(filename); 732 if (index < 0) { 733 return filename.substring(0, prefix); 734 } 735 int end = index + (includeSeparator ? 1 : 0); 736 return filename.substring(0, end); 737 } 738 739 756 public static String getName(String filename) { 757 if (filename == null) { 758 return null; 759 } 760 int index = indexOfLastSeparator(filename); 761 return filename.substring(index + 1); 762 } 763 764 781 public static String getBaseName(String filename) { 782 return removeExtension(getName(filename)); 783 } 784 785 802 public static String getExtension(String filename) { 803 if (filename == null) { 804 return null; 805 } 806 int index = indexOfExtension(filename); 807 if (index == -1) { 808 return ""; 809 } else { 810 return filename.substring(index + 1); 811 } 812 } 813 814 816 833 public static String removeExtension(String filename) { 834 if (filename == null) { 835 return null; 836 } 837 int index = indexOfExtension(filename); 838 if (index == -1) { 839 return filename; 840 } else { 841 return filename.substring(0, index); 842 } 843 } 844 845 847 850 public static boolean equals(String filename1, String filename2) { 851 return equals(filename1, filename2, false); 852 } 853 854 857 public static boolean equalsOnSystem(String filename1, String filename2) { 858 return equals(filename1, filename2, true); 859 } 860 861 870 private static boolean equals(String filename1, String filename2, boolean system) { 871 if (filename1 == filename2) { 873 return true; 874 } 875 if (filename1 == null || filename2 == null) { 876 return false; 877 } 878 if (system && (SYSTEM_SEPARATOR == WINDOWS_SEPARATOR)) { 879 return filename1.equalsIgnoreCase(filename2); 880 } else { 881 return filename1.equals(filename2); 882 } 883 } 884 885 887 891 public static String [] split(String filename) { 892 String prefix = getPrefix(filename); 893 if (prefix == null) { 894 prefix = ""; 895 } 896 int lastSeparatorIndex = indexOfLastSeparator(filename); 897 int lastExtensionIndex = indexOfExtension(filename); 898 899 String path; 900 String baseName; 901 String extension; 902 903 if (lastSeparatorIndex == -1) { 904 path = ""; 905 if (lastExtensionIndex == -1) { 906 baseName = filename.substring(prefix.length()); 907 extension = ""; 908 } else { 909 baseName = filename.substring(prefix.length(), lastExtensionIndex); 910 extension = filename.substring(lastExtensionIndex + 1); 911 } 912 } else { 913 path = filename.substring(prefix.length(), lastSeparatorIndex + 1); 914 if (lastExtensionIndex == -1) { 915 baseName = filename.substring(prefix.length() + path.length()); 916 extension = ""; 917 } else { 918 baseName = filename.substring(prefix.length() + path.length(), lastExtensionIndex); 919 extension = filename.substring(lastExtensionIndex + 1); 920 } 921 } 922 return new String [] {prefix, path, baseName, extension}; 923 } 924 925 } | Popular Tags |