<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (C) 2005  Cedric Vidal, c.vidal@proxiad.com                     -->
<!--                                                                           -->
<!-- This library is free software; you can redistribute it and/or             -->
<!-- modify it under the terms of the GNU Lesser General Public                -->
<!-- License as published by the Free Software Foundation; either              -->
<!-- version 2.1 of the License, or (at your option) any later version.        -->
<!--                                                                           -->
<!-- This library is distributed in the hope that it will be useful,           -->
<!-- but WITHOUT ANY WARRANTY; without even the implied warranty of            -->
<!-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         -->
<!-- Lesser General Public License for more details.                           -->
<!--                                                                           -->
<!-- You should have received a copy of the GNU Lesser General Public          -->
<!-- License along with this library; if not, write to the Free Software       -->
<!-- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:UML="omg.org/UML/1.4"
    version="1.0">
    <xsl:import href="Identity.xsl"/>
    <!-- ClassViews which are participating in a LinkAttributeView by class idref -->
    <xsl:key name="ClassViewByClassRefKey"
        match="//mdElement[@elementClass='ClassView'][//mdElement[@elementClass='LinkAttributeView']/linkSecondEndID/@xmi.idref=@xmi.id]"
        use="elementID[1]/@xmi.idref"/>
    <!-- AssociationViews which are participating in a LinkAttributeView by association idref -->
    <xsl:key name="AssociationViewByAssociationRefKey"
        match="//mdElement[@elementClass='AssociationView'][//mdElement[@elementClass='LinkAttributeView']/linkFirstEndID/@xmi.idref=@xmi.id]"
        use="elementID[1]/@xmi.idref"/>
    <!-- LinkAttributeViews by ClassView id -->
    <xsl:key name="LinkAttributeViewByClassViewRefKey"
        match="//mdElement[@elementClass='LinkAttributeView']" use="linkSecondEndID/@xmi.idref"/>
    <xsl:template match="//UML:Class[key('ClassViewByClassRefKey', @xmi.id)]">
        <UML:AssociationClass>
            <!-- Attributes -->
            <xsl:apply-templates select="@*"/>
            <!-- Fetch the information required to create the resulting UML:AssociationClass -->
            <xsl:comment>NEWLY CREATED ASSOCIATION CLASS <xsl:value-of select="@xmi.id"/>
            </xsl:comment>
            <xsl:variable name="classViewId" select="key('ClassViewByClassRefKey', @xmi.id)/@xmi.id"/>
            <xsl:comment>classViewId = <xsl:value-of select="$classViewId"/>
            </xsl:comment>
            <xsl:variable name="linkAttributeView"
                select="key('LinkAttributeViewByClassViewRefKey', $classViewId)"/>
            <xsl:comment>linkAttributeViewId = <xsl:value-of select="$linkAttributeView/@xmi.id"/>
            </xsl:comment>
            <xsl:variable name="associationViewId"
                select="$linkAttributeView/linkFirstEndID/@xmi.idref"/>
            <xsl:comment>associationViewId = <xsl:value-of select="$associationViewId"/>
            </xsl:comment>
            <xsl:variable name="associationId"
                select="//mdElement[@elementClass='AssociationView' and @xmi.id=$associationViewId]/elementID/@xmi.idref"/>
            <xsl:comment>associationId = <xsl:value-of select="$associationId"/>
            </xsl:comment>
            <xsl:variable name="associationConnection"
                select="//UML:Association[@xmi.id=$associationId]/UML:Association.connection"/>
            <!-- First the UML:Association.connection -->
            <xsl:apply-templates select="$associationConnection"/>
            <!-- Then the original UML:Class content -->
            <xsl:apply-templates select="node()"/>
        </UML:AssociationClass>
    </xsl:template>
    <xsl:template match="//UML:Association[key('AssociationViewByAssociationRefKey', @xmi.id)]">
        <xsl:comment>ASSOCIATION <xsl:value-of select="@xmi.id"/> WAS SKIPPED</xsl:comment>
    </xsl:template>
</xsl:stylesheet>
