/war/src/main/java/com/tommytony/war/mappers/PropertiesFile.java

https://github.com/ToxicShade/war · Java · 407 lines · 159 code · 45 blank · 203 comment · 18 complexity · 2fde2a93899ea3312b07bf1444dbc49c MD5 · raw file

  1. package com.tommytony.war.mappers;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.logging.Logger;
  6. import java.io.FileInputStream;
  7. import java.util.Map;
  8. import java.util.Properties;
  9. /**
  10. * Used for accessing and creating .[properties] files, reads them as utf-8, saves as utf-8. Internationalization is key importance especially for character codes.
  11. *
  12. * @author Nijikokun
  13. * @version 1.0.4, %G%
  14. */
  15. public final class PropertiesFile {
  16. private static final Logger log = Logger.getLogger("Minecraft");
  17. private String fileName;
  18. private Properties props = new Properties();
  19. private FileInputStream inputStream;
  20. private FileOutputStream outputStream;
  21. // private List<String> lines = new ArrayList<String>();
  22. // private Map<String, String> props = new HashMap<String, String>();
  23. /**
  24. * Creates or opens a properties file using specified filename
  25. *
  26. * @param fileName
  27. */
  28. public PropertiesFile(String fileName) {
  29. this.fileName = fileName;
  30. File file = new File(fileName);
  31. try {
  32. if (file.exists()) {
  33. this.load();
  34. } else {
  35. this.save();
  36. }
  37. } catch (IOException ex) {
  38. PropertiesFile.log.severe("[PropertiesFile] Unable to load " + fileName + "!");
  39. }
  40. }
  41. /**
  42. * The loader for property files, it reads the file as UTF8 or converts the string into UTF8. Used for simple runthrough's, loading, or reloading of the file.
  43. *
  44. * @throws IOException
  45. */
  46. public void load() throws IOException {
  47. this.inputStream = new FileInputStream(this.fileName);
  48. this.props.load(this.inputStream);
  49. }
  50. /**
  51. * Writes out the <code>key=value</code> properties that were changed into a .[properties] file in UTF8.
  52. */
  53. public void save() {
  54. try {
  55. this.outputStream = new FileOutputStream(this.fileName);
  56. this.props.store(this.outputStream, null);
  57. } catch (IOException ex) {
  58. PropertiesFile.log.severe("[PropertiesFile] Unable to save " + this.fileName + "!");
  59. }
  60. }
  61. public void close() {
  62. if (this.outputStream != null) {
  63. try {
  64. this.outputStream.close();
  65. } catch (IOException e) {
  66. PropertiesFile.log.severe("[PropertiesFile] Failed to close " + this.fileName + " writer!");
  67. }
  68. } else if (this.inputStream != null) {
  69. try {
  70. this.inputStream.close();
  71. } catch (IOException e) {
  72. PropertiesFile.log.severe("[PropertiesFile] Failed to close " + this.fileName + " reader!");
  73. }
  74. }
  75. }
  76. /**
  77. * Returns a Map of all <code>key=value</code> properties in the file as <code>&lt;key (java.lang.String), value (java.lang.String)></code> <br />
  78. * <br />
  79. * Example: <blockquote>
  80. *
  81. * <pre>
  82. * PropertiesFile settings = new PropertiesFile(&quot;settings.properties&quot;);
  83. * Map&lt;String, String&gt; mappedSettings;
  84. *
  85. * try {
  86. * mappedSettings = settings.returnMap();
  87. * } catch (Exception ex) {
  88. * log.info(&quot;Failed mapping settings.properties&quot;);
  89. * }
  90. * </pre>
  91. *
  92. * </blockquote>
  93. *
  94. * @return <code>map</code> - Simple Map HashMap of the entire <code>key=value</code> as <code>&lt;key (java.lang.String), value (java.lang.String)></code>
  95. * @throws Exception
  96. * If the properties file doesn't exist.
  97. */
  98. @SuppressWarnings("unchecked")
  99. public Map<String, String> returnMap() throws Exception {
  100. return (Map<String, String>) this.props.clone();
  101. }
  102. /**
  103. * Checks to see if the .[properties] file contains the given <code>key</code>.
  104. *
  105. * @param var
  106. * The key we are going to be checking the existance of.
  107. * @return <code>Boolean</code> - True if the <code>key</code> exists, false if it cannot be found.
  108. */
  109. public boolean containsKey(String var) {
  110. return this.props.containsKey(var);
  111. }
  112. /**
  113. * Checks to see if this <code>key</code> exists in the .[properties] file.
  114. *
  115. * @param var
  116. * The key we are grabbing the value of.
  117. * @return <code>java.lang.String</code> - True if the <code>key</code> exists, false if it cannot be found.
  118. */
  119. public String getProperty(String var) {
  120. return this.props.getProperty(var);
  121. }
  122. /**
  123. * Remove a key from the file if it exists. This will save() which will invoke a load() on the file.
  124. *
  125. * @see #save()
  126. * @param var
  127. * The <code>key</code> that will be removed from the file
  128. */
  129. public void removeKey(String var) {
  130. if (this.props.containsKey(var)) {
  131. this.props.remove(var);
  132. this.save();
  133. }
  134. }
  135. /**
  136. * Checks the existance of a <code>key</code>.
  137. *
  138. * @see #containsKey(java.lang.String)
  139. * @param key
  140. * The <code>key</code> in question of existance.
  141. * @return <code>Boolean</code> - True for existance, false for <code>key</code> found.
  142. */
  143. public boolean keyExists(String key) {
  144. return this.containsKey(key);
  145. }
  146. /**
  147. * Returns the value of the <code>key</code> given as a <code>String</code>, however we do not set a string if no <code>key</code> is found.
  148. *
  149. * @see #getProperty(java.lang.String)
  150. * @param key
  151. * The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to "" or empty.
  152. */
  153. public String getString(String key) {
  154. if (this.containsKey(key)) {
  155. return this.getProperty(key);
  156. }
  157. return "";
  158. }
  159. /**
  160. * Returns the value of the <code>key</code> given as a <code>String</code>. If it is not found, it will invoke saving the default <code>value</code> to the properties file.
  161. *
  162. * @see #setString(java.lang.String, java.lang.String)
  163. * @see #getProperty(java.lang.String)
  164. * @param key
  165. * The key that we will be grabbing the value from, if no value is found set and return <code>value</code>
  166. * @param value
  167. * The default value that we will be setting if no prior <code>key</code> is found.
  168. * @return java.lang.String Either we will return the default value or a prior existing value depending on existance.
  169. */
  170. public String getString(String key, String value) {
  171. if (this.containsKey(key)) {
  172. return this.getProperty(key);
  173. }
  174. this.setString(key, value);
  175. return value;
  176. }
  177. /**
  178. * Save the value given as a <code>String</code> on the specified key.
  179. *
  180. * @see #save()
  181. * @param key
  182. * The <code>key</code> that we will be addressing the <code>value</code> to.
  183. * @param value
  184. * The <code>value</code> we will be setting inside the <code>.[properties]</code> file.
  185. */
  186. public void setString(String key, String value) {
  187. this.props.put(key, value);
  188. this.save();
  189. }
  190. /**
  191. * Returns the value of the <code>key</code> given in a Integer, however we do not set a string if no <code>key</code> is found.
  192. *
  193. * @see #getProperty(String var)
  194. * @param key
  195. * The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to 0
  196. */
  197. public int getInt(String key) {
  198. if (this.containsKey(key)) {
  199. return Integer.parseInt(this.getProperty(key));
  200. }
  201. return 0;
  202. }
  203. /**
  204. * Returns the int value of a key
  205. *
  206. * @see #setInt(String key, int value)
  207. * @param key
  208. * The key that we will be grabbing the value from, if no value is found set and return <code>value</code>
  209. * @param value
  210. * The default value that we will be setting if no prior <code>key</code> is found.
  211. * @return <code>Integer</code> - Either we will return the default value or a prior existing value depending on existance.
  212. */
  213. public int getInt(String key, int value) {
  214. if (this.containsKey(key)) {
  215. return Integer.parseInt(this.getProperty(key));
  216. }
  217. this.setInt(key, value);
  218. return value;
  219. }
  220. /**
  221. * Save the value given as a <code>int</code> on the specified key.
  222. *
  223. * @see #save()
  224. * @param key
  225. * The <code>key</code> that we will be addressing the <code>value</code> to.
  226. * @param value
  227. * The <code>value</code> we will be setting inside the <code>.[properties]</code> file.
  228. */
  229. public void setInt(String key, int value) {
  230. this.props.put(key, String.valueOf(value));
  231. this.save();
  232. }
  233. /**
  234. * Returns the value of the <code>key</code> given in a Double, however we do not set a string if no <code>key</code> is found.
  235. *
  236. * @see #getProperty(String var)
  237. * @param key
  238. * The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to 0.0
  239. */
  240. public double getDouble(String key) {
  241. if (this.containsKey(key)) {
  242. return Double.parseDouble(this.getProperty(key));
  243. }
  244. return 0;
  245. }
  246. /**
  247. * Returns the double value of a key
  248. *
  249. * @see #setDouble(String key, double value)
  250. * @param key
  251. * The key that we will be grabbing the value from, if no value is found set and return <code>value</code>
  252. * @param value
  253. * The default value that we will be setting if no prior <code>key</code> is found.
  254. * @return <code>Double</code> - Either we will return the default value or a prior existing value depending on existance.
  255. */
  256. public double getDouble(String key, double value) {
  257. if (this.containsKey(key)) {
  258. return Double.parseDouble(this.getProperty(key));
  259. }
  260. this.setDouble(key, value);
  261. return value;
  262. }
  263. /**
  264. * Save the value given as a <code>double</code> on the specified key.
  265. *
  266. * @see #save()
  267. * @param key
  268. * The <code>key</code> that we will be addressing the <code>value</code> to.
  269. * @param value
  270. * The <code>value</code> we will be setting inside the <code>.[properties]</code> file.
  271. */
  272. public void setDouble(String key, double value) {
  273. this.props.put(key, String.valueOf(value));
  274. this.save();
  275. }
  276. /**
  277. * Returns the value of the <code>key</code> given in a Long, however we do not set a string if no <code>key</code> is found.
  278. *
  279. * @see #getProperty(String var)
  280. * @param key
  281. * The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to 0L
  282. */
  283. public long getLong(String key) {
  284. if (this.containsKey(key)) {
  285. return Long.parseLong(this.getProperty(key));
  286. }
  287. return 0;
  288. }
  289. /**
  290. * Returns the long value of a key
  291. *
  292. * @see #setLong(String key, long value)
  293. * @param key
  294. * The key that we will be grabbing the value from, if no value is found set and return <code>value</code>
  295. * @param value
  296. * The default value that we will be setting if no prior <code>key</code> is found.
  297. * @return <code>Long</code> - Either we will return the default value or a prior existing value depending on existance.
  298. */
  299. public long getLong(String key, long value) {
  300. if (this.containsKey(key)) {
  301. return Long.parseLong(this.getProperty(key));
  302. }
  303. this.setLong(key, value);
  304. return value;
  305. }
  306. /**
  307. * Save the value given as a <code>long</code> on the specified key.
  308. *
  309. * @see #save()
  310. * @param key
  311. * The <code>key</code> that we will be addressing the <code>value</code> to.
  312. * @param value
  313. * The <code>value</code> we will be setting inside the <code>.[properties]</code> file.
  314. */
  315. public void setLong(String key, long value) {
  316. this.props.put(key, String.valueOf(value));
  317. this.save();
  318. }
  319. /**
  320. * Returns the value of the <code>key</code> given in a Boolean, however we do not set a string if no <code>key</code> is found.
  321. *
  322. * @see #getProperty(String var)
  323. * @param key
  324. * The <code>key</code> we will retrieve the property from, if no <code>key</code> is found default to false
  325. */
  326. public boolean getBoolean(String key) {
  327. if (this.containsKey(key)) {
  328. return Boolean.parseBoolean(this.getProperty(key));
  329. }
  330. return false;
  331. }
  332. /**
  333. * Returns the boolean value of a key
  334. *
  335. * @see #setBoolean(String key, boolean value)
  336. * @param key
  337. * The key that we will be grabbing the value from, if no value is found set and return <code>value</code>
  338. * @param value
  339. * The default value that we will be setting if no prior <code>key</code> is found.
  340. * @return <code>Boolean</code> - Either we will return the default value or a prior existing value depending on existance.
  341. */
  342. public boolean getBoolean(String key, boolean value) {
  343. if (this.containsKey(key)) {
  344. return Boolean.parseBoolean(this.getProperty(key));
  345. }
  346. this.setBoolean(key, value);
  347. return value;
  348. }
  349. /**
  350. * Save the value given as a <code>boolean</code> on the specified key.
  351. *
  352. * @see #save()
  353. * @param key
  354. * The <code>key</code> that we will be addressing the <code>value</code> to.
  355. * @param value
  356. * The <code>value</code> we will be setting inside the <code>.[properties]</code> file.
  357. */
  358. public void setBoolean(String key, boolean value) {
  359. this.props.put(key, String.valueOf(value));
  360. this.save();
  361. }
  362. }