PageRenderTime 53ms CodeModel.GetById 35ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/help/HelpIndex.java

#
Java | 258 lines | 167 code | 29 blank | 62 comment | 36 complexity | 59b137168bbdbb86e1a58452ad313770 MD5 | raw file
  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
 23package org.gjt.sp.jedit.help;
 24
 25//{{{ Imports
 26import java.io.*;
 27import java.net.*;
 28import java.util.zip.*;
 29import java.util.*;
 30import org.gjt.sp.jedit.io.*;
 31import org.gjt.sp.jedit.*;
 32import org.gjt.sp.util.Log;
 33//}}}
 34
 35public class HelpIndex
 36{
 37	//{{{ HelpIndex constructor
 38	public HelpIndex()
 39	{
 40		words = new HashMap();
 41	} //}}}
 42
 43	//{{{ HelpIndex constructor
 44	public HelpIndex(String fileListPath, String wordIndexPath)
 45	{
 46		this();
 47	} //}}}
 48
 49	//{{{ indexEditorHelp() method
 50	/**
 51	 * Indexes all available help, including the jEdit user's guide, FAQ, and
 52	 * plugin documentation.
 53	 */
 54	public void indexEditorHelp() throws Exception
 55	{
 56		String jEditHome = jEdit.getJEditHome();
 57		if(jEditHome != null)
 58			indexDirectory(MiscUtilities.constructPath(jEditHome,"doc"));
 59
 60		EditPlugin.JAR[] jars = jEdit.getPluginJARs();
 61		for(int i = 0; i < jars.length; i++)
 62		{
 63			indexJAR(jars[i].getZipFile());
 64		}
 65
 66		Log.log(Log.DEBUG,this,"Indexed " + words.size() + " words");
 67	} //}}}
 68
 69	//{{{ indexDirectory() method
 70	/**
 71	 * Indexes all HTML and text files in the specified directory.
 72	 * @param dir The directory
 73	 */
 74	public void indexDirectory(String dir) throws Exception
 75	{
 76		String[] files = VFSManager.getFileVFS()
 77			._listDirectory(null,dir,"*.{html,txt}",true,null);
 78
 79		for(int i = 0; i < files.length; i++)
 80		{
 81			indexURL(files[i]);
 82		}
 83	} //}}}
 84
 85	//{{{ indexJAR() method
 86	/**
 87	 * Indexes all HTML and text files in the specified JAR file.
 88	 * @param jar The JAR file
 89	 */
 90	public void indexJAR(ZipFile jar) throws Exception
 91	{
 92		Enumeration enum = jar.entries();
 93		while(enum.hasMoreElements())
 94		{
 95			ZipEntry entry = (ZipEntry)enum.nextElement();
 96			String name = entry.getName();
 97			String lname = name.toLowerCase();
 98			if(lname.endsWith(".html") || lname.endsWith(".txt"))
 99			{
100				// only works for jEdit plugins
101				String url = "jeditresource:/" +
102					MiscUtilities.getFileName(jar.getName())
103					+ "!" + name;
104				Log.log(Log.DEBUG,this,url);
105				indexStream(jar.getInputStream(entry),url);
106			}
107		}
108	} //}}}
109
110	//{{{ indexURL() method
111	/**
112	 * Reads the specified HTML file and adds all words defined therein to the
113	 * index.
114	 * @param url The HTML file's URL
115	 */
116	public void indexURL(String url) throws Exception
117	{
118		InputStream _in;
119
120		if(MiscUtilities.isURL(url))
121			_in =  new URL(url).openStream();
122		else
123		{
124			_in = new FileInputStream(url);
125			// hack since HelpViewer needs a URL...
126			url = "file:" + url;
127		}
128
129		indexStream(_in,url);
130	} //}}}
131
132	//{{{ getWord() method
133	public Word getWord(String word)
134	{
135		return (Word)words.get(word);
136	} //}}}
137
138	//{{{ Private members
139	private HashMap words;
140
141	//{{{ indexStream() method
142	/**
143	 * Reads the specified HTML file and adds all words defined therein to the
144	 * index.
145	 * @param _in The input stream
146	 * @param file The file
147	 */
148	private void indexStream(InputStream _in, String file) throws Exception
149	{
150		BufferedReader in = new BufferedReader(new InputStreamReader(_in));
151
152		try
153		{
154			StringBuffer word = new StringBuffer();
155			boolean insideTag = false;
156			boolean insideEntity = false;
157
158			int c;
159			while((c = in.read()) != -1)
160			{
161				char ch = (char)c;
162				if(insideTag)
163				{
164					if(ch == '>')
165						insideTag = false;
166				}
167				else if(insideEntity)
168				{
169					if(ch == ';')
170						insideEntity = false;
171				}
172				else if(ch == '<')
173					insideTag = true;
174				else if(ch == '&')
175					insideEntity = true;
176				else if(!Character.isLetterOrDigit(ch))
177				{
178					if(word.length() != 0)
179					{
180						addWord(word.toString(),file);
181						word.setLength(0);
182					}
183				}
184				else
185					word.append(ch);
186			}
187		}
188		finally
189		{
190			in.close();
191		}
192	} //}}}
193
194	//{{{ addWord() method
195	private void addWord(String word, String file)
196	{
197		word = word.toLowerCase();
198
199		Word w = (Word)words.get(word);
200		if(w == null)
201			words.put(word,new Word(word,file));
202		else
203			w.addOccurrence(file);
204	} //}}}
205
206	//}}}
207
208	//{{{ Word class
209	public static class Word
210	{
211		// the word
212		String word;
213
214		// files it occurs in
215		int fileCount = 0;
216		String[] files;
217
218		Word(String word, String file)
219		{
220			this.word = word;
221			files = new String[5];
222			addOccurrence(file);
223		}
224
225		void addOccurrence(String file)
226		{
227			if(file == null)
228				throw new NullPointerException();
229
230			for(int i = 0; i < fileCount; i++)
231			{
232				if(files[i] == file)
233					return;
234			}
235
236			if(fileCount >= files.length)
237			{
238				String[] newFiles = new String[files.length * 2];
239				System.arraycopy(files,0,newFiles,0,fileCount);
240				files = newFiles;
241			}
242
243			files[fileCount++] = file;
244		}
245
246		public String toString()
247		{
248			StringBuffer buf = new StringBuffer();
249			for(int i = 0; i < fileCount; i++)
250			{
251				if(i != 0)
252					buf.append(",");
253				buf.append(files[i]);
254			}
255			return buf.toString();
256		}
257	} //}}}
258}