KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.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 org.apache.lucene.document.DateTools;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.index.Term;
31 import org.apache.lucene.search.BooleanClause;
32 import org.apache.lucene.search.BooleanQuery;
33 import org.apache.lucene.search.PrefixQuery;
34 import org.apache.lucene.search.Query;
35 import org.apache.lucene.search.TermQuery;
36 import org.apache.lucene.search.WildcardQuery;
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 class DocumentUtil {
48     
49     private static final String JavaDoc ROOT_NAME="/"; //NOI18N
50
private static final String JavaDoc FIELD_RESOURCE_NAME = "resName"; //NOI18N
51
private static final String JavaDoc FIELD_BINARY_NAME = "binaryName"; //NOI18N
52
private static final String JavaDoc FIELD_PACKAGE_NAME = "packageName"; //NOI18N
53
static final String JavaDoc FIELD_TIME_STAMP = "timeStamp"; //NOI18N
54
private static final String JavaDoc FIELD_REFERENCES = "references"; //NOI18N
55
private static final String JavaDoc FIELD_SIMPLE_NAME = "simpleName"; //NOI18N
56
private static final String JavaDoc FIELD_CASE_INSENSITIVE_NAME = "ciName"; //NOI18N
57

58     private static final char NO = '-'; //NOI18N
59
private static final char YES = '+'; //NOI18N
60
private static final char WILDCARD = '?'; //NOI18N
61
private static final char PKG_SEPARATOR = '.'; //NOI18N
62

63     private static final char EK_CLASS = 'C'; //NOI18N
64
private static final char EK_INTERFACE = 'I'; //NOI18N
65
private static final char EK_ENUM = 'E'; //NOI18N
66
private static final char EK_ANNOTATION = 'A'; //NOI18N
67

68     private static final int SIZE = ClassIndexImpl.UsageType.values().length;
69     private static final char[] MASK_ANY_USAGE = new char[SIZE];
70     
71     static {
72         Arrays.fill(MASK_ANY_USAGE, WILDCARD); //NOI18N
73
}
74     
75     private DocumentUtil () {
76     }
77     
78     
79     //Document field getters
80
public static String JavaDoc getBinaryName (final Document doc) {
81 /*
82         return getBinaryName(doc, null);
83     }
84     
85     public static String getBinaryName (final Document doc, final ElementKind[] kind) {
86  */

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

378     
379 }
380
Popular Tags