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