1 2 package de.jwi.jgallery; 3 4 25 26 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.Serializable ; 30 import java.util.Enumeration ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 34 35 39 public class Configuration implements Serializable 40 { 41 42 private Properties properties; 43 44 private Configuration parent = null; 45 46 private IThumbnailWriter thumbnailWriter; 47 48 public Configuration(Configuration parent) 49 { 50 this.parent = parent; 51 properties = new Properties (); 52 } 53 54 public void addProperty(String key, String val) 55 { 56 properties.put(key, val); 57 } 58 59 public Configuration(InputStream is, Configuration parent) 60 throws IOException 61 { 62 this.parent = parent; 63 64 properties = new Properties (); 65 66 if (is != null) 67 { 68 properties.load(is); 69 } 70 } 71 72 public String toString() 73 { 74 return properties.toString(); 75 } 76 77 public Configuration(InputStream is) throws IOException 78 { 79 this(is, null); 80 } 81 82 public int getInt(String key, int defaultValue) 83 { 84 String s = getString(key); 85 if (s==null) 86 { 87 return defaultValue; 88 } 89 return Integer.parseInt(s); 90 } 91 92 93 public float getFloat(String key, float defaultValue) 94 { 95 String s = getString(key); 96 if (s==null) 97 { 98 return defaultValue; 99 } 100 101 return Float.parseFloat(s); 102 } 103 104 105 public boolean getBoolean(String key, boolean defaultValue) 106 { 107 String s = getString(key); 108 if (s==null) 109 { 110 return defaultValue; 111 } 112 113 return Boolean.valueOf(s).booleanValue(); 114 } 115 116 public String getString(String key) 117 { 118 return getString(key,null); 119 } 120 121 public String getString(String key, String defaultValue) 122 { 123 String val = properties.getProperty(key); 124 if (null == val) 125 { 126 if (null != parent) 127 { 128 val = parent.getString(key); 129 } 130 } 131 if (val == null) 132 { 133 val = defaultValue; 134 } 135 return val; 136 } 137 138 147 void getUserVariables(Map map) throws GalleryException 148 { 149 for (Enumeration e = properties.propertyNames(); e.hasMoreElements();) 150 { 151 String key = (String ) e.nextElement(); 152 if (key.startsWith("variable.")) 153 { 154 String name = key.substring(key.indexOf('.') + 1); 155 if (name.length() < 1) 156 { 157 throw new GalleryException("invalid variable entry: " + key); 158 } 159 if (!map.containsKey(name)) 160 { 161 map.put(name, properties.getProperty(key)); 162 } 163 } 164 165 } 166 if (null != parent) 167 { 168 parent.getUserVariables(map); 169 } 170 } 171 172 public IThumbnailWriter getThumbnailWriter() 173 { 174 IThumbnailWriter writer = this.thumbnailWriter; 175 176 if (null == writer) 177 { 178 if (null != parent) 179 { 180 writer = parent.getThumbnailWriter(); 181 } 182 } 183 return writer; 184 } 185 186 public void setThumbnailWriter(IThumbnailWriter thumbnailWriter) 187 { 188 this.thumbnailWriter = thumbnailWriter; 189 } 190 } | Popular Tags |