]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/JcrUtils.java
Add license headers
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / JcrUtils.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.jcr;
18
19 import java.util.Calendar;
20 import java.util.StringTokenizer;
21
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;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.argeo.ArgeoException;
36
37 /** Utility methods to simplify common JCR operations. */
38 public class JcrUtils {
39 private final static Log log = LogFactory.getLog(JcrUtils.class);
40
41 /**
42 * Queries one single node.
43 *
44 * @return one single node or null if none was found
45 * @throws ArgeoException
46 * if more than one node was found
47 */
48 public static Node querySingleNode(Query query) {
49 NodeIterator nodeIterator;
50 try {
51 QueryResult queryResult = query.execute();
52 nodeIterator = queryResult.getNodes();
53 } catch (RepositoryException e) {
54 throw new ArgeoException("Cannot execute query " + query, e);
55 }
56 Node node;
57 if (nodeIterator.hasNext())
58 node = nodeIterator.nextNode();
59 else
60 return null;
61
62 if (nodeIterator.hasNext())
63 throw new ArgeoException("Query returned more than one node.");
64 return node;
65 }
66
67 /** Removes forbidden characters from a path, replacing them with '_' */
68 public static String removeForbiddenCharacters(String str) {
69 return str.replace('[', '_').replace(']', '_').replace('/', '_')
70 .replace('*', '_');
71
72 }
73
74 /** Retrieves the parent path of the provided path */
75 public static String parentPath(String path) {
76 if (path.equals("/"))
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 '/'");
80 String pathT = path;
81 if (pathT.charAt(pathT.length() - 1) == '/')
82 pathT = pathT.substring(0, pathT.length() - 2);
83
84 int index = pathT.lastIndexOf('/');
85 return pathT.substring(0, index);
86 }
87
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);
91 }
92
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
97 buf.append('/');// 1
98 int month = cal.get(Calendar.MONTH) + 1;
99 buf.append('M');
100 if (month < 10)
101 buf.append(0);
102 buf.append(month);// 3
103 buf.append('/');// 1
104 int day = cal.get(Calendar.DAY_OF_MONTH);
105 if (day < 10)
106 buf.append(0);
107 buf.append('D').append(day);// 3
108 buf.append('/');// 1
109 if (addHour) {
110 int hour = cal.get(Calendar.HOUR_OF_DAY);
111 if (hour < 10)
112 buf.append(0);
113 buf.append('H').append(hour);// 3
114 buf.append('/');// 1
115 }
116 return buf.toString();
117
118 }
119
120 public static String hostAsPath(String host) {
121 // TODO : inverse order of the elements (to have org/argeo/test IO
122 // test/argeo/org
123 return host.replace('.', '/');
124 }
125
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('/');
130 if (index < 0)
131 throw new ArgeoException("Cannot find last path element for "
132 + path);
133 return path.substring(index + 1);
134 }
135
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);
139 }
140
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) {
144 try {
145 if (path.equals('/'))
146 return session.getRootNode();
147
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())) {
155 if (type != null)
156 currentNode = currentNode.addNode(part, type);
157 else
158 currentNode = currentNode.addNode(part);
159 if (versioning)
160 currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
161 if (log.isTraceEnabled())
162 log.debug("Added folder " + part + " as " + current);
163 } else {
164 currentNode = (Node) session.getItem(current.toString());
165 }
166 }
167 session.save();
168 return currentNode;
169 } catch (RepositoryException e) {
170 throw new ArgeoException("Cannot mkdirs " + path, e);
171 }
172 }
173
174 public static void registerNamespaceSafely(Session session, String prefix,
175 String uri) {
176 try {
177 registerNamespaceSafely(session.getWorkspace()
178 .getNamespaceRegistry(), prefix, uri);
179 } catch (RepositoryException e) {
180 throw new ArgeoException("Cannot find namespace registry", e);
181 }
182 }
183
184 public static void registerNamespaceSafely(NamespaceRegistry nr,
185 String prefix, String uri) {
186 try {
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 "
194 + registeredUri
195 + " which is different from provided URI "
196 + uri);
197 else
198 return;// skip
199 }
200 nr.registerNamespace(prefix, uri);
201 } catch (RepositoryException e) {
202 throw new ArgeoException("Cannot register namespace " + uri
203 + " under prefix " + prefix, e);
204 }
205 }
206
207 /** Recursively outputs the contents of the given node. */
208 public static void debug(Node node) {
209 try {
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)) {
214 return;
215 }
216
217 // Then the children nodes (recursive)
218 NodeIterator it = node.getNodes();
219 while (it.hasNext()) {
220 Node childNode = it.nextNode();
221 debug(childNode);
222 }
223
224 // Then output the properties
225 PropertyIterator properties = node.getProperties();
226 // log.debug("Property are : ");
227
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());
236 }
237 } else {
238 // A single-valued property
239 log.debug(property.getPath() + "=" + property.getString());
240 }
241 }
242 } catch (Exception e) {
243 log.error("Could not debug " + node, e);
244 }
245
246 }
247 }