2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.jcr
;
19 import java
.util
.Calendar
;
20 import java
.util
.StringTokenizer
;
22 import javax
.jcr
.NamespaceRegistry
;
23 import javax
.jcr
.Node
;
24 import javax
.jcr
.NodeIterator
;
25 import javax
.jcr
.Property
;
26 import javax
.jcr
.PropertyIterator
;
27 import javax
.jcr
.RepositoryException
;
28 import javax
.jcr
.Session
;
29 import javax
.jcr
.Value
;
30 import javax
.jcr
.query
.Query
;
31 import javax
.jcr
.query
.QueryResult
;
33 import org
.apache
.commons
.logging
.Log
;
34 import org
.apache
.commons
.logging
.LogFactory
;
35 import org
.argeo
.ArgeoException
;
37 /** Utility methods to simplify common JCR operations. */
38 public class JcrUtils
{
39 private final static Log log
= LogFactory
.getLog(JcrUtils
.class);
42 * Queries one single node.
44 * @return one single node or null if none was found
45 * @throws ArgeoException
46 * if more than one node was found
48 public static Node
querySingleNode(Query query
) {
49 NodeIterator nodeIterator
;
51 QueryResult queryResult
= query
.execute();
52 nodeIterator
= queryResult
.getNodes();
53 } catch (RepositoryException e
) {
54 throw new ArgeoException("Cannot execute query " + query
, e
);
57 if (nodeIterator
.hasNext())
58 node
= nodeIterator
.nextNode();
62 if (nodeIterator
.hasNext())
63 throw new ArgeoException("Query returned more than one node.");
67 /** Removes forbidden characters from a path, replacing them with '_' */
68 public static String
removeForbiddenCharacters(String str
) {
69 return str
.replace('[', '_').replace(']', '_').replace('/', '_')
74 /** Retrieves the parent path of the provided path */
75 public static String
parentPath(String path
) {
77 throw new ArgeoException("Root path '/' has no parent path");
78 if (path
.charAt(0) != '/')
79 throw new ArgeoException("Path " + path
+ " must start with a '/'");
81 if (pathT
.charAt(pathT
.length() - 1) == '/')
82 pathT
= pathT
.substring(0, pathT
.length() - 2);
84 int index
= pathT
.lastIndexOf('/');
85 return pathT
.substring(0, index
);
88 /** The provided data as a path ('/' at the end, not the beginning) */
89 public static String
dateAsPath(Calendar cal
) {
90 return dateAsPath(cal
, false);
93 /** The provided data as a path ('/' at the end, not the beginning) */
94 public static String
dateAsPath(Calendar cal
, Boolean addHour
) {
95 StringBuffer buf
= new StringBuffer(14);
96 buf
.append('Y').append(cal
.get(Calendar
.YEAR
));// 5
98 int month
= cal
.get(Calendar
.MONTH
) + 1;
102 buf
.append(month
);// 3
104 int day
= cal
.get(Calendar
.DAY_OF_MONTH
);
107 buf
.append('D').append(day
);// 3
110 int hour
= cal
.get(Calendar
.HOUR_OF_DAY
);
113 buf
.append('H').append(hour
);// 3
116 return buf
.toString();
120 public static String
hostAsPath(String host
) {
121 // TODO : inverse order of the elements (to have org/argeo/test IO
123 return host
.replace('.', '/');
126 public static String
lastPathElement(String path
) {
127 if (path
.charAt(path
.length() - 1) == '/')
128 throw new ArgeoException("Path " + path
+ " cannot end with '/'");
129 int index
= path
.lastIndexOf('/');
131 throw new ArgeoException("Cannot find last path element for "
133 return path
.substring(index
+ 1);
136 /** Creates the nodes making path, if they don't exist. */
137 public static Node
mkdirs(Session session
, String path
) {
138 return mkdirs(session
, path
, null, false);
141 /** Creates the nodes making path, if they don't exist. */
142 public static Node
mkdirs(Session session
, String path
, String type
,
143 Boolean versioning
) {
145 if (path
.equals('/'))
146 return session
.getRootNode();
148 StringTokenizer st
= new StringTokenizer(path
, "/");
149 StringBuffer current
= new StringBuffer("/");
150 Node currentNode
= session
.getRootNode();
151 while (st
.hasMoreTokens()) {
152 String part
= st
.nextToken();
153 current
.append(part
).append('/');
154 if (!session
.itemExists(current
.toString())) {
156 currentNode
= currentNode
.addNode(part
, type
);
158 currentNode
= currentNode
.addNode(part
);
160 currentNode
.addMixin(ArgeoJcrConstants
.MIX_VERSIONABLE
);
161 if (log
.isTraceEnabled())
162 log
.debug("Added folder " + part
+ " as " + current
);
164 currentNode
= (Node
) session
.getItem(current
.toString());
169 } catch (RepositoryException e
) {
170 throw new ArgeoException("Cannot mkdirs " + path
, e
);
174 public static void registerNamespaceSafely(Session session
, String prefix
,
177 registerNamespaceSafely(session
.getWorkspace()
178 .getNamespaceRegistry(), prefix
, uri
);
179 } catch (RepositoryException e
) {
180 throw new ArgeoException("Cannot find namespace registry", e
);
184 public static void registerNamespaceSafely(NamespaceRegistry nr
,
185 String prefix
, String uri
) {
187 String
[] prefixes
= nr
.getPrefixes();
188 for (String pref
: prefixes
)
189 if (pref
.equals(prefix
)) {
190 String registeredUri
= nr
.getURI(pref
);
191 if (!registeredUri
.equals(uri
))
192 throw new ArgeoException("Prefix " + pref
193 + " already registered for URI "
195 + " which is different from provided URI "
200 nr
.registerNamespace(prefix
, uri
);
201 } catch (RepositoryException e
) {
202 throw new ArgeoException("Cannot register namespace " + uri
203 + " under prefix " + prefix
, e
);
207 /** Recursively outputs the contents of the given node. */
208 public static void debug(Node node
) {
210 // First output the node path
211 log
.debug(node
.getPath());
212 // Skip the virtual (and large!) jcr:system subtree
213 if (node
.getName().equals(ArgeoJcrConstants
.JCR_SYSTEM
)) {
217 // Then the children nodes (recursive)
218 NodeIterator it
= node
.getNodes();
219 while (it
.hasNext()) {
220 Node childNode
= it
.nextNode();
224 // Then output the properties
225 PropertyIterator properties
= node
.getProperties();
226 // log.debug("Property are : ");
228 while (properties
.hasNext()) {
229 Property property
= properties
.nextProperty();
230 if (property
.getDefinition().isMultiple()) {
231 // A multi-valued property, print all values
232 Value
[] values
= property
.getValues();
233 for (int i
= 0; i
< values
.length
; i
++) {
234 log
.debug(property
.getPath() + "="
235 + values
[i
].getString());
238 // A single-valued property
239 log
.debug(property
.getPath() + "=" + property
.getString());
242 } catch (Exception e
) {
243 log
.error("Could not debug " + node
, e
);