]> git.argeo.org Git - lgpl/argeo-commons.git/blob - sandbox/runtime/org.argeo.sandbox.jackrabbit/src/main/java/ThirdHop.java
Branding settings / Moved logo to RIA Core
[lgpl/argeo-commons.git] / sandbox / runtime / org.argeo.sandbox.jackrabbit / src / main / java / ThirdHop.java
1 import javax.jcr.*;
2 import org.apache.jackrabbit.core.TransientRepository;
3 import java.io.FileInputStream;
4
5 /**
6 * Third Jackrabbit example application. Imports an example XML file
7 * and outputs the contents of the entire workspace.
8 */
9 public class ThirdHop {
10
11 /** Runs the ThirdHop example. */
12 public static void main(String[] args) throws Exception {
13 // Set up a Jackrabbit repository with the specified
14 // configuration file and repository directory
15 Repository repository = new TransientRepository();
16
17 // Login to the default workspace as a dummy user
18 Session session = repository.login(
19 new SimpleCredentials("username", "password".toCharArray()));
20 try {
21 // Use the root node as a starting point
22 Node root = session.getRootNode();
23
24 // Import the XML file unless already imported
25 if (!root.hasNode("importxml")) {
26 System.out.print("Importing xml... ");
27 // Create an unstructured node under which to import the XML
28 Node node = root.addNode("importxml", "nt:unstructured");
29 // Import the file "test.xml" under the created node
30 FileInputStream xml = new FileInputStream("test.xml");
31 session.importXML(
32 "/importxml", xml, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
33 xml.close();
34 // Save the changes to the repository
35 session.save();
36 System.out.println("done.");
37 }
38
39 dump(root);
40 } finally {
41 session.logout();
42 }
43 }
44
45 /** Recursively outputs the contents of the given node. */
46 private static void dump(Node node) throws RepositoryException {
47 // First output the node path
48 System.out.println(node.getPath());
49 // Skip the virtual (and large!) jcr:system subtree
50 if (node.getName().equals("jcr:system")) {
51 return;
52 }
53
54 // Then output the properties
55 PropertyIterator properties = node.getProperties();
56 while (properties.hasNext()) {
57 Property property = properties.nextProperty();
58 if (property.getDefinition().isMultiple()) {
59 // A multi-valued property, print all values
60 Value[] values = property.getValues();
61 for (int i = 0; i < values.length; i++) {
62 System.out.println(
63 property.getPath() + " = " + values[i].getString());
64 }
65 } else {
66 // A single-valued property
67 System.out.println(
68 property.getPath() + " = " + property.getString());
69 }
70 }
71
72 // Finally output all the child nodes recursively
73 NodeIterator nodes = node.getNodes();
74 while (nodes.hasNext()) {
75 dump(nodes.nextNode());
76 }
77 }
78
79 }