return self.xsd_body
def xsl_handler(self, module_name=None, item_name=None):
- """Requests the specified statistics specification by using
- the function get_stats_spec respectively and loads the XSL
- template file and returns the string of the XSL document.The
- first argument is the module name which owns the statistics
- data, the second argument is one name of the statistics items
- which the the module owns. The second argument cannot be
- specified when the first argument is not specified."""
-
- # TODO: Separate the following recursive function by type of
- # the parameter. Because we should be sure what type there is
- # when we call it recursively.
- def stats_spec2xsl(stats_spec, xsl_elem, path=XML_URL_PATH):
- """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. The third argument is a base path used for
- making anchor tag in XSL. 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 Name"
- tr.append(th)
- th = xml.etree.ElementTree.Element("th")
- th.text = "Module Item"
- 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")
- a = xml.etree.ElementTree.Element(
- "a", attrib={ "href": urllib.parse.quote(path + "/" + mod) })
- a.text = mod
- td.append(a)
- tr.append(td)
- td = xml.etree.ElementTree.Element("td")
- stats_spec2xsl(stats_spec[mod], td,
- path + "/" + mod)
- tr.append(td)
- foreach.append(tr)
- table.append(foreach)
- xsl_elem.append(table)
- # assumed stats_spec
- else:
- if stats_spec['item_type'] == 'map':
- table = xml.etree.ElementTree.Element("table")
- tr = xml.etree.ElementTree.Element("tr")
- th = xml.etree.ElementTree.Element("th")
- th.text = "Item Name"
- tr.append(th)
- th = xml.etree.ElementTree.Element("th")
- th.text = "Item Value"
- tr.append(th)
- table.append(tr)
- foreach = xml.etree.ElementTree.Element(
- "xsl:for-each", attrib={ "select" : stats_spec['item_name'] })
- tr = xml.etree.ElementTree.Element("tr")
- td = xml.etree.ElementTree.Element(
- "td",
- attrib={ "class" : "title",
- "title" : stats_spec["item_description"] \
- if "item_description" in stats_spec \
- else "" })
- # TODO: Consider whether we should always use
- # the identical name "item_name" for the
- # user-visible name in XSL.
- td.text = stats_spec[ "item_title" if "item_title" in stats_spec else "item_name" ]
- tr.append(td)
- td = xml.etree.ElementTree.Element("td")
- stats_spec2xsl(stats_spec['map_item_spec'], td,
- path + "/" + stats_spec["item_name"])
- tr.append(td)
- foreach.append(tr)
- table.append(foreach)
- xsl_elem.append(table)
- elif stats_spec['item_type'] == 'list':
- stats_spec2xsl(stats_spec['list_item_spec'], xsl_elem,
- path + "/" + stats_spec["item_name"])
- else:
- xsl_valueof = xml.etree.ElementTree.Element(
- "xsl:value-of",
- attrib={'select': stats_spec["item_name"]})
- xsl_elem.append(xsl_valueof)
-
- # 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 Name"
- tr.append(th)
- th = xml.etree.ElementTree.Element("th")
- th.text = "Item Value"
- tr.append(th)
- table.append(tr)
- for item_spec in stats_spec:
- tr = xml.etree.ElementTree.Element("tr")
- td = xml.etree.ElementTree.Element(
- "td",
- attrib={ "class" : "title",
- "title" : item_spec["item_description"] \
- if "item_description" in item_spec \
- else "" })
- # if the path length is equal to or shorter than
- # XML_URL_PATH + /Module/Item, add the anchor tag.
- if len(path.split('/')) <= len((XML_URL_PATH + '/Module/Item').split('/')):
- a = xml.etree.ElementTree.Element(
- "a", attrib={ "href": urllib.parse.quote(path + "/" + item_spec["item_name"]) })
- a.text = item_spec[ "item_title" if "item_title" in item_spec else "item_name" ]
- td.append(a)
- else:
- td.text = item_spec[ "item_title" if "item_title" in item_spec else "item_name" ]
- tr.append(td)
- td = xml.etree.ElementTree.Element("td")
- stats_spec2xsl(item_spec, td, path)
- tr.append(td)
- if item_spec['item_type'] == 'list':
- foreach = xml.etree.ElementTree.Element(
- "xsl:for-each", attrib={ "select" : item_spec['item_name'] })
- foreach.append(tr)
- table.append(foreach)
- else:
- table.append(tr)
- xsl_elem.append(table)
-
- # for XSL
- stats_spec = self.get_stats_spec(module_name, item_name)
- xsd_root = xml.etree.ElementTree.Element( # started with xml:template tag
- "xsl:template",
- 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
- # cases transparently, we first make sure tostring() returns
- # bytes by specifying utf-8 and then convert the result to a
- # plain string (code below assume it).
- # FIXME: Non-ASCII characters might be lost here. Consider how
- # the whole system should handle non-ASCII characters.
- xsl_string = str(xml.etree.ElementTree.tostring(xsd_root, encoding='utf-8'),
- encoding='us-ascii')
+ """Loads the XSL template file, replaces the variable strings,
+ and returns the string of the XSL document."""
self.xsl_body = self.open_template(XSL_TEMPLATE_LOCATION).substitute(
- xsl_string=xsl_string,
xsd_namespace=XSD_NAMESPACE)
- assert self.xsl_body is not None
return self.xsl_body
def open_template(self, file_name):