2 package org
.de
.metux
.util
;
5 import java
.io
.IOException
;
7 public class PathNormalizer
12 public String sysroot_marker
= "@@SYSROOT@@";
14 public static String
basename(String filename
)
16 if ((filename
==null)||(filename
.length()==0))
19 int x
= filename
.lastIndexOf("/");
23 return filename
.substring(x
+1);
26 public static String
dirname(String filename
)
28 if ((filename
==null)||(filename
.length()==0))
31 int x
=filename
.lastIndexOf("/");
36 return filename
.substring(0,x
);
39 public static String
replace_suffix(String filename
, String suffix
)
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
)
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
);
67 public static String
dirname_slash(String filename
)
69 return normalize_dir(dirname(filename
));
72 public static String
normalize(String path
)
77 path
= StrReplace
.replace("//", "/",
78 StrReplace
.replace("/./", "/", path
));
80 if (path
.endsWith("/"))
81 return path
.substring(0,path
.length()-1);
86 public static String
normalize_dir(String path
)
88 path
= normalize(path
);
89 if ((path
.length()==0) || (path
.endsWith("/")))
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))
113 skip
= new String
[1];
118 String foo
[] = new String
[skip
.length
+1];
119 for (int x
=0; x
<skip
.length
; x
++)
121 foo
[skip
.length
] = sk
;
124 // normalize and strip off sysroot
125 public String
strip_sysroot(String path
)
128 throw new RuntimeException("passed null path");
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());
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("/"))
152 // what begins with sysroot_marker is already SYSROOT'ed
153 if (path
.startsWith(sysroot_marker
))
156 if ((sysroot
.length()>2) && (path
.startsWith(sysroot
)))
157 return sysroot_marker
+path
.substring(sysroot
.length());
161 for (int x
=0; x
<skip
.length
; x
++)
163 if (path
.startsWith(skip
[x
]))
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
))
182 return normalize(sr
+path
.substring(sysroot_marker
.length()));