Skip to main content
summaryrefslogtreecommitdiffstats
blob: 48c905c6d7cd246b22b94cb5be71d20072a9cb50 (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
/*******************************************************************************
 * Copyright (c) 2011, 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.webapp.parser;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

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

import org.eclipse.help.internal.webapp.utils.JSonHelper;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ResultParser extends DefaultHandler {

	protected String id;
	protected String label;
	protected ArrayList<ParseElement> items = new ArrayList<>(); //parser populates the items arrayList withe parsed data.

	public ResultParser(String label) {
		this(label, JSonHelper.ID);
	}

	public ResultParser(String label, String id) {
		this.label = label;
		this.id = id;
	}

	public void parse(URL url)
		throws ParserConfigurationException, SAXException, IOException
	{
		parse(url.openStream());
	}

	public void parse(InputStream in)
		throws ParserConfigurationException, SAXException, IOException
	{
		SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
		parser.parse(in, this);
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public void setIdentifier(String id) {
		this.id = id;
	}

	public ArrayList<ParseElement> getItems()
	{
		return items;
	}

	@Override
	public String toString()
	{
		return items.toString();
	}

	public String toJSON() {

		StringBuffer buf = new StringBuffer();

		buf.append(JSonHelper.BEGIN_BRACE);
		buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);

		buf.append(JSonHelper.IDENTIFIER);
		buf.append(JSonHelper.COLON);
		buf.append(JSonHelper.getQuotes(id));
		buf.append(JSonHelper.COMMA);

		buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
		buf.append(JSonHelper.LABEL);
		buf.append(JSonHelper.COLON);
		buf.append(JSonHelper.getQuotes(label));
		buf.append(JSonHelper.COMMA);

		buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);
		buf.append(JSonHelper.ITEMS);
		buf.append(JSonHelper.COLON);
		buf.append(JSonHelper.BEGIN_BRACKET);

		for (int i = 0; i < items.size(); i++) {

			if (i > 0)
				buf.append(JSonHelper.COMMA);

			ParseElement element = items.get(i);
			buf.append(element.toJSON(1));
		}

		if (items.size() > 0)
			buf.append(JSonHelper.NEWLINE + JSonHelper.SPACE);

		buf.append(JSonHelper.END_BRACKET);
		buf.append(JSonHelper.NEWLINE);
		buf.append(JSonHelper.END_BRACE);

		return buf.toString();
	}
}

Back to the top