groovyでstruts-config.xmlをhtml変換

struts-config.xmlの内容のうち、特にaction-mappingsについてはチェックする機会が多い。もうちょっと見やすくならないかな、ということで、htmlに変換してみた。(xslt使うのが本来のやり方なのかもしれないけれども、そこはGroovy中毒患者。)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

import groovy.xml.MarkupBuilder
import org.custommonkey.xmlunit.*


final xmlSlurper = new XmlSlurper()
def config = xmlSlurper.parseText(new File(args[0]).text.replaceFirst(/<!DOCTYPE(?ms)[^>]+>/, ""))
def actions = config."action-mappings".action

def html = new MarkupBuilder()
html.html() {
    head{
        link(
            rel:"stylesheet",
            type:"text/css",
            href:"style.css",
            )
        style{
            def i = 0
            actions.@path*.toString().collect{ it.split("/")[1] }.unique().each{
                yieldUnescaped """
.${it}{
    background-color: #${(1..6).collect{("A".."F")[(int)(Math.random()*6)]}.join("")};
}
"""
            }
        }
    }
    body{
        table{
            caption("action-mappings")
            thead{
                tr{
                    th("path")
                    th("type")
                    th("forward")
                    th("scope")
                }
            }
            tbody{
                actions.each{ action ->
                    tr(class:"${action.@path.toString().split("/")[1]}"){
                        td(action.@path)
                        td(action.@type)
                        td(style:"text-align:left;"){
                            ul{
                                action.forward.each{
                                    li(it.@name)
                                }
                            }
                        }
                        td(action.@scope)
                    }
                }
            }
        }
    }
}

これに、適当なcssファイルを付け加えてみる。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

table {
    width:90%;
    border-top:3px solid #e5eff8;
    border-right:1px solid #e5eff8;
    margin:1em auto;
    border-collapse:collapse;
}
table caption{
    font-size: 150%;
    font-weight: bold;
    border :2px solid gray;
    text-valign: middle;
}
td {
    color:#5B4167;
    border-bottom:1px solid #e5eff8;
    border-left:1px solid #e5eff8;
    padding:.3em 1em;
    text-align:center;
}


thead th {
    background:#f4f9fe;
    text-align:center;
    font:bold 1.2em/2em "Century Gothic","Trebuchet MS",Arial,Helvetica,sans-serif;
    color:#66a3d3;
}

いい加減、struts 1.Xなプロジェクト、抜けたい。でも現在保守作業中なので当分抜けられないんだろうな。