Commit 3bf84d5c authored by Naoki Kambe's avatar Naoki Kambe
Browse files

[917] implement stats_spec2xsl function and modify the xsl template file and unittest

parent 12a6217c
......@@ -17,7 +17,7 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
xmlns:stats="$xsd_namespace">
xmlns:bind10="$xsd_namespace">
<xsl:output method="html" encoding="UTF-8"
doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
doctype-system=" http://www.w3.org/TR/html4/loose.dtd " />
......@@ -42,14 +42,7 @@ td.title {
</head>
<body>
<h1>BIND 10 Statistics</h1>
<table>
<tr>
<th>Owner</th>
<th>Title</th>
<th>Value</th>
</tr>
<xsl:apply-templates />
</table>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
......
......@@ -485,15 +485,15 @@ class StatsHttpd:
if type(stats_spec) is dict:
# assumed module_spec
if 'item_name' not in stats_spec:
alltag = xml.etree.ElementTree.Element("all")
for mod in stats_spec.keys():
stats_spec2xsd(stats_spec[mod], alltag)
complextype = xml.etree.ElementTree.Element("complexType")
complextype.append(alltag)
elem = xml.etree.ElementTree.Element(
"element", { "name" : mod })
elem.append(complextype)
xsd_elem.append(elem)
alltag = xml.etree.ElementTree.Element("all")
for mod in stats_spec.keys():
stats_spec2xsd(stats_spec[mod], alltag)
complextype = xml.etree.ElementTree.Element("complexType")
complextype.append(alltag)
elem = xml.etree.ElementTree.Element(
"element", { "name" : mod })
elem.append(complextype)
xsd_elem.append(elem)
# assumed stats_spec
else:
if stats_spec['item_type'] == 'map':
......@@ -583,29 +583,80 @@ class StatsHttpd:
def xsl_handler(self, module_name=None, item_name=None):
"""Handler which just returns the body of XSL document"""
def stats_spec2xsl(stats_spec, xsl_elem):
"""Internal use for xsl_handler. Reads stats_spec
specified as first arguments, and modify the xml object
specified as second argument. xsl_elem must be
modified. Always returns None with no exceptions."""
# assumed module_spec or one stats_spec
if type(stats_spec) is dict:
# assumed module_spec
if 'item_name' not in stats_spec:
table = xml.etree.ElementTree.Element("table")
tr = xml.etree.ElementTree.Element("tr")
th = xml.etree.ElementTree.Element("th")
th.text = "Module Names"
tr.append(th)
th = xml.etree.ElementTree.Element("th")
th.text = "Items"
tr.append(th)
table.append(tr)
for mod in stats_spec.keys():
foreach = xml.etree.ElementTree.Element(
"xsl:for-each", attrib={ "select" : mod })
tr = xml.etree.ElementTree.Element("tr")
td = xml.etree.ElementTree.Element("td")
td.text = mod
tr.append(td)
td = xml.etree.ElementTree.Element("td")
stats_spec2xsl(stats_spec[mod], td)
tr.append(td)
foreach.append(tr)
table.append(foreach)
xsl_elem.append(table)
# assumed stats_spec
else:
#tr = xml.etree.ElementTree.Element("tr")
td = xml.etree.ElementTree.Element("td")
td.text = stats_spec["item_name"]
xsl_elem.append(td)
td = xml.etree.ElementTree.Element("td")
if stats_spec['item_type'] == 'map':
stats_spec2xsl(stats_spec['map_item_spec'], td)
elif stats_spec['item_type'] == 'list':
stats_spec2xsl(stats_spec['list_item_spec'], td)
else:
xsl_valueof = xml.etree.ElementTree.Element(
"xsl:value-of",
attrib={'select': stats_spec["item_name"]})
td.append(xsl_valueof)
#tr.append(td)
xsl_elem.append(td)
# multiple stats_specs
elif type(stats_spec) is list:
table = xml.etree.ElementTree.Element("table")
tr = xml.etree.ElementTree.Element("tr")
th = xml.etree.ElementTree.Element("th")
th.text = "Item Names"
tr.append(th)
th = xml.etree.ElementTree.Element("th")
th.text = "Values"
tr.append(th)
table.append(tr)
tr = xml.etree.ElementTree.Element("tr")
for item_spec in stats_spec:
stats_spec2xsl(item_spec, tr)
table.append(tr)
xsl_elem.append(table)
return None
# for XSL
xsd_root = xml.etree.ElementTree.Element(
stats_spec = self.get_stats_spec(module_name, item_name)
xsd_root = xml.etree.ElementTree.Element( # started with xml:template tag
"xsl:template",
dict(match="*")) # started with xml:template tag
for (mod, spec) in self.get_stats_spec(module_name, item_name).items():
if not spec: continue
for item in spec:
tr = xml.etree.ElementTree.Element("tr")
td0 = xml.etree.ElementTree.Element("td")
td0.text = str(mod)
td1 = xml.etree.ElementTree.Element(
"td", { "class" : "title",
"title" : item["item_description"] })
td1.text = item["item_title"]
td2 = xml.etree.ElementTree.Element("td")
xsl_valueof = xml.etree.ElementTree.Element(
"xsl:value-of",
dict(select=mod+'/'+item["item_name"]))
td2.append(xsl_valueof)
tr.append(td0)
tr.append(td1)
tr.append(td2)
xsd_root.append(tr)
attrib={'match': "bind10:statistics"})
stats_spec2xsl(stats_spec, xsd_root)
# The coding conversion is tricky. xml..tostring() of Python 3.2
# returns bytes (not string) regardless of the coding, while
# tostring() of Python 3.1 returns a string. To support both
......
......@@ -773,11 +773,7 @@ class TestStatsHttpd(unittest.TestCase):
}
xsl_body1 = self.stats_httpd.open_template(
stats_httpd.XSL_TEMPLATE_LOCATION).substitute(
xsl_string='<xsl:template match="*"><tr>' \
+ '<td>Dummy</td>' \
+ '<td class="title" title="foo is bar">Foo</td>' \
+ '<td><xsl:value-of select="Dummy/foo" /></td>' \
+ '</tr></xsl:template>',
xsl_string='<xsl:template match="bind10:statistics"><table><tr><th>Module Names</th><th>Items</th></tr><xsl:for-each select="Dummy"><tr><td>Dummy</td><td><table><tr><th>Item Names</th><th>Values</th></tr><tr><td>foo</td><td><xsl:value-of select="foo" /></td></tr></table></td></tr></xsl:for-each></table></xsl:template>',
xsd_namespace=stats_httpd.XSD_NAMESPACE)
xsl_body2 = self.stats_httpd.xsl_handler()
self.assertEqual(type(xsl_body1), str)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment