Skip to main content
summaryrefslogtreecommitdiffstats
blob: 02ece3c4479c12ac8c287ab1a637dc094073bbb8 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2006, 2015 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.base.remote;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.help.internal.search.SearchHit;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/*
 * Converts search results serialized by the SearchServlet on remote help server
 * back into model objects.
 */
public class RemoteSearchParser extends DefaultHandler {

	private SAXParser parser;
	private Stack<SearchHit> stack;
	private List<SearchHit> hits;
	private StringBuilder summary;

	/*
	 * Parses the given serialized search hits and returns generated model
	 * objects (SearchHit objects).
	 */
	public List<SearchHit> parse(InputStream in, IProgressMonitor monitor) throws ParserConfigurationException, SAXException, IOException {
		monitor.beginTask("", 1); //$NON-NLS-1$
		init();
		parser.parse(in, this);
		monitor.worked(1);
		monitor.done();
		return hits;
	}

	/*
	 * Initializes the parser's state for a new search. Must be called
	 * before each parse.
	 */
	private void init() throws ParserConfigurationException, SAXException {
		if (hits == null) {
			hits = new ArrayList<>();
		}
		else if (!hits.isEmpty()) {
			hits.clear();
		}
		if (stack == null) {
			stack = new Stack<>();
		}
		else if (!stack.isEmpty()) {
			stack.clear();
		}
		summary = null;
		if (parser == null) {
			parser = SAXParserFactory.newInstance().newSAXParser();
		}
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if (qName.equals("hit")) { //$NON-NLS-1$
			handleHit(attributes);
		}
		else if (qName.equals("summary")) { //$NON-NLS-1$
			handleSummary(attributes);
		}
	}

	/* (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (qName.equals("hit")) { //$NON-NLS-1$
			stack.pop();
		}
		else if (qName.equals("summary") && summary != null) { //$NON-NLS-1$
			// collect the summary from the buffer
			SearchHit hit = stack.peek();
			hit.setSummary(summary.toString());
			summary = null;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		// are we in <summary></summary> elements?
		if (summary != null) {
			// if so, add to the buffer
			summary.append(ch, start, length);
		}
	}

	private void handleHit(Attributes attr) {
		String href = attr.getValue("href"); //$NON-NLS-1$
		String label = attr.getValue("label"); //$NON-NLS-1$
		boolean isPotentialHit = (String.valueOf(true).equalsIgnoreCase(attr.getValue("isPotentialHit"))); //$NON-NLS-1$
		float score;
		try {
			score = Float.parseFloat(attr.getValue("score")); //$NON-NLS-1$
		}
		catch (NumberFormatException e) {
			// score was probably missing; default to 0
			score = 0;
		}
		SearchHit hit = new SearchHit(href, label, null, score, null, null, null, isPotentialHit);
		hits.add(hit);
		stack.push(hit);
	}

	private void handleSummary(Attributes attr) {
		// prepare the builder to receive text summary
		summary = new StringBuilder();
	}

	/*
	 * Note: throws clause does not declare IOException due to a bug in
	 * sun jdk: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6327149
	 *
	 * @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
	 */
	@Override
	public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
		return new InputSource(new StringReader("")); //$NON-NLS-1$
	}
}

Back to the top