<?xml version="1.0"?>

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:str="http://xsltsl.org/string">

  <xsl:import href="xsltsl-1.1/stdlib.xsl"/>

  <xsl:output
    method="xml"
    indent="yes"/>

  <xsl:template match="/">
    <fo:root font-family="Palatino Linotype">

      <fo:layout-master-set>
        <!-- title page -->
        <fo:simple-page-master master-name="title-page"
            page-height="29.7cm"
            page-width="21cm"
            margin-top="4cm"
            margin-bottom="1cm"
            margin-left="1.5cm"
            margin-right="1.5cm">
          <fo:region-body margin-top="2cm"/>
          <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>

        <!-- rest of document page -->
        <fo:simple-page-master master-name="body-page"
	    		  page-height="29.7cm"
            page-width="21cm"
            margin-top="1cm"
            margin-bottom="1cm"
            margin-left="2.5cm"
            margin-right="2.5cm">
          <fo:region-body margin-top="2cm" margin-bottom="2.70cm"/>
          <fo:region-before extent="2cm"/>
          <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <xsl:call-template name="title-page"/>
      <xsl:call-template name="toc"/>
      <fo:page-sequence master-reference="body-page">
        <fo:static-content flow-name="xsl-region-before">
          <fo:block text-align="center"
              font-size="10pt"
              font-family="serif">
            Literature review
          </fo:block>
        </fo:static-content>

        <fo:static-content flow-name="xsl-region-after">
          <fo:block text-align="right"
              font-size="10pt"
              font-family="serif">
            <fo:page-number/>
          </fo:block>
        </fo:static-content>
        
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates select="document/sections/section | document/sections/bibliography"/>
        </fo:flow>
      </fo:page-sequence>

    </fo:root>
  </xsl:template>

  <xsl:template name="title-page">
    <fo:page-sequence master-reference="title-page">
      <fo:static-content flow-name="xsl-region-after">
        <fo:block text-align="end" font-size="8pt">
          Generated from an XML document using XSLT and XSL FO.
        </fo:block>
        <fo:block text-align="end" font-size="8pt">
          The latest version is available at
          <fo:inline color="blue" text-decoration="underline">
            http://www.csse.monash.edu.au/~clm/uni/honours/#litreview</fo:inline>.
        </fo:block>
      </fo:static-content>
      
      <fo:flow flow-name="xsl-region-body">

        <fo:block text-align="center">

          <fo:block
              font-weight="bold" 
              text-decoration="underline" 
              padding-bottom="1em">
            <fo:block>School of Computer Science and Software Engineering</fo:block>
            <fo:block>Monash University</fo:block>
          </fo:block>
        
          <fo:block padding-bottom="1em">
            Bachelor of Computer Science Honours (1608), Clayton Campus
          </fo:block>

          <fo:block padding-bottom="4em">
            Literature Review <!--&#x2014; Semester 1, 2002-->
          </fo:block>

          <fo:block font-size="16pt" font-weight="bold">
            <xsl:apply-templates select="/document/meta/title"/>
          </fo:block>
          <fo:block padding-bottom="1em" font-size="14pt" font-style="italic">
            <xsl:apply-templates select="/document/meta/subtitle"/>
          </fo:block>

          <fo:block padding-bottom="1em">
            <xsl:apply-templates select="/document/meta/student-name"/>
            (<xsl:apply-templates select="/document/meta/student-id"/>)
          </fo:block>

          <fo:block padding-bottom="1em">
            Supervisor: <xsl:apply-templates select="/document/meta/supervisor"/>
          </fo:block>

        </fo:block>

      </fo:flow>
    </fo:page-sequence>
  </xsl:template>

  <xsl:template name="toc">
    <fo:page-sequence master-reference="body-page">
      <fo:static-content flow-name="xsl-region-before">
        <fo:block text-align="center"
            font-size="10pt"
            font-family="serif">
          Literature review
        </fo:block>
      </fo:static-content>

      <fo:static-content flow-name="xsl-region-after">
        <fo:block text-align="right"
            font-size="10pt"
            font-family="serif">
          <fo:page-number/>
        </fo:block>
      </fo:static-content>
      
      <fo:flow flow-name="xsl-region-body">
        <fo:block font-size="16pt" font-weight="bold" space-after="1em">
          Table of contents
        </fo:block>

        <!--xsl:for-each select="/document/sections/section">
          <fo:block text-align-last="justify" end-indent="2pc" last-line-end-indent="-2pc">
            <fo:inline keep-with-next.within-line="always">
              <xsl:value-of select="@title"/>
            </fo:inline>
            <fo:inline keep-together.within-line="always">
              <fo:leader leader-pattern="dots" keep-with-next.within-line="always"/>
              <fo:page-number-citation ref-id="{generate-id(.)}"/>
            </fo:inline>
          </fo:block>
          <fo:block>
            <xsl:value-of select="@title"/> ... <fo:page-number-citation ref-id="{generate-id(.)}"/>
          </fo:block>
        </xsl:for-each-->

        <xsl:for-each select="/document/sections/section | /document/sections/bibliography">
          <fo:block space-after="1em">
            <fo:inline>
              <xsl:number value="position()"/>.
              <xsl:choose>
                <xsl:when test="name() = 'bibliography'">Bibliography</xsl:when>
                <xsl:otherwise><xsl:apply-templates select="@title | title"/></xsl:otherwise>
              </xsl:choose>
            </fo:inline>
            <fo:leader leader-pattern="dots" leader-pattern-width="6pt"/>
            <fo:page-number-citation ref-id="{generate-id(.)}"/>
            <fo:block margin-left="2em">
              <xsl:for-each select="subsection">
                <fo:block space-before="0.5em">
                  <fo:inline>
                    <xsl:number level="multiple" format="1.1" count="section | subsection"/>.
                    <xsl:apply-templates select="@title | title"/>
                  </fo:inline>
                  <fo:leader leader-pattern="dots" leader-pattern-width="6pt"/>
                  <fo:page-number-citation ref-id="{generate-id(.)}"/>
                </fo:block>
              </xsl:for-each>
            </fo:block>
          </fo:block>
        </xsl:for-each>

      </fo:flow>
    </fo:page-sequence>
  </xsl:template>
  
  <xsl:template match="section">
    <fo:block id="{generate-id(.)}" font-size="16pt" font-weight="bold" space-after="1em" space-before="1em" keep-with-next="always">
      <xsl:number/>. <xsl:apply-templates select="@title | title"/>
    </fo:block>
    <xsl:apply-templates select="subsection | p | quote"/>
  </xsl:template>
  
  <xsl:template match="subsection">
    <fo:block id="{generate-id(.)}" font-size="14pt" font-weight="bold" space-after="1em" keep-with-next="always">
      <xsl:number level="multiple" format="1.1" count="section | subsection"/>. <xsl:apply-templates select="@title | title"/>
    </fo:block>
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="p">
    <fo:block space-after="1em">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="bibliography">
    <fo:block id="{generate-id(.)}" font-size="16pt" font-weight="bold" space-before="1em" space-after="1em" keep-with-next="always">
      <xsl:number value="position()"/>. Bibliography
    </fo:block>

    <xsl:apply-templates>
      <xsl:sort select="descendant::author/surname | descendant::organisation"/>
      <xsl:sort select="descendant::author/initials"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="bibliography/book">
    <fo:block space-after="1em">
      <xsl:apply-templates select="authors"/>
      (<xsl:value-of select="year"/>),
      <fo:inline font-style="italic"><xsl:apply-templates select="title"/></fo:inline>
      <xsl:if test="edition"> (<xsl:apply-templates select="edition"/>)</xsl:if>,
      <xsl:apply-templates select="publisher"/><xsl:if test="place">,
      <xsl:apply-templates select="place"/></xsl:if>.
    </fo:block>
  </xsl:template>
  
  <xsl:template match="bibliography/book-section">
    <fo:block space-after="1em">
      <xsl:apply-templates select="authors"/>
      (<xsl:value-of select="year"/>),
      "<xsl:apply-templates select="section-title"/>," in
      <fo:inline font-style="italic"><xsl:apply-templates select="title"/></fo:inline>,
      <xsl:apply-templates select="editors"/>,
      <xsl:apply-templates select="publisher"/>,
      <xsl:apply-templates select="place"/>,
      <xsl:apply-templates select="pages"/>.
    </fo:block>
  </xsl:template>
  
  <xsl:template match="bibliography/journal-article">
    <fo:block space-after="1em">
      <xsl:apply-templates select="authors"/>
      (<xsl:value-of select="year"/>),
      "<xsl:apply-templates select="article-title"/>,"
      <fo:inline font-style="italic"><xsl:apply-templates select="title"/></fo:inline>,
      <xsl:if test="volume">vol. <xsl:value-of select="volume"/>, </xsl:if>
      <xsl:if test="issue">no. <xsl:apply-templates select="issue"/>, </xsl:if>
      <xsl:apply-templates select="pages"/>.
    </fo:block>
  </xsl:template>
  
  <xsl:template match="bibliography/standard">
    <fo:block space-after="1em">
      <xsl:if test="authors"><xsl:apply-templates select="authors"/>, </xsl:if>
      <xsl:apply-templates select="organisation"/>
      (<xsl:value-of select="year"/>),
      <fo:inline font-style="italic"><xsl:apply-templates select="title"/></fo:inline>,
      <xsl:if test="number"><xsl:apply-templates select="number"/>, </xsl:if>
      <xsl:apply-templates select="place"/>.
    </fo:block>
  </xsl:template>
  
  <xsl:template match="bibliography/web">
    <fo:block space-after="1em">
      <xsl:choose>
        <xsl:when test="authors">
          <xsl:apply-templates select="authors"/>
          (<xsl:value-of select="date"/>),
          "<xsl:apply-templates select="title"/>,"
        </xsl:when>
        <xsl:otherwise>
          "<xsl:apply-templates select="title"/>"
          (<xsl:value-of select="date"/>),
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates select="site-title"/> [online],
      Available: <fo:inline font-size="smaller" color="blue" text-decoration="underline"><xsl:apply-templates select="url"/></fo:inline>
      [Accessed <xsl:value-of select="accessed-date"/>].
    </fo:block>
  </xsl:template>
  
  <xsl:template match="bibliography/conference-paper">
    <fo:block space-after="1em">
      <xsl:apply-templates select="authors"/>
      (<xsl:value-of select="year"/>),
      "<xsl:apply-templates select="paper-title"/>,"
      in
      <xsl:if test="editors">
        <xsl:apply-templates select="editors"/>
      </xsl:if>
      <fo:inline font-style="italic"><xsl:apply-templates select="title"/></fo:inline>,
      <xsl:apply-templates select="conference-place"/>,
      <xsl:apply-templates select="conference-date"/>,
      <xsl:apply-templates select="publisher"/>.
    </fo:block>
  </xsl:template>
  
  <xsl:template name="authors-or-editors">
    <xsl:choose>
      <xsl:when test="count(author | editor) = 1">
        <xsl:for-each select="author | editor">
          <xsl:value-of select="surname"/>, <xsl:value-of select="initials"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="author | editor">
          <xsl:variable name="num" select="last()"/>
          <xsl:value-of select="surname"/>, <xsl:value-of select="initials"/>
          <!--[<xsl:value-of select="position()"/>/<xsl:value-of select="$num"/>]-->
          <xsl:choose>
            <xsl:when test="position() = $num - 1"> &amp; </xsl:when>
            <xsl:when test="position() &lt; $num - 1">, </xsl:when>
          </xsl:choose>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="bibliography/*/editors">
    <xsl:call-template name="authors-or-editors"/> (ed<xsl:if test="count(editor) > 1">s</xsl:if>.)
  </xsl:template>

  <xsl:template match="bibliography/*/authors">
    <xsl:call-template name="authors-or-editors"/>
  </xsl:template>

  <xsl:template match="bibliography/*/pages">
    <xsl:choose>
      <xsl:when test="first = last">
        p. <xsl:value-of select="first"/>
      </xsl:when>
      <xsl:otherwise>
        pp. <xsl:value-of select="first"/>&#x2014;<xsl:value-of select="last"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="sc">
    <xsl:apply-templates mode="sc"/>
  </xsl:template>
  
  <xsl:template match="cite">
    <xsl:variable name="ref" select="@ref"/>
    <xsl:variable name="bib" select="/document/sections/bibliography/*[@id=$ref]"/>
    <xsl:choose>
      <xsl:when test="name($bib) = 'standard'">
        (<xsl:apply-templates select="$bib/number"/><xsl:text>)</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="authors" select="$bib/authors/author"/>
        <xsl:variable name="num" select="count($authors)"/>
        (<xsl:choose>
          <xsl:when test="$num > 3">
            <xsl:for-each select="$authors[position() = 1]">
              <xsl:apply-templates select="surname"/><xsl:if test="position() != $num">,<xsl:text> </xsl:text></xsl:if>
            </xsl:for-each>et al
          </xsl:when>
          <xsl:otherwise>
            <xsl:for-each select="$authors">
              <xsl:apply-templates select="surname"/><xsl:if test="position() != $num">,<xsl:text> </xsl:text></xsl:if>
            </xsl:for-each>
          </xsl:otherwise>
        </xsl:choose><xsl:text> </xsl:text><xsl:value-of select="$bib/year"/><xsl:text>)</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="text()" mode="sc">
    <fo:inline font-size="75%">
      <xsl:call-template name="str:to-upper">
        <xsl:with-param name="text" select="."/>
      </xsl:call-template>
    </fo:inline>
  </xsl:template>
  
  <xsl:template match="quote">
    <fo:block margin-left="2em" space-after="1em">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="listing">
    <fo:block margin-left="2em" space-after="1em">
      <xsl:for-each select="code">
        <fo:block><fo:inline font-family="monospace"><xsl:apply-templates/></fo:inline></fo:block>
      </xsl:for-each>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="em">
    <fo:inline font-style="italic"><xsl:apply-templates/></fo:inline>
  </xsl:template>
  
  <xsl:template match="code">
    <fo:inline font-family="monospace"><xsl:apply-templates/></fo:inline>
  </xsl:template>

  <xsl:template match="ol">
    <fo:block margin-left="2em" space-after="0.5em">
      <xsl:for-each select="li">
        <fo:block space-after="0.5em">
          <xsl:number value="position()"/>. <xsl:apply-templates/>
        </fo:block>
      </xsl:for-each>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="table">
    <fo:table margin-left="2em" space-after="1em">
      <xsl:apply-templates select="col"/>
      <fo:table-body>
        <xsl:apply-templates select="tr"/>
      </fo:table-body>
    </fo:table>
  </xsl:template>
  
  <xsl:template match="col">
    <fo:table-column column-width="{@width}"/>
  </xsl:template>
  
  <xsl:template match="tr">
    <fo:table-row>
      <xsl:if test="@type = 'header'">
        <xsl:attribute name="font-weight">bold</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </fo:table-row>
  </xsl:template>
  
  <xsl:template match="td">
    <fo:table-cell>
      <fo:block space-after="0.5em"><xsl:apply-templates/></fo:block>
    </fo:table-cell>
  </xsl:template>
  
  <xsl:template match="/document/meta"/>

</xsl:stylesheet>

<!--
  vim:syn=xml:sw=2:ts=2:et
  -->

