PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/PropertyManager.java

#
Java | 193 lines | 126 code | 22 blank | 45 comment | 15 complexity | bf1125c48113cb9c2c0c912906b1134b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. Iterator iter = plugins.iterator();
  33. while(iter.hasNext())
  34. total.putAll((Properties)iter.next());
  35. total.putAll(site);
  36. total.putAll(user);
  37. return total;
  38. } //}}}
  39. //{{{ loadSystemProps() method
  40. void loadSystemProps(InputStream in)
  41. throws IOException
  42. {
  43. loadProps(system,in);
  44. } //}}}
  45. //{{{ loadSiteProps() method
  46. void loadSiteProps(InputStream in)
  47. throws IOException
  48. {
  49. loadProps(site,in);
  50. } //}}}
  51. //{{{ loadUserProps() method
  52. void loadUserProps(InputStream in)
  53. throws IOException
  54. {
  55. loadProps(user,in);
  56. } //}}}
  57. //{{{ saveUserProps() method
  58. void saveUserProps(OutputStream out)
  59. throws IOException
  60. {
  61. user.store(out,"jEdit properties");
  62. out.close();
  63. } //}}}
  64. //{{{ loadPluginProps() method
  65. Properties loadPluginProps(InputStream in)
  66. throws IOException
  67. {
  68. Properties plugin = new Properties();
  69. loadProps(plugin,in);
  70. plugins.add(plugin);
  71. return plugin;
  72. } //}}}
  73. //{{{ addPluginProps() method
  74. void addPluginProps(Properties props)
  75. {
  76. plugins.add(props);
  77. } //}}}
  78. //{{{ removePluginProps() method
  79. void removePluginProps(Properties props)
  80. {
  81. plugins.remove(props);
  82. } //}}}
  83. //{{{ getProperty() method
  84. String getProperty(String name)
  85. {
  86. String value = user.getProperty(name);
  87. if(value != null)
  88. return value;
  89. else
  90. return getDefaultProperty(name);
  91. } //}}}
  92. //{{{ setProperty() method
  93. void setProperty(String name, String value)
  94. {
  95. String prop = getDefaultProperty(name);
  96. /* if value is null:
  97. * - if default is null, unset user prop
  98. * - else set user prop to ""
  99. * else
  100. * - if default equals value, ignore
  101. * - if default doesn't equal value, set user
  102. */
  103. if(value == null)
  104. {
  105. if(prop == null || prop.length() == 0)
  106. user.remove(name);
  107. else
  108. user.put(name,"");
  109. }
  110. else
  111. {
  112. if(value.equals(prop))
  113. user.remove(name);
  114. else
  115. user.put(name,value);
  116. }
  117. } //}}}
  118. //{{{ setTemporaryProperty() method
  119. public void setTemporaryProperty(String name, String value)
  120. {
  121. user.remove(name);
  122. system.put(name,value);
  123. } //}}}
  124. //{{{ unsetProperty() method
  125. void unsetProperty(String name)
  126. {
  127. if(getDefaultProperty(name) != null)
  128. user.put(name,"");
  129. else
  130. user.remove(name);
  131. } //}}}
  132. //{{{ resetProperty() method
  133. public void resetProperty(String name)
  134. {
  135. user.remove(name);
  136. } //}}}
  137. //{{{ Private members
  138. private Properties system = new Properties();
  139. private List plugins = new LinkedList();
  140. private Properties site = new Properties();
  141. private Properties user = new Properties();
  142. //{{{ getDefaultProperty() method
  143. private String getDefaultProperty(String name)
  144. {
  145. String value = site.getProperty(name);
  146. if(value != null)
  147. return value;
  148. Iterator iter = plugins.iterator();
  149. while(iter.hasNext())
  150. {
  151. value = ((Properties)iter.next()).getProperty(name);
  152. if(value != null)
  153. return value;
  154. }
  155. return system.getProperty(name);
  156. } //}}}
  157. //{{{ loadProps() method
  158. private void loadProps(Properties into, InputStream in)
  159. throws IOException
  160. {
  161. try
  162. {
  163. into.load(in);
  164. }
  165. finally
  166. {
  167. in.close();
  168. }
  169. } //}}}
  170. //}}}
  171. }