1 10 package org.mmbase.util; 11 12 import java.io.BufferedInputStream ; 13 import java.io.BufferedOutputStream ; 14 import java.io.File ; 15 import java.io.FileInputStream ; 16 import java.io.FileNotFoundException ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.RandomAccessFile ; 21 import java.util.Date ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Properties ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 28 import org.mmbase.util.logging.Logger; 29 import org.mmbase.util.logging.Logging; 30 31 37 public class ExtendedProperties extends Properties { 38 private static Logger log = Logging.getLoggerInstance(ExtendedProperties.class.getName()); 40 41 44 private static char[] hexDigit = { 45 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' 46 }; 47 48 52 protected String commentPrefix = "#"; 53 54 57 public ExtendedProperties() { 58 } 59 60 64 public ExtendedProperties(String filename) { 65 try { 66 getProps(filename); 67 } catch (IOException e) { 68 log.error("Failed to load the ExtendedProperties for: "+ filename); 69 } 70 } 71 72 76 public ExtendedProperties(ExtendedProperties exProp) { 77 super(exProp); 79 } 80 81 82 86 public Hashtable readProperties(String filename) { 87 clear(); 88 try { 89 getProps(filename); 90 } catch (IOException e) { 91 log.debug("Failed to load the ExtendedProperties from: "+ filename, e); 92 } 93 ExtendedProperties propsToReturn = new ExtendedProperties(); 94 Enumeration e = keys(); 95 while (e.hasMoreElements()) { 96 String s = (String ) e.nextElement(); 97 propsToReturn.put(s,get(s)); 98 } 99 return propsToReturn; 100 } 101 102 107 public synchronized void saveProperties(String filename, Hashtable propsToSave) { 108 clear(); 109 Enumeration e = propsToSave.keys(); 110 while (e.hasMoreElements()) { 111 String s = (String ) e.nextElement(); 112 put(s,propsToSave.get(s)); } 114 try { 115 save(filename); 116 } catch (IOException ioe) { 117 log.error("Fail to save the ExtendedProperties to: " + filename+" : "+ioe); 118 } 119 } 120 121 126 public Vector getPropertyValues(String whichProp, String delimeter) { 127 Vector parsedPropsToReturn = new Vector (); 128 if (containsKey(whichProp)) { 129 String value = (String ) get(whichProp); 131 StringTokenizer tok = new StringTokenizer (value,delimeter); 132 while (tok.hasMoreTokens()) { 133 parsedPropsToReturn.addElement(tok.nextToken()); 134 } 135 return parsedPropsToReturn; 136 } else { 137 return null; 140 } 141 } 142 143 147 public void getProps(String filename) throws IOException { 148 try { 149 FileInputStream fileInputStream = new FileInputStream (filename); 150 BufferedInputStream bufferedInputStream = new BufferedInputStream (fileInputStream); 151 load(bufferedInputStream); 152 bufferedInputStream.close(); 153 } catch (FileNotFoundException e ) { 154 log.debug("ExtendedProperties:: file " + filename + " not found"); 155 } 156 } 157 158 159 166 public synchronized void load(InputStream in) throws IOException { 167 in = Runtime.getRuntime().getLocalizedInputStream(in); 168 169 int ch = in.read(); 170 while (ch != -1) { 171 switch (ch) { 172 case '#': 173 case '!': 174 do { 175 ch = in.read(); 176 } while ((ch >= 0) && (ch != '\n') && (ch != '\r')); 177 continue; 178 179 case '\n': 180 case '\r': 181 case ' ': 182 case '\t': 183 ch = in.read(); 184 continue; 185 } 186 187 StringBuffer key = new StringBuffer (); 189 190 while ((ch >= 0) && (ch != '=') && (ch != ' ') && (ch != '\t') && (ch != '\n') && (ch != '\r')) { 191 key.append((char)ch); 192 ch = in.read(); 193 } 194 while ((ch == ' ') && (ch == '\t')) { 195 ch = in.read(); 196 } 197 if ((ch == '=') || (ch == ':')) { 198 ch = in.read(); 199 } 200 while ((ch == ' ') && (ch == '\t')) { 201 ch = in.read(); 202 } 203 204 StringBuffer val = new StringBuffer (); 206 while ((ch >= 0) && (ch != '\n') && (ch != '\r')) { 207 if (ch == '\\') { 208 switch (ch = in.read()) { 209 case '\r': 210 if (((ch = in.read()) == '\n') || 211 (ch == ' ') || (ch == '\t')) { 212 } else continue; 214 case '\n': 215 while (((ch = in.read()) == ' ') || (ch == '\t')); 216 continue; 217 case 't': ch = '\t'; break; 218 case 'n': ch = '\n'; break; 219 case 'r': ch = '\r'; break; 220 case 'u': { 221 while ((ch = in.read()) == 'u'); 222 int d = 0; 223 loop: 224 for (int i = 0 ; i < 4 ; i++, ch = in.read()) { 225 switch (ch) { 226 case '0': case '1': case '2': case '3': case '4': 227 case '5': case '6': case '7': case '8': case '9': 228 d = (d << 4) + ch - '0'; 229 break; 230 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': 231 d = (d << 4) + 10 + ch - 'a'; 232 break; 233 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': 234 d = (d << 4) + 10 + ch - 'A'; 235 break; 236 default: 237 break loop; 238 } 239 } 240 ch = d; 241 } 242 } 243 } 244 val.append((char)ch); 245 ch = in.read(); 246 } 247 put(key.toString(), val.toString()); 248 } 249 } 250 251 257 private void write (String filename, String header) throws IOException { 258 FileOutputStream fileOutputStream = new FileOutputStream (filename); 259 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream (fileOutputStream); 260 save(bufferedOutputStream, header); 261 bufferedOutputStream.close(); 262 } 263 264 265 271 public synchronized void save(String filename) throws IOException { 272 File file = new File (filename); 273 boolean des = false; if (file.exists()) { 275 ExtendedProperties copyOfProps = (ExtendedProperties) this.clone(); 276 String newlines = ""; 278 String lines = readOldProps(file); 279 280 StringTokenizer tok = new StringTokenizer (lines,"\n",true); 281 while(tok.hasMoreTokens()) { 282 String line =(String ) tok.nextElement(); 283 if (line.startsWith(commentPrefix)) { 285 newlines = newlines + line; 286 } else { 287 int index = line.indexOf('='); 288 if (index == -1) { 289 if (!des) { 291 newlines = newlines + "\n"; 292 } else { 293 des = false; 294 } 295 } else { 296 String name = line.substring(0,index); 298 299 if (containsKey(name)) { 300 newlines = newlines + name + "=" + getProperty(name); 302 copyOfProps.remove(name); 304 } else { 305 des = true; 308 309 } 310 311 } 312 } 313 } 314 315 Enumeration e = copyOfProps.keys(); 317 while (e.hasMoreElements()) { 318 String name = (String ) e.nextElement(); 319 newlines = newlines + "\n" + name + "=" + copyOfProps.getProperty(name); 320 } 321 323 file.delete(); 324 RandomAccessFile writefile = new RandomAccessFile (file,"rw"); 326 writefile.writeBytes(newlines); 327 writefile.close(); 328 } else { 329 write(filename, ""); 332 } 333 } 334 335 339 private String readOldProps(File file) throws IOException { 340 String line = ""; 341 342 String lines = ""; 343 RandomAccessFile readfile = new RandomAccessFile (file,"r"); 344 do { 345 try { 346 line = readfile.readLine(); 347 if (line != null) { 348 lines= lines + line + "\n"; 349 } 350 } catch(Exception e) { 351 log.error("EOF!"); 352 } 353 } while (line != null); 354 355 if (!lines.equals("")) { 356 lines = lines.substring(0,lines.length()-1); 357 } 358 readfile.close(); 359 return lines; 360 } 361 362 367 public Object setProperty(String name, String value) { 368 return(put(name, value)); 369 } 370 371 374 public void showContents() { 375 Enumeration names = propertyNames(); 376 while (names.hasMoreElements()) { 377 String name = (String ) names.nextElement(); 378 log.debug(name + "=" + getProperty(name)); 379 } 380 } 381 382 public synchronized String save() { 383 StringBuffer b=new StringBuffer (); 384 b.append('#'); 385 b.append(new Date ()); 386 b.append('\n'); 387 388 for (Enumeration e = keys() ; e.hasMoreElements() ;) { 389 String key = (String )e.nextElement(); 390 b.append(key); 391 b.append('='); 392 393 String val = (String )get(key); 394 int len = val.length(); 395 if (len>0) b.append(val); 396 b.append('\n'); 397 } 398 return(b.toString()); 399 } 400 } 401 | Popular Tags |