<!-- 
     var-service-system-status.xsl

     This stylesheet can be run on DataPower box, or off box by other XSLT
     processors. In case running on DataPower box, the status of the domain
     containing the service running this stylesheet gets processed, otherwise
     the provided status of the XML input.

     Input is expected to be of the form:
     <rootElem>
       <elemA><colA1>...</colA1><colA2>...</colA2>...<colAn>...</colAn></elemA>
       ...
       <elemB><colB1>...</colB1><colB2>...</colB2>...<colBn>...</colBm></elemB>
       ...
       <elemA><colA1>...</colA1><colA2>...</colA2>...<colAn>...</colAn></elemA>
       ...
     </rootElem>

     This stylesheet groups <rootElem> children by element name, and
     displays <h2> sections with HTML tables for each group.

     elmenA, elemB, ... will be the <h2> header, <colXy> will be the column
     names in the different tables.

     Lable+Value tables will be generated for single elemnts, "normal" 
     tables for elements occurring multiple times.

     The only special handling done is special sorting for ObjectStatus table.
-->
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dp="http://www.datapower.com/extensions"
  extension-element-prefixes="dp"
>
  <!-- map element to its name() -->
  <xsl:key name="b" match="*" use="name()"/>

  <!-- Are we running on DataPower box? -->
  <xsl:template match="/">
    <xsl:choose>
      <!-- if yes, query sytem status and process -->
      <xsl:when test="function-available('dp:variable')">
        <xsl:apply-templates 
          select="dp:variable('var://service/system/status')/statistics"/>
      </xsl:when>

      <!-- if no, process XML data provided (root element) -->
      <xsl:otherwise>
        <xsl:apply-templates select="*"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- process the root element -->
  <xsl:template match="/*">
    <html>  
      <!-- if not running on DataPower box -->
      <xsl:if test="not(function-available('dp:variable'))">
        <head> 
          <title>var://service/system/status</title> 
        </head>
        <body>
          <h1>var://service/system/status</h1>

          <xsl:call-template name="tables"/>
        </body>
      </xsl:if>

      <!-- if running on DataPower box -->
      <xsl:if test="function-available('dp:variable')">
        <!-- determine refresh value -->
        <xsl:variable name="refresh">
          <xsl:variable name="URI" select="dp:variable('var://service/URI')"/>
          <xsl:variable name="query" 
            select="substring-after($URI,'?refresh=')"/>
          <xsl:choose>
            <xsl:when test="not($query)">5</xsl:when>
            <xsl:otherwise><xsl:value-of select="$query"/></xsl:otherwise>
          </xsl:choose>
        </xsl:variable>

        <!-- the refresh mechanism -->
        <head> 
          <title>var://service/system/status</title> 

          <meta http-equiv="expires" content="0"/>
          <meta http-equiv="refresh" content="{$refresh}"/>
          <meta http-equiv="content-type" content="text/html"/>
        </head>
        <body>
          <a name="top"/>
          <h1>var://service/system/status</h1>

          <!-- allow changing the refresh interval length -->
          <table border="1">
            <tr>
              <td>refresh in [s] </td>
              <td valign="bottom">
                <!-- provide selection of different refresh values -->
                <form name="refreshForm" action="" method="get">
                  <xsl:for-each select="$delays/d">
                    <input type="radio" name="refresh" value="{.}" 
                      onClick="document.refreshForm.submit()">
  
                      <xsl:if test=".=$refresh">
                        <xsl:attribute name="checked"/>
                      </xsl:if>
  
                      <xsl:value-of select="."/>
                    </input>
                  </xsl:for-each>
                </form>
              </td>
            </tr>
          </table>
                
          <xsl:call-template name="tables"/>
        </body>
      </xsl:if>
    </html>
  </xsl:template>

  <!-- generate all tables -->
  <xsl:template name="tables">
    <!-- for each group of same name() children ... -->
    <xsl:for-each 
      select="*[generate-id(.)=generate-id(key('b',name())[1])]">

      <!-- ... produce h2 and HTML table output (sorted) -->
      <xsl:sort select="name()"/>

      <h2>&lt;<xsl:value-of select="name()"/>&gt;</h2>

      <xsl:apply-templates select="."/>
    </xsl:for-each>
  </xsl:template>

  <!-- Generate table of header+value rows (for single elements) -->
  <xsl:template match="*">
    <table border="1" cellspacing="0">
      <xsl:for-each select="*">
        <tr>
          <th bgcolor="cyan"> <xsl:copy-of select="name()"/> </th>

          <td> <xsl:value-of select="."/> </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <!-- Generate normal table (for elements occuring multiple times) -->
  <xsl:template match="*[count(key('b',name())) > 1]">
    <table border="1" cellspacing="0">

      <!-- Generate table header cells -->
      <tr>
        <xsl:for-each select="*">
          <th bgcolor="cyan"> <xsl:copy-of select="name()"/> </th>
        </xsl:for-each>
      </tr>

      <xsl:apply-templates select="." mode="rows"/>

      <!-- Generate table header cells again (bottom) -->
      <tr>
        <xsl:for-each select="*">
          <th bgcolor="cyan"> <xsl:copy-of select="name()"/> </th>
        </xsl:for-each>
      </tr>
    </table>
  </xsl:template>

  <!-- do extra sorting by 1st and 4th columns for ObjectStatus -->
  <xsl:template match="ObjectStatus" mode="rows">

    <!-- Generate rows for each element of same name() -->
    <xsl:for-each select="key('b',name())">

      <!-- sort by Class and Nmae column -->
      <xsl:sort select="*[1]"/>
      <xsl:sort select="*[4]"/>

      <tr>
        <xsl:for-each select="*">
          <td>
            <xsl:if test="position()=1 or position()=4">
              <xsl:attribute name="bgcolor">#e0e0e0</xsl:attribute> 
            </xsl:if>

            <xsl:value-of select="."/>
          </td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </xsl:template>

  <!-- no extra sorting for elements different to ObjectStatus -->
  <xsl:template match="*" mode="rows">

    <!-- Generate rows for each element of same name() -->
    <xsl:for-each select="key('b',name())">
      <tr>
        <xsl:for-each select="*">
          <td> <xsl:copy-of select="."/> </td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </xsl:template>

  <!-- define available delay values -->
  <xsl:variable name="delays">
    <d>0</d><d>1</d><d>2</d><d>5</d><d>10</d><d>60</d>
  </xsl:variable>

</xsl:stylesheet>
