1 17 package org.alfresco.filesys.server.filesys; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.util.StringTokenizer ; 22 23 27 public final class FileName 28 { 29 30 32 public static final char DOS_SEPERATOR = '\\'; 33 public static final String DOS_SEPERATOR_STR = "\\"; 34 35 37 public static final String NTFSStreamSeperator = ":"; 38 39 48 public static String buildPath(String dev, String path, String filename, char sep) 49 { 50 51 53 StringBuffer fullPath = new StringBuffer (); 54 55 57 if (dev != null) 58 { 59 60 62 fullPath.append(dev); 63 64 66 if (dev.length() > 0 && dev.charAt(dev.length() - 1) != sep) 67 fullPath.append(sep); 68 } 69 70 72 if (path != null) 73 { 74 75 77 if (fullPath.length() > 0 && path.length() > 0 78 && (path.charAt(0) == sep || path.charAt(0) == DOS_SEPERATOR)) 79 fullPath.append(path.substring(1)); 80 else 81 fullPath.append(path); 82 83 85 if (path.length() > 0 && path.charAt(path.length() - 1) != sep && filename != null) 86 fullPath.append(sep); 87 } 88 89 91 if (filename != null) 92 { 93 94 96 if (fullPath.length() > 0 && filename.length() > 0 97 && (filename.charAt(0) == sep || filename.charAt(0) == DOS_SEPERATOR)) 98 fullPath.append(filename.substring(1)); 99 else 100 fullPath.append(filename); 101 } 102 103 106 if (sep != DOS_SEPERATOR) 107 return convertSeperators(fullPath.toString(), sep); 108 return fullPath.toString(); 109 } 110 111 118 public static String convertSeperators(String path, char sep) 119 { 120 121 123 if (path.indexOf(DOS_SEPERATOR) == -1) 124 return path; 125 126 128 StringBuffer newPath = new StringBuffer (); 129 int idx = 0; 130 131 while (idx < path.length()) 132 { 133 134 137 char ch = path.charAt(idx++); 138 if (ch == DOS_SEPERATOR) 139 newPath.append(sep); 140 else 141 newPath.append(ch); 142 } 143 144 146 return newPath.toString(); 147 } 148 149 158 public static final String mapPath(String base, String path) throws java.io.FileNotFoundException 159 { 160 161 163 String pathCopy = path; 164 if (pathCopy.length() > 0 && pathCopy.startsWith(DOS_SEPERATOR_STR)) 165 pathCopy = pathCopy.substring(1); 166 167 StringTokenizer token = new StringTokenizer (pathCopy, "\\/"); 168 int tokCnt = token.countTokens(); 169 170 172 String mappedPath = null; 173 174 if (tokCnt > 0) 175 { 176 177 179 String [] dirs = new String [token.countTokens()]; 180 181 183 int idx = 0; 184 while (token.hasMoreTokens()) 185 dirs[idx++] = token.nextToken(); 186 187 189 int maxDir = dirs.length; 190 191 if (path.endsWith(DOS_SEPERATOR_STR) == false) 192 { 193 194 196 maxDir--; 197 } 198 199 201 StringBuffer pathStr = new StringBuffer (base); 202 if (base.endsWith(java.io.File.separator) == false) 203 pathStr.append(java.io.File.separator); 204 205 int lastPos = pathStr.length(); 206 idx = 0; 207 File lastDir = null; 208 if (base != null && base.length() > 0) 209 lastDir = new File (base); 210 File curDir = null; 211 212 while (idx < maxDir) 213 { 214 215 217 pathStr.append(dirs[idx]); 218 pathStr.append(java.io.File.separator); 219 220 222 curDir = new File (pathStr.toString()); 223 224 if (curDir.exists() == false) 225 { 226 227 229 if (lastDir == null) 230 throw new FileNotFoundException (); 231 232 234 String [] fileList = lastDir.list(); 235 if (fileList == null || fileList.length == 0) 236 throw new FileNotFoundException (); 237 238 int fidx = 0; 239 boolean foundPath = false; 240 241 while (fidx < fileList.length && foundPath == false) 242 { 243 244 246 if (fileList[fidx].equalsIgnoreCase(dirs[idx])) 247 { 248 249 251 pathStr.setLength(lastPos); 252 pathStr.append(fileList[fidx]); 253 pathStr.append(java.io.File.separator); 254 255 257 curDir = new File (pathStr.toString()); 258 if (curDir.exists()) 259 { 260 foundPath = true; 261 break; 262 } 263 } 264 265 267 fidx++; 268 } 269 270 272 if (foundPath == false) 273 throw new FileNotFoundException (); 274 } 275 276 278 lastDir = curDir; 279 280 282 lastPos = pathStr.length(); 283 284 286 idx++; 287 } 288 289 291 if (path.endsWith(DOS_SEPERATOR_STR) == false) 292 { 293 294 296 String [] fileList = lastDir.list(); 297 String fileName = dirs[dirs.length - 1]; 298 299 301 if (fileList == null) 302 throw new FileNotFoundException (path); 303 304 306 idx = 0; 307 boolean foundFile = false; 308 309 while (idx < fileList.length && foundFile == false) 310 { 311 if (fileList[idx].compareTo(fileName) == 0) 312 foundFile = true; 313 else 314 idx++; 315 } 316 317 319 if (foundFile == false) 320 { 321 322 324 idx = 0; 325 326 while (idx < fileList.length && foundFile == false) 327 { 328 if (fileList[idx].equalsIgnoreCase(fileName)) 329 { 330 foundFile = true; 331 fileName = fileList[idx]; 332 } 333 else 334 idx++; 335 } 336 } 337 338 340 pathStr.append(fileName); 341 } 342 343 345 mappedPath = pathStr.toString(); 346 } 347 348 350 return mappedPath; 351 } 352 353 359 public final static String removeFileName(String path) 360 { 361 362 364 int pos = path.lastIndexOf(DOS_SEPERATOR); 365 if (pos != -1) 366 return path.substring(0, pos); 367 368 370 return ""; 371 } 372 373 380 public static String [] splitPath(String path) 381 { 382 return splitPath(path, DOS_SEPERATOR, null); 383 } 384 385 392 public static String [] splitPath(String path, char sep) 393 { 394 return splitPath(path, sep, null); 395 } 396 397 405 public static String [] splitPath(String path, char sep, String [] list) 406 { 407 if (path == null) 408 throw new IllegalArgumentException ("Path may not be null"); 409 410 String [] pathStr = list; 412 if (pathStr == null) 413 pathStr = new String [] {"", ""}; 414 415 if (path.length() > 0) 417 { 418 int pos = path.lastIndexOf(sep); 420 if (pos == -1 || pos == (path.length() - 1)) 421 { 422 pathStr[0] = path; 424 } 425 else 426 { 427 pathStr[1] = path.substring(pos + 1); 429 430 if (pos == 0) 431 pathStr[0] = path.substring(0, pos + 1); 432 else 433 pathStr[0] = path.substring(0, pos); 434 } 435 } 436 437 return pathStr; 439 } 440 441 447 public static String [] splitAllPaths(String path) 448 { 449 450 452 if (path == null || path.length() == 0) 453 return null; 454 455 457 StringTokenizer token = new StringTokenizer (path, DOS_SEPERATOR_STR); 458 String [] names = new String [token.countTokens()]; 459 460 462 int i = 0; 463 464 while (i < names.length && token.hasMoreTokens()) 465 names[i++] = token.nextToken(); 466 467 469 return names; 470 } 471 472 478 public static String [] splitPathStream(String path) 479 { 480 481 483 String [] pathStr = new String [3]; 484 485 487 FileName.splitPath(path, DOS_SEPERATOR, pathStr); 488 if (pathStr[1] == null) 489 return pathStr; 490 491 493 int pos = pathStr[1].indexOf(NTFSStreamSeperator); 494 495 if (pos != -1) 496 { 497 498 500 pathStr[2] = pathStr[1].substring(pos); 501 pathStr[1] = pathStr[1].substring(0, pos); 502 } 503 504 506 return pathStr; 507 } 508 509 515 public static boolean containsStreamName(String path) 516 { 517 518 520 if (path.indexOf(NTFSStreamSeperator) != -1) 521 return true; 522 return false; 523 } 524 525 531 public final static String normalizePath(String path) 532 { 533 534 538 String normPath = path; 539 540 if (path.length() > 3) 541 { 542 543 545 int pos = path.lastIndexOf(DOS_SEPERATOR); 546 if (pos != -1) 547 { 548 549 551 String pathPart = path.substring(0, pos).toUpperCase(); 552 String namePart = path.substring(pos); 553 554 556 normPath = pathPart + namePart; 557 } 558 } 559 560 562 return normPath; 563 } 564 565 572 public final static String makeRelativePath(String basePath, String fullPath) 573 { 574 575 577 if (basePath.length() == 0 || basePath.equals(DOS_SEPERATOR_STR)) 578 { 579 580 582 if (fullPath.length() > 0 && fullPath.charAt(0) == DOS_SEPERATOR) 583 return fullPath.substring(1); 584 return fullPath; 585 } 586 587 589 String [] baseNames = splitAllPaths(basePath); 590 String [] fullNames = splitAllPaths(fullPath); 591 592 594 if (baseNames != null && baseNames.length > 0 && fullNames != null && fullNames.length > 0 595 && baseNames[0].equalsIgnoreCase(fullNames[0]) == false) 596 return null; 597 598 600 int idx = 0; 601 602 while (idx < baseNames.length && idx < fullNames.length && baseNames[idx].equalsIgnoreCase(fullNames[idx])) 603 idx++; 604 605 607 StringBuffer relPath = new StringBuffer (128); 608 609 while (idx < fullNames.length) 610 { 611 relPath.append(fullNames[idx++]); 612 if (idx < fullNames.length) 613 relPath.append(DOS_SEPERATOR); 614 } 615 616 618 return relPath.toString(); 619 } 620 } | Popular Tags |