]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jcr/JcrUtils.java
JCR XML download
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jackrabbit / src / main / java / org / argeo / jcr / JcrUtils.java
1 package org.argeo.jcr;
2
3 import java.util.Calendar;
4 import java.util.StringTokenizer;
5
6 import javax.jcr.NamespaceRegistry;
7 import javax.jcr.Node;
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;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.argeo.ArgeoException;
20
21 public class JcrUtils {
22 private final static Log log = LogFactory.getLog(JcrUtils.class);
23
24 public static Node querySingleNode(Query query) {
25 NodeIterator nodeIterator;
26 try {
27 QueryResult queryResult = query.execute();
28 nodeIterator = queryResult.getNodes();
29 } catch (RepositoryException e) {
30 throw new ArgeoException("Cannot execute query " + query, e);
31 }
32 Node node;
33 if (nodeIterator.hasNext())
34 node = nodeIterator.nextNode();
35 else
36 return null;
37
38 if (nodeIterator.hasNext())
39 throw new ArgeoException("Query returned more than one node.");
40 return node;
41 }
42
43 public static String removeForbiddenCharacters(String str) {
44 return str.replace('[', '_').replace(']', '_').replace('/', '_')
45 .replace('*', '_');
46
47 }
48
49 public static String parentPath(String path) {
50 if (path.equals("/"))
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 '/'");
54 String pathT = path;
55 if (pathT.charAt(pathT.length() - 1) == '/')
56 pathT = pathT.substring(0, pathT.length() - 2);
57
58 int index = pathT.lastIndexOf('/');
59 return pathT.substring(0, index);
60 }
61
62 public static String dateAsPath(Calendar cal) {
63 StringBuffer buf = new StringBuffer(14);
64 buf.append('Y').append(cal.get(Calendar.YEAR));// 5
65 buf.append('/');// 1
66 int month = cal.get(Calendar.MONTH) + 1;
67 buf.append('M');
68 if (month < 10)
69 buf.append(0);
70 buf.append(month);// 3
71 buf.append('/');// 1
72 int day = cal.get(Calendar.DAY_OF_MONTH);
73 if (day < 10)
74 buf.append(0);
75 buf.append('D').append(day);// 3
76 buf.append('/');// 1
77 return buf.toString();
78
79 }
80
81 public static String hostAsPath(String host) {
82 // TODO : inverse order of the elements (to have org/argeo/test IO
83 // test/argeo/org
84 return host.replace('.', '/');
85 }
86
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('/');
91 if (index < 0)
92 throw new ArgeoException("Cannot find last path element for "
93 + path);
94 return path.substring(index + 1);
95 }
96
97 public static Node mkdirs(Session session, String path) {
98 return mkdirs(session, path, null, false);
99 }
100
101 public static Node mkdirs(Session session, String path, String type,
102 Boolean versioning) {
103 try {
104 if (path.equals('/'))
105 return session.getRootNode();
106
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())) {
114 if (type != null)
115 currentNode = currentNode.addNode(part, type);
116 else
117 currentNode = currentNode.addNode(part);
118 if (versioning)
119 currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
120 if (log.isTraceEnabled())
121 log.debug("Added folder " + part + " as " + current);
122 } else {
123 currentNode = (Node) session.getItem(current.toString());
124 }
125 }
126 session.save();
127 return currentNode;
128 } catch (RepositoryException e) {
129 throw new ArgeoException("Cannot mkdirs " + path, e);
130 }
131 }
132
133 public static void registerNamespaceSafely(Session session, String prefix,
134 String uri) {
135 try {
136 registerNamespaceSafely(session.getWorkspace()
137 .getNamespaceRegistry(), prefix, uri);
138 } catch (RepositoryException e) {
139 throw new ArgeoException("Cannot find namespace registry", e);
140 }
141 }
142
143 public static void registerNamespaceSafely(NamespaceRegistry nr,
144 String prefix, String uri) {
145 try {
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 "
153 + registeredUri
154 + " which is different from provided URI "
155 + uri);
156 else
157 return;// skip
158 }
159 nr.registerNamespace(prefix, uri);
160 } catch (RepositoryException e) {
161 throw new ArgeoException("Cannot register namespace " + uri
162 + " under prefix " + prefix, e);
163 }
164 }
165
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)) {
172 return;
173 }
174
175 // Then the children nodes (recursive)
176 NodeIterator it = node.getNodes();
177 while (it.hasNext()) {
178 Node childNode = it.nextNode();
179 debug(childNode);
180 }
181
182 // Then output the properties
183 PropertyIterator properties = node.getProperties();
184 // log.debug("Property are : ");
185
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());
193 }
194 } else {
195 // A single-valued property
196 log.debug(property.getPath() + "=" + property.getString());
197 }
198 }
199
200 }
201 }