<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:test="http://www.jenitennison.com/xslt/unit-test"
                xmlns:ev="http://www.lmnl.org/event"
                exclude-result-prefixes="xs ev test"
                xmlns:clix="http://lmnl.net/clix">
  
<xsl:import href="parse-lmnl.xsl" />
<xsl:import href="events-to-clix.xsl" />
  
<xsl:param name="lmnl-doc" as="xs:anyURI" select="xs:anyURI('')" />
<xsl:param name="lmnl" as="xs:string" select="unparsed-text(resolve-uri($lmnl-doc, base-uri()))" />  

<xsl:template match="/">
  <xsl:call-template name="lmnl-to-clix">
    <xsl:with-param name="lmnl" select="string(.)" />
  </xsl:call-template>
</xsl:template>
  
<test:tests>
  <test:title>Transforming to CLIX</test:title>
  <test:test>
    <test:title>Simple text</test:title>
    <test:param name="lmnl" select="string(.)">foo</test:param>
    <test:expect>
      <clix:clix>foo</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Single range</test:title>
    <test:param name="lmnl" select="string(.)">...[foo}...{foo]...</test:param>
    <test:expect>
      <clix:clix>...<foo clix:role="start-range" />...<foo clix:role="end-range" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Non-overlapping ranges</test:title>
    <test:param name="lmnl" select="string(.)">...[foo}...{foo]...[foo}...{foo]...</test:param>
    <test:expect>
      <clix:clix>...<foo clix:role="start-range" />...<foo clix:role="end-range" />...<foo clix:role="start-range" />...<foo clix:role="end-range" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Overlapping ranges</test:title>
    <test:param name="lmnl" select="string(.)">...[foo=f1}...[foo=f2}...{foo=f1]...{foo=f2]...</test:param>
    <test:expect>
      <clix:clix>...<foo clix:role="start-range" clix:sID="f1" />...<foo clix:role="start-range" clix:sID="f2" />...<foo clix:role="end-range" clix:eID="f1" />...<foo clix:role="end-range" clix:eID="f2" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Single range with single annotation</test:title>
    <test:param name="lmnl" select="string(.)">...[foo [bar}...{bar]}...{foo]...</test:param>
    <test:expect>
      <clix:clix>...<foo clix:role="start-range">
        <bar clix:role="start-annotation" />...<bar clix:role="end-annotation" />
      </foo>...<foo clix:role="end-range" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Single range with two annotations</test:title>
    <test:param name="lmnl" select="string(.)">...[foo [bar}...{bar] [baz}...{baz]}...{foo]...</test:param>
    <test:expect>
      <clix:clix>...<foo clix:role="start-range">
        <bar clix:role="start-annotation" />...<bar clix:role="end-annotation" />
        <baz clix:role="start-annotation" />...<baz clix:role="end-annotation" />
      </foo>...<foo clix:role="end-range" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Single range with nested annotations</test:title>
    <test:param name="lmnl" select="string(.)">...[foo [bar [baz}...{baz]}...{bar]}...{foo]...</test:param>
    <test:expect>
      <clix:clix>...<foo clix:role="start-range">
        <bar clix:role="start-annotation">
          <baz clix:role="start-annotation" />...<baz clix:role="end-annotation" />
        </bar>...<bar clix:role="end-annotation" />        
      </foo>...<foo clix:role="end-range" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Single atom</test:title>
    <test:param name="lmnl" select="string(.)">...{{foo}}...</test:param>
    <test:expect>
      <clix:clix>...<foo clix:role="atom" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Anonymous range</test:title>
    <test:param name="lmnl" select="string(.)">...[}...{]...</test:param>
    <test:expect>
      <clix:clix>...<clix:anon clix:role="start-range" />...<clix:anon clix:role="end-range" />...</clix:clix>
    </test:expect>
  </test:test>
  <test:test>
    <test:title>Namespaced range</test:title>
    <test:param name="lmnl" select="string(.)">
      [!ns x="http://www.example.com"]
      ...[x:foo}...{x:foo]...
    </test:param>
    <test:expect>
      <clix:clix xmlns:x="http://www.example.com">
      
      ...<x:foo clix:role="start-range" />...<x:foo clix:role="end-range" />...
    </clix:clix>
    </test:expect>
  </test:test>
</test:tests>
<xsl:template name="lmnl-to-clix">
  <xsl:param name="lmnl" select="$lmnl" />
  <xsl:variable name="events" as="element(ev:events)">
    <xsl:call-template name="ev:parse-lmnl">
      <xsl:with-param name="lmnl" select="$lmnl" />
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="$events/ev:error">
      <xsl:message>
        ERROR parsing LMNL:
        <xsl:copy-of select="$events/ev:error" copy-namespaces="no" />
      </xsl:message>
      <xsl:sequence select="false()" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="$events" mode="ev:to-clix" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>  
  
</xsl:stylesheet>
