Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 3bbc582ac6fc24762142543e6182b13e0ea2cc3e (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
/*******************************************************************************
 * Copyright (c) 2006 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.wst.css.core.internal.contentproperties;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.wst.css.core.internal.CSSCorePlugin;
import org.eclipse.wst.css.core.internal.Logger;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
 * Properties constants used by CSS. Clients should only read and modify the
 * CSS properties programmatically using this class.
 * 
 * @since 1.1
 */
public class CSSContentProperties {
	static final String CSSCORE_ID = CSSCorePlugin.getDefault().getBundle().getSymbolicName();
	private static final String PROJECT_KEY = "<project>"; //$NON-NLS-1$

	/**
	 * The default css profile to use when none is specified.
	 * <p>
	 * Value is of type <code>String</code>.
	 * </p>
	 */
	public static final String CSS_PROFILE = "css-profile"; //$NON-NLS-1$

	/**
	 * Generates a preference key based on resourcePath
	 * 
	 * @param resourcePath
	 *            the path the key will be based off of
	 * @return preference key based on resourcePath (basically the
	 *         resourcePath without the filename); PROJECT_KEY if resourcePath
	 *         is null
	 */
	static String getKeyFor(IPath resourcePath) {
		String key = PROJECT_KEY;
		if (resourcePath != null && resourcePath.segmentCount() > 1) {
			key = resourcePath.removeFirstSegments(1).toString();
		}
		return key;
	}

	/**
	 * Get the preferences node associated with the given project scope and
	 * preference key (subNode) If create is true, the preference node will be
	 * created if one does not already exist
	 * 
	 * @param project
	 *            the project the preference node is under
	 * @param preferenceKey
	 *            the subnode/category the preference node is located in
	 * @param create
	 *            if true, a preference node will be created if one does not
	 *            already exist
	 * @return Preferences associated with the given project scope and
	 *         preference key. null if one could not be found and create is
	 *         false
	 */
	static Preferences getPreferences(IProject project, String preferenceKey, boolean create) {
		if (create)
			// create all nodes down to the one we are interested in
			return new ProjectScope(project).getNode(CSSCORE_ID).node(preferenceKey);
		// be careful looking up for our node so not to create any nodes as
		// side effect
		Preferences node = Platform.getPreferencesService().getRootNode().node(ProjectScope.SCOPE);
		try {
			// TODO once bug 90500 is fixed, should be as simple as this:
			// String path = project.getName() + IPath.SEPARATOR +
			// ResourcesPlugin.PI_RESOURCES + IPath.SEPARATOR +
			// ENCODING_PREF_NODE;
			// return node.nodeExists(path) ? node.node(path) : null;
			// for now, take the long way
			if (!node.nodeExists(project.getName()))
				return null;
			node = node.node(project.getName());
			if (!node.nodeExists(CSSCORE_ID))
				return null;
			node = node.node(CSSCORE_ID);
			if (!node.nodeExists(preferenceKey))
				return null;
			return node.node(preferenceKey);
		}
		catch (BackingStoreException e) {
			// nodeExists failed
			Logger.log(Logger.WARNING_DEBUG, "Could not retrieve preference node", e); //$NON-NLS-1$
		}
		return null;
	}

	/**
	 * Returns the value for the given key in the given context.
	 * 
	 * @param key
	 *            The property key
	 * @param resource
	 *            The current context or <code>null</code> if no context is
	 *            available and the workspace setting should be taken. Note
	 *            that passing <code>null</code> should be avoided.
	 * @param recurse
	 *            whether the parent should be queried till property is found
	 * @return Returns the current value for the key.
	 * @since 1.1
	 */
	public static String getProperty(String key, IResource resource, boolean recurse) {
		String val = null;
		// boolean preferenceFound = false;
		if (resource != null) {
			IProject project = resource.getProject();
			if (project != null) {
				Preferences preferences = getPreferences(project, key, false);
				if (preferences != null) {
					val = internalGetProperty(resource, recurse, preferences);
					// preferenceFound = true;
				}
			}
		}
		// Workbench preference may not be needed so leave out for now
		// // no preferences found - for performance reasons,
		// // short-circuit
		// // lookup by falling back to workspace's default
		// // setting
		// if (!preferenceFound)
		// val = getWorkbenchPreference(key);
		return val;
	}

	private static String internalGetProperty(IResource resource, boolean recurse, Preferences preferences) {
		String value = preferences.get(getKeyFor(resource.getFullPath()), null);
		if (value == null && resource != resource.getProject() && recurse) {
			value = preferences.get(getKeyFor(null), null);
		}

		// Workbench preference may not be needed so leave out for now
		// // ensure we default to the workspace preference if none is found
		// return value == null ? getWorkbenchPreference(key) : value;
		return value;
	}

	/**
	 * Sets the value for the given key in the given context.
	 * 
	 * @param key
	 *            The property key
	 * @param resource
	 *            The current context. Note context cannot be
	 *            <code>null</code>.
	 * @param value
	 *            The value to set for the key. If value is <code>null</code>
	 *            the key is removed from properties.
	 * @since 1.1
	 */
	public static void setProperty(String key, IResource resource, String value) throws CoreException {
		if (resource != null) {
			IProject project = resource.getProject();
			if (project != null) {
				Preferences preferences = getPreferences(project, key, true);
				if (value == null || value.trim().length() == 0)
					preferences.remove(getKeyFor(resource.getFullPath()));
				else
					preferences.put(getKeyFor(resource.getFullPath()), value);
				try {
					// save changes
					preferences.flush();
				}
				catch (BackingStoreException e) {
					throw new CoreException(new Status(IStatus.ERROR, CSSCORE_ID, IStatus.ERROR, "Unable to set property", e)); //$NON-NLS-1$
				}
			}
		}
		// Workbench preference may not be needed so leave out for now
		// just set a preference in the instance scope
		// if (!preferenceFound) {
		// setWorkbenchPreference(key);
		// }
	}
}

Back to the top