1 22 23 package org.gjt.sp.jedit.help; 24 25 import java.io.*; 27 import java.net.*; 28 import java.util.zip.*; 29 import java.util.*; 30 import org.gjt.sp.jedit.io.*; 31 import org.gjt.sp.jedit.*; 32 import org.gjt.sp.util.Log; 33 35 class HelpIndex 36 { 37 public HelpIndex() 39 { 40 words = new HashMap(); 41 files = new ArrayList(); 42 43 ignoreWord("a"); 44 ignoreWord("an"); 45 ignoreWord("and"); 46 ignoreWord("are"); 47 ignoreWord("as"); 48 ignoreWord("be"); 49 ignoreWord("by"); 50 ignoreWord("can"); 51 ignoreWord("do"); 52 ignoreWord("for"); 53 ignoreWord("from"); 54 ignoreWord("how"); 55 ignoreWord("i"); 56 ignoreWord("if"); 57 ignoreWord("in"); 58 ignoreWord("is"); 59 ignoreWord("it"); 60 ignoreWord("not"); 61 ignoreWord("of"); 62 ignoreWord("on"); 63 ignoreWord("or"); 64 ignoreWord("s"); 65 ignoreWord("that"); 66 ignoreWord("the"); 67 ignoreWord("this"); 68 ignoreWord("to"); 69 ignoreWord("will"); 70 ignoreWord("with"); 71 ignoreWord("you"); 72 } 74 79 80 85 public void indexEditorHelp() 86 { 87 try 88 { 89 String jEditHome = jEdit.getJEditHome(); 90 if(jEditHome != null) 91 { 92 indexDirectory(MiscUtilities.constructPath(jEditHome,"doc","users-guide")); 93 indexDirectory(MiscUtilities.constructPath(jEditHome,"doc","FAQ")); 94 indexDirectory(MiscUtilities.constructPath(jEditHome,"doc","news43")); 95 } 96 } 97 catch(Throwable e) 98 { 99 Log.log(Log.ERROR,this,"Error indexing editor help"); 100 Log.log(Log.ERROR,this,e); 101 } 102 103 PluginJAR[] jars = jEdit.getPluginJARs(); 104 for(int i = 0; i < jars.length; i++) 105 { 106 try 107 { 108 indexJAR(jars[i].getZipFile()); 109 } 110 catch(Throwable e) 111 { 112 Log.log(Log.ERROR,this,"Error indexing JAR: " 113 + jars[i].getPath()); 114 Log.log(Log.ERROR,this,e); 115 } 116 } 117 118 Log.log(Log.DEBUG,this,"Indexed " + words.size() + " words"); 119 } 121 126 public void indexDirectory(String dir) throws Exception 127 { 128 String [] files = VFSManager.getFileVFS() 129 ._listDirectory(null,dir,"*.{html,txt}",true,null); 130 131 for(int i = 0; i < files.length; i++) 132 { 133 indexURL(files[i]); 134 } 135 } 137 142 public void indexJAR(ZipFile jar) throws Exception 143 { 144 Enumeration e = jar.entries(); 145 while(e.hasMoreElements()) 146 { 147 ZipEntry entry = (ZipEntry)e.nextElement(); 148 String name = entry.getName(); 149 String lname = name.toLowerCase(); 150 if(lname.endsWith(".html")) 151 { 152 String url = "jeditresource:/" + 154 MiscUtilities.getFileName(jar.getName()) 155 + "!/" + name; 156 Log.log(Log.DEBUG,this,url); 157 indexStream(jar.getInputStream(entry),url); 158 } 159 } 160 } 162 168 public void indexURL(String url) throws Exception 169 { 170 InputStream _in; 171 172 if(MiscUtilities.isURL(url)) 173 _in = new URL(url).openStream(); 174 else 175 { 176 _in = new FileInputStream(url); 177 url = "file:" + url; 179 } 180 181 indexStream(_in,url); 182 } 184 public Word lookupWord(String word) 186 { 187 Object o = words.get(word); 188 if(o == IGNORE) 189 return null; 190 else 191 return (Word)o; 192 } 194 public HelpFile getFile(int index) 196 { 197 return (HelpFile)files.get(index); 198 } 200 private static Object IGNORE = new Object (); 203 private HashMap words; 204 private ArrayList files; 205 206 private void ignoreWord(String word) 208 { 209 words.put(word,IGNORE); 210 } 212 219 private void indexStream(InputStream _in, String fileName) 220 throws Exception 221 { 222 HelpFile file = new HelpFile(fileName); 223 files.add(file); 224 int index = files.size() - 1; 225 226 StringBuffer titleText = new StringBuffer (); 227 228 BufferedReader in = new BufferedReader( 229 new InputStreamReader(_in)); 230 231 try 232 { 233 StringBuffer word = new StringBuffer (); 234 boolean insideTag = false; 235 boolean insideEntity = false; 236 237 boolean title = false; 238 239 int c; 240 while((c = in.read()) != -1) 241 { 242 char ch = (char)c; 243 if(insideTag) 244 { 245 if(ch == '>') 246 { 247 if(word.toString().equals("title")) 248 title = true; 249 insideTag = false; 250 word.setLength(0); 251 } 252 else 253 word.append(ch); 254 } 255 else if(insideEntity) 256 { 257 if(ch == ';') 258 insideEntity = false; 259 } 260 else if(ch == '<') 261 { 262 if(title) 263 title = false; 264 265 if(word.length() != 0) 266 { 267 addWord(word.toString(),index,title); 268 word.setLength(0); 269 } 270 271 insideTag = true; 272 } 273 else if(ch == '&') 274 insideEntity = true; 275 else if(title) 276 titleText.append(ch); 277 else if(!Character.isLetterOrDigit(ch)) 278 { 279 if(word.length() != 0) 280 { 281 addWord(word.toString(),index,title); 282 word.setLength(0); 283 } 284 } 285 else 286 word.append(ch); 287 } 288 } 289 finally 290 { 291 in.close(); 292 } 293 294 if(titleText.length() == 0) 295 file.title = fileName; 296 else 297 file.title = titleText.toString(); 298 } 300 private void addWord(String word, int file, boolean title) 302 { 303 word = word.toLowerCase(); 304 305 Object o = words.get(word); 306 if(o == IGNORE) 307 return; 308 309 if(o == null) 310 words.put(word,new Word(word,file,title)); 311 else 312 ((Word)o).addOccurrence(file,title); 313 } 315 317 static class Word 319 { 320 static final int TITLE_OCCUR = 10; 322 323 String word; 325 326 int occurCount = 0; 328 Occurrence[] occurrences; 329 330 Word(String word, int file, boolean title) 331 { 332 this.word = word; 333 occurrences = new Occurrence[5]; 334 addOccurrence(file,title); 335 } 336 337 void addOccurrence(int file, boolean title) 338 { 339 for(int i = 0; i < occurCount; i++) 340 { 341 if(occurrences[i].file == file) 342 { 343 occurrences[i].count += (title ? TITLE_OCCUR : 1); 344 return; 345 } 346 } 347 348 if(occurCount >= occurrences.length) 349 { 350 Occurrence[] newOccur = new Occurrence[occurrences.length * 2]; 351 System.arraycopy(occurrences,0,newOccur,0,occurCount); 352 occurrences = newOccur; 353 } 354 355 occurrences[occurCount++] = new Occurrence(file,title); 356 } 357 358 static class Occurrence 359 { 360 int file; 361 int count; 362 363 Occurrence(int file, boolean title) 364 { 365 this.file = file; 366 this.count = (title ? TITLE_OCCUR : 1); 367 } 368 } 369 } 371 static class HelpFile 373 { 374 String file; 375 String title; 376 377 HelpFile(String file) 378 { 379 this.file = file; 380 } 381 382 public String toString() 383 { 384 return title; 385 } 386 387 public boolean equals(Object o) 388 { 389 if(o instanceof HelpFile) 390 return ((HelpFile)o).file.equals(file); 391 else 392 return false; 393 } 394 } } 396 | Popular Tags |