imported from svn
[metux-java.git] / util / PathNormalizer.java
blob8c810921dbe9ad6acbe15f352f6c2992e494ef71
2 package org.de.metux.util;
4 import java.io.File;
5 import java.io.IOException;
7 public class PathNormalizer
9 String sysroot;
10 String skip[];
12 public String sysroot_marker = "@@SYSROOT@@";
14 public static String basename(String filename)
16 if ((filename==null)||(filename.length()==0))
17 return "";
19 int x = filename.lastIndexOf("/");
20 if (x==-1)
21 return filename;
22 else
23 return filename.substring(x+1);
26 public static String dirname(String filename)
28 if ((filename==null)||(filename.length()==0))
29 return "";
31 int x=filename.lastIndexOf("/");
33 if ((x==-1)||(x==0))
34 return "";
35 else
36 return filename.substring(0,x);
39 public static String replace_suffix(String filename, String suffix)
41 int pos;
42 if ((pos=filename.lastIndexOf("."))<1)
43 return filename+"."+suffix;
45 return filename.substring(0,pos+1)+suffix;
48 // split an filename into name (res[0]) and suffix (res[1])
49 // the suffix contains the dot !
50 public static String [] split_filename ( String filename )
52 int pos;
53 String [] res = new String[2];
54 if ((pos=filename.lastIndexOf("."))>0)
56 res[0] = filename.substring(0,pos);
57 res[1] = filename.substring(pos);
59 else
61 res[0] = filename;
62 res[1] = "";
64 return res;
67 public static String dirname_slash(String filename)
69 return normalize_dir(dirname(filename));
72 public static String normalize(String path)
74 if (path==null)
75 return "";
77 path = StrReplace.replace("//", "/",
78 StrReplace.replace("/./", "/", path));
80 if (path.endsWith("/"))
81 return path.substring(0,path.length()-1);
82 else
83 return path;
86 public static String normalize_dir(String path)
88 path = normalize(path);
89 if ((path.length()==0) || (path.endsWith("/")))
90 return path;
91 else
92 return path+"/";
95 public PathNormalizer()
99 public void setSysroot(String sr)
101 sysroot = normalize(sr);
104 public void addSkip(String sk)
106 if ((sk==null)||(sk.length()==0))
107 return;
109 sk = normalize(sk);
111 if (skip==null)
113 skip = new String[1];
114 skip[0] = sk;
115 return;
118 String foo[] = new String[skip.length+1];
119 for (int x=0; x<skip.length; x++)
120 foo[x] = skip[x];
121 foo[skip.length] = sk;
124 // normalize and strip off sysroot
125 public String strip_sysroot(String path)
127 if (path==null)
128 throw new RuntimeException("passed null path");
130 if (sysroot==null)
131 throw new RuntimeException("no sysroot !");
133 path = normalize(path);
134 if (path.startsWith(sysroot))
135 return path.substring(sysroot.length());
136 else if (path.startsWith(sysroot_marker))
137 return path.substring(sysroot_marker.length());
138 else
139 return path;
142 // encode into sysroot'ed notation
143 // we check what has to be sysroot'ed and add @@SYSROOT@@ there
144 public String enc_sysroot(String path)
146 path = normalize(path);
148 // relative pathes are not SYSROOT'ed
149 if (!path.startsWith("/"))
150 return path;
152 // what begins with sysroot_marker is already SYSROOT'ed
153 if (path.startsWith(sysroot_marker))
154 return path;
156 if ((sysroot.length()>2) && (path.startsWith(sysroot)))
157 return sysroot_marker+path.substring(sysroot.length());
159 if (skip!=null)
161 for (int x=0; x<skip.length; x++)
163 if (path.startsWith(skip[x]))
164 return path;
168 return normalize(sysroot_marker+path);
171 public String dec_sysroot(String path)
173 return dec_sysroot(path,sysroot);
176 public String dec_sysroot(String path, String sr)
178 path = normalize(path);
179 if (!path.startsWith(sysroot_marker))
180 return path;
182 return normalize(sr+path.substring(sysroot_marker.length()));