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
removeForbiddenCharacters(String str
){
43 return str
.replace('[', '_').replace(']', '_')
44 .replace('/', '_').replace('*', '_');
48 public static String
parentPath(String path
) {
50 throw new ArgeoException("Root path '/' has no parent path");
51 if (path
.charAt(0) != '/')
52 throw new ArgeoException("Path " + path
+ " must start with a '/'");
54 if (pathT
.charAt(pathT
.length() - 1) == '/')
55 pathT
= pathT
.substring(0, pathT
.length() - 2);
57 int index
= pathT
.lastIndexOf('/');
58 return pathT
.substring(0, index
);
61 public static String
dateAsPath(Calendar cal
) {
62 StringBuffer buf
= new StringBuffer(11);
63 buf
.append(cal
.get(Calendar
.YEAR
));// 4
65 int month
= cal
.get(Calendar
.MONTH
) + 1;
68 buf
.append(month
);// 2
70 int day
= cal
.get(Calendar
.DAY_OF_MONTH
);
75 return buf
.toString();
79 public static String
hostAsPath(String host
) {
80 // TODO : inverse order of the elements (to have org/argeo/test IO
82 return host
.replace('.', '/');
85 public static String
lastPathElement(String path
) {
86 if (path
.charAt(path
.length() - 1) == '/')
87 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
88 int index
= path
.lastIndexOf('/');
90 throw new ArgeoException("Cannot find last path element for "
92 return path
.substring(index
+ 1);
95 public static Node
mkdirs(Session session
, String path
) {
96 return mkdirs(session
, path
, null, false);
99 public static Node
mkdirs(Session session
, String path
, String type
,
100 Boolean versioning
) {
102 if (path
.equals('/'))
103 return session
.getRootNode();
105 StringTokenizer st
= new StringTokenizer(path
, "/");
106 StringBuffer current
= new StringBuffer("/");
107 Node currentNode
= session
.getRootNode();
108 while (st
.hasMoreTokens()) {
109 String part
= st
.nextToken();
110 current
.append(part
).append('/');
111 if (!session
.itemExists(current
.toString())) {
113 currentNode
= currentNode
.addNode(part
, type
);
115 currentNode
= currentNode
.addNode(part
);
117 currentNode
.addMixin(ArgeoJcrConstants
.MIX_VERSIONABLE
);
118 if (log
.isTraceEnabled())
119 log
.debug("Added folder " + part
+ " as " + current
);
121 currentNode
= (Node
) session
.getItem(current
.toString());
126 } catch (RepositoryException e
) {
127 throw new ArgeoException("Cannot mkdirs " + path
, e
);
131 /** Recursively outputs the contents of the given node. */
132 public static void debug(Node node
) throws RepositoryException
{
133 // First output the node path
134 log
.debug(node
.getPath());
135 // Skip the virtual (and large!) jcr:system subtree
136 if (node
.getName().equals(ArgeoJcrConstants
.JCR_SYSTEM
)) {
140 // Then the children nodes (recursive)
141 NodeIterator it
= node
.getNodes();
142 while (it
.hasNext()) {
143 Node childNode
= it
.nextNode();
147 // Then output the properties
148 PropertyIterator properties
= node
.getProperties();
149 //log.debug("Property are : ");
151 while (properties
.hasNext()) {
152 Property property
= properties
.nextProperty();
153 if (property
.getDefinition().isMultiple()) {
154 // A multi-valued property, print all values
155 Value
[] values
= property
.getValues();
156 for (int i
= 0; i
< values
.length
; i
++) {
157 log
.debug(property
.getPath() + "=" + values
[i
].getString());
160 // A single-valued property
161 log
.debug(property
.getPath() + "=" + property
.getString());