KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > item > JCRPath


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.item;
18
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.alfresco.service.cmr.repository.Path;
22 import org.alfresco.service.namespace.NamespacePrefixResolver;
23 import org.alfresco.service.namespace.QName;
24
25 /**
26  * JCR Path Helper
27  *
28  * @author David Caruana
29  */

30 public class JCRPath
31 {
32     private Path path;
33     
34     /**
35      * Constuct path from string representation of path
36      *
37      * @param strPath
38      */

39     public JCRPath(NamespacePrefixResolver resolver, String JavaDoc strPath)
40     {
41         // TODO: replace this simple parse for full path syntax
42
boolean root = false;
43         int pos = 0;
44         path = new Path();
45         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(strPath, "/", true);
46         while (tokenizer.hasMoreTokens())
47         {
48             String JavaDoc token = tokenizer.nextToken();
49             if (pos == 0 && token.equals("/"))
50             {
51                 root = true;
52             }
53             else if (!token.equals("/"))
54             {
55                 if (root)
56                 {
57                     path.append(new RootSimpleElement(resolver, token));
58                     root = false;
59                 }
60                 else
61                 {
62                     path.append(new SimpleElement(resolver, token));
63                 }
64             }
65             pos++;
66         }
67     }
68     
69     /**
70      * Get the Path
71      *
72      * @return the underling path
73      */

74     public Path getPath()
75     {
76         return path;
77     }
78
79     
80     @Override JavaDoc
81     public String JavaDoc toString()
82     {
83         return path.toString();
84     }
85
86     /**
87      * Simple Path Element used for building JCR Paths
88      *
89      * @author David Caruana
90      */

91     public static class SimpleElement extends Path.Element
92     {
93         private static final long serialVersionUID = -6510331182652872996L;
94         private QName path;
95         
96         /**
97          * @param resolver namespace prefix resolver
98          * @param path path element name
99          */

100         public SimpleElement(QName path)
101         {
102             this.path = path;
103         }
104
105         /**
106          * @param path path element name
107          */

108         public SimpleElement(NamespacePrefixResolver resolver, String JavaDoc path)
109         {
110             this.path = QName.createQName(path, resolver);
111         }
112
113         /**
114          * Get the QName representation of Path
115          */

116         public QName getQName()
117         {
118             return path;
119         }
120         
121         @Override JavaDoc
122         public String JavaDoc getElementString()
123         {
124             return path.toString();
125         }
126
127         @Override JavaDoc
128         public String JavaDoc getPrefixedString(NamespacePrefixResolver resolver)
129         {
130             return path.toPrefixString(resolver);
131         }
132         
133         /* (non-Javadoc)
134          * @see java.lang.Object#equals(java.lang.Object)
135          */

136         public boolean equals(Object JavaDoc o)
137         {
138             if(o == this)
139             {
140                 return true;
141             }
142             if(!(o instanceof SimpleElement))
143             {
144                 return false;
145             }
146             SimpleElement other = (SimpleElement)o;
147             return this.path.equals(other.path);
148         }
149         
150         /* (non-Javadoc)
151          * @see java.lang.Object#hashCode()
152          */

153         public int hashCode()
154         {
155             return path.hashCode();
156         }
157
158     }
159
160     /**
161      * Root Path Element
162      *
163      * @author David Caruana
164      */

165     public static class RootSimpleElement extends SimpleElement
166     {
167         private static final long serialVersionUID = -4827016063963328324L;
168
169         /**
170          * Construct
171          *
172          * @param path
173          */

174         public RootSimpleElement(NamespacePrefixResolver resolver, String JavaDoc path)
175         {
176             super(resolver, path);
177         }
178         
179         @Override JavaDoc
180         public String JavaDoc getElementString()
181         {
182             return "/" + super.getElementString();
183         }
184         
185         @Override JavaDoc
186         public String JavaDoc getPrefixedString(NamespacePrefixResolver resolver)
187         {
188             return "/" + super.getPrefixedString(resolver);
189         }
190     }
191     
192 }
193
Popular Tags