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
hostAsPath(String host
) {
74 // TODO : inverse order of the elements (to have org/argeo/test IO
76 return host
.replace('.', '/');
79 public static String
lastPathElement(String path
) {
80 if (path
.charAt(path
.length() - 1) == '/')
81 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
82 int index
= path
.lastIndexOf('/');
84 throw new ArgeoException("Cannot find last path element for "
86 return path
.substring(index
+ 1);
89 public static Node
mkdirs(Session session
, String path
) {
90 return mkdirs(session
, path
, null, false);
93 public static Node
mkdirs(Session session
, String path
, String type
,
97 return session
.getRootNode();
99 StringTokenizer st
= new StringTokenizer(path
, "/");
100 StringBuffer current
= new StringBuffer("/");
101 Node currentNode
= session
.getRootNode();
102 while (st
.hasMoreTokens()) {
103 String part
= st
.nextToken();
104 current
.append(part
).append('/');
105 if (!session
.itemExists(current
.toString())) {
107 currentNode
= currentNode
.addNode(part
, type
);
109 currentNode
= currentNode
.addNode(part
);
111 currentNode
.addMixin(ArgeoJcrConstants
.MIX_VERSIONABLE
);
112 if (log
.isTraceEnabled())
113 log
.debug("Added folder " + part
+ " as " + current
);
115 currentNode
= (Node
) session
.getItem(current
.toString());
120 } catch (RepositoryException e
) {
121 throw new ArgeoException("Cannot mkdirs " + path
, e
);
125 /** Recursively outputs the contents of the given node. */
126 public static void debug(Node node
) throws RepositoryException
{
127 // First output the node path
128 log
.debug(node
.getPath());
129 // Skip the virtual (and large!) jcr:system subtree
130 if (node
.getName().equals(ArgeoJcrConstants
.JCR_SYSTEM
)) {
134 // Then the children nodes (recursive)
135 NodeIterator it
= node
.getNodes();
136 while (it
.hasNext()) {
137 Node childNode
= it
.nextNode();
141 // Then output the properties
142 PropertyIterator properties
= node
.getProperties();
143 //log.debug("Property are : ");
145 while (properties
.hasNext()) {
146 Property property
= properties
.nextProperty();
147 if (property
.getDefinition().isMultiple()) {
148 // A multi-valued property, print all values
149 Value
[] values
= property
.getValues();
150 for (int i
= 0; i
< values
.length
; i
++) {
151 log
.debug(property
.getPath() + "=" + values
[i
].getString());
154 // A single-valued property
155 log
.debug(property
.getPath() + "=" + property
.getString());