1 30 31 32 package org.hsqldb.persist; 33 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 import java.io.OutputStream ; 38 import java.util.Enumeration ; 39 import java.util.Properties ; 40 41 import org.hsqldb.Trace; 42 import org.hsqldb.lib.ArrayUtil; 43 import org.hsqldb.lib.FileAccess; 44 import org.hsqldb.lib.FileUtil; 45 import org.hsqldb.lib.java.JavaSystem; 46 47 55 public class HsqlProperties { 56 57 public static final int NO_VALUE_FOR_KEY = 1; 58 protected String fileName; 59 protected Properties stringProps; 60 protected int[] errorCodes = new int[0]; 61 protected String [] errorKeys = new String [0]; 62 protected boolean resource = false; 63 protected FileAccess fa; 64 65 public HsqlProperties() { 66 stringProps = new Properties (); 67 fileName = null; 68 } 69 70 public HsqlProperties(String name) { 71 72 stringProps = new Properties (); 73 fileName = name; 74 fa = FileUtil.getDefaultInstance(); 75 } 76 77 public HsqlProperties(String name, FileAccess accessor, boolean b) { 78 79 stringProps = new Properties (); 80 fileName = name; 81 resource = b; 82 fa = accessor; 83 } 84 85 public HsqlProperties(Properties props) { 86 stringProps = props; 87 } 88 89 public void setFileName(String name) { 90 fileName = name; 91 } 92 93 public String setProperty(String key, int value) { 94 return setProperty(key, Integer.toString(value)); 95 } 96 97 public String setProperty(String key, boolean value) { 98 return setProperty(key, String.valueOf(value)); 99 } 100 101 public String setProperty(String key, String value) { 102 return (String ) stringProps.put(key, value); 103 } 104 105 public String setPropertyIfNotExists(String key, String value) { 106 107 value = getProperty(key, value); 108 109 return setProperty(key, value); 110 } 111 112 public Properties getProperties() { 113 return stringProps; 114 } 115 116 public String getProperty(String key) { 117 return stringProps.getProperty(key); 118 } 119 120 public String getProperty(String key, String defaultValue) { 121 return stringProps.getProperty(key, defaultValue); 122 } 123 124 public int getIntegerProperty(String key, int defaultValue) { 125 126 String prop = getProperty(key); 127 128 try { 129 if (prop != null) { 130 defaultValue = Integer.parseInt(prop); 131 } 132 } catch (NumberFormatException e) {} 133 134 return defaultValue; 135 } 136 137 public int getIntegerProperty(String key, int defaultValue, int minimum, 138 int maximum) { 139 140 String prop = getProperty(key); 141 boolean badvalue = false; 142 143 try { 144 defaultValue = Integer.parseInt(prop); 145 } catch (NumberFormatException e) {} 146 147 if (defaultValue < minimum) { 148 defaultValue = minimum; 149 badvalue = true; 150 } else if (defaultValue > maximum) { 151 defaultValue = maximum; 152 badvalue = true; 153 } 154 155 return defaultValue; 156 } 157 158 161 public int getIntegerProperty(String key, int defaultValue, 162 int[] values) { 163 164 String prop = getProperty(key); 165 int value = defaultValue; 166 167 try { 168 if (prop != null) { 169 value = Integer.parseInt(prop); 170 } 171 } catch (NumberFormatException e) {} 172 173 if (ArrayUtil.find(values, value) == -1) { 174 return defaultValue; 175 } 176 177 return value; 178 } 179 180 public boolean isPropertyTrue(String key) { 181 return isPropertyTrue(key, false); 182 } 183 184 public boolean isPropertyTrue(String key, boolean defaultValue) { 185 186 String value = stringProps.getProperty(key); 187 188 if (value == null) { 189 return defaultValue; 190 } 191 192 return value.toLowerCase().equals("true"); 193 } 194 195 public void removeProperty(String key) { 196 stringProps.remove(key); 197 } 198 199 public void addProperties(Properties props) { 200 201 if (props == null) { 202 return; 203 } 204 205 Enumeration keys = props.keys(); 206 207 while (keys.hasMoreElements()) { 208 Object key = keys.nextElement(); 209 210 this.stringProps.put(key, props.get(key)); 211 } 212 } 213 214 public void addProperties(HsqlProperties props) { 215 216 if (props == null) { 217 return; 218 } 219 220 addProperties(props.stringProps); 221 } 222 223 public boolean checkFileExists() throws IOException { 225 226 String propFilename = fileName + ".properties"; 227 228 return fa.isStreamElement(propFilename); 229 } 230 231 public boolean load() throws Exception { 232 233 if (!checkFileExists()) { 234 return false; 235 } 236 237 if (fileName == null || fileName.length() == 0) { 238 throw new FileNotFoundException ( 239 Trace.getMessage(Trace.HsqlProperties_load)); 240 } 241 242 InputStream fis = null; 243 String propsFilename = fileName + ".properties"; 244 245 try { 247 fis = resource ? getClass().getResourceAsStream(propsFilename) 248 : fa.openInputStreamElement(propsFilename); 249 250 stringProps.load(fis); 251 } finally { 252 if (fis != null) { 253 fis.close(); 254 } 255 } 256 257 return true; 258 } 259 260 263 public void save() throws Exception { 264 265 if (fileName == null || fileName.length() == 0) { 266 throw new java.io.FileNotFoundException ( 267 Trace.getMessage(Trace.HsqlProperties_load)); 268 } 269 270 String filestring = fileName + ".properties"; 271 272 save(filestring); 273 } 274 275 278 public void save(String fileString) throws Exception { 279 280 fa.createParentDirs(fileString); 282 283 OutputStream fos = fa.openOutputStreamElement(fileString); 284 285 JavaSystem.saveProperties( 286 stringProps, 287 HsqlDatabaseProperties.PRODUCT_NAME + " " 288 + HsqlDatabaseProperties.THIS_FULL_VERSION, fos); 289 fos.close(); 290 291 return; 292 } 293 294 299 private void addError(int code, String key) { 300 301 errorCodes = (int[]) ArrayUtil.resizeArray(errorCodes, 302 errorCodes.length + 1); 303 errorKeys = (String []) ArrayUtil.resizeArray(errorKeys, 304 errorKeys.length + 1); 305 errorCodes[errorCodes.length - 1] = code; 306 errorKeys[errorKeys.length - 1] = key; 307 } 308 309 317 public static HsqlProperties argArrayToProps(String [] arg, String type) { 318 319 HsqlProperties props = new HsqlProperties(); 320 321 for (int i = 0; i < arg.length; i++) { 322 String p = arg[i]; 323 324 if (p.startsWith("-?")) { 325 props.addError(NO_VALUE_FOR_KEY, p.substring(1)); 326 } else if (p.charAt(0) == '-') { 327 props.setProperty(type + "." + p.substring(1), arg[i + 1]); 328 329 i++; 330 } 331 } 332 333 return props; 334 } 335 336 356 public static HsqlProperties delimitedArgPairsToProps(String s, 357 String pairsep, String dlimiter, String type) { 358 359 HsqlProperties props = new HsqlProperties(); 360 int currentpair = 0; 361 362 while (true) { 363 int nextpair = s.indexOf(dlimiter, currentpair); 364 365 if (nextpair == -1) { 366 nextpair = s.length(); 367 } 368 369 int valindex = s.substring(0, nextpair).indexOf(pairsep, 371 currentpair); 372 373 if (valindex == -1) { 374 props.addError(NO_VALUE_FOR_KEY, 375 s.substring(currentpair, nextpair).trim()); 376 } else { 377 String key = s.substring(currentpair, valindex).trim(); 378 String value = s.substring(valindex + pairsep.length(), 379 nextpair).trim(); 380 381 if (type != null) { 382 key = type + "." + key; 383 } 384 385 props.setProperty(key, value); 386 } 387 388 if (nextpair == s.length()) { 389 break; 390 } 391 392 currentpair = nextpair + dlimiter.length(); 393 } 394 395 return props; 396 } 397 398 public Enumeration propertyNames() { 399 return stringProps.propertyNames(); 400 } 401 402 public boolean isEmpty() { 403 return stringProps.isEmpty(); 404 } 405 406 public String [] getErrorKeys() { 407 return errorKeys; 408 } 409 419 } 420 | Popular Tags |