<?xml version="1.0" encoding="UTF-8" ?>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
 |                                                                           |
 | o:XML is published under the GNU General Public Licence                   |
 |   (see http://www.gnu.org/licenses/licenses.html#GPL).                    |
 |                                                                           |
 | A copy of the licence agreement (gpl.txt) must be part of any             |
 |   redistribution of any form.                                             |
 |                                                                           |
 | o:XML was created by Martin Klang <mars@pingdynasty.com>.                 |
 |                                                                           |
 | For more information please see:                                          |
 |   http://www.o-xml.org/                                                   |
 |                                                                           |
 +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<xsl:stylesheet version="1.0" xmlns:o="http://www.o-xml.org/lang/"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <o:do>
      <xsl:apply-templates/>
    </o:do>
  </xsl:template>

  <!-- template that creates a stub function body -->
  <xsl:template match="Foundation.Core.Operation" mode="function-body">
    <o:do>
      <o:log msg="in function: {Foundation.Core.ModelElement.name}"/>
      <xsl:comment> insert code here! </xsl:comment>
    </o:do>
  </xsl:template>

  <xsl:template match="Model_Management.Package">
    <o:module name="{Foundation.Core.ModelElement.name}">
      <xsl:apply-templates/>
    </o:module>
  </xsl:template>

  <xsl:template match="Foundation.Core.Class[@xmi.id]|Foundation.Core.Interface[@xmi.id]|Foundation.Core.DataType[@xmi.id]">
    <o:type name="{Foundation.Core.ModelElement.name}">
      <xsl:apply-templates select="Foundation.Core.ModelElement.visibility"/>
      <xsl:apply-templates select="Foundation.Core.GeneralizableElement.generalization"/>
<!--      <xsl:apply-templates select="Foundation.Core.GeneralizableElement.specialization"/> -->
      <xsl:apply-templates select="Foundation.Core.Classifier.feature"/>
    </o:type>
  </xsl:template>

  <!-- type variable definition -->
  <xsl:template match="Foundation.Core.Attribute[@xmi.id]">
    <xsl:variable name="type">
      <xsl:apply-templates select="Foundation.Core.StructuralFeature.type/Foundation.Core.Classifier" mode="name"/>
    </xsl:variable>
    <o:variable name="{Foundation.Core.ModelElement.name}" type="{$type}"/>
  </xsl:template>

  <!-- type function definition -->
  <xsl:template match="Foundation.Core.Operation[@xmi.id]">
    <o:function name="{Foundation.Core.ModelElement.name}">
      <xsl:apply-templates select="Foundation.Core.ModelElement.visibility"/>
      <xsl:apply-templates select="Foundation.Core.BehavioralFeature.parameter/Foundation.Core.Parameter[Foundation.Core.Parameter.kind/@xmi.value = 'return']"/>
      <xsl:apply-templates select="Foundation.Core.BehavioralFeature.parameter/Foundation.Core.Parameter[Foundation.Core.Parameter.kind/@xmi.value = 'in']"/>
      <xsl:apply-templates select="." mode="function-body"/>
    </o:function>
  </xsl:template>

  <xsl:template match="Foundation.Core.Parameter[@xmi.id and Foundation.Core.Parameter.kind/@xmi.value = 'in']">
    <xsl:variable name="type">
      <xsl:apply-templates select="Foundation.Core.Parameter.type/Foundation.Core.Classifier" mode="name"/>
    </xsl:variable>
    <o:param name="{Foundation.Core.ModelElement.name}" type="{$type}"/>
  </xsl:template>

  <xsl:template match="Foundation.Core.Parameter[@xmi.id and Foundation.Core.Parameter.kind/@xmi.value = 'return']">
    <xsl:attribute name="type">
      <xsl:apply-templates select="Foundation.Core.Parameter.type/Foundation.Core.Classifier" mode="name"/>
    </xsl:attribute>
  </xsl:template>

  <!-- parent type reference - resolve to a parent element -->
  <xsl:template match="Foundation.Core.GeneralizableElement.generalization">
    <xsl:variable name="type">
      <xsl:apply-templates select="Foundation.Core.Generalization" mode="name"/>
    </xsl:variable>    
    <o:parent name="{$type}"/>
  </xsl:template>

  <!-- de-referencing -->
  <xsl:template match="*[@xmi.idref]" mode="name">
    <xsl:variable name="ref" select="@xmi.idref"/>
    <xsl:apply-templates select="//*[@xmi.id = $ref]" mode="name"/>
  </xsl:template>

  <!-- data type definition -->
  <xsl:template match="Foundation.Core.DataType[@xmi.id]" mode="name">
    <xsl:value-of select="Foundation.Core.ModelElement.name"/>
  </xsl:template>

  <!-- inheritance definition - resolve the name of the parent type -->
  <xsl:template match="Foundation.Core.Generalization[@xmi.id]" mode="name">
    <xsl:apply-templates select="Foundation.Core.Generalization.parent/Foundation.Core.GeneralizableElement" mode="name"/>
  </xsl:template>

  <xsl:template match="Foundation.Core.GeneralizableElement[@xmi.id]" mode="name">
    <xsl:value-of select="Foundation.Core.ModelElement.name"/>
  </xsl:template>

  <xsl:template match="Foundation.Core.Class[@xmi.id]" mode="name">
    <xsl:value-of select="Foundation.Core.ModelElement.name"/>
  </xsl:template>

  <xsl:template match="Foundation.Core.Interface[@xmi.id]" mode="name">
    <xsl:value-of select="Foundation.Core.ModelElement.name"/>
  </xsl:template>

  <xsl:template match="Foundation.Core.ModelElement.visibility">
    <xsl:attribute name="access"><xsl:value-of select="@xmi.value"/></xsl:attribute>
  </xsl:template>

  <xsl:template match="text()"/>

</xsl:stylesheet>
