package org.eclipse.core.internal.resources;
import java.io.*;
import java.util.Properties;
import java.util.Set;
import org.eclipse.core.internal.utils.Messages;
import org.eclipse.core.resources.IResourceStatus;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.*;
public class SafeFileTable {
protected IPath location;
protected Properties table;
public SafeFileTable(String pluginId) throws CoreException {
location = getWorkspace().getMetaArea().getSafeTableLocationFor(pluginId);
restore();
}
public IPath[] getFiles() {
Set<Object> set = table.keySet();
String[] keys = set.toArray(new String[set.size()]);
IPath[] files = new IPath[keys.length];
for (int i = 0; i < keys.length; i++)
files[i] = new Path(keys[i]);
return files;
}
protected Workspace getWorkspace() {
return (Workspace) ResourcesPlugin.getWorkspace();
}
public IPath lookup(IPath file) {
String result = table.getProperty(file.toOSString());
return result == null ? null : new Path(result);
}
public void map(IPath file, IPath aLocation) {
if (aLocation == null)
table.remove(file);
else
table.setProperty(file.toOSString(), aLocation.toOSString());
}
public void restore() throws CoreException {
java.io.File target = location.toFile();
table = new Properties();
if (!target.exists())
return;
try (FileInputStream input = new FileInputStream(target)) {
table.load(input);
} catch (IOException e) {
String message = Messages.resources_exSafeRead;
throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e);
}
}
public void save() throws CoreException {
java.io.File target = location.toFile();
try (FileOutputStream output = new FileOutputStream(target)) {
table.store(output, "safe table");
} catch (IOException e) {
String message = Messages.resources_exSafeSave;
throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e);
}
}
public void setLocation(IPath location) {
if (location != null)
this.location = location;
}
}