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
;
13 import javax
.jcr
.query
.Query
;
14 import javax
.jcr
.query
.QueryResult
;
16 import org
.apache
.commons
.logging
.Log
;
17 import org
.apache
.commons
.logging
.LogFactory
;
18 import org
.argeo
.ArgeoException
;
20 public class JcrUtils
{
21 private final static Log log
= LogFactory
.getLog(JcrUtils
.class);
23 public static Node
querySingleNode(Query query
) {
24 NodeIterator nodeIterator
;
26 QueryResult queryResult
= query
.execute();
27 nodeIterator
= queryResult
.getNodes();
28 } catch (RepositoryException e
) {
29 throw new ArgeoException("Cannot execute query " + query
, e
);
32 if (nodeIterator
.hasNext())
33 node
= nodeIterator
.nextNode();
37 if (nodeIterator
.hasNext())
38 throw new ArgeoException("Query returned more than one node.");
42 public static String
parentPath(String path
) {
44 throw new ArgeoException("Root path '/' has no parent path");
45 if (path
.charAt(0) != '/')
46 throw new ArgeoException("Path " + path
+ " must start with a '/'");
48 if (pathT
.charAt(pathT
.length() - 1) == '/')
49 pathT
= pathT
.substring(0, pathT
.length() - 2);
51 int index
= pathT
.lastIndexOf('/');
52 return pathT
.substring(0, index
);
55 public static String
dateAsPath(Calendar cal
) {
56 StringBuffer buf
= new StringBuffer(11);
57 buf
.append(cal
.get(Calendar
.YEAR
));// 4
59 int month
= cal
.get(Calendar
.MONTH
) + 1;
62 buf
.append(month
);// 2
64 int day
= cal
.get(Calendar
.DAY_OF_MONTH
);
69 return buf
.toString();
73 public static String
lastPathElement(String path
) {
74 if (path
.charAt(path
.length() - 1) == '/')
75 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
76 int index
= path
.lastIndexOf('/');
78 throw new ArgeoException("Cannot find last path element for "
80 return path
.substring(index
+ 1);
83 public static Node
mkdirs(Session session
, String path
) {
84 return mkdirs(session
, path
, null, false);
87 public static Node
mkdirs(Session session
, String path
, String type
,
91 return session
.getRootNode();
93 StringTokenizer st
= new StringTokenizer(path
, "/");
94 StringBuffer current
= new StringBuffer("/");
95 Node currentNode
= session
.getRootNode();
96 while (st
.hasMoreTokens()) {
97 String part
= st
.nextToken();
98 current
.append(part
).append('/');
99 if (!session
.itemExists(current
.toString())) {
101 currentNode
= currentNode
.addNode(part
, type
);
103 currentNode
= currentNode
.addNode(part
);
105 currentNode
.addMixin(ArgeoJcrConstants
.MIX_VERSIONABLE
);
106 if (log
.isTraceEnabled())
107 log
.debug("Added folder " + part
+ " as " + current
);
109 currentNode
= (Node
) session
.getItem(current
.toString());
114 } catch (RepositoryException e
) {
115 throw new ArgeoException("Cannot mkdirs " + path
, e
);
119 /** Recursively outputs the contents of the given node. */
120 public static void debug(Node node
) throws RepositoryException
{
121 // First output the node path
122 log
.debug(node
.getPath());
123 // Skip the virtual (and large!) jcr:system subtree
124 if (node
.getName().equals(ArgeoJcrConstants
.JCR_SYSTEM
)) {
128 // Then the children nodes (recursive)
129 NodeIterator it
= node
.getNodes();
130 while (it
.hasNext()) {
131 Node childNode
= it
.nextNode();
135 // Then output the properties
136 PropertyIterator properties
= node
.getProperties();
137 while (properties
.hasNext()) {
138 Property property
= properties
.nextProperty();
139 if (property
.getDefinition().isMultiple()) {
140 // A multi-valued property, print all values
141 Value
[] values
= property
.getValues();
142 for (int i
= 0; i
< values
.length
; i
++) {
143 log
.debug(property
.getPath() + "=" + values
[i
].getString());
146 // A single-valued property
147 log
.debug(property
.getPath() + "=" + property
.getString());