KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > refactoring > query > readers > QueryUnusedGlobalsReader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * QueryUnusedGlobalsReader.java
21  *
22  * Created on April 11, 2006, 4:00 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.xml.schema.refactoring.query.readers;
29
30 import java.text.MessageFormat JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36 import org.netbeans.api.java.project.JavaProjectConstants;
37 import org.netbeans.api.project.SourceGroup;
38 import org.netbeans.modules.xml.nbprefuse.AnalysisConstants;
39 import org.netbeans.modules.xml.refactoring.RefactoringManager;
40 import org.netbeans.modules.xml.refactoring.spi.UIHelper;
41 import org.netbeans.modules.xml.refactoring.ui.CancelSignal;
42 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
43 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
44 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
45 import org.netbeans.modules.xml.schema.model.GlobalElement;
46 import org.netbeans.modules.xml.schema.model.GlobalGroup;
47 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
48 import org.netbeans.modules.xml.schema.model.Schema;
49 import org.netbeans.modules.xml.schema.model.SchemaComponent;
50 import org.netbeans.modules.xml.schema.model.SchemaModel;
51 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
52 import org.netbeans.modules.xml.schema.model.visitor.Preview;
53 import org.netbeans.modules.xml.schema.refactoring.ui.QueryUtilities;
54 import org.netbeans.modules.xml.xam.Model;
55 import org.netbeans.modules.xml.xam.ModelSource;
56 import org.netbeans.modules.xml.xam.NamedReferenceable;
57 import org.netbeans.modules.xml.xam.Referenceable;
58 import org.openide.awt.StatusDisplayer;
59 import org.openide.filesystems.FileObject;
60 import org.openide.nodes.AbstractNode;
61 import org.openide.nodes.Children;
62 import org.openide.nodes.FilterNode;
63 import org.openide.nodes.Node;
64 import org.openide.util.NbBundle;
65
66 /**
67  *
68  * @author Jeri Lockhart
69  */

70 public class QueryUnusedGlobalsReader {
71     
72     private enum FindUsagesResult {USAGES_FOUND,
73     NO_USAGES_FOUND,
74     CANCEL_REQUESTED};
75     
76     private static final int GCT = 2;
77     private static final int GST = 5;
78     private static final int GRP = 4;
79     private static final int AT = 0;
80     private static final int ATG = 1;
81     private static final int ELE = 3;
82     
83     
84     
85     /**
86      * Return the root node of a tree containing any unused global components
87      * by component type category
88      *
89      */

90     public Node findUnusedGlobals(CancelSignal cancelSignal,
91             SchemaModel model,
92             Boolean JavaDoc excludeGEs) {
93         if (cancelSignal == null || model == null || excludeGEs == null){
94             return null;
95         }
96         
97         AbstractNode root = new AbstractNode(new Children.Array());
98         root.setDisplayName(NbBundle.getMessage(QueryUnusedGlobalsReader.class,
99                 "LBL_Unused_Global_Components"));
100         root.setIconBaseWithExtension("org/netbeans/modules/xml/schema/refactoring/resources/unused-query.PNG");//NOI18N
101
Schema schema = model.getSchema();
102                 
103         Node[] catChildren = new Node[excludeGEs.booleanValue()?5:6];
104         
105         List JavaDoc<SchemaComponent> theGlobals = new ArrayList JavaDoc<SchemaComponent>();
106         
107         Collection JavaDoc<GlobalComplexType> cts = schema.getComplexTypes();
108         Node ctCat = QueryUtilities.createCategoryNode(AnalysisConstants.GlobalTypes.COMPLEX_TYPE);
109         catChildren[GCT] = ctCat;
110         theGlobals.addAll(cts);
111         Collection JavaDoc<GlobalElement> elems = null;
112     int groupExcludedOffset = 1;
113         if (!excludeGEs){
114         groupExcludedOffset--;
115             elems = schema.getElements();
116             Node eCat = QueryUtilities.createCategoryNode(AnalysisConstants.GlobalTypes.ELEMENT);
117             catChildren[ELE] = eCat;
118             theGlobals.addAll(elems);
119         }
120         
121         Collection JavaDoc<GlobalSimpleType> sts = schema.getSimpleTypes();
122         Node stCat = QueryUtilities.createCategoryNode(AnalysisConstants.GlobalTypes.SIMPLE_TYPE);
123         catChildren[GST-groupExcludedOffset] = stCat;
124         theGlobals.addAll(sts);
125         
126         Collection JavaDoc<GlobalGroup> ggs = schema.getGroups();
127         Node gCat = QueryUtilities.createCategoryNode(AnalysisConstants.GlobalTypes.GROUP);
128         catChildren[GRP-groupExcludedOffset] = gCat;
129         theGlobals.addAll(ggs);
130         
131         Collection JavaDoc<GlobalAttribute> ats = schema.getAttributes();
132         Node atCat = QueryUtilities.createCategoryNode(AnalysisConstants.GlobalTypes.ATTRIBUTE);
133         catChildren[AT] = atCat;
134         theGlobals.addAll(ats);
135         
136         Collection JavaDoc<GlobalAttributeGroup> ags = schema.getAttributeGroups();
137         Node agCat = QueryUtilities.createCategoryNode(AnalysisConstants.GlobalTypes.ATTRIBUTE_GROUP);
138         catChildren[ATG] = agCat;
139         theGlobals.addAll(ags);
140         
141         
142         List JavaDoc<FileObject> allFiles = new ArrayList JavaDoc<FileObject>();
143         FileObject qFile = (FileObject) model.getModelSource().getLookup().lookup(FileObject.class);
144 // System.out.println("QueryUnusedGlobalsReader file added: " + qFile.getNameExt());
145
allFiles.add(qFile);
146         
147         // then its siblings in the same folder, and the schemas in subfolders
148
FileObject queryFolder = qFile.getParent();
149         getFiles(allFiles, queryFolder, null, qFile);
150         
151         
152         List JavaDoc<SourceGroup> srcGrps =
153                 QueryUtilities.getProjectSourceGroups(
154                 model,JavaProjectConstants.SOURCES_TYPE_JAVA);
155         
156         // lastly, the other files in the project
157
for(SourceGroup srcGrp : srcGrps) {
158             FileObject f = srcGrp.getRootFolder();
159             if (f != queryFolder){
160                 getFiles(allFiles, f, queryFolder, qFile);
161             }
162         }
163         findUnused(cancelSignal, theGlobals, allFiles);
164         if (cancelSignal.isCancelRequested()){
165             StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(
166                     QueryUnusedGlobalsReader.class, "LBL_Query_Cancelled"));
167             return null;
168         }
169         for (SchemaComponent sc:theGlobals){
170             createNode(sc, catChildren, groupExcludedOffset);
171         }
172         
173         
174         root.getChildren().add(catChildren);
175         StatusDisplayer.getDefault().setStatusText((theGlobals.size()==1?
176             NbBundle.getMessage(QueryUnusedGlobalsReader.class,
177                 "LBL_Found_1_Unused_Component"):
178             MessageFormat.format(
179                 NbBundle.getMessage(QueryUnusedGlobalsReader.class,
180                 "LBL_Found_Unused_Global_Components"),
181                 new Object JavaDoc[]{Integer.valueOf(theGlobals.size())})));
182         
183 // System.out.println("Global components count: " + theGlobals.size() );
184
// System.out.println("Files searched count: " + allFiles.size() );
185
//
186
// System.out.println("Time to find unused components: " +
187
// ((System.currentTimeMillis()-start)/1000f) + " seconds");//NOI18N
188

189         return root;
190     }
191     
192     ///////////////////////////////////////////////////////////////////////////
193
// private methods
194
///////////////////////////////////////////////////////////////////////////
195

196     private void findUnused(CancelSignal cancelSignal, List JavaDoc<SchemaComponent> theGlobals, List JavaDoc<FileObject> theFiles){
197         Iterator JavaDoc<FileObject> filesIt = theFiles.iterator();
198         while(filesIt.hasNext()){
199             FileObject f = filesIt.next();
200 // System.out.println("Scanning "+ f.getNameExt());
201
ModelSource modelSource =
202                     org.netbeans.modules.xml.retriever.catalog.Utilities.getModelSource(
203                     f,
204                     true); // readOnly
205

206             SchemaModel currModel = SchemaModelFactory.getDefault().getModel(modelSource);
207             if (currModel.getState() == Model.State.NOT_WELL_FORMED) {
208                 continue;
209             }
210             assert currModel != null:"Cannot get SchemaModel for " + f.getNameExt();
211             Iterator JavaDoc<SchemaComponent> scIt = theGlobals.iterator();
212 // System.out.println("Globals remaining " + theGlobals.size());
213
while(scIt.hasNext()){
214                 if (cancelSignal.isCancelRequested()){
215                     return;
216                 }
217                 SchemaComponent sc = scIt.next();
218                 Referenceable ref = Referenceable.class.cast(sc);
219 // CR check if currModel is valid
220
Preview preview =
221                         QueryUtilities.getUsagesPreview(
222                         currModel.getSchema(),
223                         NamedReferenceable.class.cast(ref));
224
225                 assert preview != null:
226                     "QueryUtilities.getUsagesPreview() returned null preview";//NOI18N
227
Map JavaDoc<SchemaComponent, List JavaDoc<SchemaComponent>> um =
228                         preview.getUsages();
229                 assert um != null:
230                     "QueryUtilities.getUsagesPreview() returned preview with null map"; //NOI18N
231

232                 if (um.size() > 0){
233 // System.out.println("Removed " + Named.class.cast(sc).getName());
234
scIt.remove();
235                 }
236 // else {
237
// System.out.println("Kept " + Named.class.cast(sc).getName());
238
// }
239
}
240         }
241     }
242     
243     
244     /**
245      * Recursive methods to get all subfolders and files
246      *
247      */

248     private void getFiles(List JavaDoc<FileObject> allFiles,
249             FileObject fobj,
250             FileObject queryFolder,
251             FileObject queryFile){
252         
253         if (fobj != null && fobj.isFolder() && fobj != queryFolder){
254             FileObject[] children = fobj.getChildren();
255             for (FileObject f:children){
256                 if (f.isData() &&
257                         f.getExt().equals(AnalysisConstants.SCHEMA_FILE_EXTENSION) &&
258                         f != queryFile) { // it's a schema dataobject
259
// System.out.println("QueryUnusedGlobalsReader file added: " + f.getNameExt());
260
allFiles.add(f);
261                 } else { // it's a folder
262
getFiles(allFiles, f, queryFolder,queryFile);
263                 }
264             }
265         }
266         
267     }
268     
269     
270     /**
271      * Creates an AbstractNode for the schema component
272      * and adds it to the children of the categoryNode
273      * If the categoryNode is null, creates it and adds it to
274      * the children of the root node
275      *
276      */

277     private void createNode(SchemaComponent sc, Node[] categories, int groupExcludedOffset){
278         if (!(sc instanceof Referenceable) || categories == null){
279             return;
280         }
281         int index = 0;
282         if (sc instanceof GlobalComplexType){
283             index = GCT;
284         } else if (sc instanceof GlobalSimpleType){
285             index = GST - groupExcludedOffset;
286         } else if (sc instanceof GlobalGroup){
287             index = GRP - groupExcludedOffset;
288         } else if (sc instanceof GlobalAttribute){
289             index = AT;
290         } else if (sc instanceof GlobalAttributeGroup){
291             index = ATG;
292         } else if (sc instanceof GlobalElement){
293             index = ELE;
294         }
295         Node displayNode = RefactoringManager.getInstance().
296                 getTargetComponentUIHelper((Referenceable)sc).getDisplayNode(sc);
297         displayNode = new FilterNode(displayNode) {
298             public String JavaDoc getHtmlDisplayName() {
299                 return null;
300             }
301         };
302         categories[index].getChildren().add(new Node[]{displayNode});
303     }
304 }
305
Popular Tags