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