1 22 package org.jboss.util.propertyeditor; 23 24 import java.beans.PropertyEditorSupport ; 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.io.StringWriter ; 29 import java.io.PrintWriter ; 30 import java.util.Iterator ; 31 import java.util.Properties ; 32 33 import org.jboss.util.NestedRuntimeException; 34 import org.jboss.util.StringPropertyReplacer; 35 36 43 public class PropertiesEditor extends PropertyEditorSupport 44 { 45 46 49 private static final String ENC = "ISO-8859-1"; 50 51 60 public void setAsText(String propsText) 61 { 62 try 63 { 64 Properties rawProps = new Properties (System.getProperties()); 66 ByteArrayInputStream bais = new ByteArrayInputStream (propsText.getBytes(ENC)); 67 rawProps.load(bais); 68 Properties props = new Properties (); 70 Iterator keys = rawProps.keySet().iterator(); 71 while( keys.hasNext() ) 72 { 73 String key = (String ) keys.next(); 74 String value = rawProps.getProperty(key); 75 String value2 = StringPropertyReplacer.replaceProperties(value, rawProps); 76 props.setProperty(key, value2); 77 } 78 rawProps.clear(); 79 80 setValue(props); 81 } 82 catch (IOException e) 83 { 84 throw new NestedRuntimeException(e); 85 } 86 } 87 88 91 public String getAsText() 92 { 93 try 94 { 95 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 96 Properties p = (Properties )getValue(); 97 p.store(baos, null); 98 return baos.toString(ENC); 99 } 100 catch (IOException e) 101 { 102 throw new NestedRuntimeException(e); 103 } 104 } 105 106 } 107 | Popular Tags |