1 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 ; 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.util.Iterator ; 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 42 public class DumperUtil { 43 44 private static Logger log = LoggerFactory.getLogger(DumperUtil.class); 45 46 public static String dump(Content content) { 47 return dump(content, 1); 48 } 49 50 56 public static String dump(Content content, int level) { 57 if (content == null) { 58 return ""; 59 } 60 61 StringWriter str = new StringWriter (); 62 try { 63 PrintWriter writer = new PrintWriter (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 79 public static void dump(Content content, int level, PrintStream out) { 80 if (content == null) { 81 return; 82 } 83 try { 84 PrintWriter writer = new PrintWriter (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 98 public static void dump(Content content, PrintStream out) { 99 dump(content, 1, out); 100 } 101 102 109 public static void dump(Node n, int level, PrintWriter 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 154 public static void dump(Node n, PrintWriter out) throws RepositoryException { 155 dump(n, 1, out); 156 } 157 158 public static void dumpChanges(HierarchyManager hm) { 159 PrintWriter writer = new PrintWriter (System.out); 160 try { 161 dumpChanges(hm.getWorkspace().getSession(), writer); 162 } 163 catch (Exception e) { 164 log.error("can't dump", e); 165 } 166 writer.flush(); 167 } 168 169 public static void dumpChanges(Session session, PrintWriter out) throws RepositoryException { 170 if (session.hasPendingChanges()) { 171 dumpChanges(session.getRootNode(), out); 172 } 173 } 174 175 private static void dumpChanges(Node node, PrintWriter 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 iter = node.getNodes(); iter.hasNext();) { 183 Node child = (Node) iter.next(); 184 dumpChanges(child, out); 185 } 186 } 187 188 } 189 | Popular Tags |