1 12 13 59 package com.openedit.util; 60 61 import org.apache.commons.logging.Log; 62 import org.apache.commons.logging.LogFactory; 63 64 65 70 public final class PathUtilities 71 { 72 private static final Log log = LogFactory.getLog(PathUtilities.class); 73 private static final String WILDCARD = "*"; 74 75 78 public PathUtilities() 79 { 80 } 82 83 90 public static String extractDirectoryPath(String path) 91 { 92 if ((path == null) || path.equals("") || path.equals("/")) 93 { 94 return ""; 95 } 96 97 int lastSlashPos = path.lastIndexOf('/'); 98 99 if (lastSlashPos >= 0) 100 { 101 return path.substring(0, lastSlashPos); } 103 else 104 { 105 return ""; } 107 } 108 109 119 public static String buildRelative(String endPart, String fullParentPath) 120 { 121 String basepath = null; 122 if ( fullParentPath != null) 123 { 124 fullParentPath = fullParentPath.replace('\\','/'); 125 } 126 128 if (endPart.startsWith(".") && (fullParentPath != null) && !fullParentPath.endsWith("/")) 130 { 131 int lastslash = fullParentPath.lastIndexOf("/"); 134 int lastperiod = fullParentPath.lastIndexOf("."); 135 136 if (lastslash > lastperiod) 137 { 138 basepath = fullParentPath; 140 } 141 } 142 143 if (basepath == null) 144 { 145 basepath = PathUtilities.extractDirectoryPath(fullParentPath); 146 } 147 if ( basepath == null ) 148 { 149 basepath = ""; 150 } 151 152 String relative = endPart; 153 154 if (endPart.startsWith("..")) 155 { 156 if (basepath.endsWith("/")) 158 { 159 basepath = basepath.substring(0, basepath.length() - 1); 160 } 161 162 basepath = basepath.substring(0, basepath.lastIndexOf("/")); 163 relative = basepath + endPart.substring(2); 164 } 165 else if (endPart.startsWith(".")) 166 { 167 relative = basepath + endPart.substring(1); 168 } 169 if ( !relative.startsWith("/")) 170 { 171 relative = "/" + relative; 172 } 173 return relative; 174 } 175 176 185 public static String extractPageName(String path) 186 { 187 if ( path == null) 188 { 189 return null; 190 } 191 String newpath = path.replace('\\','/'); 192 int start = newpath.lastIndexOf("/"); 193 if ( start == -1) 194 { 195 start = 0; 196 } 197 else 198 { 199 start++; } 201 int dotIndex = newpath.lastIndexOf("."); 202 203 if (dotIndex == -1 || start > dotIndex ) 204 { 205 return null; 206 } 207 String pageName = newpath.substring(start, dotIndex); 208 209 return pageName; 210 } 211 212 222 public static String extractPagePath(String path) 223 { 224 if ( path != null && path.length() > 0) 225 { 226 int lastDot = path.lastIndexOf("."); 227 if( lastDot > 1) 228 { 229 String pagePath = path.substring(0,lastDot); 230 return pagePath; 231 } 232 } 233 return path; 234 } 235 236 244 public static String extractPageType(String path) 245 { 246 int dotIndex = path.lastIndexOf("."); 247 248 if (dotIndex == -1) 249 { 250 return null; 251 } 252 253 String pageType = path.substring(dotIndex + 1); 254 255 return pageType; 256 } 257 258 266 public static boolean match(String requestPath, String wildcardPath) 267 { 268 int wildcardIndex = wildcardPath.indexOf(WILDCARD); 270 271 if (wildcardIndex == -1) 272 { 273 return requestPath.equalsIgnoreCase(wildcardPath); 274 } 275 else if( wildcardPath.charAt(0) == '*' && wildcardPath.charAt(wildcardPath.length()-1) == '*' ) 276 { 277 String path = wildcardPath.substring(1,wildcardPath.length()-1); 278 return requestPath.indexOf(path) > -1; 279 } 280 else if (wildcardIndex == (wildcardPath.length() - 1)) { 282 String checkString = wildcardPath.substring(0, wildcardPath.length() - 1); 284 285 boolean answer = requestPath.startsWith(checkString); 287 288 return answer; 290 } 291 else if( wildcardPath.charAt(0) == '*') 292 { 293 String checkString = wildcardPath.substring(1); 294 295 boolean answer = requestPath.endsWith(checkString); 297 return answer; 298 } 299 else 300 { 301 String preMatch = wildcardPath.substring(0, wildcardIndex); 303 String postMatch = wildcardPath.substring(wildcardIndex + 1); 304 305 return requestPath.startsWith(preMatch) && requestPath.endsWith(postMatch); 306 } 307 } 308 309 337 public static String resolveRelativePath(String relPath, String absPath) 338 { 339 if ( relPath.startsWith( "/" ) ) 341 { 342 absPath = ""; 343 } 344 345 String newAbsPath = absPath; 346 String newRelPath = relPath; 347 if( relPath.startsWith("$")) 348 { 349 return relPath; 350 } 351 else if (absPath.endsWith("/")) 352 { 353 newAbsPath = absPath.substring(0, absPath.length() - 1); 354 } 355 else 356 { 357 int lastSlashIndex = absPath.lastIndexOf('/'); 359 if ( lastSlashIndex >= 0 ) 360 { 361 newAbsPath = absPath.substring( 0, lastSlashIndex ); 362 } 363 else 364 { 365 newAbsPath = ""; 366 } 367 } 368 369 int relPos = newRelPath.indexOf("../"); 370 while (relPos > -1) 371 { 372 newRelPath = newRelPath.substring(relPos + 3); 373 int lastSlashInAbsPath = newAbsPath.lastIndexOf( "/" ); 374 if ( lastSlashInAbsPath >= 0 ) 375 { 376 newAbsPath = newAbsPath.substring(0, newAbsPath.lastIndexOf("/")); 377 } 378 else 379 { 380 newAbsPath = ""; 382 } 383 relPos = newRelPath.indexOf("../"); 384 } 385 String returnedPath; 386 if (newRelPath.startsWith("/")) 387 { 388 returnedPath = newAbsPath + newRelPath; 389 } 390 else 391 { 392 returnedPath = newAbsPath + "/" + newRelPath; 393 } 394 395 396 while ( returnedPath.endsWith( "/." ) ) 402 { 403 returnedPath = returnedPath.substring( 0, returnedPath.length() - 2 ); 404 } 405 do 406 { 407 int dotSlashIndex = returnedPath.lastIndexOf( "./" ); 408 if ( dotSlashIndex < 0 ) 409 { 410 break; 411 } 412 else if ( dotSlashIndex == 0 || returnedPath.charAt( dotSlashIndex - 1 ) != '.' ) 413 { 414 String firstSubstring; 415 if ( dotSlashIndex > 0 ) 416 { 417 firstSubstring = returnedPath.substring( 0, dotSlashIndex ); 418 } 419 else 420 { 421 firstSubstring = ""; 422 } 423 String secondSubstring; 424 if ( dotSlashIndex + 2 < returnedPath.length() ) 425 { 426 secondSubstring = returnedPath.substring( dotSlashIndex + 2, returnedPath.length() ); 427 } 428 else 429 { 430 secondSubstring = ""; 431 } 432 returnedPath = firstSubstring + secondSubstring; 433 } 434 } while ( true ); 435 436 return returnedPath; 437 } 438 439 444 public static String extractFileName(String path) { 445 446 if ( path == null) 447 { 448 return null; 449 } 450 String newpath = path.replace('\\','/'); 451 int start = newpath.lastIndexOf("/"); 452 if ( start == -1) 453 { 454 start = 0; 455 } 456 else 457 { 458 start = start + 1; 459 } 460 String pageName = newpath.substring(start, newpath.length()); 461 462 return pageName; 463 } 464 465 public static String createDraftPath(String inPath) 466 { 467 if ( inPath != null) 468 { 469 if( !inPath.contains(".draft.")) 470 { 471 String root = PathUtilities.extractPagePath(inPath); 472 String p = root + ".draft." + PathUtilities.extractPageType(inPath); 473 return p; 474 } 475 } 476 return inPath; 477 } 478 479 public static String createLivePath(String inDraftPath) 480 { 481 if ( inDraftPath != null) 482 { 483 if( inDraftPath.contains(".draft.")) 484 { 485 return inDraftPath.replace(".draft", ""); 486 } 487 } 488 return inDraftPath; 489 } 490 491 public static String makeId(String inText) 492 { 493 String id = inText; 494 id = id.replace("\\/","_"); 495 id = id.replace(".","_"); 496 id = id.replace(" ","_"); 497 if( id.charAt(0) == '_') 498 { 499 id = id.substring(1,id.length()); 500 } 501 return id; 502 } 503 } 504 | Popular Tags |