<?xml version="1.0" encoding="UTF-8"?>

<!-- Copyright. INRIA : contributor(s) : Priscille Durville                        -->
<!--                                                                               -->
<!--  edelweiss@sophia.inria.fr                                                    -->
<!--                                                                               -->
<!--  This software is a computer program whose purpose is dedicated to ontologies -->
<!--  creation, modification and visualisation.                                    -->
<!--                                                                               -->
<!--  This software is governed by the CeCILL-C license under French law and       -->
<!--  abiding by the rules of distribution of free software.  You can  use,        -->
<!--  modify and/ or redistribute the software under the terms of the CeCILL-C     -->
<!--  license as circulated by CEA, CNRS and INRIA at the following URL            -->
<!--  "http://www.cecill.info".                                                    -->
<!--                                                                               -->
<!--  As a counterpart to the access to the source code and  rights to copy,       -->
<!--  modify and redistribute granted by the license, users are provided only      -->
<!--  with a limited warranty  and the software's author,  the holder of the       -->
<!--  economic rights,  and the successive licensors  have only  limited           -->
<!--  liability.                                                                   -->
<!--                                                                               -->
<!--  In this respect, the user's attention is drawn to the risks associated       -->
<!--  with loading,  using,  modifying and/or developing or reproducing the        -->
<!--  software by the user in light of its specific status of free software,       -->
<!--  that may mean  that it is complicated to manipulate,  and  that  also        -->
<!--  therefore means  that it is reserved for developers  and  experienced        -->
<!--  professionals having in-depth computer knowledge. Users are therefore        -->
<!--  encouraged to load and test the software's suitability as regards their      -->
<!--  requirements in conditions enabling the security of their systems and/or     -->
<!--  data to be ensured and,  more generally, to use and operate it in the        -->
<!--  same conditions as regards security.                                         -->
<!--                                                                               -->
<!-- The fact that you are presently reading this means that you have had          -->
<!--  knowledge of the CeCILL-C license and that you accept its terms.             -->


<!-- Ontology (RDFS syntax) to Topicmap (XTM syntax).                              -->
<!-- @author Priscille Durville                                                    -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
 	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
 	xmlns:owl="http://www.w3.org/2002/07/owl#"
 	xmlns:topic="http://www.topicmaps.org/xtm/1.0/" 
 	xmlns:xlink="http://www.w3.org/1999/xlink"
 	xmlns:cos="http://www.inria.fr/acacia/corese#" 
 	xmlns="http://www.topicmaps.org/xtm/1.0/">

<xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="UTF-8" />

	<xsl:template match="rdf:RDF">
	  <topicMap id="{generate-id()}" xmlns="http://www.topicmaps.org/xtm/1.0/" xmlns:xlink="http://www.w3.org/1999/xlink">
	      <xsl:for-each select="*[not(local-name() = 'Ontology')]">
            <xsl:call-template name="topic" />
	      </xsl:for-each>
	      <xsl:for-each select="*[local-name() = 'Property']">
            <xsl:call-template name="association" />
	      </xsl:for-each>
	      <xsl:for-each select="*/rdfs:subClassOf">
            <xsl:call-template name="isaAssociation" />
	      </xsl:for-each>
	      <xsl:for-each select="*[@rdf:about and not(local-name() = 'Ontology')]">
            <xsl:call-template name="istanceOfAssociation" />
	      </xsl:for-each>
      </topicMap>
	</xsl:template>
	
	<xsl:template name="topic">
	    <xsl:variable name="ID">
        <xsl:choose>
          <xsl:when test="@rdf:ID"><xsl:value-of select="@rdf:ID" /></xsl:when>
          <xsl:otherwise><xsl:value-of select="substring-after(@rdf:about, '#')" /></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <topic id="{$ID}">
        <instanceOf>
            <xsl:choose>
              <xsl:when test="local-name() = 'Property'">
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#linkingPhrase"/>
              </xsl:when>
              <xsl:otherwise>
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#concept"/>
              </xsl:otherwise>
            </xsl:choose>
        </instanceOf>
        <baseName>
            <!-- <scope><subjectIndicatorRef xlink:href="http://www.topicmaps.org/xtm/1.0/language.xtm#{@xml:lang}"/></scope> -->
            <baseNameString>
              <xsl:for-each select="rdfs:label">
                <xsl:value-of select="." />
                <xsl:if test="position() != last()">, </xsl:if>
              </xsl:for-each>
            </baseNameString>
        </baseName>
        <xsl:if test="rdfs:isDefinedBy">
          <occurrence>
              <instanceOf><topicRef xlink:href="#plain-text-format"/></instanceOf>
              <xsl:variable name="isdefinedBy"><xsl:value-of select="rdfs:isDefinedBy" /></xsl:variable>
              <resourceRef xlink:href="{$isdefinedBy}"/>
          </occurrence>
        </xsl:if>
      </topic>
	</xsl:template>
	
	<xsl:template name="association">
    <association id="assoc_{@rdf:ID}">
        <instanceOf>
            <topicRef xlink:type="simple" xlink:href="#{@rdf:ID}"/>
        </instanceOf>
        <member>
            <roleSpec>
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#incoming"/>
            </roleSpec>
            <xsl:variable name="incoming"><xsl:value-of select="rdfs:domain/@rdf:resource" /></xsl:variable>
            <topicRef xlink:type="simple" xlink:href="#{$incoming}"/>
        </member>
        <member>
            <roleSpec>
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#outgoing"/>
            </roleSpec>
            <xsl:variable name="outgoing"><xsl:value-of select="rdfs:range/@rdf:resource" /></xsl:variable>
            <topicRef xlink:type="simple" xlink:href="#{$outgoing}"/>
        </member>
    </association>
	</xsl:template>
	
	<xsl:template name="isaAssociation">
     <xsl:variable name="currentID"><xsl:value-of select="generate-id()" /></xsl:variable>
     <topic id="{$currentID}">
        <instanceOf>
            <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#linkingPhrase"/>
        </instanceOf>
        <baseName>
            <baseNameString><![CDATA[is a]]></baseNameString>
        </baseName>
    </topic>
    <association id="assoc_{$currentID}">
        <instanceOf>
            <topicRef xlink:type="simple" xlink:href="#{$currentID}"/>
        </instanceOf>
        <member>
            <roleSpec>
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#incoming"/>
            </roleSpec>
            <xsl:variable name="incoming"><xsl:value-of select="@rdf:resource" /></xsl:variable>
            <topicRef xlink:type="simple" xlink:href="{$incoming}"/>
        </member>
        <member>
            <roleSpec>
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#outgoing"/>
            </roleSpec>
            <xsl:variable name="outgoing"><xsl:value-of select="../@rdf:ID" /></xsl:variable>
            <topicRef xlink:type="simple" xlink:href="#{$outgoing}"/>
        </member>
    </association>
	</xsl:template>
	
	<xsl:template name="istanceOfAssociation">
     <xsl:variable name="currentID"><xsl:value-of select="generate-id()" /></xsl:variable>
     <topic id="{$currentID}">
        <instanceOf>
            <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#linkingPhrase"/>
        </instanceOf>
        <baseName>
            <baseNameString><![CDATA[instance of]]></baseNameString>
        </baseName>
    </topic>
    <association id="assoc_{$currentID}">
        <instanceOf>
            <topicRef xlink:type="simple" xlink:href="#{$currentID}"/>
        </instanceOf>
        <member>
            <roleSpec>
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#incoming"/>
            </roleSpec>
            <xsl:variable name="incoming"><xsl:value-of select="local-name(.)" /></xsl:variable>
            <topicRef xlink:type="simple" xlink:href="#{$incoming}"/>
        </member>
        <member>
            <roleSpec>
                <subjectIndicatorRef xlink:type="simple" xlink:href="http://cmap.coginst.uwf.edu/#outgoing"/>
            </roleSpec>
            <xsl:variable name="outgoing"><xsl:value-of select="substring-after(@rdf:about, '#')" /></xsl:variable>
            <topicRef xlink:type="simple" xlink:href="#{$outgoing}"/>
        </member>
    </association>
	</xsl:template>
	
</xsl:stylesheet>
