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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (c) 2006-2011 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.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.Reader;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.Properties;
  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
  39. * new
  40. * class).
  41. */
  42. private static final long serialVersionUID = 200711071;
  43. /** Is this properties file Case Sensitive */
  44. private boolean caseSensitive = true;
  45. /**
  46. * Creates an empty property list with no default values.
  47. */
  48. public TypedProperties() {
  49. super();
  50. }
  51. /**
  52. * Creates an empty property list with the specified defaults.
  53. *
  54. * @param defaults
  55. * The Defaults
  56. */
  57. public TypedProperties(final Properties defaults) {
  58. super(defaults);
  59. }
  60. /**
  61. * Set case sensitivity of this properties file.
  62. *
  63. * @param value
  64. * True/False for the case sensitivity of this file
  65. */
  66. public void setCaseSensitivity(final boolean value) {
  67. // Set all existing values to lowercase.
  68. if (!value) {
  69. for (final Object property : keySet()) {
  70. if (property instanceof String) {
  71. final String propertyName = (String) property;
  72. if (!propertyName.equals(propertyName.toLowerCase())) {
  73. super.setProperty(propertyName.toLowerCase(),
  74. getProperty(propertyName));
  75. super.remove(propertyName);
  76. }
  77. }
  78. }
  79. }
  80. caseSensitive = value;
  81. }
  82. /**
  83. * Load properties from an InputStream.
  84. * After loading, setCaseSensitivity(caseSensitive) is called.
  85. * If this properties file is ment to be case Insensitive, all non-lowercase
  86. * property names will be lowercased.
  87. *
  88. * @param inStream
  89. * InputStream to load from.
  90. * @throws IOException
  91. * If there is a problem reading from the Input Stream
  92. */
  93. @Override
  94. public synchronized void load(final InputStream inStream) throws IOException {
  95. super.load(inStream);
  96. setCaseSensitivity(caseSensitive);
  97. }
  98. /**
  99. * Load properties from a Reader.
  100. * After loading, setCaseSensitivity(caseSensitive) is called.
  101. * If this properties file is ment to be case Insensitive, all non-lowercase
  102. * property names will be lowercased.
  103. *
  104. * @param reader
  105. * Reader to load from.
  106. * @throws IOException
  107. * If there is an error reading from the reader
  108. */
  109. @Override
  110. public synchronized void load(final Reader reader) throws IOException {
  111. super.load(reader);
  112. setCaseSensitivity(caseSensitive);
  113. }
  114. /**
  115. * Load properties from an XML InputStream.
  116. * After loading, setCaseSensitivity(caseSensitive) is called.
  117. * If this properties file is ment to be case Insensitive, all non-lowercase
  118. * property names will be lowercased.
  119. *
  120. * @param in
  121. * InputStream to load from.
  122. * @throws java.io.IOException
  123. */
  124. @Override
  125. public synchronized void loadFromXML(final InputStream in) throws IOException {
  126. super.loadFromXML(in);
  127. setCaseSensitivity(caseSensitive);
  128. }
  129. /**
  130. * Get a property from the config
  131. *
  132. * @param key
  133. * key for property
  134. * @return the requested property, or null if not defined
  135. */
  136. @Override
  137. public String getProperty(final String key) {
  138. if (!caseSensitive) {
  139. return super.getProperty(key.toLowerCase());
  140. } else {
  141. return super.getProperty(key);
  142. }
  143. }
  144. /**
  145. * Get a property from the config
  146. *
  147. * @param key
  148. * key for property
  149. * @param fallback
  150. * Value to return if key is not found
  151. * @return the requested property, or the fallback value if not defined
  152. */
  153. @Override
  154. public String getProperty(final String key, final String fallback) {
  155. if (!caseSensitive) {
  156. return super.getProperty(key.toLowerCase(), fallback);
  157. } else {
  158. return super.getProperty(key, fallback);
  159. }
  160. }
  161. /**
  162. * Set a property in the config
  163. *
  164. * @param key
  165. * key for property
  166. * @param value
  167. * Value for property
  168. * @return Old value of property
  169. */
  170. @Override
  171. public synchronized Object setProperty(final String key, final String value) {
  172. if (!caseSensitive) {
  173. return super.setProperty(key.toLowerCase(), value);
  174. } else {
  175. return super.setProperty(key, value);
  176. }
  177. }
  178. /**
  179. * Check if a property exists
  180. *
  181. * @param key
  182. * key for property
  183. * @return True if the property exists, else false
  184. */
  185. public boolean hasProperty(final String key) {
  186. return getProperty(key) != null;
  187. }
  188. /**
  189. * Get a Byte property from the config
  190. *
  191. * @param key
  192. * key for property
  193. * @param fallback
  194. * Value to return if key is not found
  195. * @return the requested property, or the fallback value if not defined
  196. */
  197. public byte getByteProperty(final String key, final byte fallback) {
  198. try {
  199. return Byte.parseByte(getProperty(key, Byte.toString(fallback)));
  200. } catch (final NumberFormatException nfe) {
  201. return fallback;
  202. }
  203. }
  204. /**
  205. * Set a Byte property in the config
  206. *
  207. * @param key
  208. * key for property
  209. * @param value
  210. * Value for property
  211. */
  212. public void setByteProperty(final String key, final byte value) {
  213. setProperty(key, Byte.toString(value));
  214. }
  215. /**
  216. * Get a Short property from the config
  217. *
  218. * @param key
  219. * key for property
  220. * @param fallback
  221. * Value to return if key is not found
  222. * @return the requested property, or the fallback value if not defined
  223. */
  224. public short getShortProperty(final String key, final short fallback) {
  225. try {
  226. return Short.parseShort(getProperty(key, Short.toString(fallback)));
  227. } catch (final NumberFormatException nfe) {
  228. return fallback;
  229. }
  230. }
  231. /**
  232. * Set a Short property in the config
  233. *
  234. * @param key
  235. * key for property
  236. * @param value
  237. * Value for property
  238. */
  239. public void setShortProperty(final String key, final short value) {
  240. setProperty(key, Short.toString(value));
  241. }
  242. /**
  243. * Get an integer property from the config
  244. *
  245. * @param key
  246. * key for property
  247. * @param fallback
  248. * Value to return if key is not found
  249. * @return the requested property, or the fallback value if not defined
  250. */
  251. public int getIntProperty(final String key, final int fallback) {
  252. try {
  253. return Integer
  254. .parseInt(getProperty(key, Integer.toString(fallback)));
  255. } catch (final NumberFormatException nfe) {
  256. return fallback;
  257. }
  258. }
  259. /**
  260. * Set an integer property in the config
  261. *
  262. * @param key
  263. * key for property
  264. * @param value
  265. * Value for property
  266. */
  267. public void setIntProperty(final String key, final int value) {
  268. setProperty(key, Integer.toString(value));
  269. }
  270. /**
  271. * Get a Long property from the config
  272. *
  273. * @param key
  274. * key for property
  275. * @param fallback
  276. * Value to return if key is not found
  277. * @return the requested property, or the fallback value if not defined
  278. */
  279. public long getLongProperty(final String key, final long fallback) {
  280. try {
  281. return Long.parseLong(getProperty(key, Long.toString(fallback)));
  282. } catch (final NumberFormatException nfe) {
  283. return fallback;
  284. }
  285. }
  286. /**
  287. * Set a Long property in the config
  288. *
  289. * @param key
  290. * key for property
  291. * @param value
  292. * Value for property
  293. */
  294. public void setLongProperty(final String key, final long value) {
  295. setProperty(key, Long.toString(value));
  296. }
  297. /**
  298. * Get a float property from the config
  299. *
  300. * @param key
  301. * key for property
  302. * @param fallback
  303. * Value to return if key is not found
  304. * @return the requested property, or the fallback value if not defined
  305. */
  306. public float getFloatProperty(final String key, final float fallback) {
  307. try {
  308. return Float.parseFloat(getProperty(key, Float.toString(fallback)));
  309. } catch (final NumberFormatException nfe) {
  310. return fallback;
  311. }
  312. }
  313. /**
  314. * Set a float property in the config
  315. *
  316. * @param key
  317. * key for property
  318. * @param value
  319. * Value for property
  320. */
  321. public void setFloatProperty(final String key, final float value) {
  322. setProperty(key, Float.toString(value));
  323. }
  324. /**
  325. * Get a double property from the config
  326. *
  327. * @param key
  328. * key for property
  329. * @param fallback
  330. * Value to return if key is not found
  331. * @return the requested property, or the fallback value if not defined
  332. */
  333. public double getDoubleProperty(final String key, final double fallback) {
  334. try {
  335. return Double.parseDouble(getProperty(key, Double
  336. .toString(fallback)));
  337. } catch (final NumberFormatException nfe) {
  338. return fallback;
  339. }
  340. }
  341. /**
  342. * Set a double property in the config
  343. *
  344. * @param key
  345. * key for property
  346. * @param value
  347. * Value for property
  348. */
  349. public void setDoubleProperty(final String key, final double value) {
  350. setProperty(key, Double.toString(value));
  351. }
  352. /**
  353. * Get a boolean property from the config
  354. *
  355. * @param key
  356. * key for property
  357. * @param fallback
  358. * Value to return if key is not found
  359. * @return the requested property, or the fallback value if not defined
  360. */
  361. public boolean getBoolProperty(final String key, final boolean fallback) {
  362. return Boolean
  363. .parseBoolean(getProperty(key, Boolean.toString(fallback)));
  364. }
  365. /**
  366. * Set a Boolean property in the config
  367. *
  368. * @param key
  369. * key for property
  370. * @param value
  371. * Value for property
  372. */
  373. public void setBoolProperty(final String key, final boolean value) {
  374. setProperty(key, Boolean.toString(value));
  375. }
  376. /**
  377. * Get a Char property from the config
  378. *
  379. * @param key
  380. * key for property
  381. * @param fallback
  382. * Value to return if key is not found
  383. * @return the requested property, or the fallback value if not defined
  384. */
  385. public char getCharProperty(final String key, final char fallback) {
  386. final String res = getProperty(key, Character.toString(fallback));
  387. if (res == null || res.isEmpty()) {
  388. return fallback;
  389. } else {
  390. return res.charAt(0);
  391. }
  392. }
  393. /**
  394. * Set a Char property in the config
  395. *
  396. * @param key
  397. * key for property
  398. * @param value
  399. * Value for property
  400. */
  401. public void setCharProperty(final String key, final char value) {
  402. setProperty(key, Character.toString(value));
  403. }
  404. /**
  405. * Get a List property from the config.
  406. *
  407. * @param key
  408. * key for property
  409. * @param fallback
  410. * List to return if key is not found
  411. * @return the requested property, or the fallback value if not defined
  412. */
  413. public List<String> getListProperty(final String key,
  414. final List<String> fallback) {
  415. final String res = getProperty(key, "");
  416. if (res == null || res.isEmpty()) {
  417. return fallback;
  418. } else {
  419. final String[] bits = res.split("\n");
  420. final ArrayList<String> result = new ArrayList<String>();
  421. for (final String bit : bits) {
  422. result.add(bit);
  423. }
  424. return result;
  425. }
  426. }
  427. /**
  428. * Set a List property in the config
  429. *
  430. * @param key
  431. * key for property
  432. * @param value
  433. * Value for property
  434. */
  435. public void setListProperty(final String key, final List<String> value) {
  436. final StringBuilder val = new StringBuilder();
  437. final String lf = "\n";
  438. boolean first = true;
  439. for (final String bit : value) {
  440. if (first) {
  441. first = false;
  442. } else {
  443. val.append(lf);
  444. }
  445. val.append(bit);
  446. }
  447. setProperty(key, val.toString());
  448. }
  449. }