PageRenderTime 47ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/PropertyManager.java

#
Java | 190 lines | 123 code | 22 blank | 45 comment | 17 complexity | 314730d7ee0434cfda0eebf5a02bbb1a MD5 | raw file
  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
 23package org.gjt.sp.jedit;
 24
 25import java.io.*;
 26import java.util.*;
 27
 28class PropertyManager
 29{
 30	//{{{ getProperties() method
 31	Properties getProperties()
 32	{
 33		Properties total = new Properties();
 34		total.putAll(system);
 35		for (Properties plugin : plugins)
 36			total.putAll(plugin);
 37		total.putAll(site);
 38		total.putAll(user);
 39		return total;
 40	} //}}}
 41
 42	//{{{ loadSystemProps() method
 43	void loadSystemProps(InputStream in)
 44		throws IOException
 45	{
 46		loadProps(system,in);
 47	} //}}}
 48
 49	//{{{ loadSiteProps() method
 50	void loadSiteProps(InputStream in)
 51		throws IOException
 52	{
 53		loadProps(site,in);
 54	} //}}}
 55
 56	//{{{ loadUserProps() method
 57	void loadUserProps(InputStream in)
 58		throws IOException
 59	{
 60		loadProps(user,in);
 61	} //}}}
 62
 63	//{{{ saveUserProps() method
 64	void saveUserProps(OutputStream out)
 65		throws IOException
 66	{
 67		user.store(out,"jEdit properties");
 68	} //}}}
 69
 70	//{{{ loadPluginProps() method
 71	Properties loadPluginProps(InputStream in)
 72		throws IOException
 73	{
 74		Properties plugin = new Properties();
 75		loadProps(plugin,in);
 76		plugins.add(plugin);
 77		return plugin;
 78	} //}}}
 79
 80	//{{{ addPluginProps() method
 81	void addPluginProps(Properties props)
 82	{
 83		plugins.add(props);
 84	} //}}}
 85
 86	//{{{ removePluginProps() method
 87	void removePluginProps(Properties props)
 88	{
 89		plugins.remove(props);
 90	} //}}}
 91
 92	//{{{ getProperty() method
 93	String getProperty(String name)
 94	{
 95		String value = user.getProperty(name);
 96		if(value != null)
 97			return value;
 98		else
 99			return getDefaultProperty(name);
100	} //}}}
101
102	//{{{ setProperty() method
103	void setProperty(String name, String value)
104	{
105		String prop = getDefaultProperty(name);
106
107		/* if value is null:
108		 * - if default is null, unset user prop
109		 * - else set user prop to ""
110		 * else
111		 * - if default equals value, ignore
112		 * - if default doesn't equal value, set user
113		 */
114		if(value == null)
115		{
116			if(prop == null || prop.length() == 0)
117				user.remove(name);
118			else
119				user.setProperty(name,"");
120		}
121		else
122		{
123			if(value.equals(prop))
124				user.remove(name);
125			else
126				user.setProperty(name,value);
127		}
128	} //}}}
129
130	//{{{ setTemporaryProperty() method
131	public void setTemporaryProperty(String name, String value)
132	{
133		user.remove(name);
134		system.setProperty(name,value);
135	} //}}}
136
137	//{{{ unsetProperty() method
138	void unsetProperty(String name)
139	{
140		if(getDefaultProperty(name) != null)
141			user.setProperty(name,"");
142		else
143			user.remove(name);
144	} //}}}
145
146	//{{{ resetProperty() method
147	public void resetProperty(String name)
148	{
149		user.remove(name);
150	} //}}}
151
152	//{{{ Private members
153	private Properties system = new Properties();
154	private List<Properties> plugins = new LinkedList<Properties>();
155	private Properties site = new Properties();
156	private Properties user = new Properties();
157
158	//{{{ getDefaultProperty() method
159	private String getDefaultProperty(String name)
160	{
161		String value = site.getProperty(name);
162		if(value != null)
163			return value;
164
165		for (Properties plugin : plugins)
166		{
167			value = plugin.getProperty(name);
168			if (value != null)
169				return value;
170		}
171
172		return system.getProperty(name);
173	} //}}}
174
175	//{{{ loadProps() method
176	private static void loadProps(Properties into, InputStream in)
177		throws IOException
178	{
179		try
180		{
181			into.load(in);
182		}
183		finally
184		{
185			in.close();
186		}
187	} //}}}
188
189	//}}}
190}