Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 46f92a1539b9ce3fdd667f8eb8ae9f9f690e37b5 (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
/*******************************************************************************
 * Copyright (c) 2000, 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.toc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.help.ITocContribution;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.util.ProductPreferences;

public class TocSorter {

	/*
	 * A category of tocs. A category has an id and a list of contained
	 * tocs.
	 */
	private static class TocCategory extends ArrayList<ITocContribution> {

		private static final long serialVersionUID = 1L;

		/**
		 * Constructs a new empty TOC category with the given id.
		 *
		 * @param id the category's id
		 */
		public TocCategory(String id) {
		}

	}

	/*
	 * Orders the given toc contributions by category and product preference.
	 */
	public ITocContribution[] orderTocContributions(ITocContribution[] unorderedTocs) {
		// first categorize the TOCs
		List<String> itemsToOrder = new ArrayList<>();
		Map<String, Object> categorized = categorizeTocs(Arrays.asList(unorderedTocs), itemsToOrder);
		Map<String, String> nameIdMap = createNameIdMap(categorized);

		// order them
		List<String> orderedItems = ProductPreferences.getTocOrder(itemsToOrder, nameIdMap);

		// replace with actual TocContribution or category
		List<Object> actualItems = substituteValues(orderedItems, categorized);

		// expand the categories
		List<ITocContribution> expandedItems = expandCategories(actualItems);
		return expandedItems.toArray(new ITocContribution[orderedItems.size()]);
	}

	// Create a mapping from an id to a label that can be sorted
	private Map<String, String> createNameIdMap(Map<String, Object> categorized) {
		Map<String, String> map = new HashMap<>();
		for (Iterator<String> iter = categorized.keySet().iterator(); iter.hasNext();) {
			String key = iter.next();
			Object value = categorized.get(key);
			ITocContribution toc;
			if (value instanceof TocCategory) {
				TocCategory category = (TocCategory)value;
				toc = category.get(0);
			} else {
				toc = (ITocContribution)value;
			}
			map.put(key, toc.getToc().getLabel());
		}
		return map;
	}

	/*
	 * Categorizes the given toc contributions into categories, or individual
	 * toc contributions if no category (treated as a category of one). Returns
	 * mapping from category id/toc id to category/toc. Order of categories/
	 * tocs is returned via tocOrder.
	 */
	private Map<String, Object> categorizeTocs(List<ITocContribution> tocs, List<String> tocOrder) {
		Map<String, Object> categorized = new HashMap<>();
		Iterator<ITocContribution> iter = tocs.iterator();
		while (iter.hasNext()) {
			ITocContribution toc = iter.next();
			String categoryId;
			try {
				categoryId = toc.getCategoryId();
			}
			catch (Throwable t) {
				// log and skip
				String msg = "Error retrieving categoryId from " + ITocContribution.class.getName() + ": " + toc.getClass().getName(); //$NON-NLS-1$ //$NON-NLS-2$
				HelpPlugin.logError(msg, t);
				continue;
			}
			if (categoryId != null && categoryId.length() > 0) {
				// it has a category, add it to the appropriate TocCategory
				TocCategory category = (TocCategory)categorized.get(categoryId);
				if (category == null) {
					// create categories as needed
					category = new TocCategory(categoryId);
					categorized.put(categoryId, category);
					tocOrder.add(categoryId);
					category.add(toc);
				} else {
					// Add in alphabetic sequence
					String tocLabel = toc.getToc().getLabel();
					boolean done = false;
					for (int next = 0; next < category.size() && !done; next++ ) {
						String nextName = category.get(next).getToc().getLabel();
						if (tocLabel.compareToIgnoreCase(nextName) < 0) {
							done = true;
							category.add(next, toc);
						}
					}
					if (!done) {
						category.add(toc);
					}
				}
			}
			else {
				// doesn't have a category; insert the TOC directly
				String id;
				try {
					id = toc.getId();
				}
				catch (Throwable t) {
					// log and skip
					String msg = "Error retrieving id from " + ITocContribution.class.getName() + ": " + toc.getClass().getName(); //$NON-NLS-1$ //$NON-NLS-2$
					HelpPlugin.logError(msg, t);
					continue;
				}
				categorized.put(id, toc);
				tocOrder.add(id);
			}
		}
		return categorized;
	}

	/*
	 * Expands all categories in the given list to actual toc contributions
	 * organized by category.
	 */
	private List<ITocContribution> expandCategories(List<Object> entries) {
		List<ITocContribution> expanded = new ArrayList<>();
		for (Object entry : entries) {
			if (entry instanceof ITocContribution) {
				expanded.add((ITocContribution) entry);
			}
			else if (entry instanceof TocCategory) {
				expanded.addAll((TocCategory)entry);
			}
		}
		return expanded;
	}

	/*
	 * Substitutes each item with it's corresponding mapping from the map.
	 * Original List is not modified.
	 */
	private static List<Object> substituteValues(List<String> items, Map<String, Object> map) {
		if (items != null && map != null) {
			List<Object> result = new ArrayList<>(items.size());
			for (String key : items) {
				Object value = map.get(key);
				if (value != null) {
					result.add(value);
				}
			}
			return result;
		}
		return null;
	}

}

Back to the top