1 19 20 package org.netbeans.modules.java.hints.infrastructure; 21 22 import com.sun.source.tree.Tree; 23 import java.io.IOException ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import javax.swing.tree.DefaultMutableTreeNode ; 34 import javax.swing.tree.DefaultMutableTreeNode ; 35 import javax.swing.tree.DefaultTreeModel ; 36 import javax.swing.tree.DefaultTreeModel ; 37 import javax.swing.tree.DefaultTreeModel ; 38 import javax.swing.tree.TreeModel ; 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 54 public class RulesManager { 55 56 public static Logger LOG = Logger.getLogger("org.netbeans.modules.java.hints"); 59 private static final String INSTANCE_EXT = ".instance"; 61 62 private static final String RULES_FOLDER = "org-netbeans-modules-java-hints/rules/"; private static final String ERRORS = "errors"; private static final String HINTS = "hints"; private static final String SUGGESTIONS = "suggestions"; 67 private static Map <String ,List <ErrorRule>> errors = new HashMap <String , List <ErrorRule>>(); 69 private static Map <Tree.Kind,List <TreeRule>> hints = new HashMap <Tree.Kind,List <TreeRule>>(); 70 private static Map <Tree.Kind,List <TreeRule>> suggestions = new HashMap <Tree.Kind, List <TreeRule>>(); 71 72 private static TreeModel errorsTreeModel; 74 private static TreeModel hintsTreeModel; 75 private static TreeModel suggestionsTreeModel; 76 77 private static RulesManager INSTANCE; 78 79 private RulesManager() { 80 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 <String ,List <ErrorRule>> getErrors() { 94 return errors; 95 } 96 97 public Map <Tree.Kind,List <TreeRule>> getHints() { 98 return hints; 99 } 100 101 public Map <Tree.Kind,List <TreeRule>> getSuggestions() { 102 return suggestions; 103 } 104 105 public TreeModel getErrorsTreeModel() { 106 return errorsTreeModel; 107 } 108 109 public TreeModel getHintsTreeModel() { 110 return hintsTreeModel; 111 } 112 113 public TreeModel getSuggestionsTreeModel() { 114 return suggestionsTreeModel; 115 } 116 117 119 private static void initErrors() { 120 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode (); 121 errorsTreeModel = new DefaultTreeModel ( rootNode ); 122 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 123 FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + ERRORS ); 124 List <Pair<Rule,FileObject>> rules = readRules( folder ); 125 categorizeErrorRules(rules, errors, folder, rootNode); 126 } 127 128 private static void initHints() { 129 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode (); 130 hintsTreeModel = new DefaultTreeModel ( rootNode ); 131 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 132 FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + HINTS ); 133 List <Pair<Rule,FileObject>> rules = readRules(folder); 134 categorizeTreeRules( rules, hints, folder, rootNode ); 135 } 136 137 138 private static void initSuggestions() { 139 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode (); 140 suggestionsTreeModel = new DefaultTreeModel ( rootNode ); 141 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 142 FileObject folder = fs.getRoot().getFileObject( RULES_FOLDER + SUGGESTIONS ); 143 List <Pair<Rule,FileObject>> rules = readRules(folder); 144 categorizeTreeRules(rules, suggestions, folder, rootNode); 145 } 146 147 148 private static List <Pair<Rule,FileObject>> readRules( FileObject folder ) { 149 150 List <Pair<Rule,FileObject>> rules = new LinkedList <Pair<Rule,FileObject>>(); 151 152 if (folder == null) { 153 return rules; 154 } 155 156 HashMap <FileObject,DefaultMutableTreeNode > dir2node = new HashMap <FileObject,DefaultMutableTreeNode >(); 157 158 Enumeration e = folder.getData( true ); 160 while( e.hasMoreElements() ) { 161 FileObject o = (FileObject)e.nextElement(); 162 String 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 <Pair<Rule,FileObject>> rules, 178 Map <String ,List <ErrorRule>> dest, 179 FileObject rootFolder, 180 DefaultMutableTreeNode rootNode ) { 181 182 Map <FileObject,DefaultMutableTreeNode > dir2node = new HashMap <FileObject, DefaultMutableTreeNode >(); 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 category = dir2node.get( parent ); 193 if ( category == null ) { 194 category = new DefaultMutableTreeNode ( parent ); 195 rootNode.add( category ); 196 dir2node.put( parent, category ); 197 } 198 category.add( new DefaultMutableTreeNode ( 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 <Pair<Rule,FileObject>> rules, 207 Map <Tree.Kind,List <TreeRule>> dest, 208 FileObject rootFolder, 209 DefaultMutableTreeNode rootNode ) { 210 211 Map <FileObject,DefaultMutableTreeNode > dir2node = new HashMap <FileObject, DefaultMutableTreeNode >(); 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 category = dir2node.get( parent ); 222 if ( category == null ) { 223 category = new DefaultMutableTreeNode ( parent ); 224 rootNode.add( category ); 225 dir2node.put( parent, category ); 226 } 227 category.add( new DefaultMutableTreeNode ( 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 <Tree.Kind,List <TreeRule>> dest ) { 237 238 for( Tree.Kind kind : rule.getTreeKinds() ) { 239 List <TreeRule> l = dest.get( kind ); 240 if ( l == null ) { 241 l = new LinkedList <TreeRule>(); 242 dest.put( kind, l ); 243 } 244 l.add( rule ); 245 } 246 247 } 248 249 @SuppressWarnings ("unchecked") 250 private static void addRule( ErrorRule rule, Map <String ,List <ErrorRule>> dest ) { 251 252 for(String code : (Set <String >) rule.getCodes()) { 253 List <ErrorRule> l = dest.get( code ); 254 if ( l == null ) { 255 l = new LinkedList <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 instance = ic.instanceCreate(); 268 269 if (instance instanceof Rule) { 270 return (Rule) instance; 271 } else { 272 return null; 273 } 274 } catch( IOException e ) { 275 LOG.log(Level.INFO, null, e); 276 } catch ( ClassNotFoundException e ) { 277 LOG.log(Level.INFO, null, e); 278 } 279 280 return null; 281 } 282 283 284 286 289 290 303 332 339 340 } 341 | Popular Tags |