1 11 package org.eclipse.core.internal.resources; 12 13 import java.net.URI ; 14 import java.util.*; 15 import org.eclipse.core.filesystem.URIUtil; 16 import org.eclipse.core.internal.events.PathVariableChangeEvent; 17 import org.eclipse.core.internal.utils.Messages; 18 import org.eclipse.core.resources.*; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.osgi.util.NLS; 21 22 25 public class PathVariableManager implements IPathVariableManager, IManager { 26 27 static final String VARIABLE_PREFIX = "pathvariable."; private Set listeners; 29 30 private Preferences preferences; 31 32 35 public PathVariableManager() { 36 this.listeners = Collections.synchronizedSet(new HashSet()); 37 this.preferences = ResourcesPlugin.getPlugin().getPluginPreferences(); 38 } 39 40 44 public void addChangeListener(IPathVariableChangeListener listener) { 45 listeners.add(listener); 46 } 47 48 52 private void checkIsValidName(String name) throws CoreException { 53 IStatus status = validateName(name); 54 if (!status.isOK()) 55 throw new CoreException(status); 56 } 57 58 62 private void checkIsValidValue(IPath newValue) throws CoreException { 63 IStatus status = validateValue(newValue); 64 if (!status.isOK()) 65 throw new CoreException(status); 66 } 67 68 84 private void fireVariableChangeEvent(String name, IPath value, int type) { 85 if (this.listeners.size() == 0) 86 return; 87 Object [] listenerArray = this.listeners.toArray(); 89 final PathVariableChangeEvent pve = new PathVariableChangeEvent(this, name, value, type); 90 for (int i = 0; i < listenerArray.length; ++i) { 91 final IPathVariableChangeListener l = (IPathVariableChangeListener) listenerArray[i]; 92 ISafeRunnable job = new ISafeRunnable() { 93 public void handleException(Throwable exception) { 94 } 96 97 public void run() throws Exception { 98 l.pathVariableChanged(pve); 99 } 100 }; 101 SafeRunner.run(job); 102 } 103 } 104 105 108 private String getKeyForName(String varName) { 109 return VARIABLE_PREFIX + varName; 110 } 111 112 115 public String [] getPathVariableNames() { 116 List result = new LinkedList(); 117 String [] names = preferences.propertyNames(); 118 for (int i = 0; i < names.length; i++) { 119 if (names[i].startsWith(VARIABLE_PREFIX)) { 120 String key = names[i].substring(VARIABLE_PREFIX.length()); 121 if (validateName(key).isOK() && validateValue(getValue(key)).isOK()) 129 result.add(key); 130 } 131 } 132 return (String []) result.toArray(new String [result.size()]); 133 } 134 135 143 public IPath getValue(String varName) { 144 String key = getKeyForName(varName); 145 String value = preferences.getString(key); 146 return value.length() == 0 ? null : Path.fromPortableString(value); 147 } 148 149 152 public boolean isDefined(String varName) { 153 return getValue(varName) != null; 154 } 155 156 160 public void removeChangeListener(IPathVariableChangeListener listener) { 161 listeners.remove(listener); 162 } 163 164 167 public IPath resolvePath(IPath path) { 168 if (path == null || path.segmentCount() == 0 || path.isAbsolute() || path.getDevice() != null) 169 return path; 170 IPath value = getValue(path.segment(0)); 171 return value == null ? path : value.append(path.removeFirstSegments(1)); 172 } 173 174 public URI resolveURI(URI uri) { 175 if (uri == null || uri.isAbsolute()) 176 return uri; 177 IPath raw = new Path(uri.getSchemeSpecificPart()); 178 IPath resolved = resolvePath(raw); 179 return raw == resolved ? uri : URIUtil.toURI(resolved); 180 } 181 182 185 public void setValue(String varName, IPath newValue) throws CoreException { 186 checkIsValidName(varName); 187 if (newValue != null && newValue.isAbsolute() && newValue.getDevice() == null) 189 newValue = new Path(newValue.toFile().getAbsolutePath()); 190 checkIsValidValue(newValue); 191 int eventType; 192 synchronized (this) { 194 IPath currentValue = getValue(varName); 195 boolean variableExists = currentValue != null; 196 if (!variableExists && newValue == null) 197 return; 198 if (variableExists && currentValue.equals(newValue)) 199 return; 200 if (newValue == null) { 201 preferences.setToDefault(getKeyForName(varName)); 202 eventType = IPathVariableChangeEvent.VARIABLE_DELETED; 203 } else { 204 preferences.setValue(getKeyForName(varName), newValue.toPortableString()); 205 eventType = variableExists ? IPathVariableChangeEvent.VARIABLE_CHANGED : IPathVariableChangeEvent.VARIABLE_CREATED; 206 } 207 } 208 fireVariableChangeEvent(varName, newValue, eventType); 210 } 211 212 215 public void shutdown(IProgressMonitor monitor) { 216 } 219 220 223 public void startup(IProgressMonitor monitor) { 224 } 227 228 231 public IStatus validateName(String name) { 232 String message = null; 233 if (name.length() == 0) { 234 message = Messages.pathvar_length; 235 return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message); 236 } 237 238 char first = name.charAt(0); 239 if (!Character.isLetter(first) && first != '_') { 240 message = NLS.bind(Messages.pathvar_beginLetter, String.valueOf(first)); 241 return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message); 242 } 243 244 for (int i = 1; i < name.length(); i++) { 245 char following = name.charAt(i); 246 if (Character.isWhitespace(following)) 247 return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, Messages.pathvar_whitespace); 248 if (!Character.isLetter(following) && !Character.isDigit(following) && following != '_') { 249 message = NLS.bind(Messages.pathvar_invalidChar, String.valueOf(following)); 250 return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message); 251 } 252 } 253 return Status.OK_STATUS; 254 } 255 256 259 public IStatus validateValue(IPath value) { 260 if (value != null && (!value.isValidPath(value.toString()) || !value.isAbsolute())) { 261 String message = Messages.pathvar_invalidValue; 262 return new ResourceStatus(IResourceStatus.INVALID_VALUE, null, message); 263 } 264 return Status.OK_STATUS; 265 } 266 } 267 | Popular Tags |