Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 616c8dbad9dd28fa8a4418e3a14a7a6d4500587b (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 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.data;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.help.IIndexEntry;
import org.eclipse.help.IIndexEntry2;
import org.eclipse.help.IIndexSee;
import org.eclipse.help.IToc;
import org.eclipse.help.ITopic;
import org.eclipse.help.UAContentFilter;
import org.eclipse.help.internal.base.HelpBasePlugin;
import org.eclipse.help.internal.base.HelpEvaluationContext;

/**
 * Utilities to test for enabled topics, index entries etc.
 */

public class EnabledTopicUtils {

	/**
	 * Test whether a topic is enabled
	 * @param topic
	 * @return
	 */
	public static boolean isEnabled(ITopic topic) {
		if (!topic.isEnabled(HelpEvaluationContext.getContext())) {
			return false;
		}
		if (topic.getHref() != null) {
			return true;
		}
		return hasEnabledSubtopic(topic);
	}

	public static boolean hasEnabledSubtopic(ITopic topic) {
		ITopic[] subtopics = topic.getSubtopics();
		for (ITopic subtopic : subtopics) {
			if (isEnabled(subtopic)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Test whether a toc is enabled
	 * @param topic
	 * @return
	 */
	public static boolean isEnabled(IToc toc) {
		if (!HelpBasePlugin.getActivitySupport().isEnabled(toc.getHref()) ||
			UAContentFilter.isFiltered(toc, HelpEvaluationContext.getContext())) {
			return false;
		}
		// A toc is enabled only if at least one subtopic is enabled
		ITopic[] subtopics = toc.getTopics();
		for (ITopic subtopic : subtopics) {
			if (isEnabled(subtopic)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Test whether an entry is enabled
	 * @param entry
	 * @return
	 */
	public static boolean isEnabled(IIndexEntry entry) {
		if (UAContentFilter.isFiltered(entry, HelpEvaluationContext.getContext())) {
			return false;
		}
		ITopic[] topics = entry.getTopics();
		for (ITopic topic : topics) {
			if (isEnabled(topic)) {
				return true;
			}
		}
		IIndexEntry[] subentries = entry.getSubentries();
		for (IIndexEntry subentrie : subentries) {
			if (isEnabled(subentrie)) {
				return true;
			}
		}
		if (entry instanceof IIndexEntry2) {
			IIndexSee[] sees = ((IIndexEntry2)entry).getSees();
			for (IIndexSee see : sees) {
				if (isEnabled(see)) {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean isEnabled(IIndexSee see) {
		return see.isEnabled(HelpEvaluationContext.getContext());
	}

	/**
	 * Filter out any disabled entries from an array
	 * @param entries an array of entries
	 * @return an array containing only those entries which are enabled
	 */
	public static IIndexEntry[] getEnabled(IIndexEntry[] entries) {
		for (int i=0;i<entries.length;++i) {
			if (!isEnabled(entries[i])) {
				List<IIndexEntry> list = new ArrayList<>(entries.length);
				for (int j=0;j<entries.length;++j) {
					if (j < i || isEnabled(entries[j])) {
						list.add(entries[j]);
					}
				}
				return list.toArray(new IIndexEntry[list.size()]);
			}
		}
		return entries;
	}

	/**
	 * Filter out any disable topics form an array
	 * @param topics an array of topics
	 * @return an array containing only those topics which are enabled
	 */
	public static ITopic[] getEnabled(ITopic[] topics) {
		for (int i=0;i<topics.length;++i) {
			if (!isEnabled(topics[i])) {
				List<ITopic> list = new ArrayList<>(topics.length);
				for (int j=0;j<topics.length;++j) {
					if (j < i || isEnabled(topics[j])) {
						list.add(topics[j]);
					}
				}
				return list.toArray(new ITopic[list.size()]);
			}
		}
		return topics;
	}

}

Back to the top