1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration;
18
19 import java.io.Reader;
20 import java.io.Writer;
21 import java.io.File;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.net.URL;
25 import java.util.Iterator;
26
27 import org.apache.commons.configuration.reloading.ReloadingStrategy;
28
29 /***
30 * <p>Base class for implementing file based hierarchical configurations.</p>
31 * <p>This class serves an analogous purpose as the
32 * <code>{@link AbstractFileConfiguration}</code> class for non hierarchical
33 * configurations. It behaves in exactly the same way, so please refer to the
34 * documentation of <code>AbstractFileConfiguration</code> for further details.</p>
35 *
36 * @since 1.2
37 *
38 * @author Emmanuel Bourg
39 * @version $Revision$, $Date: 2005-11-22 21:40:57 +0100 (Tue, 22 Nov 2005) $
40 */
41 public abstract class AbstractHierarchicalFileConfiguration
42 extends HierarchicalConfiguration implements FileConfiguration
43 {
44 /*** Stores the delegate used for implementing functionality related to the
45 * <code>FileConfiguration</code> interface.
46 */
47 private FileConfigurationDelegate delegate = createDelegate();
48
49 protected AbstractHierarchicalFileConfiguration()
50 {
51 }
52
53 /***
54 * Creates and loads the configuration from the specified file.
55 *
56 * @param fileName The name of the plist file to load.
57 * @throws ConfigurationException Error while loading the file
58 */
59 public AbstractHierarchicalFileConfiguration(String fileName) throws ConfigurationException
60 {
61
62 delegate.setPath(fileName);
63
64
65 load();
66 }
67
68 /***
69 * Creates and loads the configuration from the specified file.
70 *
71 * @param file The configuration file to load.
72 * @throws ConfigurationException Error while loading the file
73 */
74 public AbstractHierarchicalFileConfiguration(File file) throws ConfigurationException
75 {
76
77 setFile(file);
78
79
80 if (file.exists())
81 {
82 load();
83 }
84 }
85
86 /***
87 * Creates and loads the configuration from the specified URL.
88 *
89 * @param url The location of the configuration file to load.
90 * @throws ConfigurationException Error while loading the file
91 */
92 public AbstractHierarchicalFileConfiguration(URL url) throws ConfigurationException
93 {
94
95 setURL(url);
96
97
98 load();
99 }
100
101 protected void addPropertyDirect(String key, Object obj)
102 {
103 super.addPropertyDirect(key, obj);
104 delegate.possiblySave();
105 }
106
107 public void clearProperty(String key)
108 {
109 super.clearProperty(key);
110 delegate.possiblySave();
111 }
112
113 public void clearTree(String key)
114 {
115 super.clearTree(key);
116 delegate.possiblySave();
117 }
118
119 public void setProperty(String key, Object value)
120 {
121 super.setProperty(key, value);
122 delegate.possiblySave();
123 }
124
125 public void load() throws ConfigurationException
126 {
127 delegate.load();
128 }
129
130 public void load(String fileName) throws ConfigurationException
131 {
132 delegate.load(fileName);
133 }
134
135 public void load(File file) throws ConfigurationException
136 {
137 delegate.load(file);
138 }
139
140 public void load(URL url) throws ConfigurationException
141 {
142 delegate.load(url);
143 }
144
145 public void load(InputStream in) throws ConfigurationException
146 {
147 delegate.load(in);
148 }
149
150 public void load(InputStream in, String encoding) throws ConfigurationException
151 {
152 delegate.load(in, encoding);
153 }
154
155 public void save() throws ConfigurationException
156 {
157 delegate.save();
158 }
159
160 public void save(String fileName) throws ConfigurationException
161 {
162 delegate.save(fileName);
163 }
164
165 public void save(File file) throws ConfigurationException
166 {
167 delegate.save(file);
168 }
169
170 public void save(URL url) throws ConfigurationException
171 {
172 delegate.save(url);
173 }
174
175 public void save(OutputStream out) throws ConfigurationException
176 {
177 delegate.save(out);
178 }
179
180 public void save(OutputStream out, String encoding) throws ConfigurationException
181 {
182 delegate.save(out, encoding);
183 }
184
185 public String getFileName()
186 {
187 return delegate.getFileName();
188 }
189
190 public void setFileName(String fileName)
191 {
192 delegate.setFileName(fileName);
193 }
194
195 public String getBasePath()
196 {
197 return delegate.getBasePath();
198 }
199
200 public void setBasePath(String basePath)
201 {
202 delegate.setBasePath(basePath);
203 }
204
205 public File getFile()
206 {
207 return delegate.getFile();
208 }
209
210 public void setFile(File file)
211 {
212 delegate.setFile(file);
213 }
214
215 public URL getURL()
216 {
217 return delegate.getURL();
218 }
219
220 public void setURL(URL url)
221 {
222 delegate.setURL(url);
223 }
224
225 public void setAutoSave(boolean autoSave)
226 {
227 delegate.setAutoSave(autoSave);
228 }
229
230 public boolean isAutoSave()
231 {
232 return delegate.isAutoSave();
233 }
234
235 public ReloadingStrategy getReloadingStrategy()
236 {
237 return delegate.getReloadingStrategy();
238 }
239
240 public void setReloadingStrategy(ReloadingStrategy strategy)
241 {
242 delegate.setReloadingStrategy(strategy);
243 }
244
245 public void reload()
246 {
247 delegate.reload();
248 }
249
250 public String getEncoding()
251 {
252 return delegate.getEncoding();
253 }
254
255 public void setEncoding(String encoding)
256 {
257 delegate.setEncoding(encoding);
258 }
259
260 public boolean containsKey(String key)
261 {
262 reload();
263 return super.containsKey(key);
264 }
265
266 public Iterator getKeys(String prefix)
267 {
268 reload();
269 return super.getKeys(prefix);
270 }
271
272 public Object getProperty(String key)
273 {
274 reload();
275 return super.getProperty(key);
276 }
277
278 public boolean isEmpty()
279 {
280 reload();
281 return super.isEmpty();
282 }
283
284 /***
285 * Creates the file configuration delegate, i.e. the object that implements
286 * functionality required by the <code>FileConfiguration</code> interface.
287 * This base implementation will return an instance of the
288 * <code>FileConfigurationDelegate</code> class. Derived classes may
289 * override it to create a different delegate object.
290 *
291 * @return the file configuration delegate
292 */
293 protected FileConfigurationDelegate createDelegate()
294 {
295 return new FileConfigurationDelegate();
296 }
297
298 /***
299 * Returns the file configuration delegate.
300 *
301 * @return the delegate
302 */
303 protected FileConfigurationDelegate getDelegate()
304 {
305 return delegate;
306 }
307
308 /***
309 * Allows to set the file configuration delegate.
310 * @param delegate the new delegate
311 */
312 protected void setDelegate(FileConfigurationDelegate delegate)
313 {
314 this.delegate = delegate;
315 }
316
317 /***
318 * A special implementation of the <code>FileConfiguration</code> interface that is
319 * used internally to implement the <code>FileConfiguration</code> methods
320 * for hierarchical configurations.
321 */
322 protected class FileConfigurationDelegate extends AbstractFileConfiguration
323 {
324 public void load(Reader in) throws ConfigurationException
325 {
326 AbstractHierarchicalFileConfiguration.this.load(in);
327 }
328
329 public void save(Writer out) throws ConfigurationException
330 {
331 AbstractHierarchicalFileConfiguration.this.save(out);
332 }
333
334 public void clear()
335 {
336 AbstractHierarchicalFileConfiguration.this.clear();
337 }
338 }
339 }