KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > DatabaseManagerImpl


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.languages.features;
21
22 import java.util.ListIterator JavaDoc;
23 import java.util.ListIterator JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.WeakHashMap JavaDoc;
36 import javax.swing.text.Document JavaDoc;
37 import org.netbeans.api.languages.ASTEvaluator;
38 import org.netbeans.api.languages.ASTItem;
39 import org.netbeans.api.languages.ASTItem;
40 import org.netbeans.api.languages.ASTPath;
41 import org.netbeans.api.languages.ASTToken;
42 import org.netbeans.api.languages.LanguagesManager;
43 import org.netbeans.api.languages.ParseException;
44 import org.netbeans.api.languages.DatabaseManager;
45 import org.netbeans.api.languages.ParserManager;
46 import org.netbeans.api.languages.ParserManager.State;
47 import org.netbeans.api.languages.SyntaxContext;
48 import org.netbeans.modules.editor.NbEditorDocument;
49 import org.netbeans.modules.editor.NbEditorUtilities;
50 import org.netbeans.modules.languages.Feature;
51 import org.netbeans.modules.languages.Language;
52 import org.netbeans.api.languages.SyntaxContext;
53 import org.netbeans.api.languages.ASTNode;
54 import org.netbeans.api.languages.ParseException;
55 import org.netbeans.modules.languages.LanguagesManagerImpl;
56 import org.openide.cookies.LineCookie;
57 import org.openide.loaders.DataObject;
58 import org.openide.text.Line;
59 import org.openide.text.NbDocument;
60
61
62 /**
63  *
64  * @author Jan Jancura
65  */

66 public final class DatabaseManagerImpl extends DatabaseManager {
67     
68     private Map JavaDoc<Object JavaDoc,Map JavaDoc<String JavaDoc,Object JavaDoc>> contextToId = new WeakHashMap JavaDoc<Object JavaDoc,Map JavaDoc<String JavaDoc,Object JavaDoc>> ();
69     private Map JavaDoc<Document JavaDoc,Object JavaDoc[]> documentToContexts = new WeakHashMap JavaDoc<Document JavaDoc,Object JavaDoc[]> ();
70     
71     {
72         //Utils.startTest("DatabaseManagerImpl.contextToId", contextToId);
73
//Utils.startTest("DatabaseManagerImpl.documentToContexts", documentToContexts);
74
}
75     
76     
77     public List JavaDoc<Line.Part> get (ASTPath path, String JavaDoc id, boolean recursive) {
78         List JavaDoc<Line.Part> result = new ArrayList JavaDoc<Line.Part> ();
79         ListIterator JavaDoc<ASTItem> it = path.listIterator (path.size ());
80         while (it.hasPrevious ()) {
81             ASTItem item = it.previous ();
82             Map JavaDoc idToLines = contextToId.get (item);
83             if (idToLines != null) {
84                 Object JavaDoc o = idToLines.get (id);
85                 if (o != null) {
86                     if (o instanceof Set JavaDoc)
87                         result.addAll ((Set JavaDoc<Line.Part>) o);
88                     else
89                         result.add ((Line.Part) o);
90                     if (!recursive) return result;
91                 }
92             }
93         }
94         return result;
95     }
96     
97     public List JavaDoc<Line.Part> get (Object JavaDoc context, String JavaDoc id) {
98         List JavaDoc<Line.Part> result = new ArrayList JavaDoc<Line.Part> ();
99         Map JavaDoc idToLines = contextToId.get (context);
100         if (idToLines != null) {
101             Object JavaDoc o = idToLines.get (id);
102             if (o != null) {
103                 if (o instanceof Set JavaDoc)
104                     result.addAll ((Set JavaDoc<Line.Part>) o);
105                 else
106                     result.add ((Line.Part) o);
107             }
108         }
109         return result;
110     }
111     
112     public Collection JavaDoc<String JavaDoc> getIds (ASTPath path, boolean recursive) {
113         List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc> ();
114         ListIterator JavaDoc<ASTItem> it = path.listIterator (path.size ());
115         while (it.hasPrevious ()) {
116             ASTItem item = it.previous ();
117             Map JavaDoc<String JavaDoc,Object JavaDoc> idToLines = contextToId.get (item);
118             if (idToLines != null) {
119                 result.addAll (idToLines.keySet ());
120                 if (!recursive) return result;
121             }
122         }
123         return result;
124     }
125     
126     public Collection JavaDoc<String JavaDoc> getIds (Object JavaDoc context) {
127         List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc> ();
128         Map JavaDoc<String JavaDoc,Object JavaDoc> idToLines = contextToId.get (context);
129         if (idToLines != null)
130             result.addAll (idToLines.keySet ());
131         return result;
132     }
133     
134     private void removeIDs (Document JavaDoc doc) {
135         Object JavaDoc[] contexts = documentToContexts.remove (doc);
136         if (contexts == null) return;
137         Iterator JavaDoc it = ((Set JavaDoc) contexts [0]).iterator ();
138         while (it.hasNext ()) {
139             ASTNode node = (ASTNode) it.next ();
140             contextToId.remove (node);
141         }
142         it = ((Map JavaDoc) contexts [1]).keySet ().iterator ();
143         while (it.hasNext ()) {
144             Object JavaDoc context = it.next ();
145             Map JavaDoc idToLinesTBD = (Map JavaDoc) ((Map JavaDoc) contexts [1]).get (context);
146             Map JavaDoc idToLines = contextToId.get (context);
147             if (idToLines == null) return;
148             Iterator JavaDoc it2 = idToLinesTBD.keySet ().iterator ();
149             while (it2.hasNext ()) {
150                 Object JavaDoc id = it2.next ();
151                 Object JavaDoc linesTBD = idToLinesTBD.get (id);
152                 Object JavaDoc lines = idToLines.get (id);
153                 if (linesTBD instanceof Set JavaDoc) {
154                     if (lines instanceof Set JavaDoc) {
155                         ((Set JavaDoc) lines).removeAll ((Set JavaDoc) linesTBD);
156                         if (((Set JavaDoc) lines).isEmpty ())
157                             idToLines.remove (id);
158                     } else {
159                         if (((Set JavaDoc) linesTBD).contains (lines))
160                             idToLines.remove (id);
161                     }
162                 } else
163                 if (lines instanceof Set JavaDoc) {
164                     ((Set JavaDoc) lines).remove (linesTBD);
165                     if (((Set JavaDoc) lines).isEmpty ())
166                         idToLines.remove (id);
167                 } else
168                     idToLines.remove (id);
169             }
170         }
171     }
172     
173     private void addId (Document JavaDoc doc, Object JavaDoc context, String JavaDoc id, Line.Part l) {
174         Map JavaDoc idToLines = contextToId.get (context);
175         if (idToLines == null) {
176             idToLines = new HashMap JavaDoc ();
177             contextToId.put (context, idToLines);
178         }
179         Object JavaDoc lines = idToLines.get (id);
180         if (lines == null)
181             idToLines.put (id, l);
182         else
183         if (lines instanceof Line.Part) {
184             Set JavaDoc list = new HashSet JavaDoc ();
185             list.add (lines);
186             list.add (l);
187             idToLines.put (id, list);
188         } else {
189             ((Set JavaDoc) lines).add (l);
190         }
191         
192         Object JavaDoc[] contexts = documentToContexts.get (doc);
193         if (contexts == null) {
194             contexts = new Object JavaDoc [] {new HashSet JavaDoc (), new HashMap JavaDoc ()};
195             documentToContexts.put (doc, contexts);
196         }
197         if (context instanceof ASTNode)
198             ((Set JavaDoc) contexts [0]).add (context);
199         else {
200             idToLines = (Map JavaDoc) ((Map JavaDoc) contexts [1]).get (context);
201             if (idToLines == null) {
202                 idToLines = new HashMap JavaDoc ();
203                 ((Map JavaDoc) contexts [1]).put (context, idToLines);
204             }
205             Object JavaDoc o = idToLines.get (id);
206             if (o == null)
207                 idToLines.put (id, l);
208             else
209             if (o instanceof Set JavaDoc)
210                 ((Set JavaDoc) o).add (l);
211             else {
212                 Set JavaDoc list = new HashSet JavaDoc ();
213                 list.add (o);
214                 list.add (l);
215                 idToLines.put (id, list);
216             }
217         }
218     }
219     
220     
221     public class Listener extends ASTEvaluator {
222         private NbEditorDocument doc;
223         private ParserManager parser;
224         private Line.Set lineSet;
225
226
227         /** Creates a new instance of AnnotationManager */
228         public Listener (Document JavaDoc doc) {
229             //doc.addDocumentListener (this);
230
this.doc = (NbEditorDocument) doc;
231             parser = ParserManager.get ((NbEditorDocument) doc);
232             parser.addASTEvaluator (this);
233         }
234
235         public void beforeEvaluation (State state, ASTNode root) {
236             removeIDs (doc);
237             DataObject dob = NbEditorUtilities.getDataObject (doc);
238             if (dob == null) return;
239             LineCookie lc = (LineCookie) dob.getCookie (LineCookie.class);
240             lineSet = lc.getLineSet ();
241         }
242         
243         public void afterEvaluation (State state, ASTNode root) {
244             lineSet = null;
245         }
246         
247         public void evaluate (State state, ASTPath path) {
248             if (lineSet == null) return;
249             ASTItem item = path.getLeaf ();
250             try {
251                 Language language = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).
252                     getLanguage (item.getMimeType ());
253                 Feature store = language.getFeature (Language.STORE, path);
254                 if (store != null) {
255                     if (store.getBoolean ("condition", SyntaxContext.create (doc, path), true)) {
256                         Object JavaDoc[] contextProperties = (Object JavaDoc[]) getContext (store);
257                         String JavaDoc defaultContext = (String JavaDoc) contextProperties [0];
258                         Set JavaDoc contexts = (Set JavaDoc) contextProperties [1];
259                         ListIterator JavaDoc<ASTItem> it = path.listIterator (path.size ());
260                         Object JavaDoc context = defaultContext;
261                         while (it.hasPrevious ()) {
262                             ASTItem item2 = it.previous ();
263                             if (item2 instanceof ASTToken)
264                                 continue;
265                             ASTNode cn = (ASTNode) item2;
266                             if (contexts.contains (cn.getNT ())) {
267                                 context = cn;
268                                 break;
269                             }
270                         }
271                         String JavaDoc name = (String JavaDoc) store.getValue (
272                             "name",
273                             SyntaxContext.create (doc, path)
274                         );
275                         if (name != null) {
276                             int lineNumber = NbDocument.findLineNumber (doc, item.getOffset ());
277                             int column = NbDocument.findLineColumn (doc, item.getOffset ());
278                             Line line = lineSet.getCurrent (lineNumber);
279                             Line.Part part = line.createPart (column, 0);
280                             //S ystem.out.println("addToStorage " + context + " : " + name + " : " + (context instanceof ASTNode ? ((ASTNode) context).getNT () : context));
281
addId (doc, context, name, part);
282                         }
283                     }
284                 }
285             } catch (ParseException ex) {
286             }
287         }
288         
289         private Map JavaDoc<Feature,Object JavaDoc[]> contexts = new WeakHashMap JavaDoc<Feature,Object JavaDoc[]> ();
290         
291         private Object JavaDoc[] getContext (Feature store) {
292             if (!contexts.containsKey (store)) {
293                 String JavaDoc c = (String JavaDoc) store.getValue ("context");
294                 String JavaDoc first = null;
295                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (c, ",");
296                 Set JavaDoc cs = new HashSet JavaDoc ();
297                 while (st.hasMoreElements ())
298                     if (first == null)
299                         first = st.nextToken ().trim ();
300                     else
301                         cs.add (st.nextToken ().trim ());
302                 contexts.put (store, new Object JavaDoc[] {first, cs});
303             }
304             return contexts.get (store);
305         }
306     }
307 }
308
309
Popular Tags