1 16 package org.outerj.daisy.configutil; 17 18 import java.util.Properties ; 19 import java.util.Stack ; 20 import java.util.regex.Pattern ; 21 import java.util.regex.Matcher ; 22 import java.net.URLEncoder ; 23 import java.net.URI ; 24 import java.net.URISyntaxException ; 25 import java.io.UnsupportedEncodingException ; 26 import java.io.File ; 27 28 public class PropertyResolver { 29 public static String resolveProperties(String input) { 30 return resolveProperties(input, System.getProperties()); 31 } 32 33 41 public static String resolveProperties(String input, Properties properties) { 42 StringBuffer result = new StringBuffer (input.length()); 43 StringBuffer propertyBuffer = null; 44 Stack openPropertyBuffers = new Stack (); 45 46 final int STATE_DEFAULT = 0; 47 final int STATE_IN_PROP = 1; 48 int state = STATE_DEFAULT; 49 50 for (int i = 0; i < input.length(); i++) { 51 char c = input.charAt(i); 52 switch (c) { 53 case '$': 54 if (i + 1 < input.length() && input.charAt(i + 1) == '{') { 55 if (state == STATE_IN_PROP) { 56 openPropertyBuffers.push(propertyBuffer); 57 } 58 i++; 59 state = STATE_IN_PROP; 60 propertyBuffer = new StringBuffer (); 61 } else { 62 (state == STATE_IN_PROP ? propertyBuffer : result).append("$"); 63 } 64 break; 65 case '}': 66 if (state == STATE_IN_PROP) { 67 String propName = propertyBuffer.toString(); 68 String propValue = evaluateProperty(propName, properties); 69 String propEvalResult = propValue != null ? propValue : "${" + propName + "}"; 70 if (!openPropertyBuffers.empty()) { 71 propertyBuffer = (StringBuffer )openPropertyBuffers.pop(); 72 propertyBuffer.append(propEvalResult); 73 } else { 75 result.append(propEvalResult); 76 state = STATE_DEFAULT; 77 } 78 } else { 79 result.append(c); 80 } 81 break; 82 default: 83 (state == STATE_IN_PROP ? propertyBuffer : result).append(c); 84 } 85 } 86 87 if (state == STATE_IN_PROP) { 88 do { 90 if (!openPropertyBuffers.empty()) { 91 propertyBuffer = ((StringBuffer )openPropertyBuffers.pop()).append("${").append(propertyBuffer); 92 } else { 93 result.append("${").append(propertyBuffer); 94 propertyBuffer = null; 95 } 96 } while (propertyBuffer != null); 97 } 98 99 return result.toString(); 100 } 101 102 private static Pattern PROP_PATTERN = Pattern.compile("^([^:]+):(.+)$"); 103 104 109 private static String evaluateProperty(String input, Properties properties) { 110 Matcher matcher = PROP_PATTERN.matcher(input); 111 if (matcher.matches()) { 112 String action = matcher.group(1); 113 String value = matcher.group(2); 114 115 try { 116 if (action.equals("url-encode")) { 117 return URLEncoder.encode(value, "UTF-8"); 118 } else if (action.equals("double-url-encode")) { 119 return URLEncoder.encode(URLEncoder.encode(value, "UTF-8"), "UTF-8"); 120 } else if (action.equals("tripple-url-encode")) { 121 return URLEncoder.encode(URLEncoder.encode(URLEncoder.encode(value, "UTF-8"), "UTF-8"), "UTF-8"); 122 } else if (action.equals("uri-path-encode")) { 123 try { 124 return new URI (null, null, value, null).getRawSchemeSpecificPart(); 125 } catch (URISyntaxException e) { 126 throw new RuntimeException ("Error in uri-path-encode function of property resolver.", e); 127 } 128 } else if (action.equals("file-to-uri")) { 129 return new File (value).toURI().toString(); 130 } else { 131 return null; 132 } 133 } catch (UnsupportedEncodingException e) { 134 throw new RuntimeException (e); 135 } 136 } else { 137 return properties.getProperty(input); 138 } 139 } 140 } 141 | Popular Tags |