KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > internal > defaultadaptor > MetaData


1 /*******************************************************************************
2  * Copyright (c) 2003, 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
12 package org.eclipse.osgi.framework.internal.defaultadaptor;
13
14 import java.io.*;
15 import java.util.Properties JavaDoc;
16
17 /**
18  * This class uses a Properties object to store and get MetaData information.
19  * All data is converted into String data before saving.
20  */

21 public class MetaData {
22
23     /**
24      * The Properties file to store the data.
25      */

26     Properties JavaDoc properties = new Properties JavaDoc();
27
28     /**
29      * The File object to store and load the Properties object.
30      */

31     File datafile;
32
33     /**
34      * The header string to use when storing data to the datafile.
35      */

36     String JavaDoc header;
37
38     /**
39      * Constructs a MetaData object that uses the datafile to persistently
40      * store data.
41      * @param datafile The File object used to persistently load and store data.
42      * @param header The header to use when storing data persistently.
43      */

44     public MetaData(File datafile, String JavaDoc header) {
45         this.datafile = datafile;
46         this.header = header;
47     }
48
49     /**
50      * Gets the metadata value for the key.
51      * @param key the key of the metadata
52      * @param def the default value to return if the key does not exist
53      * @return the value of the metadata or null if the key does not exist
54      * and the specified default is null.
55      */

56     public String JavaDoc get(String JavaDoc key, String JavaDoc def) {
57         return properties.getProperty(key, def);
58     }
59
60     /**
61      * Gets the integer value for the key.
62      * @param key the key of the metadata
63      * @param def the default value to return if the key does not exist
64      * @return the value of the metadata; if the key does not exist or
65      * the value cannot be converted to an int value then the
66      * specified default value is returned.
67      */

68     public int getInt(String JavaDoc key, int def) {
69         String JavaDoc result = get(key, null);
70         if (result == null) {
71             return def;
72         }
73         try {
74             return Integer.parseInt(result);
75         } catch (NumberFormatException JavaDoc nfe) {
76             return def;
77         }
78     }
79
80     /**
81      * Gets the long value for the key.
82      * @param key the key of the metadata
83      * @param def the default value to return if the key does not exist
84      * @return the value of the metadata; if the key does not exist or
85      * the value cannot be converted to an long value then the
86      * specified default value is returned.
87      */

88     public long getLong(String JavaDoc key, long def) {
89         String JavaDoc result = get(key, null);
90         if (result == null) {
91             return def;
92         }
93         try {
94             return Long.parseLong(result);
95         } catch (NumberFormatException JavaDoc nfe) {
96             return def;
97         }
98     }
99
100     /**
101      * Gets the boolean value for the key.
102      * @param key the key of the metadata
103      * @param def the default value to return if the key does not exist
104      * @return the value of the metadata; if the key does not exist then the
105      * specified default value is returned.
106      */

107     public boolean getBoolean(String JavaDoc key, boolean def) {
108         String JavaDoc result = get(key, null);
109         if (result == null) {
110             return def;
111         }
112         return Boolean.valueOf(result).booleanValue();
113     }
114
115     /**
116      * Sets the String value for a key.
117      * @param key the key of the metadata
118      * @param val the value of the metadata
119      */

120     public void set(String JavaDoc key, String JavaDoc val) {
121         properties.put(key, val);
122     }
123
124     /**
125      * Sets the int value for a key.
126      * @param key the key of the metadata
127      * @param val the value of the metadata
128      */

129     public void setInt(String JavaDoc key, int val) {
130         properties.put(key, Integer.toString(val));
131     }
132
133     /**
134      * Sets the long value for a key.
135      * @param key the key of the metadata
136      * @param val the value of the metadata
137      */

138     public void setLong(String JavaDoc key, long val) {
139         properties.put(key, Long.toString(val));
140     }
141
142     /**
143      * Sets the boolean value for a key.
144      * @param key the key of the metadata
145      * @param val the value of the metadata
146      */

147     public void setBoolean(String JavaDoc key, boolean val) {
148         properties.put(key, new Boolean JavaDoc(val).toString());
149     }
150
151     /**
152      * Removes the metadata value with the specified key.
153      * @param key the key of the metadata to be removed
154      */

155     public void remove(String JavaDoc key) {
156         properties.remove(key);
157     }
158
159     /**
160      * Saves the metadata to persistent storage.
161      * @throws IOException if there is a problem saving to persistent storage.
162      */

163     public void save() throws IOException {
164         if (!datafile.exists() && datafile.getParent() != null) {
165             File parent = new File(datafile.getParent());
166             if (!parent.exists())
167                 parent.mkdir();
168         }
169             
170         FileOutputStream fos = new FileOutputStream(datafile);
171         try {
172             properties.store(fos, header);
173         } finally {
174             fos.close();
175         }
176     }
177
178     /**
179      * Loads the metadata from persistent storage
180      * @throws IOException if there is a problem reading from persistent storage.
181      */

182     public void load() throws IOException {
183         properties.clear();
184         if (datafile.exists()) {
185             FileInputStream fis = new FileInputStream(datafile);
186             try {
187                 properties.load(fis);
188             } finally {
189                 fis.close();
190             }
191         }
192     }
193
194     /**
195      * @see Object#toString()
196      */

197     public String JavaDoc toString() {
198         return properties.toString();
199     }
200
201 }
202
Popular Tags