Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8e9ac86049a33d05e684403432f145092f9fce46 (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
118
/*******************************************************************************
 * Copyright (c) 2005, 2010 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.InputStream;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.help.internal.base.HelpBasePlugin;
import org.eclipse.help.internal.xhtml.DynamicXHTMLProcessor;
import org.eclipse.help.search.SearchParticipantXML;
import org.xml.sax.Attributes;

/**
 * The search participant responsible for indexing XHTML documents.
 */
public class XHTMLSearchParticipant extends SearchParticipantXML {
	
	private static final String KEYWORDS = "keywords"; //$NON-NLS-1$
	private static final String META_TAG = "meta"; //$NON-NLS-1$
	private static final String DESCRIPTION = "description"; //$NON-NLS-1$
	private static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
	private static final String CONTENT_ATTRIBUTE = "content"; //$NON-NLS-1$
	private String title;
	private String summary;
	private boolean hasDescriptionMetaTag = false;
	
	/* (non-Javadoc)
	 * @see org.eclipse.help.search.XMLSearchParticipant#handleEndElement(java.lang.String, org.eclipse.help.search.XMLSearchParticipant.IParsedXMLContent)
	 */
	protected void handleEndElement(String name, IParsedXMLContent data) {
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.help.search.XMLSearchParticipant#handleStartElement(java.lang.String, org.xml.sax.Attributes, org.eclipse.help.search.XMLSearchParticipant.IParsedXMLContent)
	 */
	protected void handleStartElement(String name, Attributes attributes, IParsedXMLContent data) {
	    title = null;
	    if (META_TAG.equalsIgnoreCase(name)) {
	    	String nameAttribute = attributes.getValue(NAME_ATTRIBUTE);
	    	if (DESCRIPTION.equalsIgnoreCase(nameAttribute)) {
	    		String descriptionAttribute = attributes.getValue(CONTENT_ATTRIBUTE);
	    		if (descriptionAttribute != null) {
	    			hasDescriptionMetaTag = true;
		    		data.addToSummary(descriptionAttribute);
	    			data.addText(" "); //$NON-NLS-1$
		    		data.addText(descriptionAttribute);
	    			data.addText(" "); //$NON-NLS-1$
	    		}
	    	} else if (KEYWORDS.equalsIgnoreCase(nameAttribute)) {
	    		String keywordsAttribute = attributes.getValue(CONTENT_ATTRIBUTE);
	    		if (keywordsAttribute != null) {
	    			data.addText(" "); //$NON-NLS-1$
		    		data.addText(keywordsAttribute);
	    			data.addText(" "); //$NON-NLS-1$
	    		}
	    	}
	    }
	}
	
	protected void handleStartDocument(IParsedXMLContent data) {
		hasDescriptionMetaTag = false;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.help.search.XMLSearchParticipant#handleText(java.lang.String, org.eclipse.help.search.XMLSearchParticipant.IParsedXMLContent)
	 */
	protected void handleText(String text, IParsedXMLContent data) {
		String stackPath = getElementStackPath();
		IPath path = new Path(stackPath);
		if (path.segment(1).equalsIgnoreCase("body") &&  //$NON-NLS-1$
			!isSkipped(path.segment(path.segmentCount() -1))) {
			data.addText(text);
			if (!hasDescriptionMetaTag) {
				data.addToSummary(text);
			}
		} else if (path.segment(1).equalsIgnoreCase("head")) { //$NON-NLS-1$
			if (path.segment(path.segmentCount() -1).equalsIgnoreCase("title")) { //$NON-NLS-1$
				if (title == null) { 
					title = text;
				} else {
					title = title + text;
				}
				data.setTitle(title);
			}
		}
	}
	
	private boolean isSkipped(String tag) {
		return tag.equals("script"); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.help.search.XMLSearchParticipant#preprocess(java.io.InputStream, java.lang.String, java.lang.String)
	 */
	protected InputStream preprocess(InputStream in, String name, String locale) {
		try {
			return DynamicXHTMLProcessor.process(name, in, locale, false);
		}
		catch (Throwable t) {
			String msg = "An error occured while pre-processing help XHTML document \"" + name + "\" for search indexing"; //$NON-NLS-1$ //$NON-NLS-2$
			HelpBasePlugin.logError(msg, t);
			return in;
		}
	}
		
	public String getSummary() {
		return summary;
	}
}

Back to the top