/** * Returns the charset explicitly set by the user for the given resource, or <code>null</code>. If * no setting exists for the given resource and <code>recurse</code> is <code>true</code>, every * parent up to the workspace root will be checked until a charset setting can be found. * * @param resourcePath the path for the resource * @param recurse whether the parent should be queried * @return the charset setting for the given resource */ public String getCharsetFor(IPath resourcePath, boolean recurse) { Assert.isLegal(resourcePath.segmentCount() >= 1); IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); Preferences prefs = getPreferences(project, false, false); Preferences derivedPrefs = getPreferences(project, false, true); if (prefs == null && derivedPrefs == null) // no preferences found - for performance reasons, short-circuit // lookup by falling back to workspace's default setting return recurse ? ResourcesPlugin.getEncoding() : null; return internalGetCharsetFor(prefs, derivedPrefs, resourcePath, recurse); }
public void setCharsetFor(IPath resourcePath, String newCharset) throws CoreException { // for the workspace root we just set a preference in the instance scope if (resourcePath.segmentCount() == 0) { IEclipsePreferences resourcesPreferences = InstanceScope.INSTANCE.getNode(ResourcesPlugin.PI_RESOURCES); if (newCharset != null) resourcesPreferences.put(ResourcesPlugin.PREF_ENCODING, newCharset); else resourcesPreferences.remove(ResourcesPlugin.PREF_ENCODING); try { resourcesPreferences.flush(); } catch (BackingStoreException e) { IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); String message = Messages.resources_savingEncoding; throw new ResourceException( IResourceStatus.FAILED_SETTING_CHARSET, project.getFullPath(), message, e); } return; } // for all other cases, we set a property in the corresponding project IResource resource = workspace.getRoot().findMember(resourcePath); if (resource != null) { try { // disable the listener so we don't react to changes made by ourselves Preferences encodingSettings = getPreferences( resource.getProject(), true, resource.isDerived(IResource.CHECK_ANCESTORS)); if (newCharset == null || newCharset.trim().length() == 0) encodingSettings.remove(getKeyFor(resourcePath)); else encodingSettings.put(getKeyFor(resourcePath), newCharset); flushPreferences(encodingSettings, true); } catch (BackingStoreException e) { IProject project = workspace.getRoot().getProject(resourcePath.segment(0)); String message = Messages.resources_savingEncoding; throw new ResourceException( IResourceStatus.FAILED_SETTING_CHARSET, project.getFullPath(), message, e); } } }