PageRenderTime 171ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/nijikokun/bukkit/Permissions/PropertyHandler.java

https://github.com/Travo97/Permissions
Java | 212 lines | 152 code | 35 blank | 25 comment | 18 complexity | 2e442d96160e4a9bf4f9cb0b88a04acf MD5 | raw file
  1. package com.nijikokun.bukkit.Permissions;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.Properties;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. /**
  14. * Permissions 2.x
  15. * Copyright (C) 2011 Matt 'The Yeti' Burnett <admin@theyeticave.net>
  16. * Original Credit & Copyright (C) 2010 Nijikokun <nijikokun@gmail.com>
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Permissions Public License as published by
  20. * the Free Software Foundation, either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Permissions Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Permissions Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. */
  31. /**
  32. * PropertyHandler
  33. *
  34. * Reads & Writes properties files.
  35. *
  36. * @author Nijiko
  37. */
  38. public final class PropertyHandler {
  39. private static final Logger log = Logger.getLogger("Minecraft");
  40. private Properties properties;
  41. private String fileName;
  42. public PropertyHandler(String fileName) {
  43. this.fileName = fileName;
  44. this.properties = new Properties();
  45. File file = new File(fileName);
  46. if (file.exists()) {
  47. load();
  48. } else {
  49. save();
  50. }
  51. }
  52. public void load() {
  53. try {
  54. this.properties.load(new FileInputStream(this.fileName));
  55. } catch (IOException ex) {
  56. log.log(Level.SEVERE, "Unable to load " + this.fileName, ex);
  57. }
  58. }
  59. public void save() {
  60. try {
  61. this.properties.store(new FileOutputStream(this.fileName), "Minecraft Properties File");
  62. } catch (IOException ex) {
  63. log.log(Level.SEVERE, "Unable to save " + this.fileName, ex);
  64. }
  65. }
  66. public Map<String, String> returnMap() throws Exception {
  67. Map<String, String> map = new HashMap<String, String>();
  68. BufferedReader reader = new BufferedReader(new FileReader(this.fileName));
  69. String line;
  70. while ((line = reader.readLine()) != null) {
  71. if (line.trim().length() == 0) {
  72. continue;
  73. }
  74. if (line.charAt(0) == '#') {
  75. continue;
  76. }
  77. int delimPosition = line.indexOf('=');
  78. String key = line.substring(0, delimPosition).trim();
  79. String value = line.substring(delimPosition + 1).trim();
  80. map.put(key, value);
  81. }
  82. reader.close();
  83. return map;
  84. }
  85. public void removeKey(String key) {
  86. this.properties.remove(key);
  87. save();
  88. }
  89. public boolean keyExists(String key) {
  90. return this.properties.containsKey(key);
  91. }
  92. public String getString(String key) {
  93. if (this.properties.containsKey(key)) {
  94. return this.properties.getProperty(key);
  95. }
  96. return "";
  97. }
  98. public String getString(String key, String value) {
  99. if (this.properties.containsKey(key)) {
  100. return this.properties.getProperty(key);
  101. }
  102. setString(key, value);
  103. return value;
  104. }
  105. public void setString(String key, String value) {
  106. this.properties.setProperty(key, value);
  107. save();
  108. }
  109. public int getInt(String key) {
  110. if (this.properties.containsKey(key)) {
  111. return Integer.parseInt(this.properties.getProperty(key));
  112. }
  113. return 0;
  114. }
  115. public int getInt(String key, int value) {
  116. if (this.properties.containsKey(key)) {
  117. return Integer.parseInt(this.properties.getProperty(key));
  118. }
  119. setInt(key, value);
  120. return value;
  121. }
  122. public void setInt(String key, int value) {
  123. this.properties.setProperty(key, String.valueOf(value));
  124. save();
  125. }
  126. public double getDouble(String key) {
  127. if (this.properties.containsKey(key)) {
  128. return Double.parseDouble(this.properties.getProperty(key));
  129. }
  130. return 0;
  131. }
  132. public double getDouble(String key, double value) {
  133. if (this.properties.containsKey(key)) {
  134. return Double.parseDouble(this.properties.getProperty(key));
  135. }
  136. setDouble(key, value);
  137. return value;
  138. }
  139. public void setDouble(String key, double value) {
  140. this.properties.setProperty(key, String.valueOf(value));
  141. save();
  142. }
  143. public long getLong(String key) {
  144. if (this.properties.containsKey(key)) {
  145. return Long.parseLong(this.properties.getProperty(key));
  146. }
  147. return 0;
  148. }
  149. public long getLong(String key, long value) {
  150. if (this.properties.containsKey(key)) {
  151. return Long.parseLong(this.properties.getProperty(key));
  152. }
  153. setLong(key, value);
  154. return value;
  155. }
  156. public void setLong(String key, long value) {
  157. this.properties.setProperty(key, String.valueOf(value));
  158. save();
  159. }
  160. public boolean getBoolean(String key) {
  161. if (this.properties.containsKey(key)) {
  162. return Boolean.parseBoolean(this.properties.getProperty(key));
  163. }
  164. return false;
  165. }
  166. public boolean getBoolean(String key, boolean value) {
  167. if (this.properties.containsKey(key)) {
  168. return Boolean.parseBoolean(this.properties.getProperty(key));
  169. }
  170. setBoolean(key, value);
  171. return value;
  172. }
  173. public void setBoolean(String key, boolean value) {
  174. this.properties.setProperty(key, String.valueOf(value));
  175. save();
  176. }
  177. }