Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c3231ac00f5c8c8effed7f4215726dde7c894aec (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
/*******************************************************************************
 * Copyright (c) 2009, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.help.internal.webapp.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.core.runtime.Path;
import org.eclipse.help.ITopic;
import org.eclipse.help.base.AbstractHelpScope;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.scope.ScopeUtils;
import org.eclipse.help.internal.toc.Toc;
import org.eclipse.help.internal.webapp.data.RequestScope;
import org.eclipse.help.internal.webapp.data.TocData;
import org.eclipse.help.internal.webapp.data.UrlUtil;

public class ChildLinkInserter {

	private HttpServletRequest req;
	private OutputStream out;
	private static final String NO_CHILDREN = "no_child_topics"; //$NON-NLS-1$
	private static final String HAS_CHILDREN = "has_child_topics"; //$NON-NLS-1$
	private AbstractHelpScope scope;

	public ChildLinkInserter(HttpServletRequest req, OutputStream out) {
		this.req = req;
		this.out = out;
		scope = RequestScope.getScope(req, null, false);
	}

	public void addContents(String encoding) throws IOException {
		String path = req.getParameter(TocData.COMPLETE_PATH_PARAM);
		ITopic[] subtopics = getSubtopics();
		if (subtopics.length == 0) {
			return;
		}
		StringBuilder links = new StringBuilder("\n<ul class=\"childlinks\">\n"); //$NON-NLS-1$
		for (int i=0;i<subtopics.length;++i) {
			if (ScopeUtils.showInTree(subtopics[i], scope)) {
				links.append("\n<li><a href=\""); //$NON-NLS-1$
				String href = subtopics[i].getHref();
				if (href == null) {
					if (path != null && path.length() > 0) {
						href = "/../nav/" + path + '_' + i; //$NON-NLS-1$
					} else {
						href = "nav.html"; //$NON-NLS-1$
					}
				}
				else {
					href = XMLGenerator.xmlEscape(href);
					if (path != null && path.length() > 0) {
						href = TocFragmentServlet.fixupHref(href, path + '_' + i);
					}
				}
				links.append(getBackpath(req.getPathInfo()));
				links.append(href);
				links.append("\">" + subtopics[i].getLabel() + "</a></li>\n");  //$NON-NLS-1$//$NON-NLS-2$
			}
		}
		links.append("\n</ul>\n"); //$NON-NLS-1$
		String linkString = links.toString();
		try {
			if (encoding != null) {
				out.write(linkString.getBytes(encoding));
			} else {
				out.write(linkString.getBytes(StandardCharsets.UTF_8));
			}
		} catch (UnsupportedEncodingException e) {
			out.write(linkString.getBytes());
		}
	}

	private ITopic[] getSubtopics() {
		String locale = UrlUtil.getLocale(req, null);
		String pathInfo = req.getPathInfo();
		String servletPath = req.getServletPath();
		if ("/nav".equals(servletPath)) return new ITopic[0]; //$NON-NLS-1$
		Toc[] tocs =  HelpPlugin.getTocManager().getTocs(locale);
		for (Toc toc : tocs) {
			if (pathInfo.equals(toc.getTopic())) {
				return toc.getTopics();
			}
			ITopic topic = toc.getTopic(pathInfo);
			if (topic != null) {
				return topic.getSubtopics();
			}
		}
		return   new ITopic[0];
	}

	private String getBackpath(String path) {
		int num = new Path(path).segmentCount() - 1;
		StringBuilder buf = new StringBuilder();
		for (int i=0; i < num; ++i) {
			if (i > 0) {
				buf.append('/');
			}
			buf.append(".."); //$NON-NLS-1$
		}
		return buf.toString();
	}

	public void addStyle() throws IOException {
		ITopic[] subtopics = getSubtopics();
		for (ITopic subtopic : subtopics) {
			if (ScopeUtils.showInTree(subtopic, scope)) {
				out.write(HAS_CHILDREN.getBytes(StandardCharsets.UTF_8));
				return;
			}
		}

		out.write(NO_CHILDREN.getBytes(StandardCharsets.UTF_8));
	}

}

Back to the top