]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jackrabbit/src/main/java/org/argeo/jcr/JcrUtils.java
5a46a763792d3db39c55432c4e83b78de8e141cf
[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.StringTokenizer;
4
5 import javax.jcr.Node;
6 import javax.jcr.NodeIterator;
7 import javax.jcr.Property;
8 import javax.jcr.PropertyIterator;
9 import javax.jcr.RepositoryException;
10 import javax.jcr.Session;
11 import javax.jcr.Value;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.argeo.ArgeoException;
16
17 public class JcrUtils {
18 private final static Log log = LogFactory.getLog(JcrUtils.class);
19
20 public static String parentPath(String path) {
21 if (path.equals("/"))
22 throw new ArgeoException("Root path '/' has no parent path");
23 if (path.charAt(0) != '/')
24 throw new ArgeoException("Path " + path + " must start with a '/'");
25 String pathT = path;
26 if (pathT.charAt(pathT.length() - 1) == '/')
27 pathT = pathT.substring(0, pathT.length() - 2);
28
29 int index = pathT.lastIndexOf('/');
30 return pathT.substring(0, index);
31 }
32
33 public static String lastPathElement(String path) {
34 if (path.charAt(path.length() - 1) == '/')
35 throw new ArgeoException("Path " + path + " cannot end with '/'");
36 int index = path.lastIndexOf('/');
37 if (index <= 0)
38 throw new ArgeoException("Cannot find last path element for "
39 + path);
40 return path.substring(index + 1);
41 }
42
43 public static Node mkdirs(Session session, String path, String type,
44 Boolean versioning) {
45 try {
46 if (path.equals('/'))
47 return session.getRootNode();
48
49 StringTokenizer st = new StringTokenizer(path, "/");
50 StringBuffer current = new StringBuffer("/");
51 Node currentNode = session.getRootNode();
52 while (st.hasMoreTokens()) {
53 String part = st.nextToken();
54 current.append(part).append('/');
55 if (!session.itemExists(current.toString())) {
56 if (type != null)
57 currentNode = currentNode.addNode(part, type);
58 else
59 currentNode = currentNode.addNode(part);
60 if (versioning)
61 currentNode.addMixin(ArgeoJcrConstants.MIX_VERSIONABLE);
62 if (log.isTraceEnabled())
63 log.debug("Added folder " + part + " as " + current);
64 } else {
65 currentNode = (Node) session.getItem(current.toString());
66 }
67 }
68 session.save();
69 return currentNode;
70 } catch (RepositoryException e) {
71 throw new ArgeoException("Cannot mkdirs " + path, e);
72 }
73 }
74
75 /** Recursively outputs the contents of the given node. */
76 public static void debug(Node node) throws RepositoryException {
77 // First output the node path
78 log.debug(node.getPath());
79 // Skip the virtual (and large!) jcr:system subtree
80 if (node.getName().equals(ArgeoJcrConstants.JCR_SYSTEM)) {
81 return;
82 }
83
84 // Then the children nodes (recursive)
85 NodeIterator it = node.getNodes();
86 while (it.hasNext()) {
87 Node childNode = it.nextNode();
88 debug(childNode);
89 }
90
91 // Then output the properties
92 PropertyIterator properties = node.getProperties();
93 while (properties.hasNext()) {
94 Property property = properties.nextProperty();
95 if (property.getDefinition().isMultiple()) {
96 // A multi-valued property, print all values
97 Value[] values = property.getValues();
98 for (int i = 0; i < values.length; i++) {
99 log.debug(property.getPath() + "=" + values[i].getString());
100 }
101 } else {
102 // A single-valued property
103 log.debug(property.getPath() + "=" + property.getString());
104 }
105 }
106
107 }
108 }