KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > util > HelpProperties


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.help.internal.base.util;
12
13 import java.io.*;
14 import java.util.Properties JavaDoc;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.help.internal.*;
18
19 /**
20  * Properties stored in file.
21  */

22 public class HelpProperties extends Properties JavaDoc {
23
24     private static final long serialVersionUID = 1L;
25     
26     private File file = null;
27
28     protected String JavaDoc name = null;
29
30     /**
31      * Creates empty Properties for the specified plugin
32      *
33      * @param name
34      * name of the file;
35      * @param plugin
36      * the plugin
37      */

38     public HelpProperties(String JavaDoc name, Plugin plugin) {
39         this(name, plugin.getStateLocation().toFile());
40     }
41
42     /**
43      * Creates empty Properties persisted in the specified directory
44      *
45      * @param name
46      * name of the file;
47      * @param dir
48      * directory to persist file in
49      */

50     public HelpProperties(String JavaDoc name, File dir) {
51         super();
52         this.name = name;
53         file = new File(dir, name);
54     }
55
56     /**
57      * Restores contents of the Properties from a file.
58      *
59      * @return true if persistant data was read in
60      */

61     public boolean restore() {
62         InputStream in = null;
63         boolean loaded = false;
64         clear();
65         // Test if we have a contribution file to start with
66
// If this is a clean start, then we will not have a
67
// contribution file. return false.
68
if (!file.exists())
69             return loaded;
70         try {
71             in = new FileInputStream(file);
72             super.load(in);
73             loaded = true;
74         } catch (IOException ioe00) {
75             HelpPlugin.logError("File " + file.getName() + " cannot be read."); //$NON-NLS-1$ //$NON-NLS-2$
76
} finally {
77             if (in != null)
78                 try {
79                     in.close();
80                 } catch (IOException ioe10) {
81                 }
82         }
83         return loaded;
84     }
85
86     /**
87      * Saves contents of the table to a file.
88      *
89      * @return true if operation was successful
90      */

91     public boolean save() {
92         OutputStream out = null;
93         boolean ret = false;
94         try {
95             out = new FileOutputStream(file);
96             super.store(out, "This is a generated file; do not edit."); //$NON-NLS-1$
97
ret = true;
98         } catch (IOException ioe00) {
99             HelpPlugin.logError("Exception occurred while saving table " + name //$NON-NLS-1$
100
+ " to file " + file.getAbsolutePath() + ".", ioe00); //$NON-NLS-1$ //$NON-NLS-2$
101
} finally {
102             try {
103                 if (out != null) {
104                     out.close();
105                 }
106             } catch (IOException ioe01) {
107             }
108         }
109         return ret;
110     }
111 }
112
Popular Tags