This project has retired. For details please refer to its
Attic page.
PathManager xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.chemistry.opencmis.jcr;
21
22 import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
23 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import javax.jcr.Node;
28 import javax.jcr.RepositoryException;
29
30
31
32
33 public class PathManager {
34 private static final Log log = LogFactory.getLog(PathManager.class);
35
36
37
38
39 public static final String CMIS_ROOT_ID = "[root]";
40
41
42
43
44 public static final String CMIS_ROOT_PATH = "/";
45
46 private final String jcrRootPath;
47
48
49
50
51
52 public PathManager(String jcrRootPath) {
53 this.jcrRootPath = normalize(jcrRootPath);
54 }
55
56
57
58
59 public String getJcrRootPath() {
60 return jcrRootPath;
61 }
62
63
64
65
66
67
68
69
70
71
72 public boolean isRoot(Node node) {
73 try {
74 return node.getPath().equals(jcrRootPath);
75 }
76 catch (RepositoryException e) {
77 log.debug(e.getMessage(), e);
78 throw new CmisRuntimeException(e.getMessage(), e);
79 }
80 }
81
82
83
84
85
86
87
88
89 public String getPath(Node node) {
90 try {
91 if (jcrRootPath.length() > node.getPath().length()) {
92 throw new IllegalArgumentException("Node is not part of the hierarchy: " + node.getPath());
93 }
94
95 String path = node.getPath().substring(jcrRootPath.length());
96 return path.startsWith("/") ? path : '/' + path;
97 }
98 catch (RepositoryException e) {
99 log.debug(e.getMessage(), e);
100 throw new CmisRuntimeException(e.getMessage(), e);
101 }
102 }
103
104
105
106
107
108 public static boolean isRoot(String cmisPath) {
109 return CMIS_ROOT_PATH.equals(cmisPath);
110 }
111
112
113
114
115
116 public static boolean isAbsolute(String cmisPath) {
117 return cmisPath.startsWith(CMIS_ROOT_PATH);
118 }
119
120
121
122
123
124
125
126 public static String createCmisPath(String cmisPath, String child) {
127 return cmisPath.length() > 0 && cmisPath.charAt(cmisPath.length() - 1) == '/'
128 ? cmisPath + child
129 : cmisPath + '/' + child;
130 }
131
132
133
134
135
136
137
138
139 public static String relativize(String prefix, String cmisPath) {
140 if (cmisPath.startsWith(prefix)) {
141 return cmisPath.substring(prefix.length());
142 }
143 else {
144 throw new IllegalArgumentException(prefix + " is not a prefix of " + cmisPath);
145 }
146 }
147
148
149
150 private static String normalize(String path) {
151 if (path == null || path.length() == 0) {
152 return "/";
153 }
154
155 if (!path.startsWith("/")) {
156 throw new CmisInvalidArgumentException("Root path must be absolute. Got: " + path);
157 }
158
159 while (path.length() > 1 && path.endsWith("/")) {
160 path = path.substring(0, path.length() - 1);
161 }
162 return path;
163 }
164 }