You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TypedProperties.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 2006-2010 Shane Mc Cormack
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.scriptplugin;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.Properties;
  26. import java.io.InputStream;
  27. import java.io.Reader;
  28. import java.io.IOException;
  29. /**
  30. * Properties file that allows for getting/setting of typed properties
  31. *
  32. * @author Shane 'Dataforce' McCormack
  33. */
  34. public class TypedProperties extends Properties {
  35. /**
  36. * A version number for this class.
  37. * It should be changed whenever the class structure is changed (or anything
  38. * else that would prevent serialized objects being unserialized with the new
  39. * class).
  40. */
  41. private static final long serialVersionUID = 200711071;
  42. /** Is this properties file Case Sensitive */
  43. private boolean caseSensitive = true;
  44. /**
  45. * Creates an empty property list with no default values.
  46. */
  47. public TypedProperties() {
  48. super();
  49. }
  50. /**
  51. * Creates an empty property list with the specified defaults.
  52. *
  53. * @param defaults The Defaults
  54. */
  55. public TypedProperties(final Properties defaults) {
  56. super(defaults);
  57. }
  58. /**
  59. * Set case sensitivity of this properties file.
  60. *
  61. * @param value True/False for the case sensitivity of this file
  62. */
  63. public void setCaseSensitivity(final boolean value) {
  64. // Set all existing values to lowercase.
  65. if (!value) {
  66. for (Object property : this.keySet()) {
  67. if (property instanceof String) {
  68. final String propertyName = (String)property;
  69. if (!propertyName.equals(propertyName.toLowerCase())) {
  70. super.setProperty(propertyName.toLowerCase(), getProperty(propertyName));
  71. super.remove(propertyName);
  72. }
  73. }
  74. }
  75. }
  76. caseSensitive = value;
  77. }
  78. /**
  79. * Load properties from an InputStream.
  80. * After loading, setCaseSensitivity(caseSensitive) is called.
  81. * If this properties file is ment to be case Insensitive, all non-lowercase
  82. * property names will be lowercased.
  83. *
  84. * @param inStream InputStream to load from.
  85. * @throws IOException If there is a problem reading from the Input Stream
  86. */
  87. @Override
  88. public void load(final InputStream inStream) throws IOException {
  89. super.load(inStream);
  90. setCaseSensitivity(caseSensitive);
  91. }
  92. /**
  93. * Load properties from a Reader.
  94. * After loading, setCaseSensitivity(caseSensitive) is called.
  95. * If this properties file is ment to be case Insensitive, all non-lowercase
  96. * property names will be lowercased.
  97. *
  98. * @param reader Reader to load from.
  99. * @throws IOException If there is an error reading from the reader
  100. */
  101. @Override
  102. public void load(final Reader reader) throws IOException {
  103. super.load(reader);
  104. setCaseSensitivity(caseSensitive);
  105. }
  106. /**
  107. * Load properties from an XML InputStream.
  108. * After loading, setCaseSensitivity(caseSensitive) is called.
  109. * If this properties file is ment to be case Insensitive, all non-lowercase
  110. * property names will be lowercased.
  111. *
  112. * @param in InputStream to load from.
  113. * @throws java.io.IOException
  114. */
  115. @Override
  116. public void loadFromXML(final InputStream in) throws IOException {
  117. super.loadFromXML(in);
  118. setCaseSensitivity(caseSensitive);
  119. }
  120. /**
  121. * Get a property from the config
  122. *
  123. * @param key key for property
  124. * @return the requested property, or null if not defined
  125. */
  126. @Override
  127. public String getProperty(final String key) {
  128. if (!caseSensitive) {
  129. return super.getProperty(key.toLowerCase());
  130. } else {
  131. return super.getProperty(key);
  132. }
  133. }
  134. /**
  135. * Get a property from the config
  136. *
  137. * @param key key for property
  138. * @param fallback Value to return if key is not found
  139. * @return the requested property, or the fallback value if not defined
  140. */
  141. @Override
  142. public String getProperty(final String key, final String fallback) {
  143. if (!caseSensitive) {
  144. return super.getProperty(key.toLowerCase(), fallback);
  145. } else {
  146. return super.getProperty(key, fallback);
  147. }
  148. }
  149. /**
  150. * Set a property in the config
  151. *
  152. * @param key key for property
  153. * @param value Value for property
  154. * @return Old value of property
  155. */
  156. @Override
  157. public Object setProperty(final String key, final String value) {
  158. if (!caseSensitive) {
  159. return super.setProperty(key.toLowerCase(), value);
  160. } else {
  161. return super.setProperty(key, value);
  162. }
  163. }
  164. /**
  165. * Check if a property exists
  166. *
  167. * @param key key for property
  168. * @return True if the property exists, else false
  169. */
  170. public boolean hasProperty(final String key) {
  171. return getProperty(key) != null;
  172. }
  173. /**
  174. * Get a Byte property from the config
  175. *
  176. * @param key key for property
  177. * @param fallback Value to return if key is not found
  178. * @return the requested property, or the fallback value if not defined
  179. */
  180. public byte getByteProperty(final String key, final byte fallback) {
  181. try {
  182. return Byte.parseByte(getProperty(key, Byte.toString(fallback)));
  183. } catch (NumberFormatException nfe) {
  184. return fallback;
  185. }
  186. }
  187. /**
  188. * Set a Byte property in the config
  189. *
  190. * @param key key for property
  191. * @param value Value for property
  192. */
  193. public void setByteProperty(final String key, final byte value) {
  194. setProperty(key, Byte.toString(value));
  195. }
  196. /**
  197. * Get a Short property from the config
  198. *
  199. * @param key key for property
  200. * @param fallback Value to return if key is not found
  201. * @return the requested property, or the fallback value if not defined
  202. */
  203. public short getShortProperty(final String key, final short fallback) {
  204. try {
  205. return Short.parseShort(getProperty(key, Short.toString(fallback)));
  206. } catch (NumberFormatException nfe) {
  207. return fallback;
  208. }
  209. }
  210. /**
  211. * Set a Short property in the config
  212. *
  213. * @param key key for property
  214. * @param value Value for property
  215. */
  216. public void setShortProperty(final String key, final short value) {
  217. setProperty(key, Short.toString(value));
  218. }
  219. /**
  220. * Get an integer property from the config
  221. *
  222. * @param key key for property
  223. * @param fallback Value to return if key is not found
  224. * @return the requested property, or the fallback value if not defined
  225. */
  226. public int getIntProperty(final String key, final int fallback) {
  227. try {
  228. return Integer.parseInt(getProperty(key, Integer.toString(fallback)));
  229. } catch (NumberFormatException nfe) {
  230. return fallback;
  231. }
  232. }
  233. /**
  234. * Set an integer property in the config
  235. *
  236. * @param key key for property
  237. * @param value Value for property
  238. */
  239. public void setIntProperty(final String key, final int value) {
  240. setProperty(key, Integer.toString(value));
  241. }
  242. /**
  243. * Get a Long property from the config
  244. *
  245. * @param key key for property
  246. * @param fallback Value to return if key is not found
  247. * @return the requested property, or the fallback value if not defined
  248. */
  249. public long getLongProperty(final String key, final long fallback) {
  250. try {
  251. return Long.parseLong(getProperty(key, Long.toString(fallback)));
  252. } catch (NumberFormatException nfe) {
  253. return fallback;
  254. }
  255. }
  256. /**
  257. * Set a Long property in the config
  258. *
  259. * @param key key for property
  260. * @param value Value for property
  261. */
  262. public void setLongProperty(final String key, final long value) {
  263. setProperty(key, Long.toString(value));
  264. }
  265. /**
  266. * Get a float property from the config
  267. *
  268. * @param key key for property
  269. * @param fallback Value to return if key is not found
  270. * @return the requested property, or the fallback value if not defined
  271. */
  272. public float getFloatProperty(final String key, final float fallback) {
  273. try {
  274. return Float.parseFloat(getProperty(key, Float.toString(fallback)));
  275. } catch (NumberFormatException nfe) {
  276. return fallback;
  277. }
  278. }
  279. /**
  280. * Set a float property in the config
  281. *
  282. * @param key key for property
  283. * @param value Value for property
  284. */
  285. public void setFloatProperty(final String key, final float value) {
  286. setProperty(key, Float.toString(value));
  287. }
  288. /**
  289. * Get a double property from the config
  290. *
  291. * @param key key for property
  292. * @param fallback Value to return if key is not found
  293. * @return the requested property, or the fallback value if not defined
  294. */
  295. public double getDoubleProperty(final String key, final double fallback) {
  296. try {
  297. return Double.parseDouble(getProperty(key, Double.toString(fallback)));
  298. } catch (NumberFormatException nfe) {
  299. return fallback;
  300. }
  301. }
  302. /**
  303. * Set a double property in the config
  304. *
  305. * @param key key for property
  306. * @param value Value for property
  307. */
  308. public void setDoubleProperty(final String key, final double value) {
  309. setProperty(key, Double.toString(value));
  310. }
  311. /**
  312. * Get a boolean property from the config
  313. *
  314. * @param key key for property
  315. * @param fallback Value to return if key is not found
  316. * @return the requested property, or the fallback value if not defined
  317. */
  318. public boolean getBoolProperty(final String key, final boolean fallback) {
  319. return Boolean.parseBoolean(getProperty(key, Boolean.toString(fallback)));
  320. }
  321. /**
  322. * Set a Boolean property in the config
  323. *
  324. * @param key key for property
  325. * @param value Value for property
  326. */
  327. public void setBoolProperty(final String key, final boolean value) {
  328. setProperty(key, Boolean.toString(value));
  329. }
  330. /**
  331. * Get a Char property from the config
  332. *
  333. * @param key key for property
  334. * @param fallback Value to return if key is not found
  335. * @return the requested property, or the fallback value if not defined
  336. */
  337. public char getCharProperty(final String key, final char fallback) {
  338. final String res = getProperty(key, Character.toString(fallback));
  339. if (res == null || res.isEmpty()) {
  340. return fallback;
  341. } else {
  342. return res.charAt(0);
  343. }
  344. }
  345. /**
  346. * Set a Char property in the config
  347. *
  348. * @param key key for property
  349. * @param value Value for property
  350. */
  351. public void setCharProperty(final String key, final char value) {
  352. setProperty(key, Character.toString(value));
  353. }
  354. /**
  355. * Get a List property from the config.
  356. *
  357. * @param key key for property
  358. * @param fallback List to return if key is not found
  359. * @return the requested property, or the fallback value if not defined
  360. */
  361. public List<String> getListProperty(final String key, final List<String> fallback) {
  362. final String res = getProperty(key, "");
  363. if (res == null || res.isEmpty()) {
  364. return fallback;
  365. } else {
  366. final String bits[] = res.split("\n");
  367. final ArrayList<String> result = new ArrayList<String>();
  368. for (String bit : bits) {
  369. result.add(bit);
  370. }
  371. return result;
  372. }
  373. }
  374. /**
  375. * Set a List property in the config
  376. *
  377. * @param key key for property
  378. * @param value Value for property
  379. */
  380. public void setListProperty(final String key, final List<String> value) {
  381. final StringBuilder val = new StringBuilder();
  382. final String LF = "\n";
  383. boolean first = true;
  384. for (String bit : value) {
  385. if (first) { first = false; } else { val.append(LF); }
  386. val.append(bit);
  387. }
  388. setProperty(key, val.toString());
  389. }
  390. }