Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4238da55af3bc7842f82d1ed11e94445c8e91ec7 (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
/***************************************************************************************************
 * Copyright (c) 2006 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;

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

import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.help.IHelpContentProducer;
import org.eclipse.help.internal.util.ResourceLocator;
import org.eclipse.help.internal.xhtml.UAContentParser;
import org.eclipse.help.internal.xhtml.UATransformManager;
import org.eclipse.help.internal.xhtml.XHTMLSupport;
import org.osgi.framework.Bundle;
import org.w3c.dom.Document;

/**
 * The entry for all the dynamic format support. Depending on the file extension, this content
 * producer delegates to content producers that can handle one format.
 */

public class DynamicContentProducer implements IHelpContentProducer {

	public InputStream getInputStream(String pluginID, String href, Locale locale) {
		int qloc = href.indexOf('?');
		if (qloc != -1)
			href = href.substring(0, qloc);
		int loc = href.lastIndexOf('.');
		if (loc != -1) {
			String extension = href.substring(loc + 1).toLowerCase();
			if ("xhtml".equals(extension)) //$NON-NLS-1$
				return openXHTMLFromPlugin(pluginID, href, locale.toString());
			// place support for other formats here
		}
		return null;
	}

	/**
	 * Opens an input stream to an xhtml file contained in a plugin. This includes includes OS, WS
	 * and NL lookup.
	 * 
	 * @param pluginDesc
	 *            the plugin description of the plugin that contains the file you are trying to find
	 * @param file
	 *            the relative path of the file to find
	 * @param locale
	 *            the locale used as an override or <code>null</code> to use the default locale
	 * 
	 * @return an InputStream to the file or <code>null</code> if the file wasn't found
	 */
	private InputStream openXHTMLFromPlugin(String pluginID, String file, String locale) {
		InputStream inputStream = openStreamFromPlugin(pluginID, file, locale);
		if (inputStream != null) {
			UAContentParser parser = new UAContentParser(inputStream);
			Document dom = parser.getDocument();
			XHTMLSupport support = new XHTMLSupport(pluginID, file, dom, locale);
			dom = support.processDOM();
			try {
				inputStream.close();
			} catch (IOException e) {
			}
			return UATransformManager.getAsInputStream(dom);
		}
		return null;
	}

	/**
	 * Opens the stream from a file relative to the plug-in and the provided locale.
	 * 
	 * @param pluginID
	 *            the unique plug-in ID
	 * @param file
	 *            the relative file name
	 * @param locale
	 *            the locale
	 * @return the input stream or <code>null</code> if the operation failed for some reason. The
	 *         caller is responsible for closing the stream.
	 */

	public static InputStream openStreamFromPlugin(String pluginID, String file, String locale) {
		ArrayList pathPrefix = ResourceLocator.getPathPrefix(locale);

		Bundle pluginDesc = Platform.getBundle(pluginID);

		URL flatFileURL = ResourceLocator.find(pluginDesc, new Path(file), pathPrefix);
		if (flatFileURL != null) {
			try {
				return flatFileURL.openStream();
			} catch (IOException e) {
				return null;
			}
		}
		return null;
	}
}

Back to the top