Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2ab235081c1671e3ce7c7fa46639a3a587a35431 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*******************************************************************************
 * Copyright (c) 2000, 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.webapp.data;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

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

import org.eclipse.help.IToc;
import org.eclipse.help.ITopic;
import org.eclipse.help.UAContentFilter;
import org.eclipse.help.base.AbstractHelpScope;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.HelpBasePlugin;
import org.eclipse.help.internal.base.HelpEvaluationContext;
import org.eclipse.help.internal.base.remote.RemoteHelp;
import org.eclipse.help.internal.base.scope.ScopeUtils;
import org.eclipse.help.internal.webapp.servlet.TocFragmentServlet;

/**
 * Helper class for tocView.jsp initialization
 */
public class TocData extends ActivitiesData {
	public static final String COMPLETE_PATH_PARAM = "cp"; //$NON-NLS-1$
	// Request parameters
	private String tocParameter;
	private String topicHref;
	private String expandPathParam;
	private String completePath;

	// help form of selected topic href
	private String topicHelpHref;
	// Selected TOC
	private int selectedToc = -1;
	// path from TOC to the root topic of the TOC fragment
	private int[] rootPath = null;
	// path from TOC to the selected topic, excluding TOC;
	private ITopic[] topicPath = null;
	
	// String representing the topic path as numbers separated by underscores e.g. 2_4_3
	private String numericPath;

	// List of TOC's, unfiltered
	private IToc[] tocs;
	
	// images directory
	private String imagesDirectory;
	
	// Scope
	private AbstractHelpScope scope;
	/**
	 * Constructs the xml data for the contents page.
	 * 
	 * @param context
	 * @param request
	 */
	public TocData(ServletContext context, HttpServletRequest request,
			HttpServletResponse response) {
		super(context, request, response);


		this.tocParameter = request.getParameter("toc"); //$NON-NLS-1$
		this.topicHref = request.getParameter("topic"); //$NON-NLS-1$
		this.completePath = request.getParameter(COMPLETE_PATH_PARAM); 
		this.expandPathParam = request.getParameter("expandPath"); //$NON-NLS-1$
		this.scope = RequestScope.getScope(request, response, false);
		
		
		if (tocParameter != null && tocParameter.length() == 0)
			tocParameter = null;
		if (topicHref != null && topicHref.length() == 0)
			topicHref = null;
		if (expandPathParam != null && expandPathParam.length() == 0)
			expandPathParam = null;
		if (completePath != null && completePath.length() == 0)
			completePath = null;
		
		String anchor = request.getParameter("anchor"); //$NON-NLS-1$
		if (topicHref != null && anchor != null) {
			topicHref = topicHref + '#' + anchor;
		}
		
		// initialize rootPath
		String pathStr = request.getParameter("path"); //$NON-NLS-1$
		if (pathStr != null && pathStr.length() > 0) {
			String[] paths = pathStr.split("_", -1); //$NON-NLS-1$
			int[] indexes = new int[paths.length];
			boolean indexesOK = true;
			for (int i = 0; i < paths.length; i++) {
				try {
					indexes[i] = Integer.parseInt(paths[i]);
				} catch (NumberFormatException nfe) {
					indexesOK = false;
					break;
				}
				if (indexesOK) {
					rootPath = indexes;
				}
			}
		}

		imagesDirectory = preferences.getImagesDirectory();

		loadTocs();
	}

	public boolean isRemoteHelpError() {
		boolean isError = (RemoteHelp.getError() != null);
		if (isError) {
			RemoteHelp.clearError();
		}
		return isError;
	}
	
	// Accessor methods to avoid exposing help classes directly to JSP.
	// Note: this seems ok for now, but maybe we need to reconsider this
	//       and allow help classes in JSP's.
	
	public int getTocCount() {
		return tocs.length;
	}

	public String getTocLabel(int i) {
		return tocs[i].getLabel();
	}

	public String getTocHref(int i) {
		return tocs[i].getHref();
	}

	public String getTocDescriptionTopic(int i) {
		return UrlUtil.getHelpURL(tocs[i].getTopic(null).getHref());
	}

	/**
	 * Returns the selected TOC
	 * 
	 * @return int
	 */
	public int getSelectedToc() {
		return selectedToc;
	}

	/**
	 * Returns the topic to display. If there is a TOC, return its topic
	 * description. Return null if no topic is specified and there is no toc
	 * description.
	 * 
	 * @return String
	 */
	public String getSelectedTopic() {
		if (topicHref != null && topicHref.length() > 0)
			return UrlUtil.getHelpURL(topicHref);
		else
		if (selectedToc == -1)
			return null;
		IToc toc = tocs[selectedToc];
		ITopic tocDescription = toc.getTopic(null);
		if (tocDescription != null)
			return UrlUtil.getHelpURL(tocDescription.getHref());
		return UrlUtil.getHelpURL(null);
	}
	
	/**
	 * Returns the topic to display. If there is a TOC, return its topic
	 * description. Return null if no topic is specified and there is no toc
	 * description.
	 * 
	 * @return String
	 */
	public String getSelectedTopicWithPath() {
		String href = getSelectedTopic();
		if ( completePath != null ) {
			href = TocFragmentServlet.fixupHref(href, completePath);
		}
		return href;
	}

	/**
	 * Returns a list of all the TOC's as xml elements. Individual TOC's are not
	 * loaded yet.
	 * 
	 * @return IToc[]
	 */
	public IToc[] getTocs() {
		return tocs;
	}

	/**
	 * Check if given TOC is visible
	 * 
	 * @param toc
	 * @return true if TOC should be visible
	 */
	public boolean isEnabled(int toc) {
		return ScopeUtils.showInTree(tocs[toc], scope);
	}
	/**
	 * Check if given TOC is visible
	 * 
	 * @param toc
	 * @return true if TOC should be visible
	 */
	private boolean isEnabled(IToc toc) {
		if(!isAdvancedUI()){
			// activities never filtered for basic browsers
			return true;
		}
		return HelpBasePlugin.getActivitySupport().isEnabled(toc.getHref()) &&
			!UAContentFilter.isFiltered(toc, HelpEvaluationContext.getContext());
	}

	private void loadTocs() {
		tocs = HelpPlugin.getTocManager().getTocs(getLocale());
		// Find the requested TOC
		selectedToc = -1;
		if (isExpandPath()) {
			getEnabledTopicPath();
		} else if (tocParameter != null && tocParameter.length() > 0) {
			for (int i = 0; selectedToc == -1 && i < tocs.length; i++) {
				if (tocParameter.equals(tocs[i].getHref())) {
					selectedToc = i;
				}
			}
		} else if ( completePath != null ) {
			// obtain the TOC from the complete path
			TopicFinder finder = new TopicFinder("/nav/" + completePath, tocs, scope); //$NON-NLS-1$
			topicPath = finder.getTopicPath();
			selectedToc = finder.getSelectedToc();
		    numericPath = finder.getNumericPath();
		} else {
			// toc not specified as parameter
			// try obtaining the TOC from the topic
			TopicFinder finder = new TopicFinder(topicHref, tocs, scope);
			topicPath = finder.getTopicPath();
			selectedToc = finder.getSelectedToc();
			numericPath = finder.getNumericPath();
		}
	}
	
	private void getEnabledTopicPath() {
		int[] path = UrlUtil.splitPath(expandPathParam);
		if (path != null) {
			// path[0] is the index of enabled TOCS, convert to an index into all TOCS
			int enabled = path[0] + 1;
			for (int i = 0;  enabled > 0 && i < tocs.length; i++) {
				if (ScopeUtils.showInTree(tocs[i], scope)) {
					enabled--;
					if (enabled == 0) {
						selectedToc = i;
					}
				}
			}
			if (selectedToc != -1) {
		        topicPath = decodePath(path, tocs[selectedToc], scope);
			}
		} else {
			selectedToc = -1;
		}
	}

	public static ITopic[] decodePath(int[] path, IToc toc, AbstractHelpScope scope) {
		ITopic[] topicPath = new ITopic[path.length - 1];
		try {
			if (path.length > 1) {
				ITopic[] topics = toc.getTopics();
				ITopic[] enabledTopics = ScopeUtils.inScopeTopics(topics, scope);
				topicPath[0] = enabledTopics[path[1]];
			}
			for (int i = 1; i < topicPath.length; i++) {
				ITopic[] topics = topicPath[i-1].getSubtopics();
				ITopic[] enabledTopics = ScopeUtils.inScopeTopics(topics, scope);
				topicPath[i] = enabledTopics[path[i+1]];
			}
		} catch (RuntimeException e) {
			return null;
		}
		return topicPath;
	}

	/**
	 * Generates the HTML code (a tree) for a TOC.
	 * 
	 * @param toc
	 * @param out
	 * @throws IOException
	 */
	public void generateBasicToc(int toc, Writer out) throws IOException {
		ITopic[] topics = getEnabledSubtopics(tocs[toc]);
		for (ITopic topic : topics) {
			generateBasicTopic(topic, out);
		}

	}

	private void generateBasicTopic(ITopic topic, Writer out)
			throws IOException {

		out.write("<li>"); //$NON-NLS-1$
		ITopic[] topics = getEnabledSubtopics(topic);
		boolean hasNodes = topics.length > 0;
		if (hasNodes) {
			out.write("<nobr>"); //$NON-NLS-1$
			out.write("<a "); //$NON-NLS-1$
			if (getSelectedTopicHelpHref().equals(topic.getHref())) {
				out.write("name=\"selectedItem\" "); //$NON-NLS-1$
			}
			out.write("href="+"\"" + UrlUtil.getHelpURL(topic.getHref())+"\"" + ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ 
			out.write("<img src='"); //$NON-NLS-1$
			out.write(imagesDirectory);
			out.write("/container_obj.gif' alt=\"\" border=0>&nbsp;"); //$NON-NLS-1$
			out.write(UrlUtil.htmlEncode(topic.getLabel()));
			out.write("</a>"); //$NON-NLS-1$
			out.write("</nobr>"); //$NON-NLS-1$

			out.write("<ul>\n"); //$NON-NLS-1$

			for (ITopic topic2 : topics) {
				generateBasicTopic(topic2, out);
			}

			out.write("</ul>\n"); //$NON-NLS-1$
		} else {
			out.write("<nobr>"); //$NON-NLS-1$
			out.write("<a "); //$NON-NLS-1$
			if (getSelectedTopicHelpHref().equals(topic.getHref())) {
				out.write("name=\"selectedItem\" "); //$NON-NLS-1$
			}
			out.write("href="+"\"" + UrlUtil.getHelpURL(topic.getHref()) +"\""+ ">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ 
			out.write("<img src='"); //$NON-NLS-1$
			out.write(imagesDirectory);
			out.write("/topic.gif' alt=\"\" border=0>&nbsp;"); //$NON-NLS-1$
			out.write(UrlUtil.htmlEncode(topic.getLabel()));
			out.write("</a>"); //$NON-NLS-1$
			out.write("</nobr>"); //$NON-NLS-1$
		}

		out.write("</li>\n"); //$NON-NLS-1$
	}
	/**
	 * @return String - help form of selected topic URL, or ""
	 */
	private String getSelectedTopicHelpHref() {
		if (topicHelpHref == null) {
			String topic = getSelectedTopic();
			if (topic == null || topic.length() == 0) {
				topicHelpHref = ""; //$NON-NLS-1$
				return topicHelpHref;
			}
			int index = topic.indexOf("/topic/"); //$NON-NLS-1$
			if (index != -1)
				topic = topic.substring(index + 6);
			index = topic.indexOf('?');
			if (index != -1)
				topic = topic.substring(0, index);
			topicHelpHref = topic;
			if (topic == null) {
				topicHelpHref = ""; //$NON-NLS-1$
			}
		}
		return topicHelpHref;
	}
	/**
	 * Obtains children topics for a given navigation element. Topics from TOCs
	 * not matching enabled activities are filtered out.
	 * 
	 * @param element ITopic or IToc
	 * @return ITopic[]
	 */
	public ITopic[] getEnabledSubtopics(Object element) {
		List<ITopic> topics = getEnabledSubtopicList(element);
		return topics.toArray(new ITopic[topics.size()]);
	}
	/**
	 * Obtains children topics for a given navigation element. Topics from TOCs
	 * not matching enabled activities are filtered out.
	 * 
	 * @param navigationElement
	 * @return List of ITopic
	 */
	private List<ITopic> getEnabledSubtopicList(Object element) {
		if (element instanceof IToc && !isEnabled((IToc) element))
			return Collections.emptyList();
		List<ITopic> children;
		if (element instanceof IToc) {
			children = Arrays.asList(((IToc)element).getTopics());
		}
		else if (element instanceof ITopic) {
			children = Arrays.asList(((ITopic)element).getSubtopics());
		}
		else {
			// unknown element type
			return Collections.emptyList();
		}
		List<ITopic> childTopics = new ArrayList<ITopic>(children.size());
		for (ITopic iTopic : children) {
			Object c = iTopic;
			if ((c instanceof ITopic)) {
				// add topic only if it will not end up being an empty
				// container
				if (((((ITopic) c).getHref() != null && ((ITopic) c)
						.getHref().length() > 0) || getEnabledSubtopicList(c).size() > 0) &&
						!UAContentFilter.isFiltered(c, HelpEvaluationContext.getContext())) {
					childTopics.add((ITopic) c);
				}
			} else {
				// it is a Toc, Anchor or Link,
				// which may have children attached to it.
				childTopics.addAll(getEnabledSubtopicList(c));
			}
		}
		return childTopics;
	}
	private void generateTopicLinks(ITopic topic, Writer w, int indent) {
        String topicHref = topic.getHref();
        try {
            if (indent == 0)
                w.write("<b>"); //$NON-NLS-1$
            for (int tab = 0; tab < indent; tab++) {
                w.write("&nbsp;&nbsp;"); //$NON-NLS-1$
            }
            if (topicHref != null && topicHref.length() > 0) {
                w.write("<a href=\""); //$NON-NLS-1$
                if ('/' == topicHref.charAt(0)) {
                    w.write("topic"); //$NON-NLS-1$
                }
                w.write(topicHref);
                w.write("\">"); //$NON-NLS-1$
                w.write(UrlUtil.htmlEncode(topic.getLabel()));
                w.write("</a>"); //$NON-NLS-1$
            } else {
                w.write(UrlUtil.htmlEncode(topic.getLabel()));
            }
            w.write("<br>\n"); //$NON-NLS-1$
            if (indent == 0)
                w.write("</b>"); //$NON-NLS-1$
        } catch (IOException ioe) {
        }
        ITopic[] topics = topic.getSubtopics();
        for (ITopic topic2 : topics) {
            generateTopicLinks(topic2, w, indent + 1);
        }
    }

    public void generateLinks(Writer out) {
        for (IToc toc : tocs) {
            ITopic tocTopic = toc.getTopic(null);
            generateTopicLinks(tocTopic, out, 0);
            ITopic[] topics = toc.getTopics();
            for (ITopic topic : topics) {
                generateTopicLinks(topic, out, 1);
            }
        }

    }
    
    public ITopic[] getTopicPathFromRootPath(IToc toc) {
		ITopic[] topicPath;
		// Determine the topicPath from the path passed in as a parameter
		int[] rootPath = getRootPath();
		if (rootPath == null) {
			return null;
		}
		int pathLength = rootPath.length;
		topicPath = new ITopic[pathLength];
		ITopic[] children = toc.getTopics();
		for (int i = 0; i < pathLength; i++) {
			int index = rootPath[i];
			if (index < children.length) {
				topicPath[i] = children[index];
				children = topicPath[i].getSubtopics();
			} else {
				return null;  // Mismatch between expected and actual children
			}
		}
		return topicPath;
	}
    
    public ITopic[] getTopicPath() {
    	return topicPath;
    }

    public int[] getRootPath() {
    	return rootPath;
    }
    
    public String getTopicHref() {
    	return topicHref;
    }
    
    public String getNumericPath() {
    	return numericPath;
    }
    
    public boolean isExpandPath() {
        return expandPathParam != null;
    }
}

Back to the top