Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3a33356feabf55714864e1985b28c19d6388441e (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*******************************************************************************
 * Copyright (c) 2000, 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.data;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.base.MissingContentManager;
import org.eclipse.help.internal.base.remote.RemoteStatusData;
import org.eclipse.help.internal.webapp.HelpWebappPlugin;
import org.eclipse.help.webapp.AbstractView;
import org.eclipse.osgi.service.localization.BundleLocalization;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class LayoutData extends RequestData {

	private static final String VIEW_EXTENSION_POINT = "org.eclipse.help.webapp.view"; //$NON-NLS-1$
	private String query = ""; //$NON-NLS-1$
	private AbstractView[] views;

	public LayoutData(ServletContext context, HttpServletRequest request,
			HttpServletResponse response) {
		super(context, request, response);

		// initialize the query string
		String qs = request.getQueryString();
		if (qs != null && qs.length() > 0)
			query = "?" + qs; //$NON-NLS-1$
	}

	public String getQuery() {
		return query;
	}

	public String getBannerURL() {
		String banner = preferences.getBanner();
		if (banner == null || banner.trim().length() == 0) {
			banner = "about:blank"; //$NON-NLS-1$
		} else if (banner.startsWith("http:/") || banner.startsWith("https:/")) { //$NON-NLS-1$ //$NON-NLS-2$
		} else if (banner.startsWith("file:/") || banner.startsWith("jar:file:/")) { //$NON-NLS-1$ //$NON-NLS-2$
			banner = "topic/" + banner; //$NON-NLS-1$
		} else {
			banner = "topic" + banner; //$NON-NLS-1$
		}
		return banner;
	}

	public String getBannerHeight() {
		String height = preferences.getBannerHeight();
		if (height == null || height.length() == 0) {
			height = "0"; //$NON-NLS-1$
		}
		return height;
	}
	
	public String getFooterURL() {
		String footer = preferences.getFooter();
		if (footer == null || footer.trim().length() == 0) {
			footer = "about:blank"; //$NON-NLS-1$
		} else if (footer.startsWith("http:/") || footer.startsWith("https:/")) { //$NON-NLS-1$ //$NON-NLS-2$
		} else if (footer.startsWith("file:/") || footer.startsWith("jar:file:/")) { //$NON-NLS-1$ //$NON-NLS-2$
			footer = "topic/" + footer; //$NON-NLS-1$
		} else {
			footer = "topic" + footer; //$NON-NLS-1$
		}
		return footer;
	}

	public String getFooterHeight() {
		String height = preferences.getFooterHeight();
		if (height == null || height.length() == 0) {
			height = "0"; //$NON-NLS-1$
		}
		return height;
	}
	
	/**
	 * @return the text to be added to the rows attribute of the frameset
	 */
	public String getFooterRowText() {
		String height = getFooterHeight();
		if ("0".equals(height)) { //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
		return "," + height; //$NON-NLS-1$
	}

	public String getContentURL() {
		String navHref = request.getParameter("nav"); //$NON-NLS-1$
		if (navHref != null) {
			return "../nav" + navHref; //$NON-NLS-1$
		}
		String topicHref = request.getParameter("topic"); //$NON-NLS-1$
		if (topicHref == null || topicHref.length() == 0) {
			if (BaseHelpSystem.getMode()!=BaseHelpSystem.MODE_INFOCENTER && RemoteStatusData.isAnyRemoteHelpUnavailable()) {
				return "../topic/"+HelpWebappPlugin.PLUGIN_ID+'/'+MissingContentManager.REMOTE_STATUS_HREF; //$NON-NLS-1$
			}
			if (MissingContentManager.getInstance().isUnresolvedPlaceholders()) {
			String helpMissingPage = MissingContentManager.getInstance().getHelpMissingPage(false);
			if (helpMissingPage != null) {
				return "../topic" + helpMissingPage; //$NON-NLS-1$
			}
			}
			return UrlUtil.getHelpURL(preferences.getHelpHome());
		}
		else {
			TocData tocData = new TocData(context, request, response);
			String topic = tocData.getSelectedTopicWithPath();
			if (topic == null || !UrlUtil.isValidTopicParamOrWasOpenedFromHelpDisplay(topic)) {
				return UrlUtil.getHelpURL(preferences.getHelpHome());
			}
			return  topic;
		}
	}

	/**
	 * Return array of length 0 if no views
	 */
	public AbstractView[] getViews() {
		if (views != null)
			return views;

		View tocview = new View("toc", //$NON-NLS-1$
				"", //$NON-NLS-1$
				preferences.getImagesDirectory() + "/contents_view.gif", 'C', !HelpPlugin.getTocManager().isTocLoaded(getLocale())); //$NON-NLS-1$
		View indexview = null;
		View searchview = new View("search", //$NON-NLS-1$
				"", //$NON-NLS-1$
				preferences.getImagesDirectory() + "/search_results_view.gif", 'R', false); //$NON-NLS-1$
		View bookmarksview = null;

		if (preferences.isIndexView())
			indexview = new View("index", //$NON-NLS-1$
					"", //$NON-NLS-1$
					preferences.getImagesDirectory() + "/index_view.gif", 'I', false); //$NON-NLS-1$
		if (preferences.isBookmarksView())
			bookmarksview = new View("bookmarks", //$NON-NLS-1$
					"", //$NON-NLS-1$
					preferences.getImagesDirectory() + "/bookmarks_view.gif", (char)0, false); //$NON-NLS-1$

		ArrayList viewList = new ArrayList();
		viewList.add(tocview);
		if (indexview != null) {
			viewList.add(indexview);
		}
		viewList.add(searchview);
		if (bookmarksview !=null) {
			viewList.add(bookmarksview);
		}
		
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IConfigurationElement[] elements = registry
				.getConfigurationElementsFor(VIEW_EXTENSION_POINT); 
		for (int i = 0; i < elements.length; i++) {
			Object obj = null;
			try {
				obj = elements[i].createExecutableExtension("class"); //$NON-NLS-1$
			} catch (CoreException e) {
				HelpWebappPlugin.logError("Create extension failed:[" //$NON-NLS-1$
						+ VIEW_EXTENSION_POINT + "].", e); //$NON-NLS-1$
			}
			if (obj instanceof AbstractView) {
				viewList.add(obj);
			}
		}
		
		views = (AbstractView[]) viewList.toArray(new AbstractView[viewList.size()]);
		return views;
	}

	public String getVisibleView() {
		String requestedView = request.getParameter("tab"); //$NON-NLS-1$
		AbstractView[] allViews = getViews();
		for (int i = 0; i < allViews.length; i++) {
			if (allViews[i].getName().equals(requestedView)) {
				return requestedView;
			}
		}
		return "toc"; //$NON-NLS-1$
	}

	public AbstractView getCurrentView() {
		String name = request.getParameter("view"); //$NON-NLS-1$
		views = getViews();
		for (int i = 0; i < views.length; i++)
			if (views[i].getName().equals(name))
				return views[i];
		return null;
	}
	
	public String getWindowTitle() {
		String titlePref = preferences.getTitleResource();
		int slash = titlePref.indexOf('/');
		if (slash > 0) {
			String resourceContainer = titlePref.substring(0, slash);
			String resource = titlePref.substring(slash + 1);	
			try {
				Bundle bundle = Platform.getBundle(resourceContainer);
				BundleContext bundleContext = HelpWebappPlugin.getContext();
				ServiceReference ref = bundleContext.getServiceReference(BundleLocalization.class.getName()); 
				BundleLocalization localization = (BundleLocalization) bundleContext.getService(ref); 
                return localization.getLocalization(bundle, locale).getString(resource);
			} catch (Exception e) {
				// Fall through
			}
		}
		
		if (preferences.isWindowTitlePrefix()) {
			return ServletResources.getString("browserTitle", //$NON-NLS-1$
					BaseHelpSystem.getProductName(), request);
		}
		return BaseHelpSystem.getProductName();
	}

	/**
	 * Returns the URL of a JSP file in the advanced presentation
	 */
	public String getAdvancedURL(AbstractView view, String fileSuffix) {
		return createURL(view.getURL(), view.getName(), fileSuffix);
	}
	
	/**
	 * Returns the URL of a JSP file in the basic presentation
	 */
	public String getBasicURL(AbstractView view, String fileSuffix) {
		return createURL(view.getBasicURL(), view.getName(), fileSuffix);
	}

	private String createURL(String path, String viewName, String fileSuffix) {
		if (path == null || path.length() == 0) {
			return viewName + fileSuffix;
		}
		if (path.charAt(path.length() -1) != '/') {
			path = path + '/';
		}
		return request.getContextPath() + path + viewName + fileSuffix;
	}
	
	public String getImageURL(AbstractView view) {
		String filename = view.getImageURL();
		if (filename.length() != 0 && filename.charAt(0) == '/') {
			return request.getContextPath() + filename;
		}
		return filename;
	}
	
	public String getTitle(AbstractView view) {
		return view.getTitle(UrlUtil.getLocaleObj(request, null));
	}
}

Back to the top