<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns="http://docbook.org/ns/docbook" xmlns:db="http://docbook.org/ns/docbook">
    
    <xsl:include href="common.xsl"/>
    
    <xsl:param name="generateIdPrefix">__sdwordm4342cs__</xsl:param>
    <xsl:output indent="no"/>
    <xsl:param name="lastAnswerId" select="document('lastAnswerId.xml',/)/lastAnswer/@id" />
    
    
    <xsl:key name="internRef" match="//*[@linkend]" use="@linkend"/>
    
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@*[name(.) != 'xml:base'] | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@fileref">
        <xsl:attribute name="fileref">
            <xsl:text>../</xsl:text> <!-- account for descend into "target" subdir -->
            <xsl:if test="ancestor::*/@xml:base">
                <xsl:call-template name="hdm.get.leftmost.of.separator">
                    <xsl:with-param name="path" select="ancestor::*/@xml:base"/>
                    <xsl:with-param name="separator" select="'/'"/>
                </xsl:call-template>                  
                <xsl:text>/</xsl:text>
            </xsl:if>
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>
    
    <xsl:template match="*" mode="hdmIdGenerate">
        <xsl:choose>
            <xsl:when test="@xml:id">
                <xsl:value-of select="@xml:id"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$generateIdPrefix"/>
                <xsl:value-of select="generate-id()"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <!-- 
        <xsl:template match="db:answer">
        <db:answer>
        <xsl:variable name="proximaParentId" select="ancestor::*[@xml:id and position()=1]/@xml:id"/>
        <xsl:choose>
        <xsl:when test="following::*[@xml:id=$lastAnswerId] or ancestor::*[@xml:id=$lastAnswerId]">
        <xsl:apply-templates select="@* | node()"/>
        </xsl:when>
        <xsl:otherwise>
        <para>Answer id=<xsl:value-of select="ancestor::*[@xml:id][1]/@xml:id"/> not yet published.</para>
        </xsl:otherwise>
        </xsl:choose>
        </db:answer>
        </xsl:template>
    --> 
    <xsl:template match="db:part">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
            <xsl:if test=".//db:qandaset">
                <appendix>
                    <title>
                        <xsl:text>List of exercise sets in </xsl:text>
                        <xsl:value-of select="db:title" />
                    </title>
                    
                    <table border="1" xml:id="{@xml:id}solutionsExercise">
                        <caption>Index of exercises</caption>
                        <col width="25%" />
                        <col width="45%" />
                        <col width="30%" />
                        <tr>
                            <th>Chapter</th>
                            <th>Exercise title / page</th>
                            <th>Status of completion</th>
                        </tr>
                        <xsl:for-each select=".//db:chapter[.//db:qandaset]">
                            <xsl:variable name="qandaSets" select=".//db:qandaset"/>
                            <!-- Top level chapter + first exercise's title -->
                            <tr>
                                <td valign="top" rowspan="{count($qandaSets)}">
                                    <xref linkend="{@xml:id}"/>
                                </td>
                                <td> 
                                    <xsl:apply-templates select="$qandaSets[1]" 
                                        mode="selectOwnOrInheritedTitle"/>
                                </td>
                                <td/>
                            </tr>
                            <!-- Remaining exercises -->
                            <xsl:for-each select="$qandaSets[1 &lt; position()]">
                                <tr>
                                    <td> 
                                        <xsl:apply-templates select="." 
                                            mode="selectOwnOrInheritedTitle"/>
                                    </td>
                                    <td/>
                                </tr>
                            </xsl:for-each>
                        </xsl:for-each>                    
                    </table>
                </appendix>
            </xsl:if>
        </xsl:copy>        
    </xsl:template>
    
    <xsl:template match="db:qandaset" mode="selectOwnOrInheritedTitle">
        <link linkend="{@xml:id}"> <!--  xrefstyle="template:%n, %t" -->            
            <xsl:choose>
                <xsl:when test="db:title">
                    <xsl:copy-of select="db:title/node()"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="preceding-sibling::db:title[1]/node()"/>
                </xsl:otherwise>
            </xsl:choose>
        </link>
    </xsl:template>
    
    <!-- Excluding non-referenced <bibliography> and <glossary> elements -->
    
    <xsl:template match="db:bibliography/db:biblioentry[not(key('internRef', @xml:id))]">
        <xsl:comment>
            <xsl:text>Excluding non referenced bibliography entry '</xsl:text>
            <xsl:value-of select="db:abbrev"/>
            <xsl:text>', @xml:id='</xsl:text>
            <xsl:value-of select="@xml:id"/>
            <xsl:text>'</xsl:text>
        </xsl:comment>
    </xsl:template>
    
    <xsl:template match="db:glossary//db:glosslist">
        
        <xsl:choose>
            <xsl:when test=".//db:glossentry[key('internRef', @xml:id) or key('internRef', .//*/@xml:id)]">
                <xsl:comment>Non-empty glosslist</xsl:comment>
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()"/>
                </xsl:copy>
            </xsl:when>
            <xsl:otherwise>
              <xsl:comment>
                <xsl:text>No @xml:id or all glosslist entries are not being referenced, excluding whole list.</xsl:text>
              </xsl:comment>
              <xsl:apply-templates select="db:glossentry"/>        
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="db:glossary/db:glosslist//db:glossentry[not(key('internRef', @xml:id)) and not(key('internRef', .//*/@xml:id))]">
        <xsl:comment>
            <xsl:text>Excluding non referenced glossary entry '</xsl:text>
            <xsl:value-of select="db:glossterm"/>
            <xsl:text>', @xml:id='</xsl:text>
            <xsl:value-of select="@xml:id"/>
            <xsl:text>'</xsl:text>
        </xsl:comment>
    </xsl:template>
    
    <!-- 
        <xsl:template match="db:bibliography">
        <db:bibliography>
        <xsl:apply-templates select="*[not(name(.) = 'biblioentry')]|db:biblioentry[key('internRef', @xml:id)]"/>
        </db:bibliography>
        </xsl:template>
        
        
        <xsl:template match="db:glossary/db:glosslist">
        <db:glosslist>
        <xsl:apply-templates select="*[not(name(.) = 'glossentry')]|db:glossentry[key('internRef', @xml:id)]"/>
        </db:glosslist>
        </xsl:template>
    -->
    
    <!-- make annotations -->
    
    <xsl:template match="db:annotation[@role='make']">
        <xsl:apply-templates select="*" mode="make"/>
    </xsl:template>
    
    <xsl:template match="db:para[@role='eclipse']" mode='make'>
        
        <xsl:variable name="baseDir">
            <xsl:text>P/</xsl:text>
            <xsl:value-of select="."/>
        </xsl:variable>
        
        <itemizedlist>
            <listitem>
                <para>
                    <xsl:text>Maven module source code available at sub directory </xsl:text> <filename>
                        <xsl:value-of select="$baseDir"/>
                    </filename> <xsl:text> below lecture notes' source code root, see </xsl:text> <link linkend="sd1ImportMavenSolutions"> hints regarding import.</link></para>
            </listitem>
            
            <listitem>
                <para>Online browsing of <link xlink:href="{$baseDir}/target/site/apidocs/allclasses-noframe.html">API and implementation</link>.</para>
            </listitem>
        </itemizedlist>
    </xsl:template>
    
    <xsl:template match="*" mode="make">
        <db:para>
            <xsl:text>No template of mode='make' defined for element'</xsl:text>
            <xsl:value-of select="name(.)"/>
            <xsl:text>'</xsl:text>
        </db:para>
    </xsl:template>
    
</xsl:stylesheet>