1 22 package org.jboss.util; 23 24 import java.util.Properties ; 25 import java.io.File ; 26 27 37 public final class StringPropertyReplacer 38 { 39 40 public static final String NEWLINE = SysPropertyActions.getProperty("line.separator", "\n"); 41 42 43 private static final String FILE_SEPARATOR = File.separator; 44 45 46 private static final String PATH_SEPARATOR = File.pathSeparator; 47 48 49 private static final String FILE_SEPARATOR_ALIAS = "/"; 50 51 52 private static final String PATH_SEPARATOR_ALIAS = ":"; 53 54 private static final int NORMAL = 0; 56 private static final int SEEN_DOLLAR = 1; 57 private static final int IN_BRACKET = 2; 58 59 78 public static String replaceProperties(final String string) 79 { 80 return replaceProperties(string, null); 81 } 82 83 104 public static String replaceProperties(final String string, final Properties props) 105 { 106 final char[] chars = string.toCharArray(); 107 StringBuffer buffer = new StringBuffer (); 108 boolean properties = false; 109 int state = NORMAL; 110 int start = 0; 111 for (int i = 0; i < chars.length; ++i) 112 { 113 char c = chars[i]; 114 115 if (c == '$' && state != IN_BRACKET) 117 state = SEEN_DOLLAR; 118 119 else if (c == '{' && state == SEEN_DOLLAR) 121 { 122 buffer.append(string.substring(start, i - 1)); 123 state = IN_BRACKET; 124 start = i - 1; 125 } 126 127 else if (state == SEEN_DOLLAR) 129 state = NORMAL; 130 131 else if (c == '}' && state == IN_BRACKET) 133 { 134 if (start + 2 == i) 136 { 137 buffer.append("${}"); } 139 else { 141 String value = null; 142 143 String key = string.substring(start + 2, i); 144 145 if (FILE_SEPARATOR_ALIAS.equals(key)) 147 { 148 value = FILE_SEPARATOR; 149 } 150 else if (PATH_SEPARATOR_ALIAS.equals(key)) 151 { 152 value = PATH_SEPARATOR; 153 } 154 else 155 { 156 if (props != null) 158 value = props.getProperty(key); 159 else 160 value = System.getProperty(key); 161 162 if (value == null) 163 { 164 int colon = key.indexOf(':'); 166 if (colon > 0) 167 { 168 String realKey = key.substring(0, colon); 169 if (props != null) 170 value = props.getProperty(realKey); 171 else 172 value = System.getProperty(realKey); 173 174 if (value == null) 175 { 176 value = resolveCompositeKey(realKey, props); 178 179 if (value == null) 181 value = key.substring(colon+1); 182 } 183 } 184 else 185 { 186 value = resolveCompositeKey(key, props); 188 } 189 } 190 } 191 192 if (value != null) 193 { 194 properties = true; 195 buffer.append(value); 196 } 197 } 198 start = i + 1; 199 state = NORMAL; 200 } 201 } 202 203 if (properties == false) 205 return string; 206 207 if (start != chars.length) 209 buffer.append(string.substring(start, chars.length)); 210 211 return buffer.toString(); 213 } 214 215 226 private static String resolveCompositeKey(String key, Properties props) 227 { 228 String value = null; 229 230 int comma = key.indexOf(','); 232 if (comma > -1) 233 { 234 if (comma > 0) 236 { 237 String key1 = key.substring(0, comma); 239 if (props != null) 240 value = props.getProperty(key1); 241 else 242 value = System.getProperty(key1); 243 } 244 if (value == null && comma < key.length() - 1) 246 { 247 String key2 = key.substring(comma + 1); 248 if (props != null) 249 value = props.getProperty(key2); 250 else 251 value = System.getProperty(key2); 252 } 253 } 254 return value; 256 } 257 } | Popular Tags |