3 import java
.util
.ArrayList
;
4 import java
.util
.Iterator
;
7 import javax
.jcr
.ItemNotFoundException
;
9 import javax
.jcr
.NodeIterator
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.PropertyType
;
12 import javax
.jcr
.Repository
;
13 import javax
.jcr
.RepositoryException
;
14 import javax
.jcr
.Session
;
15 import javax
.jcr
.Value
;
18 * Utility class whose purpose is to make using JCR less verbose by
19 * systematically using unchecked exceptions and returning <code>null</code>
20 * when something is not found. This is especially useful when writing user
21 * interfaces (such as with SWT) where listeners and callbacks expect unchecked
22 * exceptions. Loosely inspired by Java's <code>Files</code> singleton.
27 * @see Node#isNodeType(String)
28 * @throws IllegalStateException caused by {@link RepositoryException}
30 public static boolean isNodeType(Node node
, String nodeTypeName
) {
32 return node
.isNodeType(nodeTypeName
);
33 } catch (RepositoryException e
) {
34 throw new IllegalStateException("Cannot get whether " + node
+ " is of type " + nodeTypeName
, e
);
39 * @see Node#hasNodes()
40 * @throws IllegalStateException caused by {@link RepositoryException}
42 public static boolean hasNodes(Node node
) {
44 return node
.hasNodes();
45 } catch (RepositoryException e
) {
46 throw new IllegalStateException("Cannot get whether " + node
+ " has children.", e
);
51 * @see Node#getParent()
52 * @throws IllegalStateException caused by {@link RepositoryException}
54 public static Node
getParent(Node node
) {
56 return isRoot(node
) ?
null : node
.getParent();
57 } catch (RepositoryException e
) {
58 throw new IllegalStateException("Cannot get parent of " + node
, e
);
63 * Whether this node is the root node.
65 * @throws IllegalStateException caused by {@link RepositoryException}
67 public static boolean isRoot(Node node
) {
69 return node
.getDepth() == 0;
70 } catch (RepositoryException e
) {
71 throw new IllegalStateException("Cannot get depth of " + node
, e
);
77 * @throws IllegalStateException caused by {@link RepositoryException}
79 public static String
getPath(Node node
) {
81 return node
.getPath();
82 } catch (RepositoryException e
) {
83 throw new IllegalStateException("Cannot get path of " + node
, e
);
88 * @see Node#getIdentifier()
89 * @throws IllegalStateException caused by {@link RepositoryException}
91 public static String
getIdentifier(Node node
) {
93 return node
.getIdentifier();
94 } catch (RepositoryException e
) {
95 throw new IllegalStateException("Cannot get identifier of " + node
, e
);
100 * @see Node#getName()
101 * @throws IllegalStateException caused by {@link RepositoryException}
103 public static String
getName(Node node
) {
105 return node
.getName();
106 } catch (RepositoryException e
) {
107 throw new IllegalStateException("Cannot get name of " + node
, e
);
111 /** Accesses a {@link NodeIterator} as an {@link Iterable}. */
112 @SuppressWarnings("unchecked")
113 public static Iterable
<Node
> iterate(NodeIterator nodeIterator
) {
114 return new Iterable
<Node
>() {
117 public Iterator
<Node
> iterator() {
124 * @return the children as an {@link Iterable} for use in for-each llops.
125 * @see Node#getNodes()
126 * @throws IllegalStateException caused by {@link RepositoryException}
128 public static Iterable
<Node
> nodes(Node node
) {
130 return iterate(node
.getNodes());
131 } catch (RepositoryException e
) {
132 throw new IllegalStateException("Cannot get children of " + node
, e
);
137 * @return the children as a (possibly empty) {@link List}.
138 * @see Node#getNodes()
139 * @throws IllegalStateException caused by {@link RepositoryException}
141 public static List
<Node
> getNodes(Node node
) {
142 List
<Node
> nodes
= new ArrayList
<>();
144 if (node
.hasNodes()) {
145 NodeIterator nit
= node
.getNodes();
146 while (nit
.hasNext())
147 nodes
.add(nit
.nextNode());
151 } catch (RepositoryException e
) {
152 throw new IllegalStateException("Cannot get children of " + node
, e
);
157 * @return the child or <code>null</node> if not found
158 * @see Node#getNode(String)
159 * @throws IllegalStateException caused by {@link RepositoryException}
161 public static Node
getNode(Node node
, String child
) {
163 if (node
.hasNode(child
))
164 return node
.getNode(child
);
167 } catch (RepositoryException e
) {
168 throw new IllegalStateException("Cannot get child of " + node
, e
);
173 * @return the node at this path or <code>null</node> if not found
174 * @see Session#getNode(String)
175 * @throws IllegalStateException caused by {@link RepositoryException}
177 public static Node
getNode(Session session
, String path
) {
179 if (session
.nodeExists(path
))
180 return session
.getNode(path
);
183 } catch (RepositoryException e
) {
184 throw new IllegalStateException("Cannot get node " + path
, e
);
189 * @return the node with htis id or <code>null</node> if not found
190 * @see Session#getNodeByIdentifier(String)
191 * @throws IllegalStateException caused by {@link RepositoryException}
193 public static Node
getNodeById(Session session
, String id
) {
195 return session
.getNodeByIdentifier(id
);
196 } catch (ItemNotFoundException e
) {
198 } catch (RepositoryException e
) {
199 throw new IllegalStateException("Cannot get node with id " + id
, e
);
204 * Get property as {@link String}.
206 * @return the value of
207 * {@link Node#getProperty(String)}.{@link Property#getString()} or
208 * <code>null</code> if the property does not exist.
209 * @throws IllegalStateException caused by {@link RepositoryException}
211 public static String
get(Node node
, String property
) {
212 return get(node
, property
, null);
216 * Get property as a {@link String}.
218 * @return the value of
219 * {@link Node#getProperty(String)}.{@link Property#getString()} or
220 * <code>defaultValue</code> if the property does not exist.
221 * @throws IllegalStateException caused by {@link RepositoryException}
223 public static String
get(Node node
, String property
, String defaultValue
) {
225 if (node
.hasProperty(property
))
226 return node
.getProperty(property
).getString();
229 } catch (RepositoryException e
) {
230 throw new IllegalStateException("Cannot retrieve property " + property
+ " from " + node
);
235 * Get property as a {@link Value}.
237 * @return {@link Node#getProperty(String)} or <code>null</code> if the property
239 * @throws IllegalStateException caused by {@link RepositoryException}
241 public static Value
getValue(Node node
, String property
) {
243 if (node
.hasProperty(property
))
244 return node
.getProperty(property
).getValue();
247 } catch (RepositoryException e
) {
248 throw new IllegalStateException("Cannot retrieve property " + property
+ " from " + node
);
253 * Get property doing a best effort to cast it as the target object.
255 * @return the value of {@link Node#getProperty(String)} or
256 * <code>defaultValue</code> if the property does not exist.
257 * @throws IllegalArgumentException if the value could not be cast
258 * @throws IllegalStateException in case of unexpected
259 * {@link RepositoryException}
261 @SuppressWarnings("unchecked")
262 public static <T
> T
getAs(Node node
, String property
, T defaultValue
) {
264 if (node
.hasProperty(property
)) {
265 Property p
= node
.getProperty(property
);
267 switch (p
.getType()) {
268 case PropertyType
.STRING
:
269 return (T
) node
.getProperty(property
).getString();
270 case PropertyType
.DOUBLE
:
271 return (T
) (Double
) node
.getProperty(property
).getDouble();
272 case PropertyType
.LONG
:
273 return (T
) (Long
) node
.getProperty(property
).getLong();
274 case PropertyType
.BOOLEAN
:
275 return (T
) (Boolean
) node
.getProperty(property
).getBoolean();
276 case PropertyType
.DATE
:
277 return (T
) node
.getProperty(property
).getDate();
279 return (T
) node
.getProperty(property
).getString();
281 } catch (ClassCastException e
) {
282 throw new IllegalArgumentException(
283 "Cannot cast property of type " + PropertyType
.nameFromValue(p
.getType()), e
);
288 } catch (RepositoryException e
) {
289 throw new IllegalStateException("Cannot retrieve property " + property
+ " from " + node
);
293 /** Login to a JCR repository. */
294 public static Session
login(Repository repository
, String workspace
) {
296 return repository
.login(workspace
);
297 } catch (RepositoryException e
) {
298 throw new IllegalArgumentException("Cannot login to repository", e
);
302 /** Safely and silently logs out a session. */
303 public static void logout(Session session
) {
306 if (session
.isLive())
308 } catch (Exception e
) {