3 import java
.util
.Calendar
;
4 import java
.util
.StringTokenizer
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.PropertyIterator
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.Value
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.ArgeoException
;
18 public class JcrUtils
{
19 private final static Log log
= LogFactory
.getLog(JcrUtils
.class);
21 public static String
parentPath(String path
) {
23 throw new ArgeoException("Root path '/' has no parent path");
24 if (path
.charAt(0) != '/')
25 throw new ArgeoException("Path " + path
+ " must start with a '/'");
27 if (pathT
.charAt(pathT
.length() - 1) == '/')
28 pathT
= pathT
.substring(0, pathT
.length() - 2);
30 int index
= pathT
.lastIndexOf('/');
31 return pathT
.substring(0, index
);
34 public static String
dateAsPath(Calendar cal
) {
35 StringBuffer buf
= new StringBuffer(11);
36 buf
.append(cal
.get(Calendar
.YEAR
));// 4
38 int month
= cal
.get(Calendar
.MONTH
) + 1;
41 buf
.append(month
);// 2
43 int day
= cal
.get(Calendar
.DAY_OF_MONTH
);
48 return buf
.toString();
52 public static String
lastPathElement(String path
) {
53 if (path
.charAt(path
.length() - 1) == '/')
54 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
55 int index
= path
.lastIndexOf('/');
57 throw new ArgeoException("Cannot find last path element for "
59 return path
.substring(index
+ 1);
62 public static Node
mkdirs(Session session
, String path
) {
63 return mkdirs(session
, path
, null, false);
66 public static Node
mkdirs(Session session
, String path
, String type
,
70 return session
.getRootNode();
72 StringTokenizer st
= new StringTokenizer(path
, "/");
73 StringBuffer current
= new StringBuffer("/");
74 Node currentNode
= session
.getRootNode();
75 while (st
.hasMoreTokens()) {
76 String part
= st
.nextToken();
77 current
.append(part
).append('/');
78 if (!session
.itemExists(current
.toString())) {
80 currentNode
= currentNode
.addNode(part
, type
);
82 currentNode
= currentNode
.addNode(part
);
84 currentNode
.addMixin(ArgeoJcrConstants
.MIX_VERSIONABLE
);
85 if (log
.isTraceEnabled())
86 log
.debug("Added folder " + part
+ " as " + current
);
88 currentNode
= (Node
) session
.getItem(current
.toString());
93 } catch (RepositoryException e
) {
94 throw new ArgeoException("Cannot mkdirs " + path
, e
);
98 /** Recursively outputs the contents of the given node. */
99 public static void debug(Node node
) throws RepositoryException
{
100 // First output the node path
101 log
.debug(node
.getPath());
102 // Skip the virtual (and large!) jcr:system subtree
103 if (node
.getName().equals(ArgeoJcrConstants
.JCR_SYSTEM
)) {
107 // Then the children nodes (recursive)
108 NodeIterator it
= node
.getNodes();
109 while (it
.hasNext()) {
110 Node childNode
= it
.nextNode();
114 // Then output the properties
115 PropertyIterator properties
= node
.getProperties();
116 while (properties
.hasNext()) {
117 Property property
= properties
.nextProperty();
118 if (property
.getDefinition().isMultiple()) {
119 // A multi-valued property, print all values
120 Value
[] values
= property
.getValues();
121 for (int i
= 0; i
< values
.length
; i
++) {
122 log
.debug(property
.getPath() + "=" + values
[i
].getString());
125 // A single-valued property
126 log
.debug(property
.getPath() + "=" + property
.getString());