1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.util;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.HierarchyManager;
38
39 import java.io.PrintStream;
40 import java.io.PrintWriter;
41 import java.io.StringWriter;
42 import java.util.Iterator;
43
44 import javax.jcr.InvalidItemStateException;
45 import javax.jcr.Node;
46 import javax.jcr.NodeIterator;
47 import javax.jcr.Property;
48 import javax.jcr.PropertyIterator;
49 import javax.jcr.PropertyType;
50 import javax.jcr.RepositoryException;
51 import javax.jcr.Session;
52 import javax.jcr.Value;
53
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63 public class DumperUtil {
64
65 private static Logger log = LoggerFactory.getLogger(DumperUtil.class);
66
67
68
69
70 public static String dump(Content content) {
71 return dump(content.getJCRNode(), 1);
72 }
73
74 public static String dump(Node node) {
75 return dump(node, 1);
76 }
77
78 public static String dump(Node node, int level) {
79 if (node == null) {
80 return "";
81 }
82
83 StringWriter str = new StringWriter();
84 try {
85 PrintWriter writer = new PrintWriter(str);
86 dump(node, level, writer);
87 writer.flush();
88 } catch (RepositoryException e) {
89 log.error("can't dump", e);
90 }
91 return str.toString();
92 }
93
94
95
96
97
98
99
100
101
102 public static String dump(Content content, int level) {
103 return dump(content.getJCRNode(), level);
104 }
105
106
107
108
109
110
111
112
113
114 public static void dump(Content content, int level, PrintStream out) {
115 dump(content.getJCRNode(), level, out);
116 }
117
118 public static void dump(Node node, int level, PrintStream out) {
119 if (node == null) {
120 return;
121 }
122 try {
123 PrintWriter writer = new PrintWriter(out);
124 dump(node, level, writer);
125 writer.flush();
126 } catch (RepositoryException e) {
127 log.error("can't dump", e);
128 }
129 }
130
131
132
133
134
135
136
137
138 public static void dump(Content content, PrintStream out) {
139 dump(content, 1, out);
140 }
141
142
143
144
145
146
147
148
149
150 public static void dump(Node n, int level, PrintWriter out) throws RepositoryException {
151 out.println(n.getPath());
152
153 PropertyIterator pit = n.getProperties();
154 while (pit.hasNext()) {
155 try {
156 Property p = pit.nextProperty();
157 out.print(p.getPath() + "=");
158 if (p.getDefinition().isMultiple()) {
159 Value[] values = p.getValues();
160 for (int i = 0; i < values.length; i++) {
161 if (i > 0) {
162 out.println(",");
163 }
164 out.println(values[i].getString());
165 }
166 } else if (p.getType() == PropertyType.BINARY) {
167 out.print("binary");
168 } else {
169 out.print(p.getString());
170 }
171 } catch (InvalidItemStateException e) {
172
173 log.debug(e.getMessage(), e);
174 }
175 out.println();
176 }
177
178 level--;
179
180 NodeIterator nit = n.getNodes();
181 while (nit.hasNext()) {
182 Node cn = nit.nextNode();
183 if (level > 0) {
184 dump(cn, level, out);
185 }
186 else {
187 out.println(cn.getPath() + "[" + cn.getPrimaryNodeType().getName() + "]");
188 }
189 }
190 }
191
192
193
194
195
196
197
198
199 public static void dump(Node n, PrintWriter out) throws RepositoryException {
200 dump(n, 1, out);
201 }
202
203 public static void dumpChanges(HierarchyManager hm) {
204 PrintWriter writer = new PrintWriter(System.out);
205 try {
206 dumpChanges(hm.getWorkspace().getSession(), writer);
207 } catch (Exception e) {
208 log.error("can't dump", e);
209 }
210 writer.flush();
211 }
212
213 public static void dumpChanges(Session session, PrintWriter out) throws RepositoryException {
214 if (session.hasPendingChanges()) {
215 dumpChanges(session.getRootNode(), out);
216 }
217 }
218
219 private static void dumpChanges(Node node, PrintWriter out) throws RepositoryException {
220 if (node.isModified()) {
221 out.println(node.getPath() + " is modified");
222 }
223 else if (node.isNew()) {
224 out.println(node.getPath() + " is new");
225 }
226 for (Iterator iter = node.getNodes(); iter.hasNext();) {
227 Node child = (Node) iter.next();
228 dumpChanges(child, out);
229 }
230 }
231
232 }