KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > help > HelpIndex


1 /*
2  * HelpIndex.java - Index for help searching feature
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.help;
24
25 //{{{ Imports
26
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 //}}}
34

35 class HelpIndex
36 {
37     //{{{ HelpIndex constructor
38
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     } //}}}
73

74     /* //{{{ HelpIndex constructor
75     public HelpIndex(String fileListPath, String wordIndexPath)
76     {
77         this();
78     } //}}} */

79
80     //{{{ indexEditorHelp() method
81
/**
82      * Indexes all available help, including the jEdit user's guide, FAQ,]
83      * and plugin documentation.
84      */

85     public void indexEditorHelp()
86     {
87         try
88         {
89             String JavaDoc 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 JavaDoc 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 JavaDoc 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     } //}}}
120

121     //{{{ indexDirectory() method
122
/**
123      * Indexes all HTML and text files in the specified directory.
124      * @param dir The directory
125      */

126     public void indexDirectory(String JavaDoc dir) throws Exception JavaDoc
127     {
128         String JavaDoc[] 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     } //}}}
136

137     //{{{ indexJAR() method
138
/**
139      * Indexes all HTML and text files in the specified JAR file.
140      * @param jar The JAR file
141      */

142     public void indexJAR(ZipFile jar) throws Exception JavaDoc
143     {
144         Enumeration e = jar.entries();
145         while(e.hasMoreElements())
146         {
147             ZipEntry entry = (ZipEntry)e.nextElement();
148             String JavaDoc name = entry.getName();
149             String JavaDoc lname = name.toLowerCase();
150             if(lname.endsWith(".html")/* || lname.endsWith(".txt") */)
151             {
152                 // only works for jEdit plugins
153
String JavaDoc 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     } //}}}
161

162     //{{{ indexURL() method
163
/**
164      * Reads the specified HTML file and adds all words defined therein to the
165      * index.
166      * @param url The HTML file's URL
167      */

168     public void indexURL(String JavaDoc url) throws Exception JavaDoc
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             // hack since HelpViewer needs a URL...
178
url = "file:" + url;
179         }
180
181         indexStream(_in,url);
182     } //}}}
183

184     //{{{ lookupWord() method
185
public Word lookupWord(String JavaDoc word)
186     {
187         Object JavaDoc o = words.get(word);
188         if(o == IGNORE)
189             return null;
190         else
191             return (Word)o;
192     } //}}}
193

194     //{{{ getFile() method
195
public HelpFile getFile(int index)
196     {
197         return (HelpFile)files.get(index);
198     } //}}}
199

200     //{{{ Private members
201
// used to mark words to ignore (see constructor for the list)
202
private static Object JavaDoc IGNORE = new Object JavaDoc();
203     private HashMap words;
204     private ArrayList files;
205
206     //{{{ ignoreWord() method
207
private void ignoreWord(String JavaDoc word)
208     {
209         words.put(word,IGNORE);
210     } //}}}
211

212     //{{{ indexStream() method
213
/**
214      * Reads the specified HTML file and adds all words defined therein to the
215      * index.
216      * @param _in The input stream
217      * @param file The file
218      */

219     private void indexStream(InputStream _in, String JavaDoc fileName)
220         throws Exception JavaDoc
221     {
222         HelpFile file = new HelpFile(fileName);
223         files.add(file);
224         int index = files.size() - 1;
225
226         StringBuffer JavaDoc titleText = new StringBuffer JavaDoc();
227
228         BufferedReader in = new BufferedReader(
229             new InputStreamReader(_in));
230
231         try
232         {
233             StringBuffer JavaDoc word = new StringBuffer JavaDoc();
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     } //}}}
299

300     //{{{ addWord() method
301
private void addWord(String JavaDoc word, int file, boolean title)
302     {
303         word = word.toLowerCase();
304
305         Object JavaDoc 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     } //}}}
314

315     //}}}
316

317     //{{{ Word class
318
static class Word
319     {
320         // how much an occurrence in the title is worth
321
static final int TITLE_OCCUR = 10;
322
323         // the word
324
String JavaDoc word;
325
326         // files it occurs in
327
int occurCount = 0;
328         Occurrence[] occurrences;
329
330         Word(String JavaDoc 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     } //}}}
370

371     //{{{ HelpFile class
372
static class HelpFile
373     {
374         String JavaDoc file;
375         String JavaDoc title;
376
377         HelpFile(String JavaDoc file)
378         {
379             this.file = file;
380         }
381
382         public String JavaDoc toString()
383         {
384             return title;
385         }
386
387         public boolean equals(Object JavaDoc o)
388         {
389             if(o instanceof HelpFile)
390                 return ((HelpFile)o).file.equals(file);
391             else
392                 return false;
393         }
394     } //}}}
395
}
396
Popular Tags