Revisão | 5a6a881cd8e38b18789cb5b0d9a4d7a87a88f0b5 (tree) |
---|---|
Hora | 2014-01-02 09:44:55 |
Autor | takahiro <takahiro@192....> |
Commiter | takahiro |
TemplateEngine追加
@@ -38,5 +38,10 @@ | ||
38 | 38 | <artifactId>slf4j-api</artifactId> |
39 | 39 | <version>1.6.4</version> |
40 | 40 | </dependency> |
41 | + <dependency> | |
42 | + <groupId>io.vertx</groupId> | |
43 | + <artifactId>vertx-core</artifactId> | |
44 | + <version>2.1M2</version> | |
45 | + </dependency> | |
41 | 46 | </dependencies> |
42 | 47 | </project> |
@@ -0,0 +1,28 @@ | ||
1 | +package org.infodb.junk; | |
2 | + | |
3 | +import java.util.Map; | |
4 | +import java.util.regex.Matcher; | |
5 | +import java.util.regex.Pattern; | |
6 | +import org.vertx.java.core.buffer.Buffer; | |
7 | + | |
8 | +public class TemplateEngine { | |
9 | + private final Pattern pattern; | |
10 | + | |
11 | + public TemplateEngine() { | |
12 | + pattern = Pattern.compile("\\{(\\w+)\\}"); | |
13 | + } | |
14 | + | |
15 | + public Buffer execute(String source, Map<String, String> params) { | |
16 | + Matcher matcher = pattern.matcher(source); | |
17 | + Buffer buffer = new Buffer(); | |
18 | + int lastEnd = 0; | |
19 | + while(matcher.find()) { | |
20 | + buffer.appendString(source.substring(lastEnd, matcher.start())); | |
21 | + lastEnd = matcher.end(); | |
22 | + buffer.appendString(params.get(matcher.group(1))); | |
23 | + } | |
24 | + buffer.appendString(source.substring(lastEnd)); | |
25 | + return buffer; | |
26 | + } | |
27 | + | |
28 | +} |