]> git.argeo.org Git - lgpl/argeo-commons.git/blob - JcrPath.java
8782be9eed5b5659b52ea8aa806205a1bb3722dc
[lgpl/argeo-commons.git] / JcrPath.java
1 package org.argeo.jcr.fs;
2
3 import java.nio.file.Path;
4
5 import javax.jcr.Node;
6 import javax.jcr.RepositoryException;
7
8 import org.argeo.api.gcr.fs.AbstractFsPath;
9
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 = '/';
14
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;
20 //
21 // // optim
22 // private final int hashCode;
23
24 public JcrPath(JcrFileSystem filesSystem, String path) {
25 super(filesSystem, path);
26 // this.fs = filesSystem;
27 // if (path == null)
28 // throw new JcrFsException("Path cannot be null");
29 // if (path.equals(separator)) {// root
30 // this.path = null;
31 // this.absolute = true;
32 // this.hashCode = 0;
33 // this.fileStore = fs.getBaseFileStore();
34 // return;
35 // } else if (path.equals("")) {// empty path
36 // this.path = new String[] { "" };
37 // this.absolute = false;
38 // this.fileStore = null;
39 // this.hashCode = "".hashCode();
40 // return;
41 // }
42 //
43 // if (path.equals("~")) {// home
44 // path = filesSystem.getUserHomePath();
45 // if (path == null)
46 // throw new JcrFsException("No home directory available");
47 // }
48 //
49 // this.absolute = path.charAt(0) == separatorChar ? true : false;
50 //
51 // this.fileStore = absolute ? fs.getFileStore(path) : null;
52 //
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]);
58 // }
59 // this.hashCode = this.path[this.path.length - 1].hashCode();
60 // assert !(absolute && fileStore == null);
61 }
62
63 public JcrPath(JcrFileSystem filesSystem, Node node) throws RepositoryException {
64 this(filesSystem, filesSystem.getFileStore(node).toFsPath(node));
65 }
66
67 /** Internal optimisation */
68 private JcrPath(JcrFileSystem filesSystem, WorkspaceFileStore fileStore, String[] path, boolean absolute) {
69 super(filesSystem, fileStore, path, absolute);
70 // this.fs = filesSystem;
71 // this.path = path;
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);
80 }
81
82 protected String cleanUpSegment(String segment) {
83 return Text.unescapeIllegalJcrChars(segment);
84 }
85
86 @Override
87 protected JcrPath newInstance(String path) {
88 return new JcrPath(getFileSystem(), path);
89 }
90
91 @Override
92 protected JcrPath newInstance(String[] segments, boolean absolute) {
93 return new JcrPath(getFileSystem(), getFileStore(), segments, absolute);
94
95 }
96
97 // @Override
98 // public FileSystem getFileSystem() {
99 // return fs;
100 // }
101 //
102 // @Override
103 // public boolean isAbsolute() {
104 // return absolute;
105 // }
106 //
107 // @Override
108 // public Path getRoot() {
109 // if (path == null)
110 // return this;
111 // return new JcrPath(fs, separator);
112 // }
113 //
114 // @Override
115 // public String toString() {
116 // return toFsPath(path);
117 // }
118 //
119 // private String toFsPath(String[] path) {
120 // if (path == null)
121 // return "/";
122 // StringBuilder sb = new StringBuilder();
123 // if (isAbsolute())
124 // sb.append('/');
125 // for (int i = 0; i < path.length; i++) {
126 // if (i != 0)
127 // sb.append('/');
128 // sb.append(path[i]);
129 // }
130 // return sb.toString();
131 // }
132
133 // @Deprecated
134 // private String toJcrPath() {
135 // return toJcrPath(path);
136 // }
137 //
138 // @Deprecated
139 // private String toJcrPath(String[] path) {
140 // if (path == null)
141 // return "/";
142 // StringBuilder sb = new StringBuilder();
143 // if (isAbsolute())
144 // sb.append('/');
145 // for (int i = 0; i < path.length; i++) {
146 // if (i != 0)
147 // sb.append('/');
148 // sb.append(Text.escapeIllegalJcrChars(path[i]));
149 // }
150 // return sb.toString();
151 // }
152
153 // @Override
154 // public Path getFileName() {
155 // if (path == null)
156 // return null;
157 // return new JcrPath(fs, path[path.length - 1]);
158 // }
159 //
160 // @Override
161 // public Path getParent() {
162 // if (path == null)
163 // return null;
164 // if (path.length == 1)// root
165 // return new JcrPath(fs, separator);
166 // String[] parentPath = Arrays.copyOfRange(path, 0, path.length - 1);
167 // if (!absolute)
168 // return new JcrPath(fs, null, parentPath, absolute);
169 // else
170 // return new JcrPath(fs, toFsPath(parentPath));
171 // }
172 //
173 // @Override
174 // public int getNameCount() {
175 // if (path == null)
176 // return 0;
177 // return path.length;
178 // }
179 //
180 // @Override
181 // public Path getName(int index) {
182 // if (path == null)
183 // return null;
184 // return new JcrPath(fs, path[index]);
185 // }
186 //
187 // @Override
188 // public Path subpath(int beginIndex, int endIndex) {
189 // if (path == null)
190 // return null;
191 // String[] parentPath = Arrays.copyOfRange(path, beginIndex, endIndex);
192 // return new JcrPath(fs, null, parentPath, false);
193 // }
194 //
195 // @Override
196 // public boolean startsWith(Path other) {
197 // return toString().startsWith(other.toString());
198 // }
199 //
200 // @Override
201 // public boolean startsWith(String other) {
202 // return toString().startsWith(other);
203 // }
204 //
205 // @Override
206 // public boolean endsWith(Path other) {
207 // return toString().endsWith(other.toString());
208 // }
209 //
210 // @Override
211 // public boolean endsWith(String other) {
212 // return toString().endsWith(other);
213 // }
214
215 // @Override
216 // public Path normalize() {
217 // // always normalized
218 // return this;
219 // }
220
221 // @Override
222 // public Path resolve(Path other) {
223 // JcrPath otherPath = (JcrPath) other;
224 // if (otherPath.isAbsolute())
225 // return other;
226 // String[] newPath;
227 // if (path == null) {
228 // newPath = new String[otherPath.path.length];
229 // System.arraycopy(otherPath.path, 0, newPath, 0, otherPath.path.length);
230 // } else {
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);
234 // }
235 // if (!absolute)
236 // return new JcrPath(fs, null, newPath, absolute);
237 // else {
238 // return new JcrPath(fs, toFsPath(newPath));
239 // }
240 // }
241 //
242 // @Override
243 // public final Path resolve(String other) {
244 // return resolve(getFileSystem().getPath(other));
245 // }
246 //
247 // @Override
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);
253 // }
254 //
255 // @Override
256 // public final Path resolveSibling(String other) {
257 // return resolveSibling(getFileSystem().getPath(other));
258 // }
259 //
260 // @Override
261 // public final Iterator<Path> iterator() {
262 // return new Iterator<Path>() {
263 // private int i = 0;
264 //
265 // @Override
266 // public boolean hasNext() {
267 // return (i < getNameCount());
268 // }
269 //
270 // @Override
271 // public Path next() {
272 // if (i < getNameCount()) {
273 // Path result = getName(i);
274 // i++;
275 // return result;
276 // } else {
277 // throw new NoSuchElementException();
278 // }
279 // }
280 //
281 // @Override
282 // public void remove() {
283 // throw new UnsupportedOperationException();
284 // }
285 // };
286 // }
287 //
288 // @Override
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);
299 // }
300 // throw new IllegalArgumentException(other + " cannot be relativized against " + this);
301 // }
302
303 // @Override
304 // public URI toUri() {
305 // try {
306 // return new URI(fs.provider().getScheme(), toString(), null);
307 // } catch (URISyntaxException e) {
308 // throw new JcrFsException("Cannot create URI for " + toString(), e);
309 // }
310 // }
311 //
312 // @Override
313 // public Path toAbsolutePath() {
314 // if (isAbsolute())
315 // return this;
316 // return new JcrPath(fs, fileStore, path, true);
317 // }
318 //
319 // @Override
320 // public Path toRealPath(LinkOption... options) throws IOException {
321 // return this;
322 // }
323 //
324 // @Override
325 // public File toFile() {
326 // throw new UnsupportedOperationException();
327 // }
328
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))
338 // return null;
339 // return session.getNode(pathStr);
340 }
341 //
342 // @Override
343 // public boolean equals(Object obj) {
344 // if (!(obj instanceof JcrPath))
345 // return false;
346 // JcrPath other = (JcrPath) obj;
347 //
348 // if (path == null) {// root
349 // if (other.path == null)// root
350 // return true;
351 // else
352 // return false;
353 // } else {
354 // if (other.path == null)// root
355 // return false;
356 // }
357 // // non root
358 // if (path.length != other.path.length)
359 // return false;
360 // for (int i = 0; i < path.length; i++) {
361 // if (!path[i].equals(other.path[i]))
362 // return false;
363 // }
364 // return true;
365 // }
366
367 // @Override
368 // public int hashCode() {
369 // return hashCode;
370 // }
371
372 // @Override
373 // protected Object clone() throws CloneNotSupportedException {
374 // return new JcrPath(fs, toString());
375 // }
376
377 // @Override
378 // protected void finalize() throws Throwable {
379 // Arrays.fill(path, null);
380 // }
381
382
383
384 }