<!--
 Copyright (C) 2004- Commonwealth Scientific and Industrial Research
 Organisation (CSIRO) Australia

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 - Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.

 - Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.

 - Neither the name of CSIRO Australia nor the names of its
 contributors may be used to endorse or promote products derived from
 this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- create a timeslot list -->
  <xsl:key name="timeorder" match="TIME_SLOT" use="@TIME_SLOT_ID" />

  <xsl:template match="ANNOTATION_DOCUMENT">
    <cmml>
      <!-- inserting stream tag -->
      <stream>
        <!-- inserting link to media file and its timing information -->
        <import>
          <xsl:attribute name="contenttype">
            <xsl:value-of select="HEADER/MEDIA_DESCRIPTOR/@MIME_TYPE"/>
          </xsl:attribute>
          <xsl:attribute name="src">
            <xsl:value-of select="HEADER/MEDIA_DESCRIPTOR/@MEDIA_URL"/>
          </xsl:attribute>
        </import>
      </stream>

      <!-- inserting head tag -->
      <head>
        <xsl:apply-templates select="TIER[@TIER_ID='ilocano text']"/>
        <xsl:apply-templates select="TIER[@TIER_ID='english translation']"/>
      </head>

      <!-- inserting clip tags -->
      <xsl:apply-templates select="TIER[@TIER_ID='ilocano text']/ANNOTATION"/>
    </cmml>
  </xsl:template>

  <xsl:template match="TIER[@TIER_ID='ilocano text']">
        <title>
          <xsl:value-of select="@PARTICIPANT" />
          <xsl:text> - </xsl:text>
          <xsl:value-of select="@TIER_ID" />
        </title>
        <meta name="DC.language" scheme="Dublin Core">
          <xsl:attribute name="content">
            <xsl:value-of select="@LINGUISTIC_TYPE_REF"/>
          </xsl:attribute>
        </meta>
        <meta name="Speaker" >
          <xsl:attribute name="content">
            <xsl:value-of select="@PARTICIPANT"/>
          </xsl:attribute>
        </meta>
  </xsl:template>

  <xsl:template match="TIER[@TIER_ID='english translation']">
        <meta name="Translator" >
          <xsl:attribute name="content">
            <xsl:value-of select="@PARTICIPANT"/>
          </xsl:attribute>
        </meta>
  </xsl:template>

  <xsl:template match="TIER[@TIER_ID='ilocano text']/ANNOTATION">
      <clip track="default">

        <!-- get the starttime of this annotation -->
        <xsl:variable name="starttime">
          <xsl:value-of select="key('timeorder',ALIGNABLE_ANNOTATION/@TIME_SLOT_REF1)/@TIME_VALUE"/>
        </xsl:variable>

        <xsl:attribute name="start">
          <xsl:value-of select="$starttime div 1000"/>
        </xsl:attribute>

        <desc>
          <xsl:value-of select="ALIGNABLE_ANNOTATION/ANNOTATION_VALUE"/>

        <!-- find the translated annotation that has the same starttime -->
        <xsl:variable name="translation"/>
        <xsl:for-each select="//ANNOTATION_DOCUMENT/TIER[@TIER_ID='english translation']/ANNOTATION">
          <xsl:variable name="starttest">
            <xsl:value-of select="key('timeorder',ALIGNABLE_ANNOTATION/@TIME_SLOT_REF1)/@TIME_VALUE"/>
          </xsl:variable>

          <xsl:if test="number($starttest) = number($starttime)">
            <xsl:text> - </xsl:text>
            <xsl:value-of select="ALIGNABLE_ANNOTATION/ANNOTATION_VALUE"/>
          </xsl:if>
        </xsl:for-each>  

        </desc>
      </clip>
  </xsl:template>

</xsl:stylesheet>
