Added README file
[metux-java.git] / util / PathNormalize.java
blob2cb2737187ffdfeb8914d6129fbe5e0579300873
2 package org.de.metux.util;
4 import java.io.File;
5 import java.io.IOException;
7 public class PathNormalize
9 public static String normalize(String path)
11 if (path==null)
12 return "";
14 path = StrReplace.replace("//", "/",
15 StrReplace.replace("/./", "/", path));
17 if (path.endsWith("/"))
18 return path.substring(0,path.length()-1);
19 else
20 return path;
23 // normalize and strip off sysroot
24 public static String normalize_strip_sysroot(String path, String sysroot)
26 sysroot = normalize(sysroot);
27 path = normalize(path);
29 if (path.startsWith(sysroot))
30 return path.substring(sysroot.length());
31 else
32 return path;
35 public static String normalize_add_sysroot(String path, String sysroot)
37 sysroot = normalize(sysroot);
38 path = normalize(path);
40 if (path.startsWith("/") && (!path.startsWith(sysroot)))
41 return normalize(sysroot+"/"+path);
42 else
43 return path;
46 public static String strip_cwd(String path)
48 try
50 return strip_cwd(path,new File(".").getCanonicalPath());
52 catch (IOException e)
54 throw new RuntimeException("File() failed ",e);
58 public static String strip_cwd(String path, String cwd)
60 int pos;
62 path = normalize(path);
63 cwd = normalize(cwd);
65 System.err.println("==>strip_cwd: path=\""+path+"\"");
66 System.err.println("==> cwd=\""+cwd+"\"");
68 if ((pos=path.indexOf(cwd))==0)
69 return normalize("./"+path.substring(cwd.length()));
70 else
71 return path;