Skip to main content
summaryrefslogtreecommitdiffstats
blob: a985a68ac736608ecc33b04c6c48d757d93a43ea (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
/*******************************************************************************
 * Copyright (c) 2011 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.Properties;

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.help.internal.base.util.ProxyUtil;
import org.eclipse.help.internal.webapp.utils.JSonHelper;
import org.eclipse.help.internal.webapp.utils.XMLHelper;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class TocFragmentParser extends ResultParser {

	private Properties properties;
	private String parentId = ""; //$NON-NLS-1$
	private int tagLevel = -1;
	private int level = 0;
	
	public TocFragmentParser() {
		super(JSonHelper.TITLE);
	}

	public void parse(URL tocURL, int level) 
		throws ParserConfigurationException, SAXException, IOException
	{
		parse(ProxyUtil.getStream(tocURL), level);
	}
	
	public void parse(InputStream in, int level) 
		throws ParserConfigurationException, SAXException, IOException
	{
		this.level = level;
		super.parse(in);
	}

	public void startElement(String uri, 
			String lname, String name, Attributes attrs) {
		
		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_NODE) 
				|| name.equalsIgnoreCase(XMLHelper.ELEMENT_NUMERIC_PATH)) {
			
			tagLevel++;
			
			if (tagLevel == level) {
				properties = new Properties();
				if (name.equalsIgnoreCase(XMLHelper.ELEMENT_NUMERIC_PATH)) {
					setLabel(""); //$NON-NLS-1$
					properties.put(JSonHelper.PROPERTY_NAME, JSonHelper.NUMERIC_PATH);
				}
				else
					properties.put(JSonHelper.PROPERTY_NAME, JSonHelper.TOPIC);
				
				for (int i = 0; i < attrs.getLength(); i++) {
					String qname = attrs.getQName(i);
					String val = attrs.getValue(i);
					if (qname.equals(XMLHelper.ATTR_ID)) {
						if (parentId.length() > 0)
							val = parentId + '$' + val;
					}
					properties.put(qname, val);
				}
			} else if (parentId.length() <= 0) {
				for (int i = 0; i < attrs.getLength(); i++) {
					if (attrs.getQName(i).equals(XMLHelper.ATTR_ID))
						parentId = attrs.getValue(i);
				}
			}
		}
	}

	public void endElement(String uri, String lname, String name) {

		if (name.equalsIgnoreCase(XMLHelper.ELEMENT_NODE) 
				|| name.equalsIgnoreCase(XMLHelper.ELEMENT_NUMERIC_PATH))
		{
			if (tagLevel == level && properties != null ) {
				
				if (name.equalsIgnoreCase(XMLHelper.ELEMENT_NUMERIC_PATH)) {
					properties.setProperty("id", "0"); //$NON-NLS-1$ //$NON-NLS-2$
					properties.setProperty("type", "topic"); //$NON-NLS-1$ //$NON-NLS-2$
				}
				else
					properties.setProperty("type", "toc"); //$NON-NLS-1$ //$NON-NLS-2$
				
				ParseElement element = new ParseElement(properties);
				items.add(element);
				
				properties = null;
			}
			
			tagLevel--;
		}
	}
}

Back to the top