<?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="1cm"
            margin-bottom="1cm"
            margin-left="1.5cm"
            margin-right="1.5cm">
          <fo:region-body margin-top="5cm"/>
          <fo:region-before extent="1.5cm" margin-left="1cm"/>
          <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>

        <!-- declaration page -->
        <fo:simple-page-master master-name="declaration-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-before extent="2cm"/>
          <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>

        <!-- abstract page -->
        <fo:simple-page-master master-name="abstract-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-before extent="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" margin-right="2.5cm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <xsl:call-template name="title-page"/>
      <xsl:call-template name="declaration-page"/>
      <xsl:call-template name="abstract-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">
            <xsl:apply-templates select="/document/meta/title"/>
          </fo:block>
        </fo:static-content-->

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

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

  <xsl:template name="abstract-page">
    <fo:page-sequence master-reference="abstract-page">
      <fo:flow flow-name="xsl-region-body">

        <fo:block text-align="justify" font-size="11pt" margin-left="3em" margin-right="3em" line-height="1.3em">

          <fo:block
              font-weight="bold" 
              text-align="center"
              padding-bottom="1em">
            <fo:block>Abstract</fo:block>
          </fo:block>
        
          <fo:block padding-bottom="3em">
            <xsl:apply-templates select="document/meta/abstract/*"/>
          </fo:block>

        </fo:block>

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

  <xsl:template name="declaration-page">
    <fo:page-sequence master-reference="declaration-page">
      <fo:flow flow-name="xsl-region-body">

        <fo:block text-align="justify" line-height="1.3em">

          <fo:block
              font-weight="bold" 
              font-size="16pt"
              padding-bottom="1em">
            <fo:block>Declaration of Originality</fo:block>
          </fo:block>
        
          <fo:block padding-bottom="3em">
            I, Cameron Leigh McCormack, declare that this thesis is my own work and has not
            been submitted in any form for another degree or diploma at any university or
            other institute of tertiary education.  Information derived from the published
            and unpublished works of others has been acknowledged in the text and a list of
            references is given in the bibliography.
          </fo:block>

          <fo:block> ______________________________ </fo:block>
          <fo:block> Cameron Leigh McCormack </fo:block>
          <fo:block> November 5, 2002 </fo:block>

        </fo:block>

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

  <xsl:template name="title-page">
    <fo:page-sequence master-reference="title-page">
      <!--fo:static-content flow-name="xsl-region-before">
        <fo:block text-align="left" font-size="10pt" font-weight="bold" color="silver" font-family="monospace">
          &lt;thesis&gt;
        </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="3em">
            Bachelor of Computer Science Honours (1608), Clayton Campus
          </fo:block>

          <!--fo:block padding-bottom="4em">
            Literature Review
          </fo:block-->

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

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

          <fo:block padding-bottom="2em" font-family="monospace" font-size="10pt">
            clm@csse.monash.edu.au
          </fo:block>

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

          <fo:block padding-bottom="1em" font-family="monospace" font-size="10pt">
            ajh@csse.monash.edu.au
          </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">
          <xsl:apply-templates select="/document/meta/title"/>
        </fo:block>
      </fo:static-content-->

      <fo:static-content flow-name="xsl-region-after">
        <fo:block text-align="right"
            font-size="10pt"
            font-family="serif"
            margin-right="-2em">
          <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">
          <fo:block space-after="1em">
            <fo:inline font-weight="bold">
              <xsl:number value="position()"/>.
              <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 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>
                <fo:block margin-left="4em">
                  <xsl:for-each select="subsubsection">
                    <fo:block space-before="0.5em">
                      <fo:inline>
                        <xsl:number level="multiple" format="1.1.1" count="section | subsection | subsubsection"/>.
                        <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>
              </xsl:for-each>
            </fo:block>
          </fo:block>
        </xsl:for-each>

        <xsl:for-each select="/document/sections/bibliography">
          <fo:block space-after="1em">
            <fo:inline font-weight="bold">
              Bibliography
            </fo:inline>
            <fo:leader leader-pattern="dots" leader-pattern-width="6pt"/>
            <fo:page-number-citation ref-id="{generate-id(.)}"/>
          </fo:block>
        </xsl:for-each>

        <xsl:for-each select="/document/sections/appendix">
          <fo:block space-after="1em">
            <fo:inline font-weight="bold">
              Appendix
              <xsl:number value="position()" format="A"/>.
              <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 margin-left="2em">
              <xsl:for-each select="subsection">
                <fo:block space-before="0.5em">
                  <fo:inline>
                    <xsl:number level="multiple" format="A.1" count="appendix | 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>
                <fo:block margin-left="4em">
                  <xsl:for-each select="subsubsection">
                    <fo:block space-before="0.5em">
                      <fo:inline>
                        <xsl:number level="multiple" format="A.1.1" count="appendix | subsection | subsubsection"/>.
                        <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>
              </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:if test="position() != 1">
        <xsl:attribute name="break-before">page</xsl:attribute>
      </xsl:if>
      <xsl:number/>. <xsl:apply-templates select="@title | title"/>
    </fo:block>
    <fo:block line-height="1.3em">
      <xsl:apply-templates select="subsection | p | quote | listing | table"/>
    </fo:block>
  </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:if test="@break = 'true'">
        <xsl:attribute name="break-before">page</xsl:attribute>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="name(..)='appendix'">
          <xsl:number level="multiple" format="A.1" count="appendix | subsection"/>. <xsl:apply-templates select="@title | title"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:number level="multiple" format="1.1" count="section | subsection"/>. <xsl:apply-templates select="@title | title"/>
        </xsl:otherwise>
      </xsl:choose>
    </fo:block>
    <fo:block line-height="1.3em">
      <xsl:apply-templates select="subsubsection | p | quote | listing | table"/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="subsubsection">
    <fo:block id="{generate-id(.)}" font-size="12pt" font-weight="bold" space-after="1em" keep-with-next="always">
      <xsl:if test="@break = 'true'">
        <xsl:attribute name="break-before">page</xsl:attribute>
      </xsl:if>
      <xsl:number level="multiple" format="1.1.1" count="section | subsection | subsubsection"/>. <xsl:apply-templates select="@title | title"/>
    </fo:block>
    <fo:block line-height="1.3em">
      <xsl:apply-templates select="p | quote | listing | table"/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="p">
    <fo:block space-after="1em" text-align="justify">
      <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" break-before="page">
      Bibliography
    </fo:block>

    <fo:block line-height="1.3em">
      <xsl:apply-templates>
        <xsl:sort select="descendant::author/surname | descendant::organisation"/>
        <xsl:sort select="descendant::author/initials"/>
      </xsl:apply-templates>
    </fo:block>
  </xsl:template>

  <xsl:template match="appendix">
    <fo:block id="{generate-id(.)}" font-size="16pt" font-weight="bold" space-after="1em" space-before="1em" keep-with-next="always" break-before="page">
      Appendix <xsl:number format="A"/>. <xsl:apply-templates select="@title | title"/>
    </fo:block>
    <fo:block line-height="1.3em">
      <xsl:apply-templates select="subsection | p | quote | listing"/>
    </fo:block>
  </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" text-align="justify">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="listing">
    <fo:block margin-left="2em" space-after="1em" font-family="monospace" font-size="10pt" white-space-collapse="false" wrap-option="no-wrap">
      <xsl:if test="name(../..)='appendix'">
        <xsl:attribute name="font-size">8pt</xsl:attribute>
      </xsl:if>
      <!--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-weight="bold"><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:block>
      <fo:table margin-left="1em" space-after="1em">
        <xsl:apply-templates select="col"/>
        <fo:table-body>
          <xsl:apply-templates select="tr"/>
        </fo:table-body>
      </fo:table>
    </fo:block>
  </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 select="td"/>
    </fo:table-row>
  </xsl:template>
  
  <xsl:template match="td">
    <fo:table-cell>
      <xsl:if test="position() &gt; 1">
        <xsl:attribute name="border-left">0.5px solid black</xsl:attribute>
      </xsl:if>
      <xsl:if test="parent::tr/@type = 'header'">
        <xsl:attribute name="border-bottom">0.5px solid black</xsl:attribute>
      </xsl:if>
      <fo:block padding-left="1em" space-before="0.25em" space-after="0.25em"><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
  -->

