1 11 package org.eclipse.help.internal.search; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 17 import org.apache.lucene.document.Document; 18 import org.apache.lucene.document.Field; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.core.runtime.content.IContentDescriber; 22 import org.eclipse.help.internal.base.BaseHelpSystem; 23 import org.eclipse.help.internal.base.HelpBasePlugin; 24 import org.eclipse.help.internal.xhtml.XHTMLContentDescriber; 25 import org.eclipse.help.search.ISearchIndex; 26 import org.eclipse.help.search.LuceneSearchParticipant; 27 28 29 public class HTMLSearchParticipant extends LuceneSearchParticipant { 30 31 private static final String HELP_BASE_XHTML = "org.eclipse.help.base.xhtml"; private HTMLDocParser parser; 33 private String indexPath; 34 private IContentDescriber xhtmlDescriber; 35 private XHTMLSearchParticipant xhtmlParticipant; 36 37 public HTMLSearchParticipant(String indexPath) { 38 parser = new HTMLDocParser(); 39 this.indexPath = indexPath; 40 } 41 42 public IStatus addDocument(ISearchIndex index, String pluginId, String name, URL url, String id, 43 Document doc) { 44 if (isXHTML(pluginId, url)) { 46 LocalSearchManager manager = BaseHelpSystem.getLocalSearchManager(); 47 LuceneSearchParticipant participant = manager.getParticipant(HELP_BASE_XHTML); 48 if (participant == null) { 49 participant = getXhtmlParticipant(); 50 } 51 return participant.addDocument(index, pluginId, name, url, id, doc); 52 } 53 else { 55 try { 56 try { 57 try { 58 parser.openDocument(url); 59 } catch (IOException ioe) { 60 return new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, IStatus.ERROR, 61 "Help document " + name + " cannot be opened.", null); 64 } 65 ParsedDocument parsed = new ParsedDocument(parser.getContentReader()); 66 doc.add(new Field("contents", parsed.newContentReader())); doc.add(new Field("exact_contents", parsed.newContentReader())); String title = parser.getTitle(); 69 doc.add(new Field("title", title, Field.Store.NO, Field.Index.TOKENIZED)); doc.add(new Field("exact_title", title, Field.Store.NO, Field.Index.TOKENIZED)); doc.add(new Field("raw_title", title, Field.Store.YES, Field.Index.NO)); doc.add(new Field("summary", parser.getSummary(title), Field.Store.YES, Field.Index.NO)); } finally { 74 parser.closeDocument(); 75 } 76 } catch (IOException e) { 77 return new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, IStatus.ERROR, 78 "IO exception occurred while adding document " + name + " to index " + indexPath + ".", e); 81 } 82 return Status.OK_STATUS; 83 } 84 } 85 86 private XHTMLSearchParticipant getXhtmlParticipant() { 87 if (xhtmlParticipant == null) { 88 xhtmlParticipant = new XHTMLSearchParticipant(); 89 } 90 return xhtmlParticipant; 91 } 92 93 100 private boolean isXHTML(String pluginId, URL url) { 101 if (xhtmlDescriber == null) { 102 xhtmlDescriber = new XHTMLContentDescriber(); 103 } 104 InputStream in = null; 105 try { 106 in = url.openStream(); 107 return (xhtmlDescriber.describe(in, null) == IContentDescriber.VALID); 108 } catch (Exception e) { 109 } finally { 111 if (in != null) { 112 try { 113 in.close(); 114 } catch (IOException e) { 115 } 117 } 118 } 119 120 return false; 121 } 122 } 123 | Popular Tags |