1 16 17 package org.springframework.util; 18 19 import java.io.BufferedReader ; 20 import java.io.BufferedWriter ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.Reader ; 25 import java.io.Writer ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.util.Date ; 29 import java.util.Enumeration ; 30 import java.util.Properties ; 31 32 67 public class DefaultPropertiesPersister implements PropertiesPersister { 68 69 public void load(Properties props, InputStream is) throws IOException { 70 props.load(is); 71 } 72 73 public void load(Properties props, Reader reader) throws IOException { 74 BufferedReader in = new BufferedReader (reader); 75 while (true) { 76 String line = in.readLine(); 77 if (line == null) { 78 return; 79 } 80 line = StringUtils.trimLeadingWhitespace(line); 81 if (line.length() > 0) { 82 char firstChar = line.charAt(0); 83 if (firstChar != '#' && firstChar != '!') { 84 while (endsWithContinuationMarker(line)) { 85 String nextLine = in.readLine(); 86 line = line.substring(0, line.length() - 1); 87 if (nextLine != null) { 88 line += StringUtils.trimLeadingWhitespace(nextLine); 89 } 90 } 91 int separatorIndex = line.indexOf("="); 92 if (separatorIndex == -1) { 93 separatorIndex = line.indexOf(":"); 94 } 95 String key = (separatorIndex != -1 ? line.substring(0, separatorIndex) : line); 96 String value = (separatorIndex != -1) ? line.substring(separatorIndex + 1) : ""; 97 key = StringUtils.trimTrailingWhitespace(key); 98 value = StringUtils.trimLeadingWhitespace(value); 99 props.put(unescape(key), unescape(value)); 100 } 101 } 102 } 103 } 104 105 protected boolean endsWithContinuationMarker(String line) { 106 boolean evenSlashCount = true; 107 int index = line.length() - 1; 108 while (index >= 0 && line.charAt(index) == '\\') { 109 evenSlashCount = !evenSlashCount; 110 index--; 111 } 112 return !evenSlashCount; 113 } 114 115 protected String unescape(String str) { 116 StringBuffer outBuffer = new StringBuffer (str.length()); 117 for (int index = 0; index < str.length();) { 118 char c = str.charAt(index++); 119 if (c == '\\') { 120 c = str.charAt(index++); 121 if (c == 't') { 122 c = '\t'; 123 } 124 else if (c == 'r') { 125 c = '\r'; 126 } 127 else if (c == 'n') { 128 c = '\n'; 129 } 130 else if (c == 'f') { 131 c = '\f'; 132 } 133 } 134 outBuffer.append(c); 135 } 136 return outBuffer.toString(); 137 } 138 139 140 public void store(Properties props, OutputStream os, String header) throws IOException { 141 props.store(os, header); 142 } 143 144 public void store(Properties props, Writer writer, String header) throws IOException { 145 BufferedWriter out = new BufferedWriter (writer); 146 if (header != null) { 147 out.write("#" + header); 148 out.newLine(); 149 } 150 out.write("#" + new Date ()); 151 out.newLine(); 152 for (Enumeration keys = props.keys(); keys.hasMoreElements();) { 153 String key = (String ) keys.nextElement(); 154 String val = props.getProperty(key); 155 out.write(escape(key, true) + "=" + escape(val, false)); 156 out.newLine(); 157 } 158 out.flush(); 159 } 160 161 protected String escape(String str, boolean isKey) { 162 int len = str.length(); 163 StringBuffer outBuffer = new StringBuffer (len * 2); 164 for (int index = 0; index < len; index++) { 165 char c = str.charAt(index); 166 switch (c) { 167 case ' ': 168 if (index == 0 || isKey) { 169 outBuffer.append('\\'); 170 } 171 outBuffer.append(' '); 172 break; 173 case '\\': 174 outBuffer.append("\\\\"); 175 break; 176 case '\t': 177 outBuffer.append("\\t"); 178 break; 179 case '\n': 180 outBuffer.append("\\n"); 181 break; 182 case '\r': 183 outBuffer.append("\\r"); 184 break; 185 case '\f': 186 outBuffer.append("\\f"); 187 break; 188 default: 189 if ("=: \t\r\n\f#!".indexOf(c) != -1) { 190 outBuffer.append('\\'); 191 } 192 outBuffer.append(c); 193 } 194 } 195 return outBuffer.toString(); 196 } 197 198 199 public void loadFromXml(Properties props, InputStream is) throws IOException { 200 try { 202 Method loadMethod = props.getClass().getMethod("loadFromXML", new Class [] {InputStream .class}); 203 loadMethod.invoke(props, new Object [] {is}); 204 } 205 catch (NoSuchMethodException ex) { 206 throw new IOException ("Cannot load properties XML file - not running on JDK 1.5+: " + ex.getMessage()); 207 } 208 catch (InvocationTargetException ex) { 209 if (ex.getTargetException() instanceof IOException ) { 210 throw (IOException ) ex.getTargetException(); 211 } 212 ReflectionUtils.handleInvocationTargetException(ex); 213 } 214 catch (Exception ex) { 215 ReflectionUtils.handleReflectionException(ex); 216 } 217 } 218 219 public void storeToXml(Properties props, OutputStream os, String header) throws IOException { 220 try { 222 Method storeMethod = props.getClass().getMethod( 223 "storeToXML", new Class [] {OutputStream .class, String .class}); 224 storeMethod.invoke(props, new Object [] {os, header}); 225 } 226 catch (NoSuchMethodException ex) { 227 throw new IOException ("Cannot store properties XML file - not running on JDK 1.5+: " + ex.getMessage()); 228 } 229 catch (InvocationTargetException ex) { 230 if (ex.getTargetException() instanceof IOException ) { 231 throw (IOException ) ex.getTargetException(); 232 } 233 ReflectionUtils.handleInvocationTargetException(ex); 234 } 235 catch (Exception ex) { 236 ReflectionUtils.handleReflectionException(ex); 237 } 238 } 239 240 public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException { 241 try { 243 Method storeMethod = props.getClass().getMethod( 244 "storeToXML", new Class [] {OutputStream .class, String .class, String .class}); 245 storeMethod.invoke(props, new Object [] {os, header, encoding}); 246 } 247 catch (NoSuchMethodException ex) { 248 throw new IOException ("Cannot store properties XML file - not running on JDK 1.5+: " + ex.getMessage()); 249 } 250 catch (InvocationTargetException ex) { 251 if (ex.getTargetException() instanceof IOException ) { 252 throw (IOException ) ex.getTargetException(); 253 } 254 ReflectionUtils.handleInvocationTargetException(ex); 255 } 256 catch (Exception ex) { 257 ReflectionUtils.handleReflectionException(ex); 258 } 259 } 260 261 } 262 | Popular Tags |