1 5 6 package org.exoplatform.services.jcr.core; 7 8 9 import javax.jcr.PathNotFoundException; 10 import org.exoplatform.services.jcr.util.PathUtil; 11 12 18 19 public class ItemLocation { 20 21 public static final String ROOT_PATH = "/"; 22 private String path; 23 24 public ItemLocation(String absPath) throws PathNotFoundException { 25 this.path = PathUtil.makeCanonicalPath(absPath); 26 } 27 28 public ItemLocation(String parentAbsPath, String path) throws PathNotFoundException { 29 this.path = PathUtil.makeCanonicalPath(parentAbsPath, path); 30 } 31 32 public String getName() { 33 int pos = path.lastIndexOf("/"); 34 if (pos < 0) { 35 return path; 36 } else if (pos == path.length()) { 37 return ""; 38 } 39 return path.substring(pos + 1); 40 } 41 42 public String getPath() { 43 return path; 44 } 45 46 public String getParentPath() throws PathNotFoundException { 47 String parent; 48 try { 49 return getAncestorPath(1); 50 } catch (PathNotFoundException e) { 51 return ROOT_PATH; 52 } 53 } 54 55 public String getAncestorPath(int relativeDegree) throws PathNotFoundException { 56 int pos = path.length(); 57 int cnt = relativeDegree; 58 while (cnt-- > 0) { 59 pos = path.lastIndexOf('/', pos - 1); 60 if (pos < 0) { 61 throw new PathNotFoundException(relativeDegree + "nth ancestor of " + path); 62 } 63 } 64 String ancestorPath = path.substring(0, pos); 65 return ancestorPath.equals("") ? "/" : ancestorPath; 66 } 67 68 public int getDepth() { 69 int cnt = 0; 70 for (int i = 0; i < path.length() - 1; i++) 72 if (path.charAt(i) == '/') 73 cnt++; 74 return cnt; 75 } 76 77 public boolean equals(Object obj) { 78 if (!(obj instanceof ItemLocation)) 79 return false; 80 return ((ItemLocation) obj).getPath() == this.getPath(); 81 } 82 } 83 | Popular Tags |