1 10 11 package org.mule.util; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.regex.Matcher ; 19 import java.util.regex.Pattern ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 28 public class TemplateParser 29 { 30 public static final String ANT_TEMPLATE_STYLE = "ant"; 31 public static final String SQUARE_TEMPLATE_STYLE = "square"; 32 33 36 protected static Log logger = LogFactory.getLog(TemplateParser.class); 37 38 private Pattern pattern = null; 39 private int pre = 1; 40 private int post = 1; 41 private String style = null; 42 43 public static TemplateParser createAntStyleParser() 44 { 45 return new TemplateParser(ANT_TEMPLATE_STYLE); 46 } 47 48 public static TemplateParser createSquareBracesStyleParser() 49 { 50 return new TemplateParser(SQUARE_TEMPLATE_STYLE); 51 } 52 53 private TemplateParser(String style) 54 { 55 if (ANT_TEMPLATE_STYLE.equals(style)) 56 { 57 pattern = Pattern.compile("\\$\\{[^\\}]+\\}"); 58 pre = 2; 59 } 60 else if (SQUARE_TEMPLATE_STYLE.equals(style)) 61 { 62 pattern = Pattern.compile("\\[[^\\]]+\\]"); 63 } 64 else 65 { 66 throw new IllegalArgumentException ("Unknown template style: " + style); 67 } 68 this.style = style; 69 } 70 71 81 public String parse(Map props, String template) 82 { 83 return parse(props, template, null); 84 } 85 86 96 public String parse(TemplateCallback callback, String template) 97 { 98 return parse(null, template, callback); 99 } 100 101 protected String parse(Map props, String template, TemplateCallback callback) 102 { 103 String result = template; 104 Matcher m = pattern.matcher(template); 105 106 while (m.find()) 107 { 108 Object value = null; 109 110 String match = m.group(); 111 String propname = match.substring(pre, match.length() - post); 112 113 if (callback != null) 114 { 115 value = callback.match(propname); 116 } 117 else if (props != null) 118 { 119 value = props.get(propname); 120 } 121 122 if (value == null) 123 { 124 if (logger.isWarnEnabled()) 125 { 126 logger.warn("Value " + propname + " not found in context"); 127 } 128 } 129 else 130 { 131 String matchRegex = escape(match); 132 String valueString = value.toString(); 133 134 if (valueString.indexOf('\\') != -1) 135 { 136 valueString = valueString.replaceAll("\\\\", "\\\\\\\\"); 137 } 138 139 result = result.replaceAll(matchRegex, valueString); 140 } 141 } 142 return result; 143 } 144 145 154 public List parse(Map props, List templates) 155 { 156 if (templates == null) 157 { 158 return new ArrayList (); 159 } 160 List list = new ArrayList (templates.size()); 161 for (Iterator iterator = templates.iterator(); iterator.hasNext();) 162 { 163 list.add(parse(props, iterator.next().toString())); 164 } 165 return list; 166 } 167 168 178 public Map parse(Map props, Map templates) 179 { 180 if (templates == null) 181 { 182 return new HashMap (); 183 } 184 Map map = new HashMap (templates.size()); 185 Map.Entry entry; 186 for (Iterator iterator = templates.entrySet().iterator(); iterator.hasNext();) 187 { 188 entry = (Map.Entry )iterator.next(); 189 map.put(entry.getKey(), parse(props, entry.getValue().toString())); 190 } 191 return map; 192 } 193 194 private String escape(String string) 195 { 196 int length = string.length(); 197 if (length == 0) 198 { 199 return string; 201 } 202 else 203 { 204 StringBuffer buffer = new StringBuffer (length * 2); 205 for (int i = 0; i < length; i++) 206 { 207 char currentCharacter = string.charAt(i); 208 switch (currentCharacter) 209 { 210 case '[' : 211 case ']' : 212 case '{' : 213 case '}' : 214 case '$' : 215 buffer.append("\\"); 216 default : 218 buffer.append(currentCharacter); 219 } 220 } 221 return buffer.toString(); 222 } 223 } 224 225 public String getStyle() 226 { 227 return style; 228 } 229 230 public boolean isContainsTemplate(String value) 231 { 232 Matcher m = pattern.matcher(value); 233 return m.find(); 234 } 235 236 public static interface TemplateCallback 237 { 238 public Object match(String token); 239 } 240 241 } 242 | Popular Tags |