KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > debug > NodeStoreInspector


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util.debug;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.service.cmr.repository.ChildAssociationRef;
24 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
25 import org.alfresco.service.cmr.repository.AssociationRef;
26 import org.alfresco.service.cmr.repository.NodeRef;
27 import org.alfresco.service.cmr.repository.NodeService;
28 import org.alfresco.service.cmr.repository.StoreRef;
29 import org.alfresco.service.namespace.QName;
30 import org.alfresco.service.namespace.RegexQNamePattern;
31
32 /**
33  * Debug class that has methods to inspect the contents of a node store.
34  *
35  * @author Roy Wetherall
36  */

37 public class NodeStoreInspector
38 {
39     /**
40      * Dumps the contents of a store to a string.
41      *
42      * @param nodeService the node service
43      * @param storeRef the store reference
44      * @return string containing textual representation of the contents of the store
45      */

46     public static String JavaDoc dumpNodeStore(NodeService nodeService, StoreRef storeRef)
47     {
48         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
49         
50         if (nodeService.exists(storeRef) == true)
51         {
52             NodeRef rootNode = nodeService.getRootNode(storeRef);
53             builder.append(outputNode(0, nodeService, rootNode));
54         }
55         else
56         {
57             builder.
58                 append("The store ").
59                 append(storeRef.toString()).
60                 append(" does not exist.");
61         }
62         
63         return builder.toString();
64     }
65
66     /**
67      * Dumps the contents of a node
68      *
69      * @param nodeService
70      * @param nodeRef
71      * @return
72      */

73     public static String JavaDoc dumpNode(NodeService nodeService, NodeRef nodeRef)
74     {
75         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
76         
77         if (nodeService.exists(nodeRef) == true)
78         {
79             builder.append(outputNode(0, nodeService, nodeRef));
80         }
81         else
82         {
83             builder.
84                 append("The node ").
85                 append(nodeRef.toString()).
86                 append(" does not exist.");
87         }
88         
89         return builder.toString();
90     }
91     
92     /**
93      * Output the node
94      *
95      * @param iIndent
96      * @param nodeService
97      * @param nodeRef
98      * @return
99      */

100     private static String JavaDoc outputNode(int iIndent, NodeService nodeService, NodeRef nodeRef)
101     {
102         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
103         
104         try
105         {
106             QName nodeType = nodeService.getType(nodeRef);
107             builder.
108                 append(getIndent(iIndent)).
109                 append("node: ").
110                 append(nodeRef.getId()).
111                 append(" (").
112                 append(nodeType.getLocalName());
113             
114             Collection JavaDoc<QName> aspects = nodeService.getAspects(nodeRef);
115             for (QName aspect : aspects)
116             {
117                 builder.
118                     append(", ").
119                     append(aspect.getLocalName());
120             }
121             
122             builder.append(")\n");
123         
124             Map JavaDoc<QName, Serializable JavaDoc> props = nodeService.getProperties(nodeRef);
125             for (QName name : props.keySet())
126             {
127                 String JavaDoc valueAsString = "null";
128                 Serializable JavaDoc value = props.get(name);
129                 if (value != null)
130                 {
131                     valueAsString = value.toString();
132                 }
133                 
134                 builder.
135                     append(getIndent(iIndent+1)).
136                     append("@").
137                     append(name.getLocalName()).
138                     append(" = ").
139                     append(valueAsString).
140                     append("\n");
141                 
142             }
143
144             Collection JavaDoc<ChildAssociationRef> childAssocRefs = nodeService.getChildAssocs(nodeRef);
145             for (ChildAssociationRef childAssocRef : childAssocRefs)
146             {
147                 builder.
148                     append(getIndent(iIndent+1)).
149                     append("-> ").
150                     append(childAssocRef.getQName().toString()).
151                     append(" (").
152                     append(childAssocRef.getQName().toString()).
153                     append(")\n");
154                 
155                 builder.append(outputNode(iIndent+2, nodeService, childAssocRef.getChildRef()));
156             }
157
158             Collection JavaDoc<AssociationRef> assocRefs = nodeService.getTargetAssocs(nodeRef, RegexQNamePattern.MATCH_ALL);
159             for (AssociationRef assocRef : assocRefs)
160             {
161                 builder.
162                     append(getIndent(iIndent+1)).
163                     append("-> associated to ").
164                     append(assocRef.getTargetRef().getId()).
165                     append("\n");
166             }
167         }
168         catch (InvalidNodeRefException invalidNode)
169         {
170             invalidNode.printStackTrace();
171         }
172         
173         return builder.toString();
174     }
175     
176     /**
177      * Get the indent
178      *
179      * @param iIndent the indent value
180      * @return the indent string
181      */

182     private static String JavaDoc getIndent(int iIndent)
183     {
184         StringBuilder JavaDoc builder = new StringBuilder JavaDoc(iIndent*3);
185         for (int i = 0; i < iIndent; i++)
186         {
187             builder.append(" ");
188         }
189         return builder.toString();
190     }
191
192 }
193
Popular Tags