1 package org.enhydra.server.util; 2 3 import java.io.File ; 4 5 13 14 public class PathUtil { 15 16 private PathUtil() { 17 } 18 19 27 public static String makeRelativePath(String parentPath, String absolutePath){ 28 29 String relativePath = null; 30 31 if(parentPath!=null && absolutePath!=null && (parentPath.length()<absolutePath.length())){ 32 File absoluteFile = new File (absolutePath); 33 if(absoluteFile.isAbsolute()){ 34 File parentFile = new File (parentPath); 35 if(parentFile.isAbsolute()){ 37 String dirPath = parentFile.getAbsolutePath().replace('\\','/'); 38 absolutePath = absolutePath.replace('\\','/'); 39 int dirLength = dirPath.length(); 40 if(absolutePath.substring(0,dirLength).equalsIgnoreCase(dirPath)){ 41 relativePath = absolutePath.substring(dirLength); 43 if(relativePath.startsWith("/")){ 44 relativePath = relativePath.substring(1); 45 } 46 } 47 }else{ 48 relativePath = absoluteFile.getPath().replace('\\','/'); 49 } 50 }else{ 51 relativePath = absoluteFile.getPath().replace('\\','/'); 52 } 53 }else{ 54 if(absolutePath != null){ 55 relativePath = absolutePath.replace('\\','/'); 56 } 57 58 } 59 return relativePath; 60 } 61 62 68 public static String makeAbsolutePath(String parentPath, String relativePath){ 69 String absolutePath = relativePath; 70 71 if(parentPath != null){ 72 parentPath = parentPath.replace('\\','/'); 73 74 File parentFile = new File (parentPath); 75 File relativeFile = new File (relativePath); 76 File dir = null; 77 parentFile.mkdirs(); 79 if(parentFile.isAbsolute() && (!relativeFile.isAbsolute())){ 80 if(parentFile.isDirectory()){ 82 dir = parentFile; 83 }else{ 84 dir = parentFile.getParentFile(); 85 } 86 File absoluteFile = new File (dir,relativeFile.getPath()); 87 absolutePath = absoluteFile.getAbsolutePath(); 88 absolutePath = absolutePath.replace('\\','/'); 89 } 90 } 91 return absolutePath; 92 } 93 } | Popular Tags |