KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > source > usages > SourceAnalyser


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.retouche.source.usages;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import org.netbeans.api.gsf.Indexer;
30 import org.netbeans.api.gsf.ParserFile;
31 import org.netbeans.api.gsf.ParserResult;
32 import org.netbeans.api.retouche.source.ParserTaskImpl;
33 import org.netbeans.api.retouche.source.ParserTaskImpl;
34 import org.netbeans.modules.gsf.Language;
35 import org.netbeans.modules.gsf.LanguageRegistry;
36 import org.openide.filesystems.FileObject;
37
38 /**
39  * This file is originally from Retouche, the Java Support
40  * infrastructure in NetBeans. I have modified the file as little
41  * as possible to make merging Retouche fixes back as simple as
42  * possible.
43  *
44  *
45  * @author Tomas Zezula
46  */

47 public class SourceAnalyser {
48     
49     private final Index index;
50     private final Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> references;
51     private final Set JavaDoc<String JavaDoc> toDelete;
52     
53     /** Creates a new instance of SourceAnalyser */
54     public SourceAnalyser (final Index index) {
55         assert index != null;
56         this.index = index;
57         this.references = new HashMap JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> ();
58         this.toDelete = new HashSet JavaDoc<String JavaDoc> ();
59     }
60     
61     public final boolean isUpToDate(String JavaDoc resourceName, long resourceMTime) throws IOException JavaDoc {
62         return this.index.isUpToDate(resourceName, resourceMTime);
63     }
64     
65     public void store () throws IOException JavaDoc {
66     }
67
68     public boolean isValid () throws IOException JavaDoc {
69         return this.index.isValid(true);
70     }
71
72     public void analyse (final Iterable JavaDoc<ParserResult/*? extends CompilationUnitTree*/> data, ParserTaskImpl jt, /*JavaFileManager manager, */ParserFile sibling) throws IOException JavaDoc {
73         // I should stash some shit into this.references here such that I can try storing them later, for example
74
// all the class names I can find. This would be a good place to look for the desired language...
75
// Of course, I can have multiple, so I have to index these by the language type, right? Otherwise
76
// how do I choose which one to ask?
77
// Actually, do it once per filetype - it's cheap compared to all the other crap I do per file anyway
78
for (ParserResult result : data) {
79             FileObject fo = result.getFile().getFileObject();
80             Language language = LanguageRegistry.getInstance().getLanguageByMimeType(fo.getMIMEType());
81             if (language != null) {
82                 Indexer indexer = language.getIndexer();
83                 if (indexer != null) {
84                     indexer.updateIndex(index, result);
85                 }
86                 
87             }
88         }
89     }
90     
91     void analyseUnitAndStore (ParserResult result/*final CompilationUnitTree cu*/, final ParserTaskImpl/*JavacTaskImpl*/ jt) throws IOException JavaDoc {
92             FileObject fo = result.getFile().getFileObject();
93             Language language = LanguageRegistry.getInstance().getLanguageByMimeType(fo.getMIMEType());
94             if (language != null) {
95                 Indexer indexer = language.getIndexer();
96                 if (indexer != null) {
97                     indexer.updateIndex(index, result);
98                 }
99             }
100     }
101     
102     public void delete (final String JavaDoc className) throws IOException JavaDoc {
103         if (!this.index.isValid(false)) {
104             return;
105         }
106         this.toDelete.add(className);
107     }
108 }
109
Popular Tags