KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > SafeFileTable


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * 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  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.io.*;
14 import java.util.Properties JavaDoc;
15 import java.util.Set JavaDoc;
16 import org.eclipse.core.internal.utils.Messages;
17 import org.eclipse.core.resources.IResourceStatus;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.*;
20
21 /**
22  * Represents a table of keys and paths used by a plugin to maintain its
23  * configuration files' names.
24  */

25 public class SafeFileTable {
26     protected IPath location;
27     protected Properties JavaDoc table;
28
29     public SafeFileTable(String JavaDoc pluginId) throws CoreException {
30         location = getWorkspace().getMetaArea().getSafeTableLocationFor(pluginId);
31         restore();
32     }
33
34     public IPath[] getFiles() {
35         Set JavaDoc set = table.keySet();
36         String JavaDoc[] keys = (String JavaDoc[]) set.toArray(new String JavaDoc[set.size()]);
37         IPath[] files = new IPath[keys.length];
38         for (int i = 0; i < keys.length; i++)
39             files[i] = new Path(keys[i]);
40         return files;
41     }
42
43     protected Workspace getWorkspace() {
44         return (Workspace) ResourcesPlugin.getWorkspace();
45     }
46
47     public IPath lookup(IPath file) {
48         String JavaDoc result = table.getProperty(file.toOSString());
49         return result == null ? null : new Path(result);
50     }
51
52     public void map(IPath file, IPath aLocation) {
53         if (aLocation == null)
54             table.remove(file);
55         else
56             table.setProperty(file.toOSString(), aLocation.toOSString());
57     }
58
59     public void restore() throws CoreException {
60         java.io.File JavaDoc target = location.toFile();
61         table = new Properties JavaDoc();
62         if (!target.exists())
63             return;
64         try {
65             FileInputStream input = new FileInputStream(target);
66             try {
67                 table.load(input);
68             } finally {
69                 input.close();
70             }
71         } catch (IOException e) {
72             String JavaDoc message = Messages.resources_exSafeRead;
73             throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e);
74         }
75     }
76
77     public void save() throws CoreException {
78         java.io.File JavaDoc target = location.toFile();
79         try {
80             FileOutputStream output = new FileOutputStream(target);
81             try {
82                 table.store(output, "safe table"); //$NON-NLS-1$
83
} finally {
84                 output.close();
85             }
86         } catch (IOException e) {
87             String JavaDoc message = Messages.resources_exSafeSave;
88             throw new ResourceException(IResourceStatus.INTERNAL_ERROR, null, message, e);
89         }
90     }
91
92     public void setLocation(IPath location) {
93         if (location != null)
94             this.location = location;
95     }
96 }
97
Popular Tags