1 19 20 package org.netbeans.modules.retouche.source.usages; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.Set ; 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 47 class DocumentUtil { 48 49 private static final String ROOT_NAME="/"; private static final String FIELD_RESOURCE_NAME = "resName"; private static final String FIELD_BINARY_NAME = "binaryName"; private static final String FIELD_PACKAGE_NAME = "packageName"; static final String FIELD_TIME_STAMP = "timeStamp"; private static final String FIELD_REFERENCES = "references"; private static final String FIELD_SIMPLE_NAME = "simpleName"; private static final String FIELD_CASE_INSENSITIVE_NAME = "ciName"; 58 private static final char NO = '-'; private static final char YES = '+'; private static final char WILDCARD = '?'; private static final char PKG_SEPARATOR = '.'; 63 private static final char EK_CLASS = 'C'; private static final char EK_INTERFACE = 'I'; private static final char EK_ENUM = 'E'; private static final char EK_ANNOTATION = 'A'; 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); } 74 75 private DocumentUtil () { 76 } 77 78 79 public static String getBinaryName (final Document doc) { 81 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 tmp = snField.stringValue(); 94 final String snName = tmp.substring(0,tmp.length()-1); 95 if (pkgField == null) { 100 return snName; 101 } 102 return pkgField.stringValue() + PKG_SEPARATOR + snName; } 104 105 public static String 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 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 getRefereneType (final Document doc, final String 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 rawUsage = field.stringValue(); 129 final int rawUsageLen = rawUsage.length(); 130 assert rawUsageLen>SIZE; 131 final int index = rawUsageLen - SIZE; 132 final String usageName = rawUsage.substring(0,index); 133 final String map = rawUsage.substring (index); 134 if (className.equals(usageName)) { 135 return map; 136 } 137 } 138 return null; 139 } 140 141 public static List <String > getReferences (final Document doc) { 142 assert doc != null; 143 Field[] fields = doc.getFields(FIELD_REFERENCES); 144 assert fields != null; 145 List <String > result = new ArrayList <String > (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 { 153 assert doc != null; 154 Field field = doc.getField(FIELD_TIME_STAMP); 155 assert field != null; 156 String data = field.stringValue(); 157 assert data != null; 158 return DateTools.stringToTime(data); 159 } 160 161 162 public static Query binaryNameQuery (final String resourceName) { 164 BooleanQuery query = new BooleanQuery (); 165 int index = resourceName.lastIndexOf(PKG_SEPARATOR); String pkgName, sName; 167 if (index < 0) { 168 pkgName = ""; 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 resourceName) { 182 int index = resourceName.lastIndexOf(PKG_SEPARATOR); String pkgName, sName; 184 if (index < 0) { 185 pkgName = ""; 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 resourceFileName) { 207 assert resourceFileName != null; 208 return new Term (FIELD_BINARY_NAME, resourceFileName); 209 } 210 211 public static Term packageNameTerm (final String packageName) { 212 assert packageName != null; 213 return new Term (FIELD_PACKAGE_NAME, packageName); 214 } 215 216 public static Term referencesTerm (String resourceName, final Set <ClassIndexImpl.UsageType> usageType) { 217 assert resourceName != null; 218 if (usageType != null) { 219 resourceName = encodeUsage (resourceName, usageType, WILDCARD).toString(); 220 } 221 else { 222 StringBuilder sb = new StringBuilder (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 resourceSimpleName) { 230 assert resourceSimpleName != null; 231 return new Term (FIELD_SIMPLE_NAME, resourceSimpleName); 232 } 233 234 public static Term caseInsensitiveNameTerm (final String caseInsensitiveName) { 235 assert caseInsensitiveName != null; 236 return new Term (FIELD_CASE_INSENSITIVE_NAME, caseInsensitiveName); 237 } 238 239 public static Document createDocument (final String binaryName, final long timeStamp, List <String > references) { 241 assert binaryName != null; 242 assert references != null; 243 int index = binaryName.lastIndexOf(PKG_SEPARATOR); String fileName, pkgName, simpleName, caseInsensitiveName; 245 if (index<0) { 246 fileName = binaryName; 247 pkgName = ""; } 249 else { 250 fileName = binaryName.substring(index+1); 251 pkgName = binaryName.substring(0,index); 252 } 253 index = fileName.lastIndexOf('$'); 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(); 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 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 public static StringBuilder createUsage (final String className) { 290 Set <ClassIndexImpl.UsageType> EMPTY = Collections.emptySet(); 291 return encodeUsage (className, EMPTY,NO); 292 } 293 294 public static void addUsage (final StringBuilder 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 encodeUsage (final String className, final Set <ClassIndexImpl.UsageType> usageTypes) { 303 return encodeUsage (className, usageTypes, NO).toString(); 304 } 305 306 private static StringBuilder encodeUsage (final String className, final Set <ClassIndexImpl.UsageType> usageTypes, char fill) { 307 assert className != null; 308 assert usageTypes != null; 309 StringBuilder builder = new StringBuilder (); 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 encodeUsage (final String className, final String usageMap) { 323 assert className != null; 324 assert usageMap != null; 325 StringBuilder sb = new StringBuilder (); 326 sb.append(className); 327 sb.append(usageMap); 328 return sb.toString(); 329 } 330 331 public static String decodeUsage (final String rawUsage, final Set <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 className = rawUsage.substring(0,index); 339 final String 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 378 379 } 380 | Popular Tags |