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