14. やってみた(1)
静的型チェック可能なMarkupBuilderモドキ
@TypeCheckeddef
test()
{
new
HtmlBuilder().html
{
head
{
title
"Groovy
2.0.1は凄い!"
}
body
{
p
"Groovy
2.0.1は凄い!"
ul
{
li
"リスト1"
ul
"リスト2"
}
h1
"凄いわ!" コンパイル時エラーになる
} →flymake-groovyでデモ
}
} https://gist.github.com/4563201
Slide # 14 JGGUG G*Workshop Copyright(C) 2012 NTT Software Corporation All rights reserved.
13年1月18日金曜日
15. やってみた(2)
import
groovy.transform.*
class
HtmlBuilder
{
def
html(@DelegatesTo(Html)
Closure
c)
{
c.delegate
=
new
Html()
println
"<html>";
c();
println
"</html>"
}
}
class
Html
{
def
head(@DelegatesTo(Head)
Closure
c)
{
c.delegate
=
new
Head()
println
"
<head>";
c();
println
"
</head>"
}
def
body(@DelegatesTo(Body)
Closure
c)
{
c.delegate
=
new
Body()
println
"
<body>";
c();
println
"
</body>"
}
}
class
Head
{
def
title(String
s)
{
println
"
<title>$s</title>"
}
}
class
Body
{
def
h1(String
s)
{
println
"
<h1>$s</h1>"
}
def
p(String
s)
{
println
"
<p>$s</p>"
}
def
ul(@DelegatesTo(Ul)
Closure
c)
{
c.delegate
=
new
Ul()
println
"
<ul>";
c();
println
"
</ul>"
}
}
class
Ul
{
def
li(String
s){
println
"
<li>$s</li>"
}
}
Slide # 15 JGGUG G*Workshop Copyright(C) 2012 NTT Software Corporation All rights reserved.
13年1月18日金曜日