1 19 20 package org.netbeans.modules.xml.wsdl.ui.wsdl.util; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 32 public class RelativePath { 33 40 private static List getPathList(File f) { 41 List l = new ArrayList (); 42 File r; 43 try { 44 r = f.getCanonicalFile(); 45 while(r != null) { 46 l.add(r.getName()); 47 r = r.getParentFile(); 48 } 49 } 50 catch (IOException e) { 51 e.printStackTrace(); 52 l = null; 53 } 54 return l; 55 } 56 57 63 private static String matchPathLists(List r,List f) { 64 int i; 65 int j; 66 StringBuffer s; 67 s = new StringBuffer (); 70 i = r.size()-1; 71 j = f.size()-1; 72 73 while((i >= 0)&&(j >= 0)&&(r.get(i).equals(f.get(j)))) { 75 i--; 76 j--; 77 } 78 79 for(;i>=0;i--) { 81 s.append("..").append("/"); 85 } 86 87 for(;j>=1;j--) { 89 s.append(f.get(j)).append("/"); 93 } 94 95 s.append(f.get(j)); 97 return s.toString(); 98 } 99 100 110 public static String getRelativePath(File home,File f){ 111 File r; 112 List homelist; 113 List filelist; 114 String s; 115 116 homelist = getPathList(home); 117 filelist = getPathList(f); 118 s = matchPathLists(homelist,filelist); 119 120 return s; 121 } 122 123 126 public static void main(String args[]) { 127 if (args.length != 2) { 128 System.out.println("RelativePath <home> <file>"); 129 return; 130 } 131 System.out.println("home = " + args[0]); 132 System.out.println("file = " + args[1]); 133 System.out.println("path = " + getRelativePath(new File (args[0]),new 134 File (args[1]))); 135 } 136 } 137 138 | Popular Tags |