2 import org
.apache
.jackrabbit
.core
.TransientRepository
;
3 import java
.io
.FileInputStream
;
6 * Third Jackrabbit example application. Imports an example XML file
7 * and outputs the contents of the entire workspace.
9 public class ThirdHop
{
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();
17 // Login to the default workspace as a dummy user
18 Session session
= repository
.login(
19 new SimpleCredentials("username", "password".toCharArray()));
21 // Use the root node as a starting point
22 Node root
= session
.getRootNode();
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");
32 "/importxml", xml
, ImportUUIDBehavior
.IMPORT_UUID_CREATE_NEW
);
34 // Save the changes to the repository
36 System
.out
.println("done.");
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")) {
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
++) {
63 property
.getPath() + " = " + values
[i
].getString());
66 // A single-valued property
68 property
.getPath() + " = " + property
.getString());
72 // Finally output all the child nodes recursively
73 NodeIterator nodes
= node
.getNodes();
74 while (nodes
.hasNext()) {
75 dump(nodes
.nextNode());