Skip to main content
summaryrefslogtreecommitdiffstats
blob: a5b0ae82158cb4be17f9972a002a3a2d8a62b987 (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
/*******************************************************************************
 * Copyright (c) 2000, 2010 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.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

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

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.criteria.CriterionResource;
import org.eclipse.help.internal.util.URLCoder;
import org.eclipse.help.internal.webapp.HelpWebappPlugin;
import org.eclipse.help.internal.workingset.AdaptableHelpResource;
import org.eclipse.help.internal.workingset.AdaptableToc;
import org.eclipse.help.internal.workingset.AdaptableTocsArray;
import org.eclipse.help.internal.workingset.AdaptableTopic;
import org.eclipse.help.internal.workingset.IHelpWorkingSetManager;
import org.eclipse.help.internal.workingset.WorkingSet;
import org.eclipse.help.internal.workingset.WorkingSetComparator;

/**
 * The Infocenter working set manager stores help working sets. Working sets are
 * persisted in client cookies whenever one is added or removed.
 * 
 * @since 3.0
 */
public class InfocenterWorkingSetManager implements IHelpWorkingSetManager {
	private static final String COOKIE_WSET_CONTENTS = "wset_contents"; //$NON-NLS-1$
	private static final String COOKIE_WSET_CRITERIA = "wset_criteria"; //$NON-NLS-1$
	private static final int MAX_COOKIES = 15;
	private HttpServletRequest request;
	private HttpServletResponse response;

	// Current working set , empty string means all documents
	private String currentWorkingSet = ""; //$NON-NLS-1$
	private SortedSet workingSets = new TreeSet(new WorkingSetComparator());
	private String locale;
	private AdaptableTocsArray root;
	
	private static final String UNCATEGORIZED = "Uncategorized"; //$NON-NLS-1$
	private Map allCriteriaValues;

	/**
	 * Constructor
	 * 
	 * @param locale
	 */
	public InfocenterWorkingSetManager(HttpServletRequest request,
			HttpServletResponse response, String locale) {
		this.request = request;
		this.response = response;
		this.locale = locale;
		restoreState();
	}

	public AdaptableTocsArray getRoot() {
		if (root == null)
			root = new AdaptableTocsArray(HelpPlugin.getTocManager().getTocs(
					locale));
		return root;
	}

	/**
	 * Adds a new working set and saves it
	 */
	public void addWorkingSet(WorkingSet workingSet) throws IOException {
		if (workingSet == null || workingSets.contains(workingSet))
			return;
		workingSets.add(workingSet);
		saveState();
	}

	/**
	 * Creates a new working set
	 */
	public WorkingSet createWorkingSet(String name,
			AdaptableHelpResource[] elements) {
		return new WorkingSet(name, elements);
	}

	public WorkingSet createWorkingSet(String name, AdaptableHelpResource[] elements, CriterionResource[] criteria) {
		return new WorkingSet(name, elements, criteria);
	}
	
	/**
	 * Returns a working set by name
	 *  
	 */
	public WorkingSet getWorkingSet(String name) {
		if (name == null || workingSets == null)
			return null;

		Iterator iter = workingSets.iterator();
		while (iter.hasNext()) {
			WorkingSet workingSet = (WorkingSet) iter.next();
			if (name.equals(workingSet.getName()))
				return workingSet;
		}
		return null;
	}

	/**
	 * Implements IWorkingSetManager.
	 * 
	 * @see org.eclipse.help.internal.workingset.IHelpWorkingSetManager#getWorkingSets()
	 */
	public WorkingSet[] getWorkingSets() {
		return (WorkingSet[]) workingSets.toArray(new WorkingSet[workingSets
				.size()]);
	}

	/**
	 * Removes specified working set
	 */
	public void removeWorkingSet(WorkingSet workingSet) {
		workingSets.remove(workingSet);
		try {
			saveState();
		} catch (IOException ioe) {
		}
	}

	private void restoreState() {		
		restoreContents();
		restoreCriteria();
	}
	
	private void restoreContents(){
		String data = CookieUtil.restoreString(COOKIE_WSET_CONTENTS, request);
		if (data == null) {
			return;
		}

		String[] values = data.split("\\|", -1); //$NON-NLS-1$
		if (values.length < 1) {
			return;
		}
		
		currentWorkingSet = URLCoder.decode(values[0]);
		i : for (int i = 1; i < values.length; i++) {
			String[] nameAndHrefs = values[i].split("&", -1); //$NON-NLS-1$

			String name = URLCoder.decode(nameAndHrefs[0]);

			AdaptableHelpResource[] elements = new AdaptableHelpResource[nameAndHrefs.length - 1];
			// for each href (working set resource)
			String previousToc = ""; //$NON-NLS-1$
			for (int e = 0; e < nameAndHrefs.length - 1; e++) {
				int h = e + 1;
				String decodedName = URLCoder.decode(nameAndHrefs[h]);
				elements[e] = getAdaptableToc(decodedName);
				if (elements[e] == null) {
					// Check for a suffix of type _nn_
					// If there is only a suffix this means use the same toc as the previous entry
					int suffixStart = decodedName.lastIndexOf('_', decodedName.length() - 2);
					if (suffixStart > 0) {
						previousToc = decodedName.substring(0, suffixStart);
					} else if (suffixStart == 0) {
					    decodedName = previousToc + decodedName;
					}
					elements[e] = getAdaptableTopic(decodedName);
				}
				if (elements[e] == null) {
					// working set cannot be restored
					continue i;
				}
			}
			WorkingSet ws = createWorkingSet(name, elements, null);
			workingSets.add(ws);
		}
	}
	
	private void restoreCriteria(){
		
		String data = CookieUtil.restoreString(COOKIE_WSET_CRITERIA, request);
		if (data == null) {
			return;
		}
		String[] values = data.split("\\|", -1); //$NON-NLS-1$
		if (values.length < 1) {
			return;
		}
		//scope1$platform#AIX,WINDOWS,$version#1.0,2.0,
		for (int i = 1; i < values.length; ++i) {
			String[] nameAndCriteria = values[i].split("\\$", -1); //$NON-NLS-1$
			if(nameAndCriteria.length < 2){
				continue;
			}
			String name = URLCoder.decode(nameAndCriteria[0]);
		    List criteriaResource = new ArrayList();
			for (int j = 1; j < nameAndCriteria.length; ++j) {
				String criterion = nameAndCriteria[j];
				String[] keyAndValue = criterion.split("#", -1); //$NON-NLS-1$
				if(keyAndValue.length != 2)
					continue;
				String key = URLCoder.decode(keyAndValue[0]);
			    String value = URLCoder.decode(keyAndValue[1]);
				String[] criterionValues = value.split(",", -1); //$NON-NLS-1$
				if(criterionValues.length < 1)
					continue;

				List criterionValuesList = Arrays.asList(criterionValues);
				CriterionResource criterionResource = new CriterionResource(key, criterionValuesList);
				criteriaResource.add(criterionResource);

			}
			
			WorkingSet workingset = getWorkingSet(name);
			if(workingset != null){
				CriterionResource[] criteria = new CriterionResource[criteriaResource.size()];
				criteriaResource.toArray(criteria);
				workingset.setCriteria(criteria);
			}
		}
	}

	/***************************************************************************
	 * Persists all working sets. Should only be called by the webapp working
	 * set dialog. Saves the working sets in the persistence store (cookie)
	 * format: curentWorkingSetName|name1&href11&href12|name2&href22
	 */
	private void saveState() throws IOException {
		saveContents();
		saveCriteria();
	}
	
	private void saveContents() throws IOException {
		
		StringBuffer data = new StringBuffer();
		data.append(URLCoder.compactEncode(currentWorkingSet /* , "UTF8" */
		));

		for (Iterator i = workingSets.iterator(); i.hasNext();) {
			data.append('|');
			WorkingSet ws = (WorkingSet) i.next();
			data.append(URLCoder.compactEncode(ws.getName() /* , "UTF8" */
			));

			AdaptableHelpResource[] resources = ws.getElements();
			AdaptableToc lastTopicParent = null;
			for (int j = 0; j < resources.length; j++) {

				IAdaptable parent = resources[j].getParent();
				if (parent == getRoot()) {
					// saving toc
					data.append('&');
					data.append(URLCoder.compactEncode(resources[j].getHref()
					/* , "UTF8" */
					));
					lastTopicParent = null;
				} else {
					// saving topic as tochref_topic#_
					AdaptableToc toc = (AdaptableToc) parent;
					AdaptableHelpResource[] siblings = (toc).getChildren();
					for (int t = 0; t < siblings.length; t++) {
						if (siblings[t] == resources[j]) {
							data.append('&');
							if (!toc.equals(lastTopicParent)) {
								data.append(URLCoder.compactEncode(toc.getHref()
								/* , "UTF8" */
								));
							}
							data.append('_');
							data.append(t);
							data.append('_');
							lastTopicParent = toc;
							break;
						}
					}
				}
			}
		}
		
		saveToCookie(COOKIE_WSET_CONTENTS, data.toString());
	}
	
	private void saveCriteria() throws IOException {
		
		StringBuffer data = new StringBuffer();
		data.append(URLCoder.compactEncode(currentWorkingSet));
		//|scope1$platform#AIX,WINDOWS,$version#1.0,2.0,
		for (Iterator i = workingSets.iterator(); i.hasNext();) {
			data.append('|');
			WorkingSet ws = (WorkingSet) i.next();
			data.append(URLCoder.compactEncode(ws.getName()));

			CriterionResource[] criteria = ws.getCriteria();
			for (int j = 0; j < criteria.length; ++ j){
				CriterionResource criterion = criteria[j];
				String criterionName = criterion.getCriterionName();
				List criterionValues = criterion.getCriterionValues();
				if(null != criterionValues && !criterionValues.isEmpty()){
					data.append('$');
					data.append(URLCoder.compactEncode(criterionName));
					data.append('#');
					for (Iterator iter = criterionValues.iterator(); iter.hasNext();) {
						String value = (String) iter.next();						
						data.append(URLCoder.compactEncode(value+','));
					}
				}
			}	
		}
		
		saveToCookie(COOKIE_WSET_CRITERIA, data.toString());
	}
	
	private void saveToCookie(String name, String data) throws IOException{
		
		try {
			CookieUtil.saveString(name, data, MAX_COOKIES, request, response);
		} catch (IOException ioe) {
			if (HelpWebappPlugin.DEBUG_WORKINGSETS) {
				String msg = "InfocenterWorkingSetManager.saveState(): Too much data to save: " + data; //$NON-NLS-1$
				System.out.println(msg);
			}
			throw ioe;
		}
	}
	

	/**
	 * *
	 * 
	 * @param changedWorkingSet
	 *            the working set that has changed
	 */
	public void workingSetChanged(WorkingSet changedWorkingSet)
			throws IOException {
		saveState();
	}

	public AdaptableToc getAdaptableToc(String href) {
		return getRoot().getAdaptableToc(href);
	}

	public AdaptableTopic getAdaptableTopic(String id) {

		if (id == null || id.length() == 0)
			return null;

		// toc id's are hrefs: /pluginId/path/to/toc.xml
		// topic id's are based on parent toc id and index of topic:
		// /pluginId/path/to/toc.xml_index_
		int len = id.length();
		if (id.charAt(len - 1) == '_') {
			// This is a first level topic
			String indexStr = id.substring(id.lastIndexOf('_', len - 2) + 1,
					len - 1);
			int index = 0;
			try {
				index = Integer.parseInt(indexStr);
			} catch (Exception e) {
			}

			String tocStr = id.substring(0, id.lastIndexOf('_', len - 2));
			AdaptableToc toc = getAdaptableToc(tocStr);
			if (toc == null)
				return null;
			IAdaptable[] topics = toc.getChildren();
			if (index < 0 || index >= topics.length)
				return null;
			return (AdaptableTopic) topics[index];
		}

		return null;
	}

	public String getCurrentWorkingSet() {
		return currentWorkingSet;
	}

	public void setCurrentWorkingSet(String workingSet) {
		currentWorkingSet = workingSet;
		try {
			saveState();
		} catch (IOException ioe) {
		}
	}

	public boolean isCriteriaScopeEnabled(){
		if(null == allCriteriaValues){
			allCriteriaValues = HelpPlugin.getCriteriaManager().getAllCriteriaValues(locale);
		}
		if(HelpPlugin.getCriteriaManager().isCriteriaEnabled() && !allCriteriaValues.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}
	
	public String[] getCriterionIds() {
		if(null == allCriteriaValues){
			allCriteriaValues = HelpPlugin.getCriteriaManager().getAllCriteriaValues(locale);
		}
		List criterionIds = new ArrayList();
		if(null != allCriteriaValues){
			for(Iterator iter = allCriteriaValues.keySet().iterator(); iter.hasNext();){
				String criterion = (String) iter.next();
				if(null == criterion || 0 == criterion.length() || 0 == getCriterionValueIds(criterion).length)
					continue;
				criterionIds.add(criterion);
			}
			Collections.sort(criterionIds);
		}
		String[] ids = new String[criterionIds.size()];                                        		
		criterionIds.toArray(ids);
		return ids;
	}
	

	public String[] getCriterionValueIds(String criterionName) {
		if(null == allCriteriaValues){
			allCriteriaValues = HelpPlugin.getCriteriaManager().getAllCriteriaValues(locale);
		}
		List valueIds = new ArrayList();
		if(null != criterionName && null != allCriteriaValues) {
			Set criterionValues = (Set)allCriteriaValues.get(criterionName);
			if(null != criterionValues && !criterionValues.isEmpty()) {
				valueIds.addAll(criterionValues);
				Collections.sort(valueIds);
				valueIds.add(UNCATEGORIZED);
			}
		}
		String[] valueIdsArray = new String[valueIds.size()];                                        		
		valueIds.toArray(valueIdsArray);
		return valueIdsArray;
	}
	

	public String getCriterionDisplayName(String criterionId) {
		return HelpPlugin.getCriteriaManager().getCriterionDisplayName(criterionId, locale);
	}

	public String getCriterionValueDisplayName(String criterionId, String criterionValueId) {
		return HelpPlugin.getCriteriaManager().getCriterionValueDisplayName(criterionId, criterionValueId, locale);
	}

}

Back to the top