1 package org.jicengine.io; 2 import java.io.File ; 3 import java.util.StringTokenizer ; 4 5 14 public class PathResolver { 15 16 private static final String DIRECTORY_SEPARATOR = "/"; 17 18 34 public static String getRealPath(String contextPath, String relativePath) 35 { 36 File baseFile; 37 if( contextPath.endsWith(DIRECTORY_SEPARATOR) || contextPath.endsWith(File.separator) ){ 38 baseFile = new File (contextPath); 40 } 41 else { 42 baseFile = new File (contextPath).getParentFile(); 45 } 46 47 File newFile = new File (baseFile, relativePath); 48 49 String realPath = getPathOf(newFile); 50 51 if( relativePath.endsWith(DIRECTORY_SEPARATOR) && !realPath.endsWith(DIRECTORY_SEPARATOR)){ 55 realPath = realPath + DIRECTORY_SEPARATOR; 56 } 57 58 return realPath; 59 } 60 61 public static String getPathOf(File file) 62 { 63 String path; 64 if( File.separator.equals(DIRECTORY_SEPARATOR) ){ 65 path = file.getPath(); 67 } 68 else { 69 StringTokenizer tokenizer = new StringTokenizer (file.getPath(), File.separator, true); 71 String token; 72 StringBuffer pathBuffer = new StringBuffer (); 73 while(tokenizer.hasMoreElements() ){ 74 token = tokenizer.nextToken(); 75 if( token.equals(File.separator) ){ 76 pathBuffer.append(DIRECTORY_SEPARATOR); 77 } 78 else { 79 pathBuffer.append(token); 80 } 81 } 82 path = pathBuffer.toString(); 83 } 84 85 return path; 86 } 87 88 } 89 | Popular Tags |