View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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 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   * Used to dump 1:1 repository content. The level defines how deep the recursion should go.
59   * 
60   * @author philipp
61   * @version $Revision$ ($Author$)
62   */
63  public class DumperUtil {
64  
65      private static Logger log = LoggerFactory.getLogger(DumperUtil.class);
66  
67      /**
68       * @deprecated since 5.2.1. Use {@link #dump(Node)} instead.
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       * Used to dump into a String.
96       * 
97       * @param content
98       * @param level
99       * @return
100      * @deprecated since 5.2.1. Use {@link #dump(Node, int)} instead.
101      */
102     public static String dump(Content content, int level) {
103         return dump(content.getJCRNode(), level);
104     }
105 
106     /**
107      * Dump to a stream.
108      * 
109      * @param content
110      * @param level
111      * @param out
112      * @deprecated since 5.2.1. Use {@link #dump(Node, int, PrintStream)} instead.
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      * Dump this node to a stream.
133      * 
134      * @param content
135      * @param out
136      * @deprecated since 5.2.1. Use {@link #dump(Node, PrintWriter)} instead.
137      */
138     public static void dump(Content content, PrintStream out) {
139         dump(content, 1, out);
140     }
141 
142     /**
143      * Dump a JCR Node to a Writer.
144      * 
145      * @param n
146      * @param level
147      * @param out
148      * @throws RepositoryException
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                 // somebody might have removed the item concurrently
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      * Dump only this JCR-Node to a writer.
194      * 
195      * @param n
196      * @param out
197      * @throws RepositoryException
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 }