1 package org
.argeo
.jcr
.fs
;
4 import java
.io
.IOException
;
6 import java
.net
.URISyntaxException
;
7 import java
.nio
.file
.FileSystem
;
8 import java
.nio
.file
.LinkOption
;
9 import java
.nio
.file
.Path
;
10 import java
.nio
.file
.WatchEvent
.Kind
;
11 import java
.nio
.file
.WatchEvent
.Modifier
;
12 import java
.nio
.file
.WatchKey
;
13 import java
.nio
.file
.WatchService
;
14 import java
.util
.Arrays
;
15 import java
.util
.Iterator
;
16 import java
.util
.NoSuchElementException
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.RepositoryException
;
20 import javax
.jcr
.Session
;
22 public class JcrPath
implements Path
{
23 private final static String delimStr
= "/";
24 private final static char delimChar
= '/';
26 private final JcrFileSystem fs
;
27 private final String
[] path
;// null means root
28 private final boolean absolute
;
31 private final int hashCode
;
33 public JcrPath(JcrFileSystem filesSystem
, String path
) {
34 // this(filesSystem, path.equals("/") ? null : path.split("/"), path ==
35 // null ? true : path.startsWith("/"));
36 this.fs
= filesSystem
;
38 throw new JcrFsException("Path cannot be null");
39 if (path
.equals(delimStr
)) {// root
44 } else if (path
.equals("")) {// empty path
45 this.path
= new String
[] { "" };
46 this.absolute
= false;
47 this.hashCode
= "".hashCode();
50 this.absolute
= path
.charAt(0) == delimChar ?
true : false;
51 String trimmedPath
= path
.substring(absolute ?
1 : 0,
52 path
.charAt(path
.length() - 1) == delimChar ? path
.length() - 1 : path
.length());
53 this.path
= trimmedPath
.split(delimStr
);
54 this.hashCode
= this.path
[this.path
.length
- 1].hashCode();
57 public JcrPath(JcrFileSystem filesSystem
, Node node
) throws RepositoryException
{
58 this(filesSystem
, node
.getPath());
61 /** Internal optimisation */
62 private JcrPath(JcrFileSystem filesSystem
, String
[] path
, boolean absolute
) {
63 this.fs
= filesSystem
;
65 this.absolute
= path
== null ?
true : absolute
;
66 this.hashCode
= path
== null ?
0 : path
[path
.length
- 1].hashCode();
70 public FileSystem
getFileSystem() {
75 public boolean isAbsolute() {
80 public Path
getRoot() {
84 return new JcrPath(fs
, fs
.getSession().getRootNode());
85 } catch (RepositoryException e
) {
86 throw new JcrFsException("Cannot get root", e
);
91 public String
toString() {
94 StringBuilder sb
= new StringBuilder();
97 for (int i
= 0; i
< path
.length
; i
++) {
102 return sb
.toString();
106 public Path
getFileName() {
109 return new JcrPath(fs
, path
[path
.length
- 1]);
113 public Path
getParent() {
116 if (path
.length
== 1)// root
117 return new JcrPath(fs
, delimStr
);
118 String
[] parentPath
= Arrays
.copyOfRange(path
, 0, path
.length
- 1);
119 return new JcrPath(fs
, parentPath
, absolute
);
123 public int getNameCount() {
130 public Path
getName(int index
) {
133 return new JcrPath(fs
, path
[index
]);
137 public Path
subpath(int beginIndex
, int endIndex
) {
140 String
[] parentPath
= Arrays
.copyOfRange(path
, beginIndex
, endIndex
);
141 return new JcrPath(fs
, parentPath
, false);
145 public boolean startsWith(Path other
) {
146 return toString().startsWith(other
.toString());
150 public boolean startsWith(String other
) {
151 return toString().startsWith(other
);
155 public boolean endsWith(Path other
) {
156 return toString().endsWith(other
.toString());
160 public boolean endsWith(String other
) {
161 return toString().endsWith(other
);
165 public Path
normalize() {
171 public Path
resolve(Path other
) {
172 JcrPath otherPath
= (JcrPath
) other
;
173 if (otherPath
.isAbsolute())
177 newPath
= new String
[otherPath
.path
.length
];
178 System
.arraycopy(otherPath
.path
, 0, newPath
, 0, otherPath
.path
.length
);
180 newPath
= new String
[path
.length
+ otherPath
.path
.length
];
181 System
.arraycopy(path
, 0, newPath
, 0, path
.length
);
182 System
.arraycopy(otherPath
.path
, 0, newPath
, path
.length
, otherPath
.path
.length
);
184 return new JcrPath(fs
, newPath
, absolute
);
188 public final Path
resolve(String other
) {
189 return resolve(getFileSystem().getPath(other
));
193 public final Path
resolveSibling(Path other
) {
195 throw new NullPointerException();
196 Path parent
= getParent();
197 return (parent
== null) ? other
: parent
.resolve(other
);
201 public final Path
resolveSibling(String other
) {
202 return resolveSibling(getFileSystem().getPath(other
));
206 public final Iterator
<Path
> iterator() {
207 return new Iterator
<Path
>() {
211 public boolean hasNext() {
212 return (i
< getNameCount());
217 if (i
< getNameCount()) {
218 Path result
= getName(i
);
222 throw new NoSuchElementException();
227 public void remove() {
228 throw new UnsupportedOperationException();
234 public Path
relativize(Path other
) {
236 return new JcrPath(fs
, "");
237 if (other
.startsWith(this)) {
238 String p1
= toString();
239 String p2
= other
.toString();
240 return new JcrPath(fs
, p2
.substring(p1
.length(), p2
.length()));
242 throw new IllegalArgumentException(other
+ " cannot be realtivized against " + this);
248 return new URI("jcr", toString(), null);
249 } catch (URISyntaxException e
) {
250 throw new JcrFsException("Cannot create URI for " + toString(), e
);
255 public Path
toAbsolutePath() {
258 return new JcrPath(fs
, path
, true);
262 public Path
toRealPath(LinkOption
... options
) throws IOException
{
267 public File
toFile() {
268 throw new UnsupportedOperationException();
272 public WatchKey
register(WatchService watcher
, Kind
<?
>[] events
, Modifier
... modifiers
) throws IOException
{
273 // TODO Auto-generated method stub
278 public WatchKey
register(WatchService watcher
, Kind
<?
>... events
) throws IOException
{
279 // TODO Auto-generated method stub
284 public int compareTo(Path other
) {
285 return toString().compareTo(other
.toString());
288 public Node
getNode() throws RepositoryException
{
289 if (!isAbsolute())// TODO default dir
290 throw new JcrFsException("Cannot get node from relative path");
291 String pathStr
= toString();
292 Session session
= fs
.getSession();
293 // TODO synchronize on the session ?
294 if (!session
.itemExists(pathStr
))
296 return session
.getNode(pathStr
);
300 public boolean equals(Object obj
) {
301 if (!(obj
instanceof JcrPath
))
303 JcrPath other
= (JcrPath
) obj
;
305 if (path
== null) {// root
306 if (other
.path
== null)// root
311 if (other
.path
== null)// root
315 if (path
.length
!= other
.path
.length
)
317 for (int i
= 0; i
< path
.length
; i
++) {
318 if (!path
[i
].equals(other
.path
[i
]))
325 public int hashCode() {
330 protected Object
clone() throws CloneNotSupportedException
{
331 return new JcrPath(fs
, toString());
335 protected void finalize() throws Throwable
{
336 Arrays
.fill(path
, null);