KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > util > DumperUtil


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13
14 package info.magnolia.cms.util;
15
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.HierarchyManager;
18
19 import java.io.PrintStream JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import javax.jcr.Node;
25 import javax.jcr.NodeIterator;
26 import javax.jcr.Property;
27 import javax.jcr.PropertyIterator;
28 import javax.jcr.PropertyType;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Session;
31 import javax.jcr.Value;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 /**
38  * Used to dump 1:1 repository content. The level defines how deep the recursion should go.
39  * @author philipp
40  * @version $Revision: 6341 $ ($Author: philipp $)
41  */

42 public class DumperUtil {
43
44     private static Logger log = LoggerFactory.getLogger(DumperUtil.class);
45
46     public static String JavaDoc dump(Content content) {
47         return dump(content, 1);
48     }
49
50     /**
51      * Used to dump into a String.
52      * @param content
53      * @param level
54      * @return
55      */

56     public static String JavaDoc dump(Content content, int level) {
57         if (content == null) {
58             return "";
59         }
60
61         StringWriter JavaDoc str = new StringWriter JavaDoc();
62         try {
63             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(str);
64             dump(content.getJCRNode(), level, writer);
65             writer.flush();
66         }
67         catch (RepositoryException e) {
68             log.error("can't dump", e);
69         }
70         return str.toString();
71     }
72
73     /**
74      * Dump to a stream.
75      * @param content
76      * @param level
77      * @param out
78      */

79     public static void dump(Content content, int level, PrintStream JavaDoc out) {
80         if (content == null) {
81             return;
82         }
83         try {
84             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(out);
85             dump(content.getJCRNode(), level, writer);
86             writer.flush();
87         }
88         catch (RepositoryException e) {
89             log.error("can't dump", e);
90         }
91     }
92
93     /**
94      * Dump this node to a stream.
95      * @param content
96      * @param out
97      */

98     public static void dump(Content content, PrintStream JavaDoc out) {
99         dump(content, 1, out);
100     }
101
102     /**
103      * Dump a JCR Node to a Writer.
104      * @param n
105      * @param level
106      * @param out
107      * @throws RepositoryException
108      */

109     public static void dump(Node n, int level, PrintWriter JavaDoc out) throws RepositoryException {
110         out.println(n.getPath());
111
112         PropertyIterator pit = n.getProperties();
113         while (pit.hasNext()) {
114             Property p = pit.nextProperty();
115             out.print(p.getPath() + "=");
116             if (p.getDefinition().isMultiple()) {
117                 Value[] values = p.getValues();
118                 for (int i = 0; i < values.length; i++) {
119                     if (i > 0) {
120                         out.println(",");
121                     }
122                     out.println(values[i].getString());
123                 }
124             }
125             else if (p.getType() == PropertyType.BINARY) {
126                 out.print("binary");
127             }
128             else {
129                 out.print(p.getString());
130             }
131             out.println();
132         }
133
134         level--;
135
136         NodeIterator nit = n.getNodes();
137         while (nit.hasNext()) {
138             Node cn = nit.nextNode();
139             if (level > 0) {
140                 dump(cn, level, out);
141             }
142             else{
143                 out.println(cn.getPath() + "[" + cn.getPrimaryNodeType().getName() + "]");
144             }
145         }
146     }
147
148     /**
149      * Dump only this JCR-Node to a writer.
150      * @param n
151      * @param out
152      * @throws RepositoryException
153      */

154     public static void dump(Node n, PrintWriter JavaDoc out) throws RepositoryException {
155         dump(n, 1, out);
156     }
157
158     public static void dumpChanges(HierarchyManager hm) {
159         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(System.out);
160         try {
161             dumpChanges(hm.getWorkspace().getSession(), writer);
162         }
163         catch (Exception JavaDoc e) {
164             log.error("can't dump", e);
165         }
166         writer.flush();
167     }
168
169     public static void dumpChanges(Session session, PrintWriter JavaDoc out) throws RepositoryException {
170         if (session.hasPendingChanges()) {
171             dumpChanges(session.getRootNode(), out);
172         }
173     }
174
175     private static void dumpChanges(Node node, PrintWriter JavaDoc out) throws RepositoryException {
176         if (node.isModified()) {
177             out.println(node.getPath() + " is modified");
178         }
179         else if (node.isNew()) {
180             out.println(node.getPath() + " is new");
181         }
182         for (Iterator JavaDoc iter = node.getNodes(); iter.hasNext();) {
183             Node child = (Node) iter.next();
184             dumpChanges(child, out);
185         }
186     }
187
188 }
189
Popular Tags