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