KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > HTMLSearchParticipant


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.search;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
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 JavaDoc HELP_BASE_XHTML = "org.eclipse.help.base.xhtml"; //$NON-NLS-1$
32
private HTMLDocParser parser;
33     private String JavaDoc indexPath;
34     private IContentDescriber xhtmlDescriber;
35     private XHTMLSearchParticipant xhtmlParticipant;
36
37     public HTMLSearchParticipant(String JavaDoc indexPath) {
38         parser = new HTMLDocParser();
39         this.indexPath = indexPath;
40     }
41
42     public IStatus addDocument(ISearchIndex index, String JavaDoc pluginId, String JavaDoc name, URL JavaDoc url, String JavaDoc id,
43             Document doc) {
44         // if it's XHTML, forward it on to the proper search participant
45
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         // otherwise, treat it as HTML
54
else {
55             try {
56                 try {
57                     try {
58                         parser.openDocument(url);
59                     } catch (IOException JavaDoc ioe) {
60                         return new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, IStatus.ERROR,
61                                 "Help document " //$NON-NLS-1$
62
+ name + " cannot be opened.", //$NON-NLS-1$
63
null);
64                     }
65                     ParsedDocument parsed = new ParsedDocument(parser.getContentReader());
66                     doc.add(new Field("contents", parsed.newContentReader())); //$NON-NLS-1$
67
doc.add(new Field("exact_contents", parsed.newContentReader())); //$NON-NLS-1$
68
String JavaDoc title = parser.getTitle();
69                     doc.add(new Field("title", title, Field.Store.NO, Field.Index.TOKENIZED)); //$NON-NLS-1$
70
doc.add(new Field("exact_title", title, Field.Store.NO, Field.Index.TOKENIZED)); //$NON-NLS-1$
71
doc.add(new Field("raw_title", title, Field.Store.YES, Field.Index.NO)); //$NON-NLS-1$
72
doc.add(new Field("summary", parser.getSummary(title), Field.Store.YES, Field.Index.NO)); //$NON-NLS-1$
73
} finally {
74                     parser.closeDocument();
75                 }
76             } catch (IOException JavaDoc e) {
77                 return new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, IStatus.ERROR,
78                         "IO exception occurred while adding document " + name //$NON-NLS-1$
79
+ " to index " + indexPath + ".", //$NON-NLS-1$ //$NON-NLS-2$
80
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     /**
94      * Returns whether or not the given content should be treated as XHTML.
95      *
96      * @param pluginId the plugin id containing the content
97      * @param url the URL to the content
98      * @return whether the content should be treated as XHTML
99      */

100     private boolean isXHTML(String JavaDoc pluginId, URL JavaDoc url) {
101         if (xhtmlDescriber == null) {
102             xhtmlDescriber = new XHTMLContentDescriber();
103         }
104         InputStream JavaDoc in = null;
105         try {
106             in = url.openStream();
107             return (xhtmlDescriber.describe(in, null) == IContentDescriber.VALID);
108         } catch (Exception JavaDoc e) {
109             // if anything goes wrong, treat it as not xhtml
110
} finally {
111             if (in != null) {
112                 try {
113                     in.close();
114                 } catch (IOException JavaDoc e) {
115                     // nothing we can do
116
}
117             }
118         }
119
120         return false;
121     }
122 }
123
Popular Tags