1 package org
.argeo
.jcr
.fs
;
3 import java
.nio
.file
.Path
;
6 import javax
.jcr
.RepositoryException
;
8 import org
.argeo
.api
.acr
.fs
.AbstractFsPath
;
10 /** A {@link Path} which contains a reference to a JCR {@link Node}. */
11 public class JcrPath
extends AbstractFsPath
<JcrFileSystem
, WorkspaceFileStore
> {
12 final static String separator
= "/";
13 final static char separatorChar
= '/';
15 // private final JcrFileSystem fs;
16 // /** null for non absolute paths */
17 // private final WorkspaceFileStore fileStore;
18 // private final String[] path;// null means root
19 // private final boolean absolute;
22 // private final int hashCode;
24 public JcrPath(JcrFileSystem filesSystem
, String path
) {
25 super(filesSystem
, path
);
26 // this.fs = filesSystem;
28 // throw new JcrFsException("Path cannot be null");
29 // if (path.equals(separator)) {// root
31 // this.absolute = true;
33 // this.fileStore = fs.getBaseFileStore();
35 // } else if (path.equals("")) {// empty path
36 // this.path = new String[] { "" };
37 // this.absolute = false;
38 // this.fileStore = null;
39 // this.hashCode = "".hashCode();
43 // if (path.equals("~")) {// home
44 // path = filesSystem.getUserHomePath();
46 // throw new JcrFsException("No home directory available");
49 // this.absolute = path.charAt(0) == separatorChar ? true : false;
51 // this.fileStore = absolute ? fs.getFileStore(path) : null;
53 // String trimmedPath = path.substring(absolute ? 1 : 0,
54 // path.charAt(path.length() - 1) == separatorChar ? path.length() - 1 : path.length());
55 // this.path = trimmedPath.split(separator);
56 // for (int i = 0; i < this.path.length; i++) {
57 // this.path[i] = Text.unescapeIllegalJcrChars(this.path[i]);
59 // this.hashCode = this.path[this.path.length - 1].hashCode();
60 // assert !(absolute && fileStore == null);
63 public JcrPath(JcrFileSystem filesSystem
, Node node
) throws RepositoryException
{
64 this(filesSystem
, filesSystem
.getFileStore(node
).toFsPath(node
));
67 /** Internal optimisation */
68 private JcrPath(JcrFileSystem filesSystem
, WorkspaceFileStore fileStore
, String
[] path
, boolean absolute
) {
69 super(filesSystem
, fileStore
, path
, absolute
);
70 // this.fs = filesSystem;
72 // this.absolute = path == null ? true : absolute;
73 // if (this.absolute && fileStore == null)
74 // throw new IllegalArgumentException("Absolute path requires a file store");
75 // if (!this.absolute && fileStore != null)
76 // throw new IllegalArgumentException("A file store should not be provided for a relative path");
77 // this.fileStore = fileStore;
78 // this.hashCode = path == null ? 0 : path[path.length - 1].hashCode();
79 // assert !(absolute && fileStore == null);
82 protected String
cleanUpSegment(String segment
) {
83 return Text
.unescapeIllegalJcrChars(segment
);
87 protected JcrPath
newInstance(String path
) {
88 return new JcrPath(getFileSystem(), path
);
92 protected JcrPath
newInstance(String
[] segments
, boolean absolute
) {
93 return new JcrPath(getFileSystem(), getFileStore(), segments
, absolute
);
98 // public FileSystem getFileSystem() {
103 // public boolean isAbsolute() {
108 // public Path getRoot() {
111 // return new JcrPath(fs, separator);
115 // public String toString() {
116 // return toFsPath(path);
119 // private String toFsPath(String[] path) {
122 // StringBuilder sb = new StringBuilder();
125 // for (int i = 0; i < path.length; i++) {
128 // sb.append(path[i]);
130 // return sb.toString();
134 // private String toJcrPath() {
135 // return toJcrPath(path);
139 // private String toJcrPath(String[] path) {
142 // StringBuilder sb = new StringBuilder();
145 // for (int i = 0; i < path.length; i++) {
148 // sb.append(Text.escapeIllegalJcrChars(path[i]));
150 // return sb.toString();
154 // public Path getFileName() {
157 // return new JcrPath(fs, path[path.length - 1]);
161 // public Path getParent() {
164 // if (path.length == 1)// root
165 // return new JcrPath(fs, separator);
166 // String[] parentPath = Arrays.copyOfRange(path, 0, path.length - 1);
168 // return new JcrPath(fs, null, parentPath, absolute);
170 // return new JcrPath(fs, toFsPath(parentPath));
174 // public int getNameCount() {
177 // return path.length;
181 // public Path getName(int index) {
184 // return new JcrPath(fs, path[index]);
188 // public Path subpath(int beginIndex, int endIndex) {
191 // String[] parentPath = Arrays.copyOfRange(path, beginIndex, endIndex);
192 // return new JcrPath(fs, null, parentPath, false);
196 // public boolean startsWith(Path other) {
197 // return toString().startsWith(other.toString());
201 // public boolean startsWith(String other) {
202 // return toString().startsWith(other);
206 // public boolean endsWith(Path other) {
207 // return toString().endsWith(other.toString());
211 // public boolean endsWith(String other) {
212 // return toString().endsWith(other);
216 // public Path normalize() {
217 // // always normalized
222 // public Path resolve(Path other) {
223 // JcrPath otherPath = (JcrPath) other;
224 // if (otherPath.isAbsolute())
227 // if (path == null) {
228 // newPath = new String[otherPath.path.length];
229 // System.arraycopy(otherPath.path, 0, newPath, 0, otherPath.path.length);
231 // newPath = new String[path.length + otherPath.path.length];
232 // System.arraycopy(path, 0, newPath, 0, path.length);
233 // System.arraycopy(otherPath.path, 0, newPath, path.length, otherPath.path.length);
236 // return new JcrPath(fs, null, newPath, absolute);
238 // return new JcrPath(fs, toFsPath(newPath));
243 // public final Path resolve(String other) {
244 // return resolve(getFileSystem().getPath(other));
248 // public final Path resolveSibling(Path other) {
249 // if (other == null)
250 // throw new NullPointerException();
251 // Path parent = getParent();
252 // return (parent == null) ? other : parent.resolve(other);
256 // public final Path resolveSibling(String other) {
257 // return resolveSibling(getFileSystem().getPath(other));
261 // public final Iterator<Path> iterator() {
262 // return new Iterator<Path>() {
263 // private int i = 0;
266 // public boolean hasNext() {
267 // return (i < getNameCount());
271 // public Path next() {
272 // if (i < getNameCount()) {
273 // Path result = getName(i);
277 // throw new NoSuchElementException();
282 // public void remove() {
283 // throw new UnsupportedOperationException();
289 // public Path relativize(Path other) {
290 // if (equals(other))
291 // return new JcrPath(fs, "");
292 // if (other.startsWith(this)) {
293 // String p1 = toString();
294 // String p2 = other.toString();
295 // String relative = p2.substring(p1.length(), p2.length());
296 // if (relative.charAt(0) == '/')
297 // relative = relative.substring(1);
298 // return new JcrPath(fs, relative);
300 // throw new IllegalArgumentException(other + " cannot be relativized against " + this);
304 // public URI toUri() {
306 // return new URI(fs.provider().getScheme(), toString(), null);
307 // } catch (URISyntaxException e) {
308 // throw new JcrFsException("Cannot create URI for " + toString(), e);
313 // public Path toAbsolutePath() {
316 // return new JcrPath(fs, fileStore, path, true);
320 // public Path toRealPath(LinkOption... options) throws IOException {
325 // public File toFile() {
326 // throw new UnsupportedOperationException();
329 public Node
getNode() throws RepositoryException
{
330 if (!isAbsolute())// TODO default dir
331 throw new JcrFsException("Cannot get a JCR node from a relative path");
332 assert getFileStore() != null;
333 return getFileStore().toNode(getSegments());
334 // String pathStr = toJcrPath();
335 // Session session = fs.getSession();
336 // // TODO synchronize on the session ?
337 // if (!session.itemExists(pathStr))
339 // return session.getNode(pathStr);
343 // public boolean equals(Object obj) {
344 // if (!(obj instanceof JcrPath))
346 // JcrPath other = (JcrPath) obj;
348 // if (path == null) {// root
349 // if (other.path == null)// root
354 // if (other.path == null)// root
358 // if (path.length != other.path.length)
360 // for (int i = 0; i < path.length; i++) {
361 // if (!path[i].equals(other.path[i]))
368 // public int hashCode() {
373 // protected Object clone() throws CloneNotSupportedException {
374 // return new JcrPath(fs, toString());
378 // protected void finalize() throws Throwable {
379 // Arrays.fill(path, null);