<!DOCTYPE xsl:stylesheet [
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
]>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:tt="http://djpowell.net/schemas/treetriples/1/"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
                >
 
  <xsl:import href="splitns.xsl" />
  
  <xsl:output method="xml"
              encoding="utf-8"
              media-type="application/rdf+xml"
      />
  
  <xsl:template match="/tt:rdf">
    <rdf:RDF>
      <xsl:apply-templates select="tt:s" />
      <xsl:apply-templates select="tt:d" />
    </rdf:RDF>
  </xsl:template>
  
  <xsl:template match="tt:d">
    <xsl:choose>
      <xsl:when test="@parse = 'alt'">
        <rdf:Alt>
          <xsl:call-template name="handleContainer" />
        </rdf:Alt>
      </xsl:when>
      <xsl:when test="@parse = 'bag'">
        <rdf:Bag>
          <xsl:call-template name="handleContainer" />
        </rdf:Bag>
      </xsl:when>
      <xsl:when test="@parse = 'seq'">
        <rdf:Seq>
          <xsl:call-template name="handleContainer" />
        </rdf:Seq>
      </xsl:when>
      <xsl:when test="@parse = 'container'">
        <rdf:Description>
          <xsl:call-template name="handleContainer" />
        </rdf:Description>
      </xsl:when>
      <xsl:when test="@parse = 'list'">
        <xsl:for-each select="tt:o[1]">
          <xsl:call-template name="handleList">           
            <xsl:with-param name="first" select="true()" />
          </xsl:call-template>
        </xsl:for-each>
      </xsl:when>
      <xsl:when test="@parse = 'statement'">
        <xsl:for-each select="tt:s/tt:p/tt:o">
          <xsl:call-template name="handleStatement" />
        </xsl:for-each>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="handleStatement">
    <rdf:Statement>
      <xsl:call-template name="setStmtId" />
      <xsl:for-each select="../..">
        <rdf:subject>
          <xsl:call-template name="setIdRef" />
        </rdf:subject>
      </xsl:for-each>
      <xsl:for-each select="..">
        <rdf:predicate>        
          <xsl:call-template name="setIdRef" />
        </rdf:predicate>
      </xsl:for-each>
      <rdf:object>
        <xsl:call-template name="handleObject" />
      </rdf:object>
    </rdf:Statement>
  </xsl:template>

  <xsl:template name="handleContainer">
    <xsl:call-template name="setId" />
    <xsl:for-each select="tt:o">
      <rdf:li>
        <xsl:call-template name="handleObject" />
      </rdf:li>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="handleList">
    <xsl:param name="first" select="false()" />
    <rdf:List>
      <!-- rdf/xml doesn't allow a URI to be a collection, and doesn't
           support defining a standalone collection.  this is ugly, but
           lets do this long-hand -->
      <xsl:if test="$first">
        <xsl:call-template name="setListId" />
      </xsl:if>

      <rdf:first>
        <xsl:call-template name="handleObject" />
      </rdf:first>
      <xsl:choose>
        <xsl:when test="following-sibling::tt:o[position()=1]">
          <xsl:for-each select="following-sibling::tt:o[position()=1]">
            <rdf:rest>
              <xsl:call-template name="handleList" />
            </rdf:rest>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <rdf:rest rdf:resource="&rdf;nil" />
        </xsl:otherwise>
      </xsl:choose>
    </rdf:List>
  </xsl:template>

  <xsl:template match="tt:s">
    <rdf:Description>
      <xsl:call-template name="setId" />
      <xsl:apply-templates select="tt:p" />
    </rdf:Description>
    <!-- handle asserted reification -->
    <xsl:for-each select="tt:p/tt:o[@stmtId]">
      <xsl:call-template name="handleStatement" />
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="tt:p">
    <xsl:variable name="split">
      <xsl:call-template name="findNamespaceSplit">
        <xsl:with-param name="uri" select="@id" />
        <xsl:with-param name="terminateOnError" select="true()" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:element namespace="{substring(@id, 1, $split - 1)}" name="{substring(@id, $split)}">
      <xsl:apply-templates select="tt:o" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="tt:o">
    <xsl:call-template name="handleObject" />
  </xsl:template>

  <xsl:template name="handleObject">
    <xsl:choose>
      <xsl:when test="@id">
        <xsl:call-template name="setIdRef" />
      </xsl:when>
      <xsl:when test="@parse = 'xml'">
        <xsl:attribute name="rdf:parseType">Literal</xsl:attribute>
        <xsl:copy-of select="*|text()" />
      </xsl:when>
      <xsl:otherwise>

        <xsl:if test="@datatype">
          <xsl:attribute name="rdf:datatype">
            <xsl:value-of select="@datatype" />
          </xsl:attribute>
        </xsl:if>

        <xsl:if test="@xml:lang">
          <xsl:attribute name="xml:lang">
            <xsl:value-of select="@xml:lang" />
          </xsl:attribute>
        </xsl:if>

        <xsl:value-of select="text()" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="setId">
    <xsl:choose>
      <xsl:when test="starts-with(@id, '_:')">
        <xsl:attribute name="rdf:nodeID">
          <xsl:text>b</xsl:text>
          <xsl:value-of select="substring(@id, 3)" />
        </xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="rdf:about">
          <xsl:value-of select="@id" />
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="setStmtId">
    <xsl:choose>
      <xsl:when test="starts-with(@stmtId, '_:')">
        <xsl:attribute name="rdf:nodeID">
          <xsl:text>b</xsl:text>
          <xsl:value-of select="substring(@stmtId, 3)" />
        </xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="rdf:about">
          <xsl:value-of select="@stmtId" />
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="setListId">
    <xsl:choose>
      <xsl:when test="starts-with(@listId, '_:')">
        <xsl:attribute name="rdf:nodeID">
          <xsl:text>b</xsl:text>
          <xsl:value-of select="substring(@listId, 3)" />
        </xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="rdf:about">
          <xsl:value-of select="@listId" />
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="setIdRef">
    <xsl:choose>
      <xsl:when test="starts-with(@id, '_:')">
        <xsl:attribute name="rdf:nodeID">
          <xsl:text>b</xsl:text>
          <xsl:value-of select="substring(@id, 3)" />
        </xsl:attribute>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="rdf:resource">
          <xsl:value-of select="@id" />
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" />

</xsl:stylesheet>