Skip to main content
summaryrefslogtreecommitdiffstats
blob: 468c5454cfbdf8200e149dbdc695dd6e6ee73bde (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2005, 2016 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.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.base.util.ProxyUtil;
import org.eclipse.help.internal.xhtml.XHTMLContentDescriber;
import org.eclipse.help.search.IHelpSearchIndex;
import org.eclipse.help.search.ISearchDocument;
import org.eclipse.help.search.SearchParticipant;


public class HTMLSearchParticipant extends SearchParticipant {

	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;
	}


	@Override
	public IStatus addDocument(IHelpSearchIndex index, String pluginId, String name, URL url, String id,
			ISearchDocument doc) {
		// if it's XHTML, forward it on to the proper search participant
		if (isXHTML(pluginId, url)) {
			LocalSearchManager manager = BaseHelpSystem.getLocalSearchManager();
			SearchParticipant 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.addContents(parser.getContentReader(), parser.getContentReader());
					String title = parser.getTitle();
					doc.setTitle(title);
					doc.setSummary(parser.getSummary(title));
					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 SearchParticipant 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();
		}
		try (InputStream in = ProxyUtil.getStream(url)) {
			return (xhtmlDescriber.describe(in, null) == IContentDescriber.VALID);
		} catch (Exception e) {
			// if anything goes wrong, treat it as not xhtml
		}

		return false;
	}

}

Back to the top