Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9a0a01190a39a597d04aa740049201164fbf4ab (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
/*******************************************************************************
 * Copyright (c) 2007, 2012 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 java.util.List;
import java.util.StringTokenizer;

import org.eclipse.help.IToc;
import org.eclipse.help.ITopic;
import org.eclipse.help.base.AbstractHelpScope;
import org.eclipse.help.internal.base.scope.ScopeUtils;

/**
 * Class for finding a topic in a set of TOCs based on its href.  Some of this code was
 * refactored from the TocData class
 */

public class TopicFinder {
	
	private ITopic[] foundTopicPath;
	private int selectedToc;
	private IToc[] tocs;
	private String numericPath = null;
	private AbstractHelpScope scope;
	
	public TopicFinder(String topicHref, IToc[] tocs, AbstractHelpScope scope) {
		this.tocs = tocs;
		this.scope = scope;
		if (topicHref != null && topicHref.length() > 0) {
			int index = -1;
			do {
				selectedToc = findTocContainingTopic(topicHref);
				index = topicHref.indexOf("/nav/"); //$NON-NLS-1$
				if (index != -1) {
					foundTopicPath = getTopicPathFromNav(topicHref.substring(index + 5));
			
				} else {
					ITopic topic = findTopic(UrlUtil.getHelpURL(topicHref));
					if (topic != null && selectedToc >= 0) {
						foundTopicPath = getTopicPathInToc(topic,
								tocs[selectedToc]);
					}
				}
				// if no match has been found, check if there is an anchor
				if (foundTopicPath == null) {
					index = topicHref.indexOf('#');
					if (index != -1)
						topicHref = topicHref.substring(0, index);
				}
				// if there was an anchor, search again without it
			} while (foundTopicPath == null && index != -1);
	    } else {
	    	selectedToc = -1;
			foundTopicPath = null;
	    }	
	}
	
	public ITopic[] getTopicPathFromNav(String nav) {
		StringTokenizer tok = new StringTokenizer(nav, "_"); //$NON-NLS-1$
		try {
			int segments = tok.countTokens();
			ITopic[] path = new ITopic[segments - 1];
			// first number is toc index
			int index = Integer.parseInt(tok.nextToken());
			IToc toc = tocs[index];
			ITopic current = toc.getTopic(null);
			for (int i = 0; tok.hasMoreTokens(); i++) {
				index = Integer.parseInt(tok.nextToken());
				appendFilteredIndex(index, current.getSubtopics());
				current = current.getSubtopics()[index];
				path[i] = current;
			}
			return path;
		} catch (Exception e) {
			numericPath = null;
			return null;
		}
	}

    public ITopic[] getTopicPath() {
		return foundTopicPath;
	}
    
    public int getSelectedToc() {
    	return selectedToc;
    }
    
    public String getNumericPath() {
    	return numericPath;
    }

	/*
     * Finds a path of ITopics in the given IToc to the given topic. If the
     * toc doesn't contain the topic, returns null.
     */
	private ITopic[] getTopicPathInToc(ITopic topicToFind, IToc toc) {
		if (topicToFind.getLabel().equals(toc.getLabel())) {
			return new ITopic[0];
		}
		ITopic topics[] = toc.getTopics();
		if (topics != null) {
			for (int i = 0; i < topics.length; ++i) {
				// returns path in reverse order
				List<ITopic> reversePath = getTopicPathInTopic(topicToFind, topics[i]);
				if (reversePath != null) {
					prependFilteredIndex(i, topics);
					return invertPath(reversePath);
				}
			}
		}
		return null;
	}

	private ITopic[] invertPath(List<ITopic> reversePath) {
		// reverse and return
		ITopic[] path = new ITopic[reversePath.size()];
		for (int j = 0; j < path.length; ++j) {
			path[j] = reversePath.get((path.length - 1)
					- j);
		}
		return path;
	}
	
	private boolean sameTopic(ITopic topicToFind, ITopic topic) {
		if (! topic.getLabel().equals(topicToFind.getLabel())) {
			return false;
		}
		if (topicToFind.getHref() == null) {
			return topic.getHref() == null;
		}
		return topicToFind.getHref().equals(topic.getHref());
	}

	/*
	 * Finds the topic in the given topic sub-tree. Returns a path of ITopics to
	 * that topic in reverse order (from the topic up).
	 */
	private List<ITopic> getTopicPathInTopic(ITopic topicToFind, ITopic topic) {
		if (sameTopic(topicToFind, topic)) {
			// found it. start the list to be created recursively
			List<ITopic> path = new ArrayList<ITopic>();
			path.add(topic);
			return path;
		} else {
			ITopic[] subtopics = topic.getSubtopics();
			for (int i = 0; i < subtopics.length; ++i) {
				List<ITopic> path = getTopicPathInTopic(topicToFind, subtopics[i]);
				if (path != null) {
					// it was in a subtopic.. add to the path and return
					path.add(topic);
					// Add to the numeric path counting only enabled topics
					prependFilteredIndex(i, subtopics);
					return path;
				}
			}
		}
		return null;
	}

	// Append an entry to the numeric path representing the position in the list 
	// of filtered topics. Note that we need to convert the index in the unfiltered
	// list to an index in a filtered list of topics
	private void appendFilteredIndex(int indexInUnfilteredList, ITopic[] unfiltered) {
		int indexInFilteredList = 0;
		for (int i = 0; i < indexInUnfilteredList; i++) {
			if (ScopeUtils.showInTree(unfiltered[i], scope)) {
				indexInFilteredList++;
			}
		}
		
		if (numericPath == null) {
			numericPath = "" + indexInFilteredList; //$NON-NLS-1$
		} else {
			numericPath = numericPath  + '_' + indexInFilteredList;
		}						
	}
	
	// Prepend an entry to the numeric path representing the position in the list 
	// of filtered topics. Note that we need to convert the index in the unfiltered
	// list to an index in a filtered list of topics
	private void prependFilteredIndex(int indexInUnfilteredList, ITopic[] unfiltered) {
		int indexInFilteredList = 0;
		for (int i = 0; i < indexInUnfilteredList; i++) {
			if (ScopeUtils.showInTree(unfiltered[i], scope)) {
				indexInFilteredList++;
			}
		}
		
		if (numericPath == null) {
			numericPath = "" + indexInFilteredList; //$NON-NLS-1$
		} else {
			numericPath = "" + indexInFilteredList + '_' + numericPath; //$NON-NLS-1$
		}				
		
	}

	/**
	 * Finds a TOC that contains specified topic
	 * 
	 * @param topic
	 *            the topic href
	 * @return -1 if the toc is not found
	 */
	private int findTocContainingTopic(String topic) {
		if (topic == null || topic.equals("")) //$NON-NLS-1$
			return -1;

		int index = topic.indexOf("/topic/"); //$NON-NLS-1$
		if (index != -1) {
			topic = topic.substring(index + 6);
		} else {
			// auto-generated nav urls, e.g. "/help/nav/0_1_5"
			index = topic.indexOf("/nav/"); //$NON-NLS-1$
			if (index != -1) {
				// first number is toc index
				String nav = topic.substring(index + 5);
				String book;
				index = nav.indexOf('_');
				if (index == -1) {
					book = nav;
				} else {
					book = nav.substring(0, index);
				}

				try {
					return Integer.parseInt(book);
				} catch (Exception e) {
					// shouldn't happen
				}
			}
		}
		index = topic.indexOf('?');
		if (index != -1)
			topic = topic.substring(0, index);

		if (topic == null || topic.equals("")) //$NON-NLS-1$
			return -1;

		// try to find in enabled tocs first
		for (int i = 0; i < tocs.length; i++)
			if (isEnabled(tocs[i])) {
				if (tocs[i].getTopic(topic) != null) {
					return i;
				}
				ITopic tocTopic = tocs[i].getTopic(null);
				if (tocTopic != null && topic.equals(tocTopic.getHref())) {
					return i;
				}
			}
		// try disabled tocs second
		for (int i = 0; i < tocs.length; i++)
			if (!isEnabled(tocs[i]))
				if (tocs[i].getTopic(topic) != null)
					return i;
		// nothing found
		return -1;
	}

	/**
	 * Finds topic in a TOC
	 * 
	 * @return ITopic or null
	 */
	private ITopic findTopic(String topic) {

		int index = topic.indexOf("/topic/"); //$NON-NLS-1$
		if (index != -1) {
			topic = topic.substring(index + 6);
		} else {
			// auto-generated nav urls, e.g. "/help/nav/0_1_5"
			index = topic.indexOf("/nav/"); //$NON-NLS-1$
			if (index != -1) {
				String nav = topic.substring(index + 5);
				StringTokenizer tok = new StringTokenizer(nav, "_"); //$NON-NLS-1$
				try {
					// first number is toc index
					index = Integer.parseInt(tok.nextToken());
					ITopic current = tocs[index].getTopic(null);
					while (tok.hasMoreTokens()) {
						index = Integer.parseInt(tok.nextToken());
						current = current.getSubtopics()[index];
					}
					return current;
				} catch (Exception e) {
					// shouldn't happen
				}
			}
		}
		index = topic.indexOf('?');
		if (index != -1)
			topic = topic.substring(0, index);

		if (topic == null || topic.equals("")) //$NON-NLS-1$
			return null;

		if (selectedToc < 0)
			return null;
		IToc selectedToc2 = tocs[selectedToc];
		if (selectedToc2 == null)
			return null;
		ITopic selectedTopic = selectedToc2.getTopic(topic);
		if (selectedTopic != null) {
			return selectedTopic;
		}

		ITopic tocTopic = selectedToc2.getTopic(null);
		if (tocTopic != null && topic.equals(tocTopic.getHref())) {
			return tocTopic;
		}
		return null;
	}

	/**
	 * Check if given TOC is visible and non empty
	 * 
	 * @param toc
	 * @return true if TOC should be visible
	 */
	private boolean isEnabled(IToc toc) {
		boolean tocEnabled = ScopeUtils.showInTree(toc, scope);
		if (!tocEnabled)
			return false;
		ITopic[] topics = toc.getTopics();
		for (ITopic topic : topics) {
			if (ScopeUtils.showInTree(topic, scope)) {
				return true;
			}
		}
		return false;
	}
}

Back to the top