View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.cms.util;
35  
36  import java.io.PrintStream;
37  import java.io.PrintWriter;
38  import java.io.StringWriter;
39  import java.util.Iterator;
40  
41  import javax.jcr.InvalidItemStateException;
42  import javax.jcr.Node;
43  import javax.jcr.NodeIterator;
44  import javax.jcr.Property;
45  import javax.jcr.PropertyIterator;
46  import javax.jcr.PropertyType;
47  import javax.jcr.RepositoryException;
48  import javax.jcr.Session;
49  import javax.jcr.Value;
50  
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * Used to dump 1:1 repository content. The level defines how deep the recursion should go.
56   */
57  public class DumperUtil {
58  
59      private static Logger log = LoggerFactory.getLogger(DumperUtil.class);
60  
61      public static String dump(Node node) {
62          return dump(node, 1);
63      }
64  
65      public static String dump(Node node, int level) {
66          if (node == null) {
67              return "";
68          }
69  
70          StringWriter str = new StringWriter();
71          try {
72              PrintWriter writer = new PrintWriter(str);
73              dump(node, level, writer);
74              writer.flush();
75          } catch (RepositoryException e) {
76              log.error("can't dump", e);
77          }
78          return str.toString();
79      }
80  
81      public static void dump(Node node, int level, PrintStream out) {
82          if (node == null) {
83              return;
84          }
85          try {
86              PrintWriter writer = new PrintWriter(out);
87              dump(node, level, writer);
88              writer.flush();
89          } catch (RepositoryException e) {
90              log.error("can't dump", e);
91          }
92      }
93  
94      /**
95       * Dump a JCR Node to a Writer.
96       */
97      public static void dump(Node n, int level, PrintWriter out) throws RepositoryException {
98          out.println(n.getPath());
99  
100         PropertyIterator pit = n.getProperties();
101         while (pit.hasNext()) {
102             try {
103                 Property p = pit.nextProperty();
104                 out.print(p.getPath() + "=");
105                 if (p.getDefinition().isMultiple()) {
106                     Value[] values = p.getValues();
107                     for (int i = 0; i < values.length; i++) {
108                         if (i > 0) {
109                             out.print(",");
110                         }
111                         out.print(values[i].getString());
112                     }
113                 } else if (p.getType() == PropertyType.BINARY) {
114                     out.print("<binary>");
115                 } else {
116                     out.print(p.getString());
117                 }
118             } catch (InvalidItemStateException e) {
119                 // somebody might have removed the item concurrently
120                 log.debug(e.getMessage(), e);
121             }
122             out.println();
123         }
124 
125         level--;
126 
127         NodeIterator nit = n.getNodes();
128         while (nit.hasNext()) {
129             Node cn = nit.nextNode();
130             if (level > 0) {
131                 dump(cn, level, out);
132             } else {
133                 out.println(cn.getPath() + "[" + cn.getPrimaryNodeType().getName() + "]");
134             }
135         }
136     }
137 
138     /**
139      * Dump only this JCR-Node to a writer.
140      */
141     public static void dump(Node n, PrintWriter out) throws RepositoryException {
142         dump(n, 1, out);
143     }
144 
145     public static void dumpChanges(Session session, PrintWriter out) throws RepositoryException {
146         if (session.hasPendingChanges()) {
147             dumpChanges(session.getRootNode(), out);
148         }
149     }
150 
151     private static void dumpChanges(Node node, PrintWriter out) throws RepositoryException {
152         if (node.isModified()) {
153             out.println(node.getPath() + " is modified");
154         } else if (node.isNew()) {
155             out.println(node.getPath() + " is new");
156         }
157         for (Iterator iter = node.getNodes(); iter.hasNext(); ) {
158             Node child = (Node) iter.next();
159             dumpChanges(child, out);
160         }
161     }
162 
163 }