KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > PropertyManager


1 /*
2  * PropertyManager.java - Manages property files
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2004 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit;
24
25 import java.io.*;
26 import java.util.*;
27
28 class PropertyManager
29 {
30     //{{{ getProperties() method
31
Properties getProperties()
32     {
33         Properties total = new Properties();
34         total.putAll(system);
35         Iterator iter = plugins.iterator();
36         while(iter.hasNext())
37             total.putAll((Properties)iter.next());
38         total.putAll(site);
39         total.putAll(user);
40         return total;
41     } //}}}
42

43     //{{{ loadSystemProps() method
44
void loadSystemProps(InputStream in)
45         throws IOException
46     {
47         loadProps(system,in);
48     } //}}}
49

50     //{{{ loadSiteProps() method
51
void loadSiteProps(InputStream in)
52         throws IOException
53     {
54         loadProps(site,in);
55     } //}}}
56

57     //{{{ loadUserProps() method
58
void loadUserProps(InputStream in)
59         throws IOException
60     {
61         loadProps(user,in);
62     } //}}}
63

64     //{{{ saveUserProps() method
65
void saveUserProps(OutputStream out)
66         throws IOException
67     {
68         user.store(out,"jEdit properties");
69         out.close();
70     } //}}}
71

72     //{{{ loadPluginProps() method
73
Properties loadPluginProps(InputStream in)
74         throws IOException
75     {
76         Properties plugin = new Properties();
77         loadProps(plugin,in);
78         plugins.add(plugin);
79         return plugin;
80     } //}}}
81

82     //{{{ addPluginProps() method
83
void addPluginProps(Properties props)
84     {
85         plugins.add(props);
86     } //}}}
87

88     //{{{ removePluginProps() method
89
void removePluginProps(Properties props)
90     {
91         plugins.remove(props);
92     } //}}}
93

94     //{{{ getProperty() method
95
String JavaDoc getProperty(String JavaDoc name)
96     {
97         String JavaDoc value = user.getProperty(name);
98         if(value != null)
99             return value;
100         else
101             return getDefaultProperty(name);
102     } //}}}
103

104     //{{{ setProperty() method
105
void setProperty(String JavaDoc name, String JavaDoc value)
106     {
107         String JavaDoc prop = getDefaultProperty(name);
108
109         /* if value is null:
110          * - if default is null, unset user prop
111          * - else set user prop to ""
112          * else
113          * - if default equals value, ignore
114          * - if default doesn't equal value, set user
115          */

116         if(value == null)
117         {
118             if(prop == null || prop.length() == 0)
119                 user.remove(name);
120             else
121                 user.put(name,"");
122         }
123         else
124         {
125             if(value.equals(prop))
126                 user.remove(name);
127             else
128                 user.put(name,value);
129         }
130     } //}}}
131

132     //{{{ setTemporaryProperty() method
133
public void setTemporaryProperty(String JavaDoc name, String JavaDoc value)
134     {
135         user.remove(name);
136         system.put(name,value);
137     } //}}}
138

139     //{{{ unsetProperty() method
140
void unsetProperty(String JavaDoc name)
141     {
142         if(getDefaultProperty(name) != null)
143             user.put(name,"");
144         else
145             user.remove(name);
146     } //}}}
147

148     //{{{ resetProperty() method
149
public void resetProperty(String JavaDoc name)
150     {
151         user.remove(name);
152     } //}}}
153

154     //{{{ Private members
155
private Properties system = new Properties();
156     private List plugins = new LinkedList();
157     private Properties site = new Properties();
158     private Properties user = new Properties();
159
160     //{{{ getDefaultProperty() method
161
private String JavaDoc getDefaultProperty(String JavaDoc name)
162     {
163         String JavaDoc value = site.getProperty(name);
164         if(value != null)
165             return value;
166
167         Iterator iter = plugins.iterator();
168         while(iter.hasNext())
169         {
170             value = ((Properties)iter.next()).getProperty(name);
171             if(value != null)
172                 return value;
173         }
174
175         return system.getProperty(name);
176     } //}}}
177

178     //{{{ loadProps() method
179
private void loadProps(Properties into, InputStream in)
180         throws IOException
181     {
182         try
183         {
184             into.load(in);
185         }
186         finally
187         {
188             in.close();
189         }
190     } //}}}
191

192     //}}}
193
}
194
Popular Tags