1 19 package gcc.properties; 20 21 import gcc.*; 22 import java.io.*; 23 import java.util.*; 24 25 public class PropertyMap extends HashMap 26 { 27 public PropertyMap() 28 { 29 super(); 30 } 31 32 public PropertyMap(Map map) 33 { 34 super(map); 35 } 36 37 public Object put(Object key, Object value) 38 { 39 if (key == null) 40 { 41 throw new NullPointerException("key"); 42 } 43 if (value == null) 44 { 45 throw new NullPointerException("value"); 47 } 48 return super.put(key, value); 49 } 50 51 public String getProperty(String name) 52 { 53 return (String)super.get(name); 54 } 55 56 public String getProperty(String name, String defaultValue) 57 { 58 String value = getProperty(name); 59 if (value == null || value.length() == 0) 60 { 61 value = defaultValue; 62 } 63 return value; 64 } 65 66 public Properties getProperties() 67 { 68 Properties props = new Properties(); 69 for (Iterator i = entrySet().iterator(); i.hasNext();) 70 { 71 Map.Entry entry = (Map.Entry)i.next(); 72 String key = (String)entry.getKey(); 73 String value = (String)entry.getValue(); 74 props.put(key, value); 75 } 76 return props; 77 } 78 79 public static PropertyMap readFile(String fileName) 80 { 81 try 82 { 83 Properties props = new Properties(); 84 BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileName)); 85 props.load(input); 86 input.close(); 87 PropertyMap map = new PropertyMap(); 88 for (Iterator i = props.entrySet().iterator(); i.hasNext();) 89 { 90 Map.Entry entry = (Map.Entry)i.next(); 91 String key = entry.getKey().toString().trim(); 92 String value = entry.getValue().toString().trim(); 93 map.put(key, value); 94 } 95 return map; 96 } 97 catch (Exception ex) 98 { 99 throw new SystemException(ex); 100 } 101 } 102 } 103 | Popular Tags |