KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > usages > DocumentUtil


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.source.usages;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.lang.model.element.ElementKind;
28 import org.apache.lucene.document.DateTools;
29 import org.apache.lucene.document.Document;
30 import org.apache.lucene.document.Field;
31 import org.apache.lucene.index.Term;
32 import org.apache.lucene.search.BooleanClause;
33 import org.apache.lucene.search.BooleanQuery;
34 import org.apache.lucene.search.PrefixQuery;
35 import org.apache.lucene.search.Query;
36 import org.apache.lucene.search.TermQuery;
37 import org.apache.lucene.search.WildcardQuery;
38
39 /**
40  *
41  * @author Tomas Zezula
42  */

43 class DocumentUtil {
44     
45     private static final String JavaDoc ROOT_NAME="/"; //NOI18N
46
private static final String JavaDoc FIELD_RESOURCE_NAME = "resName"; //NOI18N
47
private static final String JavaDoc FIELD_BINARY_NAME = "binaryName"; //NOI18N
48
private static final String JavaDoc FIELD_PACKAGE_NAME = "packageName"; //NOI18N
49
private static final String JavaDoc FIELD_TIME_STAMP = "timeStamp"; //NOI18N
50
private static final String JavaDoc FIELD_REFERENCES = "references"; //NOI18N
51
private static final String JavaDoc FIELD_SIMPLE_NAME = "simpleName"; //NOI18N
52
private static final String JavaDoc FIELD_CASE_INSENSITIVE_NAME = "ciName"; //NOI18N
53

54     private static final char NO = '-'; //NOI18N
55
private static final char YES = '+'; //NOI18N
56
private static final char WILDCARD = '?'; //NOI18N
57
private static final char PKG_SEPARATOR = '.'; //NOI18N
58

59     private static final char EK_CLASS = 'C'; //NOI18N
60
private static final char EK_INTERFACE = 'I'; //NOI18N
61
private static final char EK_ENUM = 'E'; //NOI18N
62
private static final char EK_ANNOTATION = 'A'; //NOI18N
63

64     private static final int SIZE = ClassIndexImpl.UsageType.values().length;
65     private static final char[] MASK_ANY_USAGE = new char[SIZE];
66     
67     static {
68         Arrays.fill(MASK_ANY_USAGE, WILDCARD); //NOI18N
69
}
70     
71     private DocumentUtil () {
72     }
73     
74     
75     //Document field getters
76
public static String JavaDoc getBinaryName (final Document doc) {
77         return getBinaryName(doc, null);
78     }
79     
80     public static String JavaDoc getBinaryName (final Document doc, final ElementKind[] kind) {
81         assert doc != null;
82         final Field pkgField = doc.getField(FIELD_PACKAGE_NAME);
83         final Field snField = doc.getField (FIELD_BINARY_NAME);
84         if (snField == null) {
85             return null;
86         }
87         final String JavaDoc tmp = snField.stringValue();
88         final String JavaDoc snName = tmp.substring(0,tmp.length()-1);
89         if (kind != null) {
90             assert kind.length == 1;
91             kind[0] = decodeKind (tmp.charAt(tmp.length()-1));
92         }
93         if (pkgField == null) {
94             return snName;
95         }
96         String JavaDoc pkg = pkgField.stringValue();
97         if (pkg.length() == 0) {
98             return snName;
99         }
100         return pkg + PKG_SEPARATOR + snName; //NO I18N
101
}
102     
103     public static String JavaDoc getSimpleBinaryName (final Document doc) {
104         assert doc != null;
105         Field field = doc.getField(FIELD_BINARY_NAME);
106         if (field == null) {
107             return null;
108         }
109         else {
110             return field.stringValue();
111         }
112     }
113     
114     public static String JavaDoc getPackageName (final Document doc) {
115         assert doc != null;
116         Field field = doc.getField(FIELD_PACKAGE_NAME);
117         return field == null ? null : field.stringValue();
118     }
119     
120     static String JavaDoc getRefereneType (final Document doc, final String JavaDoc className) {
121         assert doc != null;
122         assert className != null;
123         Field[] fields = doc.getFields(FIELD_REFERENCES);
124         assert fields != null;
125         for (Field field : fields) {
126             final String JavaDoc rawUsage = field.stringValue();
127             final int rawUsageLen = rawUsage.length();
128             assert rawUsageLen>SIZE;
129             final int index = rawUsageLen - SIZE;
130             final String JavaDoc usageName = rawUsage.substring(0,index);
131             final String JavaDoc map = rawUsage.substring (index);
132             if (className.equals(usageName)) {
133                 return map;
134             }
135         }
136         return null;
137     }
138     
139     public static List JavaDoc<String JavaDoc> getReferences (final Document doc) {
140         assert doc != null;
141         Field[] fields = doc.getFields(FIELD_REFERENCES);
142         assert fields != null;
143         List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc> (fields.length);
144         for (Field field : fields) {
145             result.add (field.stringValue());
146         }
147         return result;
148     }
149     
150     public static long getTimeStamp (final Document doc) throws java.text.ParseException JavaDoc {
151         assert doc != null;
152         Field field = doc.getField(FIELD_TIME_STAMP);
153         assert field != null;
154         String JavaDoc data = field.stringValue();
155         assert data != null;
156         return DateTools.stringToTime(data);
157     }
158     
159     
160     //Term and query factories
161
public static Query binaryNameQuery (final String JavaDoc resourceName) {
162         BooleanQuery query = new BooleanQuery ();
163         int index = resourceName.lastIndexOf(PKG_SEPARATOR); // NOI18N
164
String JavaDoc pkgName, sName;
165         if (index < 0) {
166             pkgName = ""; // NOI18N
167
sName = resourceName;
168         }
169         else {
170             pkgName = resourceName.substring(0,index);
171             sName = resourceName.substring(index+1);
172         }
173         sName = sName + WILDCARD;
174         query.add (new TermQuery (new Term (FIELD_PACKAGE_NAME, pkgName)),BooleanClause.Occur.MUST);
175         query.add (new WildcardQuery (new Term (FIELD_BINARY_NAME, sName)),BooleanClause.Occur.MUST);
176         return query;
177     }
178     
179     public static Query binaryContentNameQuery (final String JavaDoc resourceName) {
180         int index = resourceName.lastIndexOf(PKG_SEPARATOR); // NOI18N
181
String JavaDoc pkgName, sName;
182         if (index < 0) {
183             pkgName = ""; // NOI18N
184
sName = resourceName;
185         }
186         else {
187             pkgName = resourceName.substring(0,index);
188             sName = resourceName.substring(index+1);
189         }
190         BooleanQuery query = new BooleanQuery ();
191         BooleanQuery subQuery = new BooleanQuery();
192         subQuery.add (new WildcardQuery (new Term (FIELD_BINARY_NAME, sName + WILDCARD)),BooleanClause.Occur.SHOULD);
193         subQuery.add (new PrefixQuery (new Term (FIELD_BINARY_NAME, sName + '$')),BooleanClause.Occur.SHOULD);
194         query.add (new TermQuery (new Term (FIELD_PACKAGE_NAME, pkgName)),BooleanClause.Occur.MUST);
195         query.add (subQuery,BooleanClause.Occur.MUST);
196         return query;
197     }
198     
199     
200     public static Term rootDocumentTerm () {
201         return new Term (FIELD_RESOURCE_NAME,ROOT_NAME);
202     }
203     
204     public static Term simpleBinaryNameTerm (final String JavaDoc resourceFileName) {
205         assert resourceFileName != null;
206         return new Term (FIELD_BINARY_NAME, resourceFileName);
207     }
208     
209     public static Term packageNameTerm (final String JavaDoc packageName) {
210         assert packageName != null;
211         return new Term (FIELD_PACKAGE_NAME, packageName);
212     }
213     
214     public static Term referencesTerm (String JavaDoc resourceName, final Set JavaDoc<ClassIndexImpl.UsageType> usageType) {
215         assert resourceName != null;
216         if (usageType != null) {
217             resourceName = encodeUsage (resourceName, usageType, WILDCARD).toString();
218         }
219         else {
220             StringBuilder JavaDoc sb = new StringBuilder JavaDoc (resourceName);
221             sb.append(MASK_ANY_USAGE);
222             resourceName = sb.toString();
223         }
224         return new Term (FIELD_REFERENCES, resourceName);
225     }
226     
227     public static Term simpleNameTerm (final String JavaDoc resourceSimpleName) {
228         assert resourceSimpleName != null;
229         return new Term (FIELD_SIMPLE_NAME, resourceSimpleName);
230     }
231     
232     public static Term caseInsensitiveNameTerm (final String JavaDoc caseInsensitiveName) {
233         assert caseInsensitiveName != null;
234         return new Term (FIELD_CASE_INSENSITIVE_NAME, caseInsensitiveName);
235     }
236     
237     //Factories for lucene document
238
public static Document createDocument (final String JavaDoc binaryName, final long timeStamp, List JavaDoc<String JavaDoc> references) {
239         assert binaryName != null;
240         assert references != null;
241         int index = binaryName.lastIndexOf(PKG_SEPARATOR); //NOI18N
242
String JavaDoc fileName, pkgName, simpleName, caseInsensitiveName;
243         if (index<0) {
244             fileName = binaryName;
245             pkgName = ""; //NOI18N
246
}
247         else {
248             fileName = binaryName.substring(index+1);
249             pkgName = binaryName.substring(0,index);
250         }
251         index = fileName.lastIndexOf('$'); //NOI18N
252
if (index<0) {
253             simpleName = fileName.substring(0, fileName.length()-1);
254         }
255         else {
256             simpleName = fileName.substring(index+1,fileName.length()-1);
257         }
258         caseInsensitiveName = simpleName.toLowerCase(); //XXX: I18N, Locale
259
Document doc = new Document ();
260         Field field = new Field (FIELD_BINARY_NAME,fileName,Field.Store.YES, Field.Index.UN_TOKENIZED);
261         doc.add (field);
262         field = new Field (FIELD_PACKAGE_NAME,pkgName,Field.Store.YES, Field.Index.UN_TOKENIZED);
263         doc.add (field);
264         field = new Field (FIELD_TIME_STAMP,DateTools.timeToString(timeStamp,DateTools.Resolution.MILLISECOND),Field.Store.YES,Field.Index.NO);
265         doc.add (field);
266         field = new Field (FIELD_SIMPLE_NAME,simpleName, Field.Store.YES, Field.Index.UN_TOKENIZED);
267         doc.add (field);
268         field = new Field (FIELD_CASE_INSENSITIVE_NAME, caseInsensitiveName, Field.Store.YES, Field.Index.UN_TOKENIZED);
269         doc.add (field);
270         for (String JavaDoc reference : references) {
271             field = new Field (FIELD_REFERENCES,reference,Field.Store.YES,Field.Index.UN_TOKENIZED);
272             doc.add(field);
273         }
274         return doc;
275     }
276     
277     public static Document createRootTimeStampDocument (final long timeStamp) {
278         Document doc = new Document ();
279         Field field = new Field (FIELD_RESOURCE_NAME, ROOT_NAME,Field.Store.YES, Field.Index.UN_TOKENIZED);
280         doc.add (field);
281         field = new Field (FIELD_TIME_STAMP,DateTools.timeToString(timeStamp,DateTools.Resolution.MILLISECOND),Field.Store.YES,Field.Index.NO);
282         doc.add (field);
283         return doc;
284     }
285     
286     // Functions for encoding and decoding of UsageType
287
public static StringBuilder JavaDoc createUsage (final String JavaDoc className) {
288         Set JavaDoc<ClassIndexImpl.UsageType> EMPTY = Collections.emptySet();
289         return encodeUsage (className, EMPTY,NO);
290     }
291     
292     public static void addUsage (final StringBuilder JavaDoc rawUsage, final ClassIndexImpl.UsageType type) {
293         assert rawUsage != null;
294         assert type != null;
295         final int rawUsageLen = rawUsage.length();
296         final int startIndex = rawUsageLen - SIZE;
297         rawUsage.setCharAt (startIndex + type.getOffset(),YES);
298     }
299     
300     public static String JavaDoc encodeUsage (final String JavaDoc className, final Set JavaDoc<ClassIndexImpl.UsageType> usageTypes) {
301         return encodeUsage (className, usageTypes, NO).toString();
302     }
303     
304     private static StringBuilder JavaDoc encodeUsage (final String JavaDoc className, final Set JavaDoc<ClassIndexImpl.UsageType> usageTypes, char fill) {
305         assert className != null;
306         assert usageTypes != null;
307         StringBuilder JavaDoc builder = new StringBuilder JavaDoc ();
308         builder.append(className);
309         char[] map = new char [SIZE];
310         Arrays.fill(map,fill);
311         for (ClassIndexImpl.UsageType usageType : usageTypes) {
312             int offset = usageType.getOffset ();
313             assert offset >= 0 && offset < SIZE;
314             map[offset] = YES;
315         }
316         builder.append(map);
317         return builder;
318     }
319     
320     public static String JavaDoc encodeUsage (final String JavaDoc className, final String JavaDoc usageMap) {
321         assert className != null;
322         assert usageMap != null;
323         StringBuilder JavaDoc sb = new StringBuilder JavaDoc ();
324         sb.append(className);
325         sb.append(usageMap);
326         return sb.toString();
327     }
328     
329     public static String JavaDoc decodeUsage (final String JavaDoc rawUsage, final Set JavaDoc<ClassIndexImpl.UsageType> usageTypes) {
330         assert rawUsage != null;
331         assert usageTypes != null;
332         assert usageTypes.isEmpty();
333         final int rawUsageLen = rawUsage.length();
334         assert rawUsageLen>SIZE;
335         final int index = rawUsageLen - SIZE;
336         final String JavaDoc className = rawUsage.substring(0,index);
337         final String JavaDoc map = rawUsage.substring (index);
338         for (ClassIndexImpl.UsageType usageType : ClassIndexImpl.UsageType.values()) {
339             if (map.charAt(usageType.getOffset()) == YES) {
340                 usageTypes.add (usageType);
341             }
342         }
343         return className;
344     }
345     
346     public static ElementKind decodeKind (char kind) {
347         switch (kind) {
348             case EK_CLASS:
349                 return ElementKind.CLASS;
350             case EK_INTERFACE:
351                 return ElementKind.INTERFACE;
352             case EK_ENUM:
353                 return ElementKind.ENUM;
354             case EK_ANNOTATION:
355                 return ElementKind.ANNOTATION_TYPE;
356             default:
357                 throw new IllegalArgumentException JavaDoc ();
358         }
359     }
360     
361     public static char encodeKind (ElementKind kind) {
362         switch (kind) {
363             case CLASS:
364                 return EK_CLASS;
365             case INTERFACE:
366                 return EK_INTERFACE;
367             case ENUM:
368                 return EK_ENUM;
369             case ANNOTATION_TYPE:
370                 return EK_ANNOTATION;
371             default:
372                 throw new IllegalArgumentException JavaDoc ();
373         }
374     }
375     
376     
377 }
378
Popular Tags