1 63 64 package org.exoplatform.services.jcr.util; 65 66 import java.util.Iterator ; 67 import java.util.LinkedList ; 68 import javax.jcr.PathNotFoundException; 69 70 77 public class PathUtil { 78 79 90 public static String makeCanonicalPath(String absolutePath) 91 throws PathNotFoundException { 92 if (!absolutePath.startsWith("/")) { 93 throw new PathNotFoundException("'" + absolutePath + "' is not an absolute path"); 94 } 95 if (absolutePath.indexOf("./") >= 0 || absolutePath.indexOf("/.") >= 0) { 99 LinkedList queue = new LinkedList (); 100 int start = 0; 101 while (start >= 0) { 102 int end = absolutePath.indexOf('/', start + 1); 103 String element = absolutePath.substring(start + 1, end == -1 ? absolutePath.length() : end); 104 if (element.equals(".")) { 105 } else if (element.equals("..")) { 107 if (queue.isEmpty()) { 108 throw new PathNotFoundException("'" + absolutePath + "' is not a valid path"); 109 } 110 queue.removeLast(); 111 } else { 112 queue.add(element); 113 } 114 start = end; 115 } 116 StringBuffer buf = new StringBuffer (); 117 Iterator iter = queue.iterator(); 118 while (iter.hasNext()) { 119 buf.append('/'); 120 buf.append((String ) iter.next()); 121 } 122 return buf.toString(); 123 } else { 124 return absolutePath; 125 } 126 } 127 128 144 public static String makeCanonicalPath(String parentPath, String somePath) 145 throws PathNotFoundException { 146 if (somePath.startsWith("/")) { 148 return makeCanonicalPath(somePath); 149 } else { 150 String absPath = (parentPath.equals("/") ? "" : parentPath) + "/" + somePath; 152 return makeCanonicalPath(absPath); 153 } 154 } 155 156 167 public static String getAncestorPath(String descendantPath, int degree) 168 throws PathNotFoundException { 169 descendantPath = makeCanonicalPath(descendantPath); 170 int pos = descendantPath.length(); 171 int cnt = degree; 172 while (cnt-- > 0) { 173 pos = descendantPath.lastIndexOf('/', pos - 1); 174 if (pos < 0) { 175 throw new PathNotFoundException(degree + "nth ancestor of " + descendantPath); 176 } 177 } 178 179 String ancestorPath = descendantPath.substring(0, pos); 180 return ancestorPath.equals("") ? "/" : ancestorPath; 181 } 182 183 190 public static String getName(String path) throws PathNotFoundException { 191 path = makeCanonicalPath(path); 192 193 int pos = path.lastIndexOf("/"); 194 if (pos < 0) { 195 return path; 196 } else if (pos == path.length()) { 197 return ""; 198 } 199 return path.substring(pos + 1); 200 } 201 202 public static String rewriteSuffix(String path, String from, String to) throws PathNotFoundException { 203 path = makeCanonicalPath(path); 204 205 int pos = from.length(); 206 if (pos == path.length()) { 207 return to; 208 } else { 209 return to + path.substring(pos); 210 } 211 212 } 213 214 215 public static boolean isDescendant(String testPath, String ancestorPath, boolean direct) { 216 217 try { 218 219 int depth = getDepth(makeCanonicalPath(testPath)); 220 if(depth == 0) 221 return false; 222 223 if(direct) 224 return getAncestorPath( makeCanonicalPath(testPath),1).equals(makeCanonicalPath(ancestorPath)); 225 else { 226 for(int i=1; i<=depth; i++) 227 if(getAncestorPath( makeCanonicalPath(testPath),i).equals(makeCanonicalPath(ancestorPath))) 228 return true; 229 } 230 231 } catch (PathNotFoundException e) {throw new RuntimeException ("isDescendanr failed "+e); } 232 233 return false; 234 235 269 } 270 271 public static int getDepth(String absolutePath) throws PathNotFoundException { 272 int cnt = 0; 273 String _path = makeCanonicalPath(absolutePath); 274 for (int i = 0; i < _path.length() - 1; i++) 276 if (_path.charAt(i) == '/') 277 cnt++; 278 return cnt; 279 } 280 281 } 282 | Popular Tags |