KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > hints > infrastructure > RulesManager


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.hints.infrastructure;
21
22 import com.sun.source.tree.Tree;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
34 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
35 import javax.swing.tree.DefaultTreeModel JavaDoc;
36 import javax.swing.tree.DefaultTreeModel JavaDoc;
37 import javax.swing.tree.DefaultTreeModel JavaDoc;
38 import javax.swing.tree.TreeModel JavaDoc;
39 import org.netbeans.modules.java.hints.Pair;
40 import org.netbeans.modules.java.hints.spi.ErrorRule;
41 import org.netbeans.modules.java.hints.spi.Rule;
42 import org.netbeans.modules.java.hints.spi.TreeRule;
43 import org.openide.cookies.InstanceCookie;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileSystem;
46 import org.openide.filesystems.Repository;
47 import org.openide.loaders.DataObject;
48 import org.openide.loaders.DataObjectNotFoundException;
49
50 /** Manages rules read from the system filesystem.
51  *
52  * @author Petr Hrebejk
53  */

54 public class RulesManager {
55
56     // The logger
57
public static Logger JavaDoc LOG = Logger.getLogger("org.netbeans.modules.java.hints"); // NOI18N
58

59     // Extensions of files
60
private static final String JavaDoc INSTANCE_EXT = ".instance";
61
62     private static final String JavaDoc RULES_FOLDER = "org-netbeans-modules-java-hints/rules/"; // NOI18N
63
private static final String JavaDoc ERRORS = "errors"; // NOI18N
64
private static final String JavaDoc HINTS = "hints"; // NOI18N
65
private static final String JavaDoc SUGGESTIONS = "suggestions"; // NOI18N
66

67     // Maps of registered rules
68
private static Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> errors = new HashMap JavaDoc<String JavaDoc, List JavaDoc<ErrorRule>>();
69     private static Map JavaDoc<Tree.Kind,List JavaDoc<TreeRule>> hints = new HashMap JavaDoc<Tree.Kind,List JavaDoc<TreeRule>>();
70     private static Map JavaDoc<Tree.Kind,List JavaDoc<TreeRule>> suggestions = new HashMap JavaDoc<Tree.Kind, List JavaDoc<TreeRule>>();
71
72     // Tree models for the settings GUI
73
private static TreeModel JavaDoc errorsTreeModel;
74     private static TreeModel JavaDoc hintsTreeModel;
75     private static TreeModel JavaDoc suggestionsTreeModel;
76
77     private static RulesManager INSTANCE;
78
79     private RulesManager() {
80         // XXX Start listening on the rules forder. To handle module set changes.
81
initErrors();
82         initHints();
83         initSuggestions();
84     }
85
86     public static synchronized RulesManager getInstance() {
87         if ( INSTANCE == null ) {
88             INSTANCE = new RulesManager();
89         }
90         return INSTANCE;
91     }
92
93     public Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> getErrors() {
94         return errors;
95     }
96
97     public Map JavaDoc<Tree.Kind,List JavaDoc<TreeRule>> getHints() {
98         return hints;
99     }
100
101     public Map JavaDoc<Tree.Kind,List JavaDoc<TreeRule>> getSuggestions() {
102         return suggestions;
103     }
104
105     public TreeModel JavaDoc getErrorsTreeModel() {
106         return errorsTreeModel;
107     }
108
109     public TreeModel JavaDoc getHintsTreeModel() {
110         return hintsTreeModel;
111     }
112
113     public TreeModel JavaDoc getSuggestionsTreeModel() {
114         return suggestionsTreeModel;
115     }
116
117     // Private methods ---------------------------------------------------------
118

119     private static void initErrors() {
120         DefaultMutableTreeNode JavaDoc rootNode = new DefaultMutableTreeNode JavaDoc();
121         errorsTreeModel = new DefaultTreeModel JavaDoc( rootNode );
122         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
123         FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + ERRORS );
124         List JavaDoc<Pair<Rule,FileObject>> rules = readRules( folder );
125         categorizeErrorRules(rules, errors, folder, rootNode);
126     }
127
128     private static void initHints() {
129         DefaultMutableTreeNode JavaDoc rootNode = new DefaultMutableTreeNode JavaDoc();
130         hintsTreeModel = new DefaultTreeModel JavaDoc( rootNode );
131         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
132         FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + HINTS );
133         List JavaDoc<Pair<Rule,FileObject>> rules = readRules(folder);
134         categorizeTreeRules( rules, hints, folder, rootNode );
135     }
136
137
138     private static void initSuggestions() {
139         DefaultMutableTreeNode JavaDoc rootNode = new DefaultMutableTreeNode JavaDoc();
140         suggestionsTreeModel = new DefaultTreeModel JavaDoc( rootNode );
141         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
142         FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + SUGGESTIONS );
143         List JavaDoc<Pair<Rule,FileObject>> rules = readRules(folder);
144         categorizeTreeRules(rules, suggestions, folder, rootNode);
145     }
146
147     /** Read rules from system filesystem */
148     private static List JavaDoc<Pair<Rule,FileObject>> readRules( FileObject folder ) {
149
150         List JavaDoc<Pair<Rule,FileObject>> rules = new LinkedList JavaDoc<Pair<Rule,FileObject>>();
151         
152         if (folder == null) {
153             return rules;
154         }
155
156         HashMap JavaDoc<FileObject,DefaultMutableTreeNode JavaDoc> dir2node = new HashMap JavaDoc<FileObject,DefaultMutableTreeNode JavaDoc>();
157
158         // XXX Probably not he best order
159
Enumeration JavaDoc e = folder.getData( true );
160         while( e.hasMoreElements() ) {
161             FileObject o = (FileObject)e.nextElement();
162             String JavaDoc name = o.getNameExt().toLowerCase();
163
164             if ( o.canRead() ) {
165                 Rule r = null;
166                 if ( name.endsWith( INSTANCE_EXT ) ) {
167                     r = instantiateRule(o);
168                 }
169                 if ( r != null ) {
170                     rules.add( new Pair<Rule,FileObject>( r, o ) );
171                 }
172             }
173         }
174         return rules;
175     }
176
177     private static void categorizeErrorRules( List JavaDoc<Pair<Rule,FileObject>> rules,
178                                              Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> dest,
179                                              FileObject rootFolder,
180                                              DefaultMutableTreeNode JavaDoc rootNode ) {
181
182         Map JavaDoc<FileObject,DefaultMutableTreeNode JavaDoc> dir2node = new HashMap JavaDoc<FileObject, DefaultMutableTreeNode JavaDoc>();
183         dir2node.put(rootFolder, rootNode);
184
185         for( Pair<Rule,FileObject> pair : rules ) {
186             Rule rule = pair.getA();
187             FileObject fo = pair.getB();
188
189             if ( rule instanceof ErrorRule ) {
190                 addRule( (ErrorRule)rule, dest );
191                 FileObject parent = fo.getParent();
192                 DefaultMutableTreeNode JavaDoc category = dir2node.get( parent );
193                 if ( category == null ) {
194                     category = new DefaultMutableTreeNode JavaDoc( parent );
195                     rootNode.add( category );
196                     dir2node.put( parent, category );
197                 }
198                 category.add( new DefaultMutableTreeNode JavaDoc( rule, false ) );
199             }
200             else {
201                 LOG.log( Level.WARNING, "The rule defined in " + fo.getPath() + "is not instance of ErrorRule" );
202             }
203         }
204     }
205
206     private static void categorizeTreeRules( List JavaDoc<Pair<Rule,FileObject>> rules,
207                                              Map JavaDoc<Tree.Kind,List JavaDoc<TreeRule>> dest,
208                                              FileObject rootFolder,
209                                              DefaultMutableTreeNode JavaDoc rootNode ) {
210
211         Map JavaDoc<FileObject,DefaultMutableTreeNode JavaDoc> dir2node = new HashMap JavaDoc<FileObject, DefaultMutableTreeNode JavaDoc>();
212         dir2node.put(rootFolder, rootNode);
213
214         for( Pair<Rule,FileObject> pair : rules ) {
215             Rule rule = pair.getA();
216             FileObject fo = pair.getB();
217
218             if ( rule instanceof TreeRule ) {
219                 addRule( (TreeRule)rule, dest );
220                 FileObject parent = fo.getParent();
221                 DefaultMutableTreeNode JavaDoc category = dir2node.get( parent );
222                 if ( category == null ) {
223                     category = new DefaultMutableTreeNode JavaDoc( parent );
224                     rootNode.add( category );
225                     dir2node.put( parent, category );
226                 }
227                 category.add( new DefaultMutableTreeNode JavaDoc( rule, false ) );
228             }
229             else {
230                 LOG.log( Level.WARNING, "The rule defined in " + fo.getPath() + "is not instance of TreeRule" );
231             }
232
233         }
234     }
235
236     private static void addRule( TreeRule rule, Map JavaDoc<Tree.Kind,List JavaDoc<TreeRule>> dest ) {
237
238         for( Tree.Kind kind : rule.getTreeKinds() ) {
239             List JavaDoc<TreeRule> l = dest.get( kind );
240             if ( l == null ) {
241                 l = new LinkedList JavaDoc<TreeRule>();
242                 dest.put( kind, l );
243             }
244             l.add( rule );
245         }
246
247     }
248
249     @SuppressWarnings JavaDoc("unchecked")
250     private static void addRule( ErrorRule rule, Map JavaDoc<String JavaDoc,List JavaDoc<ErrorRule>> dest ) {
251
252         for(String JavaDoc code : (Set JavaDoc<String JavaDoc>) rule.getCodes()) {
253             List JavaDoc<ErrorRule> l = dest.get( code );
254             if ( l == null ) {
255                 l = new LinkedList JavaDoc<ErrorRule>();
256                 dest.put( code, l );
257             }
258             l.add( rule );
259         }
260
261     }
262
263     private static Rule instantiateRule( FileObject fileObject ) {
264         try {
265             DataObject dobj = DataObject.find(fileObject);
266             InstanceCookie ic = dobj.getCookie( InstanceCookie.class );
267             Object JavaDoc instance = ic.instanceCreate();
268             
269             if (instance instanceof Rule) {
270                 return (Rule) instance;
271             } else {
272                 return null;
273             }
274         } catch( IOException JavaDoc e ) {
275             LOG.log(Level.INFO, null, e);
276         } catch ( ClassNotFoundException JavaDoc e ) {
277             LOG.log(Level.INFO, null, e);
278         }
279
280         return null;
281     }
282
283
284     // Unused code -------------------------------------------------------------
285

286 // /** Rules to be run on elements */
287
// private static Map<ElementKind,List<ElementRule>> elementRules = new HashMap<ElementKind,List<ElementRule>>();
288

289
290 // private static void addRule( ElementRule rule ) {
291
//
292
// for( ElementKind kind : rule.getElementKinds()) {
293
// List<ElementRule> l = elementRules.get( kind );
294
// if ( l == null ) {
295
// l = new LinkedList<ElementRule>();
296
// elementRules.put( kind, l );
297
// }
298
// l.add( rule );
299
// }
300
//
301
// }
302

303 // private static class ElementWalker extends ElementScanner6<List<ErrorDescription>,CompilationInfo> {
304
//
305
// private List<ErrorDescription> warnings = new LinkedList<ErrorDescription>();
306
//
307
// @Override
308
// public List<ErrorDescription> scan( Element element, CompilationInfo compilationInfo ) {
309
//
310
// if ( element == null ) {
311
// return warnings;
312
// }
313
//
314
// List<ElementRule> rules = elementRules.get( element.getKind() ); // Find list of rules associated with given kind
315
// if ( rules != null ) {
316
// for (ElementRule rule : rules) { // Run the rules for given node
317
// List<ErrorDescription> w = rule.run( compilationInfo, element, runNumber );
318
// if ( w != null ) {
319
// warnings.addAll( w );
320
// }
321
// }
322
// }
323
//
324
// super.scan( element, compilationInfo );
325
//
326
// return warnings;
327
// }
328
//
329
// }
330
//
331

332 // /** Runs all rules registered to ElementKinds */
333
// private static List<ErrorDescription> runElementRules() {
334
// ElementScanner6<List<ErrorDescription>,CompilationInfo> v = new ElementWalker();
335
// // XXX How to implement?
336
// return Collections.<ErrorDescription>emptyList();
337
// }
338

339
340 }
341
Popular Tags