Skip to main content
summaryrefslogtreecommitdiffstats
blob: f7343eeb513e98b1e01b47e178ce68bc788d6f44 (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
/*******************************************************************************
 * Copyright (c) 2009, 2016 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.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.help.internal.webapp.WebappResources;
import org.eclipse.help.internal.webapp.data.UrlUtil;
import org.osgi.service.prefs.BackingStoreException;

/**
 * Used by the about.html page to display help system preferences
 */

public class PreferenceWriter {
	private StringBuffer buf;
	private Locale locale;
	private boolean isXML;

	public PreferenceWriter(StringBuffer buf, Locale locale) {
		this(buf, locale, false);
	}

	public PreferenceWriter(StringBuffer buf, Locale locale, boolean isXML) {
		this.buf = buf;
		this.locale = locale;
		this.isXML = isXML;
	}

	public void writePreferences() {
		writePreference("org.eclipse.help"); //$NON-NLS-1$
		writePreference("org.eclipse.help.base"); //$NON-NLS-1$
	}

	private void writePreference(String plugin) {
		try {
		    IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode(plugin);
			Set<String> keySet = new HashSet<>();
		    prefs = DefaultScope.INSTANCE.getNode(plugin);
			keySet.addAll(Arrays.asList(prefs.keys()));
			String[] allKeys = keySet.toArray(new String[keySet.size()]);
			if (allKeys.length > 0) {
			    Arrays.sort(allKeys);

			    if (!isXML) {
			    	buf.append("\n<h3>"); //$NON-NLS-1$
			    	buf.append(plugin);
			    	buf.append("</h3>\n"); //$NON-NLS-1$
			    	buf.append("<table>");  //$NON-NLS-1$
			    } else {
					buf.append("\n    <plugin\n          title=\""); //$NON-NLS-1$
					buf.append(XMLGenerator.xmlEscape(plugin));
					buf.append("\">"); //$NON-NLS-1$
			    }
			    for (String key : allKeys) {
			    	String value = Platform.getPreferencesService().getString
			    			(plugin, key, "", null); //$NON-NLS-1$
			    	if (!isXML) {
				    	buf.append("\n    <tr>\n"); //$NON-NLS-1$
				    	buf.append("        <td>"); //$NON-NLS-1$
				    	buf.append(UrlUtil.htmlEncode(key));
				    	buf.append("</td>\n        <td>"); //$NON-NLS-1$
						buf.append(UrlUtil.htmlEncode(value));
						buf.append("</td>\n    </tr>"); //$NON-NLS-1$
			    	} else {
				    	buf.append("\n        <"); //$NON-NLS-1$
			    		buf.append(key);
			    		buf.append(">"); //$NON-NLS-1$
						buf.append(value);
				    	buf.append("</"); //$NON-NLS-1$
			    		buf.append(key);
			    		buf.append(">"); //$NON-NLS-1$
			    	}
			    }
			    if (!isXML) {
			    	buf.append("\n</table>"); //$NON-NLS-1$
			    } else {
			    	buf.append("\n    </plugin>"); //$NON-NLS-1$
			    }
			}
		} catch (BackingStoreException e) {
			buf.append(WebappResources.getString("badPreferences", locale)); //$NON-NLS-1$
		}
	}

}

Back to the top