KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > core > ItemLocation


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

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 /**
13  * Created by The eXo Platform SARL .
14  *
15  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
16  * @version $Id: ItemLocation.java,v 1.1 2004/09/16 15:26:54 geaz Exp $
17  */

18
19 public class ItemLocation {
20
21   public static final String JavaDoc ROOT_PATH = "/";
22   private String JavaDoc path;
23
24   public ItemLocation(String JavaDoc absPath) throws PathNotFoundException {
25     this.path = PathUtil.makeCanonicalPath(absPath);
26   }
27
28   public ItemLocation(String JavaDoc parentAbsPath, String JavaDoc path) throws PathNotFoundException {
29     this.path = PathUtil.makeCanonicalPath(parentAbsPath, path);
30   }
31
32   public String JavaDoc 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 JavaDoc getPath() {
43     return path;
44   }
45
46   public String JavaDoc getParentPath() throws PathNotFoundException {
47     String JavaDoc parent;
48     try {
49       return getAncestorPath(1);
50     } catch (PathNotFoundException e) {
51       return ROOT_PATH;
52     }
53   }
54
55   public String JavaDoc 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 JavaDoc ancestorPath = path.substring(0, pos);
65     return ancestorPath.equals("") ? "/" : ancestorPath;
66   }
67
68   public int getDepth() {
69     int cnt = 0;
70     // Except trailing / - for ex. '/test/'
71
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 JavaDoc obj) {
78     if (!(obj instanceof ItemLocation))
79       return false;
80     return ((ItemLocation) obj).getPath() == this.getPath();
81   }
82 }
83
Popular Tags