1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*******************************************************************************
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.help.internal.search;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentDescriber;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.base.HelpBasePlugin;
import org.eclipse.help.internal.xhtml.XHTMLContentDescriber;
import org.eclipse.help.search.ISearchIndex;
import org.eclipse.help.search.LuceneSearchParticipant;
public class HTMLSearchParticipant extends LuceneSearchParticipant {
private static final String HELP_BASE_XHTML = "org.eclipse.help.base.xhtml"; //$NON-NLS-1$
private HTMLDocParser parser;
private String indexPath;
private IContentDescriber xhtmlDescriber;
private XHTMLSearchParticipant xhtmlParticipant;
public HTMLSearchParticipant(String indexPath) {
parser = new HTMLDocParser();
this.indexPath = indexPath;
}
public IStatus addDocument(ISearchIndex index, String pluginId, String name, URL url, String id,
Document doc) {
// if it's XHTML, forward it on to the proper search participant
if (isXHTML(pluginId, url)) {
LocalSearchManager manager = BaseHelpSystem.getLocalSearchManager();
LuceneSearchParticipant participant = manager.getParticipant(HELP_BASE_XHTML);
if (participant == null) {
participant = getXhtmlParticipant();
}
return participant.addDocument(index, pluginId, name, url, id, doc);
}
// otherwise, treat it as HTML
else {
try {
try {
try {
parser.openDocument(url);
} catch (IOException ioe) {
return new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, IStatus.ERROR,
"Help document " //$NON-NLS-1$
+ name + " cannot be opened.", //$NON-NLS-1$
null);
}
doc.add(new Field("contents", parser.getContentReader())); //$NON-NLS-1$
doc.add(new Field("exact_contents", parser.getContentReader())); //$NON-NLS-1$
String title = parser.getTitle();
doc.add(new Field("title", title, Field.Store.NO, Field.Index.TOKENIZED)); //$NON-NLS-1$
doc.add(new Field("exact_title", title, Field.Store.NO, Field.Index.TOKENIZED)); //$NON-NLS-1$
doc.add(new Field("raw_title", title, Field.Store.YES, Field.Index.NO)); //$NON-NLS-1$
doc.add(new Field("summary", parser.getSummary(title), Field.Store.YES, Field.Index.NO)); //$NON-NLS-1$
if (parser.getException() != null) {
return new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, IStatus.ERROR,
"Parse error occurred while adding document " + name //$NON-NLS-1$
+ " to search index " + indexPath + ".", //$NON-NLS-1$ //$NON-NLS-2$
parser.getException());
}
} finally {
parser.closeDocument();
}
} catch (IOException e) {
return new Status(IStatus.ERROR, HelpBasePlugin.PLUGIN_ID, IStatus.ERROR,
"IO exception occurred while adding document " + name //$NON-NLS-1$
+ " to search index " + indexPath + ".", //$NON-NLS-1$ //$NON-NLS-2$
e);
}
return Status.OK_STATUS;
}
}
private XHTMLSearchParticipant getXhtmlParticipant() {
if (xhtmlParticipant == null) {
xhtmlParticipant = new XHTMLSearchParticipant();
}
return xhtmlParticipant;
}
/**
* Returns whether or not the given content should be treated as XHTML.
*
* @param pluginId the plugin id containing the content
* @param url the URL to the content
* @return whether the content should be treated as XHTML
*/
private boolean isXHTML(String pluginId, URL url) {
if (xhtmlDescriber == null) {
xhtmlDescriber = new XHTMLContentDescriber();
}
InputStream in = null;
try {
in = url.openStream();
return (xhtmlDescriber.describe(in, null) == IContentDescriber.VALID);
} catch (Exception e) {
// if anything goes wrong, treat it as not xhtml
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// nothing we can do
}
}
}
return false;
}
}
|