PageRenderTime 2520ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PropertiesFile.java

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