blob: f76fd8e23b8781cbf9969e960fbfc8b5655a6444 [file] [log] [blame]
amywu3a205672006-02-23 05:05:23 +00001/*******************************************************************************
nsandonato49ba2ea2010-10-05 15:24:10 +00002 * Copyright (c) 2006, 2010 IBM Corporation and others.
amywu3a205672006-02-23 05:05:23 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.wst.html.core.internal.contentproperties;
12
13import org.eclipse.core.resources.IProject;
14import org.eclipse.core.resources.IResource;
15import org.eclipse.core.resources.ProjectScope;
16import org.eclipse.core.runtime.CoreException;
17import org.eclipse.core.runtime.IPath;
18import org.eclipse.core.runtime.IStatus;
19import org.eclipse.core.runtime.Platform;
20import org.eclipse.core.runtime.Status;
21import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
22import org.eclipse.wst.html.core.internal.Logger;
23import org.osgi.service.prefs.BackingStoreException;
24import org.osgi.service.prefs.Preferences;
25
26/**
27 * Properties constants used by HTML. Clients should only read and modify the
28 * HTML properties programmatically using this class.
29 *
30 * @since 1.1
31 */
32public class HTMLContentProperties {
33 static final String HTMLCORE_ID = HTMLCorePlugin.getDefault().getBundle().getSymbolicName();
34 private static final String PROJECT_KEY = "<project>"; //$NON-NLS-1$
35
36 /**
37 * The default document type to use when none is specified.
38 * <p>
39 * Value is of type <code>String</code>.
40 * </p>
41 */
42 public static final String DOCUMENT_TYPE = "document-type"; //$NON-NLS-1$
43
44 /**
45 * Generates a preference key based on resourcePath
46 *
47 * @param resourcePath
48 * the path the key will be based off of
49 * @return preference key based on resourcePath (basically the
50 * resourcePath without the filename); PROJECT_KEY if resourcePath
51 * is null
52 */
53 static String getKeyFor(IPath resourcePath) {
54 String key = PROJECT_KEY;
55 if (resourcePath != null && resourcePath.segmentCount() > 1) {
56 key = resourcePath.removeFirstSegments(1).toString();
57 }
58 return key;
59 }
60
61 /**
62 * Get the preferences node associated with the given project scope and
63 * preference key (subNode) If create is true, the preference node will be
64 * created if one does not already exist
65 *
66 * @param project
67 * the project the preference node is under
68 * @param preferenceKey
69 * the subnode/category the preference node is located in
70 * @param create
71 * if true, a preference node will be created if one does not
72 * already exist
73 * @return Preferences associated with the given project scope and
74 * preference key. null if one could not be found and create is
75 * false
76 */
77 static Preferences getPreferences(IProject project, String preferenceKey, boolean create) {
78 if (create)
79 // create all nodes down to the one we are interested in
80 return new ProjectScope(project).getNode(HTMLCORE_ID).node(preferenceKey);
81 // be careful looking up for our node so not to create any nodes as
82 // side effect
83 Preferences node = Platform.getPreferencesService().getRootNode().node(ProjectScope.SCOPE);
84 try {
85 // TODO once bug 90500 is fixed, should be as simple as this:
86 // String path = project.getName() + IPath.SEPARATOR +
87 // ResourcesPlugin.PI_RESOURCES + IPath.SEPARATOR +
88 // ENCODING_PREF_NODE;
89 // return node.nodeExists(path) ? node.node(path) : null;
90 // for now, take the long way
91 if (!node.nodeExists(project.getName()))
92 return null;
93 node = node.node(project.getName());
94 if (!node.nodeExists(HTMLCORE_ID))
95 return null;
96 node = node.node(HTMLCORE_ID);
97 if (!node.nodeExists(preferenceKey))
98 return null;
99 return node.node(preferenceKey);
100 }
101 catch (BackingStoreException e) {
102 // nodeExists failed
103 Logger.log(Logger.WARNING_DEBUG, "Could not retrieve preference node", e); //$NON-NLS-1$
104 }
105 return null;
106 }
107
108 /**
109 * Returns the value for the given key in the given context.
110 *
111 * @param key
112 * The property key
113 * @param resource
114 * The current context or <code>null</code> if no context is
115 * available and the workspace setting should be taken. Note
116 * that passing <code>null</code> should be avoided.
117 * @param recurse
118 * whether the parent should be queried till property is found
119 * @return Returns the current value for the key.
120 * @since 1.1
121 */
122 public static String getProperty(String key, IResource resource, boolean recurse) {
123 String val = null;
124 // boolean preferenceFound = false;
125 if (resource != null) {
126 IProject project = resource.getProject();
127 if (project != null) {
128 Preferences preferences = getPreferences(project, key, false);
129 if (preferences != null) {
130 val = internalGetProperty(resource, recurse, preferences);
131 // preferenceFound = true;
132 }
133 }
134 }
135 // Workbench preference may not be needed so leave out for now
136 // // no preferences found - for performance reasons,
137 // // short-circuit
138 // // lookup by falling back to workspace's default
139 // // setting
140 // if (!preferenceFound)
141 // val = getWorkbenchPreference(key);
142 return val;
143 }
144
145 private static String internalGetProperty(IResource resource, boolean recurse, Preferences preferences) {
146 String value = preferences.get(getKeyFor(resource.getFullPath()), null);
147 if (value == null && resource != resource.getProject() && recurse) {
148 value = preferences.get(getKeyFor(null), null);
149 }
150
151 // Workbench preference may not be needed so leave out for now
152 // // ensure we default to the workspace preference if none is found
153 // return value == null ? getWorkbenchPreference(key) : value;
154 return value;
155 }
156
157 /**
158 * Sets the value for the given key in the given context.
159 *
160 * @param key
161 * The property key
162 * @param resource
163 * The current context. Note context cannot be
164 * <code>null</code>.
165 * @param value
166 * The value to set for the key. If value is <code>null</code>
167 * the key is removed from properties.
168 * @since 1.1
169 */
170 public static void setProperty(String key, IResource resource, String value) throws CoreException {
171 if (resource != null) {
172 IProject project = resource.getProject();
173 if (project != null) {
174 Preferences preferences = getPreferences(project, key, true);
nsandonato49ba2ea2010-10-05 15:24:10 +0000175 if (value == null)
amywu3a205672006-02-23 05:05:23 +0000176 preferences.remove(getKeyFor(resource.getFullPath()));
177 else
178 preferences.put(getKeyFor(resource.getFullPath()), value);
179 try {
180 // save changes
181 preferences.flush();
182 }
183 catch (BackingStoreException e) {
184 throw new CoreException(new Status(IStatus.ERROR, HTMLCORE_ID, IStatus.ERROR, "Unable to set property", e)); //$NON-NLS-1$
185 }
186 }
187 }
188 // Workbench preference may not be needed so leave out for now
189 // just set a preference in the instance scope
190 // if (!preferenceFound) {
191 // setWorkbenchPreference(key);
192 // }
193 }
194}