3 import java
.util
.Calendar
;
4 import java
.util
.StringTokenizer
;
6 import javax
.jcr
.NamespaceRegistry
;
8 import javax
.jcr
.NodeIterator
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.PropertyIterator
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
13 import javax
.jcr
.Value
;
14 import javax
.jcr
.query
.Query
;
15 import javax
.jcr
.query
.QueryResult
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.ArgeoException
;
21 public class JcrUtils
{
22 private final static Log log
= LogFactory
.getLog(JcrUtils
.class);
24 public static Node
querySingleNode(Query query
) {
25 NodeIterator nodeIterator
;
27 QueryResult queryResult
= query
.execute();
28 nodeIterator
= queryResult
.getNodes();
29 } catch (RepositoryException e
) {
30 throw new ArgeoException("Cannot execute query " + query
, e
);
33 if (nodeIterator
.hasNext())
34 node
= nodeIterator
.nextNode();
38 if (nodeIterator
.hasNext())
39 throw new ArgeoException("Query returned more than one node.");
43 public static String
removeForbiddenCharacters(String str
) {
44 return str
.replace('[', '_').replace(']', '_').replace('/', '_')
49 public static String
parentPath(String path
) {
51 throw new ArgeoException("Root path '/' has no parent path");
52 if (path
.charAt(0) != '/')
53 throw new ArgeoException("Path " + path
+ " must start with a '/'");
55 if (pathT
.charAt(pathT
.length() - 1) == '/')
56 pathT
= pathT
.substring(0, pathT
.length() - 2);
58 int index
= pathT
.lastIndexOf('/');
59 return pathT
.substring(0, index
);
62 public static String
dateAsPath(Calendar cal
) {
63 StringBuffer buf
= new StringBuffer(14);
64 buf
.append('Y').append(cal
.get(Calendar
.YEAR
));// 5
66 int month
= cal
.get(Calendar
.MONTH
) + 1;
70 buf
.append(month
);// 3
72 int day
= cal
.get(Calendar
.DAY_OF_MONTH
);
75 buf
.append('D').append(day
);// 3
77 return buf
.toString();
81 public static String
hostAsPath(String host
) {
82 // TODO : inverse order of the elements (to have org/argeo/test IO
84 return host
.replace('.', '/');
87 public static String
lastPathElement(String path
) {
88 if (path
.charAt(path
.length() - 1) == '/')
89 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
90 int index
= path
.lastIndexOf('/');
92 throw new ArgeoException("Cannot find last path element for "
94 return path
.substring(index
+ 1);
97 public static Node
mkdirs(Session session
, String path
) {
98 return mkdirs(session
, path
, null, false);
101 public static Node
mkdirs(Session session
, String path
, String type
,
102 Boolean versioning
) {
104 if (path
.equals('/'))
105 return session
.getRootNode();
107 StringTokenizer st
= new StringTokenizer(path
, "/");
108 StringBuffer current
= new StringBuffer("/");
109 Node currentNode
= session
.getRootNode();
110 while (st
.hasMoreTokens()) {
111 String part
= st
.nextToken();
112 current
.append(part
).append('/');
113 if (!session
.itemExists(current
.toString())) {
115 currentNode
= currentNode
.addNode(part
, type
);
117 currentNode
= currentNode
.addNode(part
);
119 currentNode
.addMixin(ArgeoJcrConstants
.MIX_VERSIONABLE
);
120 if (log
.isTraceEnabled())
121 log
.debug("Added folder " + part
+ " as " + current
);
123 currentNode
= (Node
) session
.getItem(current
.toString());
128 } catch (RepositoryException e
) {
129 throw new ArgeoException("Cannot mkdirs " + path
, e
);
133 public static void registerNamespaceSafely(Session session
, String prefix
,
136 registerNamespaceSafely(session
.getWorkspace()
137 .getNamespaceRegistry(), prefix
, uri
);
138 } catch (RepositoryException e
) {
139 throw new ArgeoException("Cannot find namespace registry", e
);
143 public static void registerNamespaceSafely(NamespaceRegistry nr
,
144 String prefix
, String uri
) {
146 String
[] prefixes
= nr
.getPrefixes();
147 for (String pref
: prefixes
)
148 if (pref
.equals(prefix
)) {
149 String registeredUri
= nr
.getURI(pref
);
150 if (!registeredUri
.equals(uri
))
151 throw new ArgeoException("Prefix " + pref
152 + " already registered for URI "
154 + " which is different from provided URI "
159 nr
.registerNamespace(prefix
, uri
);
160 } catch (RepositoryException e
) {
161 throw new ArgeoException("Cannot register namespace " + uri
162 + " under prefix " + prefix
, e
);
166 /** Recursively outputs the contents of the given node. */
167 public static void debug(Node node
) throws RepositoryException
{
168 // First output the node path
169 log
.debug(node
.getPath());
170 // Skip the virtual (and large!) jcr:system subtree
171 if (node
.getName().equals(ArgeoJcrConstants
.JCR_SYSTEM
)) {
175 // Then the children nodes (recursive)
176 NodeIterator it
= node
.getNodes();
177 while (it
.hasNext()) {
178 Node childNode
= it
.nextNode();
182 // Then output the properties
183 PropertyIterator properties
= node
.getProperties();
184 // log.debug("Property are : ");
186 while (properties
.hasNext()) {
187 Property property
= properties
.nextProperty();
188 if (property
.getDefinition().isMultiple()) {
189 // A multi-valued property, print all values
190 Value
[] values
= property
.getValues();
191 for (int i
= 0; i
< values
.length
; i
++) {
192 log
.debug(property
.getPath() + "=" + values
[i
].getString());
195 // A single-valued property
196 log
.debug(property
.getPath() + "=" + property
.getString());