1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.io.File ; 58 import java.io.FileWriter ; 59 import java.io.IOException ; 60 import java.io.InputStream ; 61 import java.io.InputStreamReader ; 62 import java.io.LineNumberReader ; 63 import java.io.Reader ; 64 import java.io.UnsupportedEncodingException ; 65 66 import java.util.Date ; 67 import java.util.Iterator ; 68 69 import org.apache.commons.lang.StringUtils; 70 71 161 public abstract class BasePropertiesConfiguration 162 extends BasePathConfiguration 163 { 164 165 private boolean includesAllowed = false; 166 167 171 protected static String include = "include"; 172 173 181 protected abstract InputStream getPropertyStream(String resourceName) 182 throws IOException ; 183 184 190 public void load(InputStream input) 191 throws IOException 192 { 193 load(input, null); 194 } 195 196 204 public synchronized void load(InputStream input, String enc) 205 throws IOException 206 { 207 PropertiesReader reader = null; 208 if (enc != null) 209 { 210 try 211 { 212 reader = 213 new PropertiesReader(new InputStreamReader (input, enc)); 214 } 215 catch (UnsupportedEncodingException e) 216 { 217 } 219 } 220 221 if (reader == null) 222 { 223 reader = new PropertiesReader(new InputStreamReader (input)); 224 } 225 226 while (true) 227 { 228 String line = reader.readProperty(); 229 230 if (line == null) 231 { 232 break; } 234 235 int equalSign = line.indexOf('='); 236 if (equalSign > 0) 237 { 238 String key = line.substring(0, equalSign).trim(); 239 String value = line.substring(equalSign + 1).trim(); 240 241 246 if (StringUtils.isNotEmpty(getInclude()) 247 && key.equalsIgnoreCase(getInclude())) 248 { 249 if (getIncludesAllowed()) 250 { 251 String [] files = StringUtils.split(value, ","); 252 for (int cnt = 0 ; cnt < files.length ; cnt++) 253 { 254 load(getPropertyStream(files[cnt].trim())); 255 } 256 } 257 } 258 else 259 { 260 addProperty(key, value); 261 } 262 } 263 } 264 } 265 266 273 public void save(String filename) 274 throws IOException 275 { 276 File file = new File (filename); 277 PropertiesWriter out = new PropertiesWriter(file); 278 279 out.writeComment("written by PropertiesConfiguration"); 280 out.writeComment(new Date ().toString()); 281 282 for (Iterator i = this.getKeys(); i.hasNext();) 283 { 284 String key = (String ) i.next(); 285 String value = StringUtils.join(this.getStringArray(key), ", "); 286 out.writeProperty(key, value); 287 } 288 out.flush(); 289 out.close(); 290 } 291 292 298 public String getInclude() 299 { 300 return BasePropertiesConfiguration.include; 301 } 302 303 309 public void setInclude(String inc) 310 { 311 BasePropertiesConfiguration.include = inc; 312 } 313 314 321 protected void setIncludesAllowed(boolean includesAllowed) 322 { 323 this.includesAllowed = includesAllowed; 324 } 325 326 331 public boolean getIncludesAllowed() 332 { 333 return this.includesAllowed; 334 } 335 336 342 class PropertiesReader 343 extends LineNumberReader 344 { 345 350 public PropertiesReader(Reader reader) 351 { 352 super(reader); 353 } 354 355 364 public String readProperty() 365 throws IOException 366 { 367 StringBuffer buffer = new StringBuffer (); 368 369 while (true) 370 { 371 String line = readLine(); 372 if (line == null) 373 { 374 return null; 376 } 377 378 line = line.trim(); 379 380 if (StringUtils.isEmpty(line) 381 || (line.charAt(0) == '#')) 382 { 383 continue; 384 } 385 386 if (line.endsWith("\\")) 387 { 388 line = line.substring(0, line.length() - 1); 389 buffer.append(line); 390 } 391 else 392 { 393 buffer.append(line); 394 break; 395 } 396 } 397 return buffer.toString(); 398 } 399 } 401 404 class PropertiesWriter 405 extends FileWriter 406 { 407 413 public PropertiesWriter(File file) 414 throws IOException 415 { 416 super(file); 417 } 418 419 426 public void writeProperty(String key, String value) 427 throws IOException 428 { 429 write(key); 430 write(" = "); 431 write(value != null ? value : ""); 432 write('\n'); 433 } 434 435 441 public void writeComment(String comment) 442 throws IOException 443 { 444 write("# " + comment + "\n"); 445 } 446 } } 448 | Popular Tags |