/webportal/src/main/java/au/org/emii/portal/settings/SettingsSupplementaryImpl.java

http://alageospatialportal.googlecode.com/ · Java · 123 lines · 49 code · 16 blank · 58 comment · 5 complexity · 71d6ab487bc281bc85f005726b0eb482 MD5 · raw file

  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package au.org.emii.portal.settings;
  6. import java.util.Map;
  7. /**
  8. *
  9. * @author geoff
  10. */
  11. public class SettingsSupplementaryImpl implements SettingsSupplementary {
  12. /**
  13. * Key/value pairs
  14. * From the xml config file's configuration section
  15. */
  16. private Map<String, String> values = null;
  17. @Override
  18. public void setValues(Map<String, String> values) {
  19. this.values = values;
  20. }
  21. /**
  22. * Read a value from the xml config file's configuration section.
  23. *
  24. * If the key is missing, throw a null pointer exception
  25. * @param key
  26. * @return corresponding value as a String
  27. */
  28. public String getValue(String key) {
  29. String value;
  30. if (values != null) {
  31. value = values.get(key);
  32. if (value == null) {
  33. throw new NullPointerException(
  34. "Value for key '" + key + "' missing from XML configuration file");
  35. }
  36. } else {
  37. throw new RuntimeException(
  38. "Attempt to read configuration value for '" + key + "' with Config.getValue() "
  39. + "before values have been loaded from configuration file");
  40. }
  41. return value;
  42. }
  43. /**
  44. * Read a value from the xml config file's configuration section
  45. * and parse an integer from it. Will throw a runtime exception
  46. * if parsing fails (NumberFormatException)
  47. * @param key
  48. * @return
  49. */
  50. @Override
  51. public int getValueAsInt(String key) {
  52. return Integer.parseInt(getValue(key));
  53. }
  54. /**
  55. * Read a value from the xml config file's configuration section
  56. * and parse a float from it. Will throw a runtime exception
  57. * if parsing fails (NumberFormatException)
  58. * @param key
  59. * @return
  60. */
  61. @Override
  62. public float getValueAsFloat(String key) {
  63. return Float.parseFloat(getValue(key));
  64. }
  65. /**
  66. * Read a value from the xml config file's configuration section
  67. * and parse a double from it. Will throw a runtime exception
  68. * if parsing fails (NumberFormatException)
  69. * @param key
  70. * @return
  71. */
  72. @Override
  73. public double getValueAsDouble(String key) {
  74. return Double.parseDouble(getValue(key));
  75. }
  76. /**
  77. * Read a value from the xml config file's configuration section
  78. * and parse a boolean from it. Will throw a runtime exception
  79. * if parsing fails
  80. * @param key
  81. * @return
  82. */
  83. @Override
  84. public boolean getValueAsBoolean(String key) {
  85. return Boolean.parseBoolean(getValue(key));
  86. }
  87. /**
  88. * Non-static accessor for use in el
  89. * @return
  90. */
  91. @Override
  92. public Map<String, String> getValues() {
  93. return values;
  94. }
  95. /**
  96. * Find the size of a pixel value key, eg (String) 320px will return
  97. * (int) 320
  98. *
  99. * @param key to lookup
  100. * @return
  101. */
  102. @Override
  103. public int getValueAsPx(String key) {
  104. String value = getValue(key);
  105. return Integer.parseInt(value.substring(0, value.length() - 2));
  106. }
  107. }