1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
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 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public final class TemplateParser |
29 | |
{ |
30 | |
public static final String ANT_TEMPLATE_STYLE = "ant"; |
31 | |
public static final String SQUARE_TEMPLATE_STYLE = "square"; |
32 | |
public static final String CURLY_TEMPLATE_STYLE = "curly"; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 2 | protected static final Log logger = LogFactory.getLog(TemplateParser.class); |
38 | |
|
39 | |
private final Pattern pattern; |
40 | |
private final int pre; |
41 | |
private final int post; |
42 | |
private final String style; |
43 | |
|
44 | |
|
45 | |
public static TemplateParser createAntStyleParser() |
46 | |
{ |
47 | 10 | return new TemplateParser(ANT_TEMPLATE_STYLE); |
48 | |
} |
49 | |
|
50 | |
public static TemplateParser createSquareBracesStyleParser() |
51 | |
{ |
52 | 444 | return new TemplateParser(SQUARE_TEMPLATE_STYLE); |
53 | |
} |
54 | |
|
55 | |
public static TemplateParser createCurlyBracesStyleParser() |
56 | |
{ |
57 | 0 | return new TemplateParser(CURLY_TEMPLATE_STYLE); |
58 | |
} |
59 | |
|
60 | |
private TemplateParser(String style) |
61 | 454 | { |
62 | 454 | if (ANT_TEMPLATE_STYLE.equals(style)) |
63 | |
{ |
64 | 10 | pattern = Pattern.compile("\\$\\{[^\\}]+\\}"); |
65 | 10 | pre = 2; |
66 | 10 | post = 1; |
67 | |
} |
68 | 444 | else if (SQUARE_TEMPLATE_STYLE.equals(style)) |
69 | |
{ |
70 | 444 | pattern = Pattern.compile("\\[[^\\]]+\\]"); |
71 | 444 | pre = 1; |
72 | 444 | post = 1; |
73 | |
} |
74 | 0 | else if (CURLY_TEMPLATE_STYLE.equals(style)) |
75 | |
{ |
76 | 0 | pattern = Pattern.compile("\\{[^\\}]+\\}"); |
77 | 0 | pre = 1; |
78 | 0 | post = 1; |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | 0 | throw new IllegalArgumentException("Unknown template style: " + style); |
83 | |
} |
84 | 454 | this.style = style; |
85 | 454 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public String parse(Map props, String template) |
98 | |
{ |
99 | 30 | return parse(props, template, null); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public String parse(TemplateCallback callback, String template) |
113 | |
{ |
114 | 18 | return parse(null, template, callback); |
115 | |
} |
116 | |
|
117 | |
protected String parse(Map props, String template, TemplateCallback callback) |
118 | |
{ |
119 | 48 | String result = template; |
120 | 48 | Matcher m = pattern.matcher(template); |
121 | |
|
122 | 108 | while (m.find()) |
123 | |
{ |
124 | 60 | Object value = null; |
125 | |
|
126 | 60 | String match = m.group(); |
127 | 60 | String propname = match.substring(pre, match.length() - post); |
128 | |
|
129 | 60 | if (callback != null) |
130 | |
{ |
131 | 20 | value = callback.match(propname); |
132 | |
} |
133 | 40 | else if (props != null) |
134 | |
{ |
135 | 40 | value = props.get(propname); |
136 | |
} |
137 | |
|
138 | 60 | if (value == null) |
139 | |
{ |
140 | 4 | if (logger.isDebugEnabled()) |
141 | |
{ |
142 | 0 | logger.debug("Value " + propname + " not found in context"); |
143 | |
} |
144 | |
} |
145 | |
else |
146 | |
{ |
147 | 56 | String matchRegex = escape(match); |
148 | 56 | String valueString = value.toString(); |
149 | |
|
150 | 56 | if (valueString.indexOf('\\') != -1) |
151 | |
{ |
152 | 2 | valueString = valueString.replaceAll("\\\\", "\\\\\\\\"); |
153 | |
} |
154 | |
|
155 | 56 | result = result.replaceAll(matchRegex, valueString); |
156 | |
} |
157 | 60 | } |
158 | 48 | return result; |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public List parse(Map props, List templates) |
171 | |
{ |
172 | 4 | if (templates == null) |
173 | |
{ |
174 | 2 | return new ArrayList(); |
175 | |
} |
176 | 2 | List list = new ArrayList(templates.size()); |
177 | 2 | for (Iterator iterator = templates.iterator(); iterator.hasNext();) |
178 | |
{ |
179 | 4 | list.add(parse(props, iterator.next().toString())); |
180 | |
} |
181 | 2 | return list; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
public Map parse(final Map props, Map templates) |
195 | |
{ |
196 | 4 | return parse(new TemplateCallback(){ |
197 | 4 | public Object match(String token) |
198 | |
{ |
199 | 6 | return props.get(token); |
200 | |
} |
201 | |
}, templates); |
202 | |
} |
203 | |
|
204 | |
public Map parse(TemplateCallback callback, Map templates) |
205 | |
{ |
206 | 4 | if (templates == null) |
207 | |
{ |
208 | 2 | return new HashMap(); |
209 | |
} |
210 | 2 | Map map = new HashMap(templates.size()); |
211 | |
Map.Entry entry; |
212 | 2 | for (Iterator iterator = templates.entrySet().iterator(); iterator.hasNext();) |
213 | |
{ |
214 | 4 | entry = (Map.Entry) iterator.next(); |
215 | 4 | map.put(entry.getKey(), parse(callback, entry.getValue().toString())); |
216 | |
} |
217 | 2 | return map; |
218 | |
} |
219 | |
|
220 | |
private String escape(String string) |
221 | |
{ |
222 | 56 | int length = string.length(); |
223 | 56 | if (length == 0) |
224 | |
{ |
225 | |
|
226 | 0 | return string; |
227 | |
} |
228 | |
else |
229 | |
{ |
230 | 56 | StringBuffer buffer = new StringBuffer(length * 2); |
231 | 628 | for (int i = 0; i < length; i++) |
232 | |
{ |
233 | 572 | char currentCharacter = string.charAt(i); |
234 | 572 | switch (currentCharacter) |
235 | |
{ |
236 | |
case '[' : |
237 | |
case ']' : |
238 | |
case '{' : |
239 | |
case '}' : |
240 | |
case '$' : |
241 | 160 | buffer.append("\\"); |
242 | |
|
243 | |
default : |
244 | 572 | buffer.append(currentCharacter); |
245 | |
} |
246 | |
} |
247 | 56 | return buffer.toString(); |
248 | |
} |
249 | |
} |
250 | |
|
251 | |
public String getStyle() |
252 | |
{ |
253 | 0 | return style; |
254 | |
} |
255 | |
|
256 | |
public boolean isContainsTemplate(String value) |
257 | |
{ |
258 | 96 | if(value==null) return false; |
259 | |
|
260 | 96 | Matcher m = pattern.matcher(value); |
261 | 96 | return m.find(); |
262 | |
} |
263 | |
|
264 | |
public static interface TemplateCallback |
265 | |
{ |
266 | |
Object match(String token); |
267 | |
} |
268 | |
|
269 | |
} |