JMustache のサイトからダウンロードした samskivert-jmustache-jmustache-1.3-8-g7255cb7.zip から Mustache.java, MustacheException.java, MustacheParseException.java, Template.java のソースコード4ファイルを取り出して使う。外部ライブラリに依存していないので簡単に導入できる。ちなみに JMustache のライセンスは BSD License っぽい。
環境: Windows XP + Eclipse 3.4 + Java SE 6
いくつかサンプルコードを書いてみる。
まずは、JMustache のサイトにあったシンプルなサンプルを書いて実行。
メイン処理は Mustache.compiler().compile(String) だけなのでお手軽。
JMustacheSample1 サンプルコード:
import java.util.*;
import com.samskivert.mustache.*;
public class JMustacheSample1 {
public static void main(String[] args) {
String text = "One, two, {{three}}. Three sir!";
Template tmpl = Mustache.compiler().compile(text);
Map<String, String> data = new HashMap<String, String>();
data.put("three", "five");
System.out.println(tmpl.execute(data));
}
}
JMustacheSample1 実行結果:
One, two, five. Three sir!
次に、テンプレートがテキストファイルだったパターンを想定して、 Reader と Writer を使って処理してみる。
JMustacheSample2 サンプルコード:
import java.io.*;
import java.util.*;
import com.samskivert.mustache.*;
public class JMustacheSample2 {
public static void main(String[] args) {
Map<String, String> data = new HashMap<String, String>();
data.put("three", "five");
StringReader template = new StringReader("One, two, {{three}}. Three sir!");
StringWriter out = new StringWriter();
Mustache.compiler().compile(template).execute(data, out);
System.out.println(out.toString());
}
}
JMustacheSample2 実行結果:
One, two, five. Three sir!
次は、クラスの getter を利用するサンプル。
JMustacheSample3 サンプルコード:
import java.util.*;
import com.samskivert.mustache.Mustache;
public class JMustacheSample3 {
public static void main(String[] args) {
String tmpl = "{{#persons}}{{name}}: {{age}}\n{{/persons}}\n";
Data data = new Data();
data.persons = Arrays.asList(
new Person("Elvis", 75),
new Person("Madonna", 52));
String result = Mustache.compiler().compile(tmpl).execute(data);
System.out.println(result);
}
public static class Data {
List<Person> persons;
}
public static class Person {
public final String name;
public Person(String name, int age) {
this.name = name;
_age = age;
}
public int getAge() {
return _age;
}
protected int _age;
}
}
JMustacheSample3 実行結果:
Elvis: 75
Madonna: 52
最後のサンプルは、Java の Map と 配列 がどんなふうにテンプレートに埋め込めるのか試してみた。
以前に Ruby の Mustache ライブラリで試したやつ の Java バージョン。
JMustacheSample4 サンプルコード:
import java.util.*;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;
public class JMustacheSample4 {
public static void main(String[] args) {
StringBuffer temp = new StringBuffer();
temp.append("d: {{d}}\n");
temp.append("d.h: {{d.h}}\n");
temp.append("d.h.symb: {{d.h.symb}}\n");
temp.append("d.h.name: {{d.h.name}}\n");
temp.append("d.a: {{d.a}}\n");
temp.append("#d.a:\n");
temp.append("{{#d.a}}\n");
temp.append("{{.}}\n");
temp.append("{{/d.a}}\n");
temp.append("/d.a:\n");
temp.append("d.ah: {{d.ah}}\n");
temp.append("#d.ah:\n");
temp.append("{{#d.ah}}\n");
temp.append("{{.}}\n");
temp.append("{{/d.ah}}\n");
temp.append("/d.ah:\n");
temp.append("#d.ah:\n");
temp.append("{{#d.ah}}\n");
temp.append(" d.ah.x: {{x}}\n");
temp.append(" d.ah.y: {{y}}\n");
temp.append("{{/d.ah}}\n");
temp.append("/d.ah:\n");
Map _h = new HashMap();
_h.put("symb", "symbhoge");
_h.put("name", "namehoge");
Object[] _a = new String[] { "item1", "item2" };
Map[] _ah = new HashMap[] { new HashMap(), new HashMap() };
_ah[0].put("x", "1x");
_ah[0].put("y", "1y");
_ah[1].put("x", "2x");
_ah[1].put("y", "2y");
final D _d = new D(); // 無名クラスから参照できるようにfinal指定
_d.h = _h;
_d.a = _a;
_d.ah = _ah;
Object data = new Object() {
Object d = _d;
};
Mustache.Compiler compiler = Mustache.compiler();
Template template = compiler.compile(temp.toString());
String result = template.execute(data);
System.out.println(result);
}
public static class D {
Object h;
Object a;
Object ah;
}
}
JMustacheSample4 実行結果:
d: JMustacheSample4$D@863399
d.h: {name=namehoge, symb=symbhoge}
d.h.symb: symbhoge
d.h.name: namehoge
d.a: [Ljava.lang.String;@16a55fa
#d.a:
item1
item2
/d.a:
d.ah: [Ljava.util.HashMap;@e89b94
#d.ah:
{y=1y, x=1x}
{y=2y, x=2x}
/d.ah:
#d.ah:
d.ah.x: 1x
d.ah.y: 1y
d.ah.x: 2x
d.ah.y: 2y
/d.ah:
こうやって実際に使ってみた感じでは、Java + JMustache は HashMap と配列でけっこうお気楽に使える。
以下、余談。
この JMustache のサイトにはこんなことが書かれている。
This is a Java implementation of the Mustache template language. There exists another Java implementation of Mustache, but the motivations for this version are sufficiently different as to justify (in the author's mind, anyhow) the duplication.
samskivert/jmustache - GitHub
すでに Mustache.java というライブラリが世の中にはあるのにわざわざ JMustache を作ったとのこと。
Mustache.java は {{ mustache }} の公式サイトからリンクされている唯一の Java ライブラリ。
しかし、これがどうもシンプルでなく使いにくい。
com.google.common (Guava: Google Core Libraries for Java 1.5+) とか org.codehaus.jackson (Jackson JSON Processor) とかに依存しているので、まずはそのへんのライブラリを揃える必要があったり、サンプルコードがぜんぜん無いし、coreディレクトリのコードだけで足りるのか足りないのかいまいちわかりにくい。
JSONのデータ構造にも対応していたりたぶん高機能だと思うが、ちょっと敷居が高いので自分はさっくりあきらめた。
というわけで、シンプルに使いたいなら Mustache.java よりも JMustache のほうが良いと思う。
Ref.
- {{ mustache }}
- samskivert/jmustache - GitHub
- ヅラッシュ! - プログラミング言語に依存しないテンプレートエンジン仕様 (・w・)
- ヅラッシュ! - Ruby のハッシュを Mustache オブジェクトのルートとして追加する (・w・)
tags: mustache java
Posted by NI-Lab. (@nilab)