3 import java
.util
.StringTokenizer
;
6 import javax
.jcr
.NodeIterator
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.PropertyIterator
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.Session
;
11 import javax
.jcr
.Value
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.ArgeoException
;
17 public class JcrUtils
{
18 private final static Log log
= LogFactory
.getLog(JcrUtils
.class);
20 public static String
parentPath(String path
) {
22 throw new ArgeoException("Root path '/' has no parent path");
23 if (path
.charAt(0) != '/')
24 throw new ArgeoException("Path " + path
+ " must start with a '/'");
26 if (pathT
.charAt(pathT
.length() - 1) == '/')
27 pathT
= pathT
.substring(0, pathT
.length() - 2);
29 int index
= pathT
.lastIndexOf('/');
30 return pathT
.substring(0, index
);
33 public static String
lastPathElement(String path
) {
34 if (path
.charAt(path
.length() - 1) == '/')
35 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
36 int index
= path
.lastIndexOf('/');
38 throw new ArgeoException("Cannot find last path element for "
40 return path
.substring(index
+ 1);
43 public static Node
mkdirs(Session session
, String path
, String type
,
47 return session
.getRootNode();
49 StringTokenizer st
= new StringTokenizer(path
, "/");
50 StringBuffer current
= new StringBuffer("/");
51 Node currentNode
= session
.getRootNode();
52 while (st
.hasMoreTokens()) {
53 String part
= st
.nextToken();
54 current
.append(part
).append('/');
55 if (!session
.itemExists(current
.toString())) {
57 currentNode
= currentNode
.addNode(part
, type
);
59 currentNode
= currentNode
.addNode(part
);
61 currentNode
.addMixin(ArgeoJcrConstants
.MIX_VERSIONABLE
);
62 if (log
.isTraceEnabled())
63 log
.debug("Added folder " + part
+ " as " + current
);
65 currentNode
= (Node
) session
.getItem(current
.toString());
70 } catch (RepositoryException e
) {
71 throw new ArgeoException("Cannot mkdirs " + path
, e
);
75 /** Recursively outputs the contents of the given node. */
76 public static void debug(Node node
) throws RepositoryException
{
77 // First output the node path
78 log
.debug(node
.getPath());
79 // Skip the virtual (and large!) jcr:system subtree
80 if (node
.getName().equals(ArgeoJcrConstants
.JCR_SYSTEM
)) {
84 // Then the children nodes (recursive)
85 NodeIterator it
= node
.getNodes();
86 while (it
.hasNext()) {
87 Node childNode
= it
.nextNode();
91 // Then output the properties
92 PropertyIterator properties
= node
.getProperties();
93 while (properties
.hasNext()) {
94 Property property
= properties
.nextProperty();
95 if (property
.getDefinition().isMultiple()) {
96 // A multi-valued property, print all values
97 Value
[] values
= property
.getValues();
98 for (int i
= 0; i
< values
.length
; i
++) {
99 log
.debug(property
.getPath() + "=" + values
[i
].getString());
102 // A single-valued property
103 log
.debug(property
.getPath() + "=" + property
.getString());