Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5826ed4020df86f700c0e4609aafec5312e0aff6 (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
/*******************************************************************************
 * 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.dynamic;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.help.HelpSystem;
import org.eclipse.help.IUAElement;
import org.eclipse.help.internal.UAElement;
import org.xml.sax.SAXException;

/*
 * Resolves includes by parsing the target document, extracting the node and
 * replacing the original include with it.
 */
public class IncludeResolver {

	private static final String ATTRIBUTE_ID = "id"; //$NON-NLS-1$

	private DocumentProcessor processor;
	private DocumentReader reader;
	private String locale;

	public IncludeResolver(DocumentProcessor processor, DocumentReader reader, String locale) {
		this.processor = processor;
		this.reader = reader;
		this.locale = locale;
	}

	/*
	 * Resolves the include target to a processed Element.
	 */
	public UAElement resolve(String bundleId, String relativePath, String elementId) throws IOException, SAXException, ParserConfigurationException {
		String href = '/' + bundleId + '/' + relativePath;
		InputStream in = HelpSystem.getHelpContent(href, locale);
		try {
			UAElement element = findElement(in, elementId);
			processor.process(element, href);
			return element;
		}
		finally {
			try {
				in.close();
			}
			catch (IOException e) {}
		}
	}

	/*
	 * Finds the specified element from the given XML input stream.
	 */
	private UAElement findElement(InputStream in, String elementId) throws IOException, SAXException, ParserConfigurationException {
		UAElement element = reader.read(in);
		return findElement(element, elementId);
	}

	/*
	 * Finds the specified element under the given subtree.
	 */
	private UAElement findElement(UAElement element, String elementId) {
		String id = element.getAttribute(ATTRIBUTE_ID);
		if (id != null && id.equals(elementId)) {
			return element;
		}
		IUAElement[] children = element.getChildren();
		for (int i=0;i<children.length;++i) {
			UAElement result = findElement((UAElement)children[i], elementId);
			if (result != null) {
				return result;
			}
		}
		return null;
	}
}

Back to the top