2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 import org
.apache
.jackrabbit
.core
.TransientRepository
;
19 import java
.io
.FileInputStream
;
22 * Third Jackrabbit example application. Imports an example XML file
23 * and outputs the contents of the entire workspace.
25 public class ThirdHop
{
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();
33 // Login to the default workspace as a dummy user
34 Session session
= repository
.login(
35 new SimpleCredentials("username", "password".toCharArray()));
37 // Use the root node as a starting point
38 Node root
= session
.getRootNode();
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");
48 "/importxml", xml
, ImportUUIDBehavior
.IMPORT_UUID_CREATE_NEW
);
50 // Save the changes to the repository
52 System
.out
.println("done.");
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")) {
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
++) {
79 property
.getPath() + " = " + values
[i
].getString());
82 // A single-valued property
84 property
.getPath() + " = " + property
.getString());
88 // Finally output all the child nodes recursively
89 NodeIterator nodes
= node
.getNodes();
90 while (nodes
.hasNext()) {
91 dump(nodes
.nextNode());