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

<!--
    Document   : xmi2svg.xsl
    Created on : September 14, 2003, 2:31 PM
    Author     : Bastian Stritt
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/2000/svg"
    xmlns:UML="org.omg.xmi.namespace.UML" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output media-type="image/svg+xml" method="xml"/>
	
	<xsl:template match="text()" />   
	
	<xsl:param name="diagramID" />
	
    
	<!-- ********************************************************
	        ** Kopf des SVG Dokuments. Mit der Groessen angabe **
	        ******************************************************** -->
	<xsl:template match="XMI/XMI.content/UML:Model">
		<xsl:param name="xmiDiagramID" />
		
		<!-- Which Diagram to transform? -->
		<xsl:variable name="DiagramID">
			<xsl:choose>
				<!-- Diagram choosen in xmi2svg.xsl -->
				<xsl:when test="$xmiDiagramID"><xsl:value-of select="$xmiDiagramID"/></xsl:when>
				<xsl:when test="$diagramID"><xsl:value-of select="$diagramID"/></xsl:when>								
				<!-- The first ClassDiagram found in XMI-File -->
				<xsl:otherwise>
				   	<xsl:value-of select="//UML:Diagram[position()=1][child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='CollaborationDiagram']]/attribute::xmi.id"/>
				</xsl:otherwise>
			</xsl:choose>			
		</xsl:variable>
		
	   	<!--
	   		Sometime the x-Pos of the most left Objects (Classes, Associations and so on) is negative. If this happens, we add the negative value to every other x-Pos, so that the diagram moves right, an nothing will be cut.
	   		The Reason is because the Position in Diagram are relative to other Diagrams. But in SVG the top right point ist 0,0 . No negative Values are allowed.
	   		The Same for the y-Pos of some Objects 
	   	-->
		<xsl:variable name="shift_x">	
			<xsl:call-template name="getGlobal_x">
				<xsl:with-param name="DiagramID" select="$DiagramID" />
			</xsl:call-template>			
		</xsl:variable>
	
		<xsl:variable name="shift_y">			
			<xsl:call-template name="getGlobal_y">
				<xsl:with-param name="DiagramID" select="$DiagramID" />
			</xsl:call-template>			
		</xsl:variable>	
	
		<!-- Auswahl der Breite -->
		<xsl:variable name="width">
			<xsl:call-template name="getWidth">			
				<xsl:with-param name="DiagramID" select="$DiagramID" />
			</xsl:call-template>
		</xsl:variable>
	
		<!-- Berechnen der Höhe -->
		<xsl:variable name="height">
			<xsl:call-template name="getHeight">
				<xsl:with-param name="DiagramID" select="$DiagramID" />
			</xsl:call-template>			
		</xsl:variable>
		
		<svg>
			<xsl:attribute name="width"><xsl:value-of select="$width + $shift_x + 20"/></xsl:attribute>
			<xsl:attribute name="height"><xsl:value-of select="$height + $shift_y + 20"/></xsl:attribute>		
			<defs>
				<!-- schwarze Pfeilspitze mit 3 Kanten -->
				<marker id="start_black_arrowhead" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="11" markerHeight="11" orient="0" fill="black">
					<path stroke="black" d="M 0 0 L 10 5 L 0 10 z"/>
				</marker>
				<marker id="start2_black_arrowhead" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="11" markerHeight="11" orient="180" fill="black">
					<path stroke="black" d="M 0 0 L 10 5 L 0 10 z"/>
				</marker>
				<marker id="end_black_arrowhead" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="11" markerHeight="11" orient="auto" fill="black">
					<path stroke="black" d="M 0 0 L 10 5 L 0 10 z"/>
				</marker>
			</defs>
			
			<xsl:apply-templates select="UML:Namespace.ownedElement//UML:Object" mode="communicationDiagram">
				<xsl:with-param name="DiagramID" select="$DiagramID" />
				<xsl:with-param name="shift_x" select="$shift_x" />
				<xsl:with-param name="shift_y" select="$shift_y" />				
			</xsl:apply-templates>			

			<xsl:text>
			</xsl:text>
		</svg>
	</xsl:template>
    
    
	<!-- ***************************
	        ** Die einzelnen Objekte **
	        *************************** -->	
	<xsl:template match="UML:Collaboration/UML:Namespace.ownedElement/UML:Object" mode="communicationDiagram">
		<xsl:param name="DiagramID" />
		<xsl:param name="shift_x" />
		<xsl:param name="shift_y" />
		
		<xsl:variable name="name" select="@name" />
		<xsl:variable name="id" select="@xmi.id" />
		
		<xsl:variable name="classifierID" select="UML:Instance.classifier/*/attribute::xmi.idref" />
		<xsl:variable name="classifierName" select="//*[attribute::xmi.id=$classifierID]/attribute::name" />

		<xsl:variable name="control" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]/UML:GraphElement.position/XMI.field[position()=1]" />
		
		<xsl:if test="$control">
		
			<!-- x/y Position + height/width  des Objects -->
			<xsl:variable name="pos_x" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]/UML:GraphElement.position/XMI.field[position()=1]"/>
			<xsl:variable name="pos_y" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]/UML:GraphElement.position/XMI.field[position()=2]"/>
			<xsl:variable name="width" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]/UML:GraphNode.size/XMI.field[position()=1]"/>
			<xsl:variable name="height" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]/UML:GraphNode.size/XMI.field[position()=2]"/>		
	
			<!-- x/y Position + height/width  des NameCompartments -->
			<xsl:variable name="ncPos_x" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]//UML:GraphElement.contained/UML:GraphNode[child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='NameCompartment']]/UML:GraphElement.position/XMI.field[position()=1]" />		
			<xsl:variable name="ncPos_y" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]//UML:GraphElement.contained/UML:GraphNode[child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='NameCompartment']]/UML:GraphElement.position/XMI.field[position()=2]" />
			
			<!-- x/y Position + height/width  des Name/Types -->		
			<xsl:variable name="ntPos_x" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]//UML:GraphElement.contained/UML:GraphNode[child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='NameAndType']]/UML:GraphElement.position/XMI.field[position()=1]" />		
			<xsl:variable name="ntPos_y" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]//UML:GraphElement.contained/UML:GraphNode[child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='NameAndType']]/UML:GraphElement.position/XMI.field[position()=2]" />		
	
			<!-- x/y Position + height/width  des Names -->
			<xsl:variable name="namePos_x" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]//UML:GraphElement.contained/UML:GraphNode[child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='Name']]/UML:GraphElement.position/XMI.field[position()=1]" />
			<xsl:variable name="namePos_y" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::*/*/*/UML:Object[attribute::xmi.idref=$id]]//UML:GraphElement.contained/UML:GraphNode[child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='Name']]/UML:GraphElement.position/XMI.field[position()=2]" />
	
			<xsl:text>
			</xsl:text>
		
			<line x1="{$pos_x + ($width div 2) + $shift_x}" x2="{$pos_x + $width div 2 + $shift_x}" y1="{$pos_y + $shift_y}" y2="{$pos_y + $shift_y}" style="stroke:black;stroke-width=5;" stroke-dasharray="5,5"/>						
	
			<xsl:text>
			</xsl:text>
			<xsl:comment>Object Header</xsl:comment>
			<xsl:text>
			</xsl:text>
			
			<!-- aktiviert? -->
			<xsl:variable name="tagDefinitionID" select="UML:ModelElement.taggedValue//UML:TagDefinition/attribute::xmi.idref" />
			<xsl:variable name="tagDefinitionValue" select="UML:ModelElement.taggedValue//UML:TaggedValue.dataValue" />		
			<xsl:text>
			</xsl:text>
			
			<rect 	x="{$pos_x +  $shift_x}" y="{$pos_y  + $shift_y}"
				height="{$height}"
				width="{$width}" fill="none" stroke="black">
				<xsl:if test="$tagDefinitionValue = 'true'">
					<xsl:if test="//UML:TagDefinition[attribute::xmi.id=$tagDefinitionID]/attribute::name = 'isActive' ">
						<xsl:attribute name="stroke-width">3</xsl:attribute>								
					</xsl:if>
				</xsl:if>
			</rect>				
			<xsl:text>
			</xsl:text>
			<!-- Objektname -->				
			<text style="text-decoration:underline;font-size:11px">
				<xsl:attribute name="x"><xsl:value-of select="$pos_x + $ncPos_x + $namePos_x + $shift_x"/> </xsl:attribute>
				<xsl:attribute name="y"><xsl:value-of select="$pos_y + $ncPos_y + $namePos_y + 12 + $shift_y"/> </xsl:attribute>
				<xsl:value-of select="$name"/>
				<xsl:if test="$classifierName">
					:<xsl:value-of select="$classifierName"/>
				</xsl:if>
			</text>
	
			<xsl:apply-templates select="*/UML:Link" mode="communicationDiagram">
				<xsl:with-param name="DiagramID" select="$DiagramID" />
				<xsl:with-param name="shift_x" select="$shift_x" />
				<xsl:with-param name="shift_y" select="$shift_y" />				
			</xsl:apply-templates>
		</xsl:if>
		
	</xsl:template>	   
	
	<!-- ************************
	        ** Die einzelnen Links **
	        ************************ -->	
	<xsl:template match="UML:Link" mode="communicationDiagram">		
		<xsl:param name="DiagramID" />
		<xsl:param name="shift_x" />
		<xsl:param name="shift_y" />
	
		<xsl:variable name="id" select="@xmi.id" />

		<!-- Die ID des zugehörigen UML:GraphEdge -->
		<xsl:variable name="edgeID" select="//UML:GraphEdge[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][descendant::UML:Link[attribute::xmi.idref=$id]]/attribute::xmi.id" />

		<!-- Die IDs der beiden Connectoren -->
		<xsl:variable name="connectorID_1" select="//UML:GraphEdge[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][attribute::xmi.id=$edgeID]/UML:GraphEdge.anchor/UML:GraphConnector[position()=1]/attribute::xmi.idref" />
		<xsl:variable name="connectorID_2" select="//UML:GraphEdge[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][attribute::xmi.id=$edgeID]/UML:GraphEdge.anchor/UML:GraphConnector[position()=2]/attribute::xmi.idref" />
		
		<xsl:variable name="actionType" select="local-name(//UML:Stimulus[child::UML:Stimulus.communicationLink/UML:Link[attribute::xmi.idref=$id]]/UML:Stimulus.dispatchAction/*)" />
	
		<!-- Die ID des Receivers des Stimulus (da wo die Pfeilspitze ist) -->
		<xsl:variable name="receiverID" select="//UML:Stimulus[child::UML:Stimulus.communicationLink/UML:Link[attribute::xmi.idref=$id]]/UML:Stimulus.receiver/*/attribute::xmi.idref" />
		
		<xsl:variable name="stimulusName" select="//UML:Stimulus[child::UML:Stimulus.communicationLink/UML:Link[attribute::xmi.idref=$id]]/attribute::name" />
		<xsl:variable name="stimulusID" select="//UML:Stimulus[child::UML:Stimulus.communicationLink/UML:Link[attribute::xmi.idref=$id]]/attribute::xmi.id" />

		<xsl:variable name="ActionID" select="//UML:Stimulus[child::UML:Stimulus.communicationLink/UML:Link[attribute::xmi.idref=$id]]/UML:Stimulus.dispatchAction/*/attribute::xmi.idref" />
		<xsl:variable name="ActionName" select="//*[attribute::xmi.id=$ActionID]/attribute::name" />		
		
		<xsl:variable name="stimPos_x" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::UML:GraphElement.semanticModel/*/*/UML:Stimulus[attribute::xmi.idref=$stimulusID]]/UML:GraphElement.position/XMI.field[position()=1]" />
		<xsl:variable name="stimPos_y" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::UML:GraphElement.semanticModel/*/*/UML:Stimulus[attribute::xmi.idref=$stimulusID]]/UML:GraphElement.position/XMI.field[position()=2]" />		
		
		<xsl:variable name="arrow_x" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::UML:GraphElement.semanticModel/*[attribute::typeInfo='Arrow']][ancestor::UML:GraphEdge[attribute::xmi.id=$edgeID]]/UML:GraphElement.position/XMI.field[position()=1]" />
		<xsl:variable name="arrow_y" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::UML:GraphElement.semanticModel/*[attribute::typeInfo='Arrow']][ancestor::UML:GraphEdge[attribute::xmi.id=$edgeID]]/UML:GraphElement.position/XMI.field[position()=2]" />		
		<xsl:variable name="arrow_width" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::UML:GraphElement.semanticModel/*[attribute::typeInfo='Arrow']][ancestor::UML:GraphEdge[attribute::xmi.id=$edgeID]]/UML:GraphNode.size/XMI.field[position()=1]" />
		<xsl:variable name="arrow_height" select="//UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::UML:GraphElement.semanticModel/*[attribute::typeInfo='Arrow']][ancestor::UML:GraphEdge[attribute::xmi.id=$edgeID]]/UML:GraphNode.size/XMI.field[position()=2]" />		
		
		<xsl:variable name="path">
			<xsl:call-template name="buildPath">
				<xsl:with-param name="waypoints" select="//UML:GraphEdge[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][descendant::UML:Link[attribute::xmi.idref=$id]]/UML:GraphEdge.waypoints/XMI.field" />
				<xsl:with-param name="pos" select="count(//UML:GraphEdge[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][descendant::UML:Link[attribute::xmi.idref=$id]]/UML:GraphEdge.waypoints/XMI.field) -1" />
				<xsl:with-param name="package_x" select="0 + $shift_x" />
				<xsl:with-param name="package_y" select="0 + $shift_y" />							
			</xsl:call-template> 
		</xsl:variable>
		
		<xsl:text>
		</xsl:text>
		
		<!-- Die Linie -->    
		<path stroke="black" fill="none"> 
			<xsl:attribute name="d">M<xsl:value-of select="substring-after($path,'L')"/></xsl:attribute>
		</path>		

		<xsl:text>
		</xsl:text>
		
		<xsl:if test="$stimPos_x">			
			<!-- Die Connectoren sind ordered. D.h. der erste Connector ist dem ersten Waypoint des GraphEdge zugeordnet. Der 2. Connector dem letzten Waypoint. 
				Hier teste ich nun, welcher Connector der erste ist -->
			<xsl:variable name="connectorTest_1" select="//UML:GraphConnector[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][attribute::xmi.id=$connectorID_1][following::UML:GraphConnector[attribute::xmi.id=$connectorID_2]]" />		
		
			<!-- Hier teste ich ob die connectorID_1 unterhalb der Activation des Receiver-Objects angeordnet ist. Wenn ja, dann muß an diesen Connector die Pfeilspitze angebracht werden -->
			<xsl:variable name="connectorTest_3" select=" //UML:GraphNode[ancestor::UML:Diagram[attribute::xmi.id=$DiagramID]][child::UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement[attribute::typeInfo='Activation']][child::UML:GraphElement.anchorage/UML:GraphConnector[attribute::xmi.id=$connectorID_1]][../../UML:GraphElement.semanticModel/*/*/UML:Object[attribute::xmi.idref=$receiverID]]" />
			
			<!-- Der Pfeil -->    
			<path stroke="black" fill="none"> 
				<xsl:attribute name="d">M<xsl:value-of select="$stimPos_x + $arrow_x + $shift_x"/>,<xsl:value-of select="$stimPos_y + $arrow_y + ($arrow_height div 2) + $shift_y"/> L<xsl:value-of select="$stimPos_x + $arrow_x + $shift_x + $arrow_width"/>,<xsl:value-of select="$stimPos_y + $arrow_y + ($arrow_height div 2) + $shift_y"/></xsl:attribute>
				<xsl:choose>
					<xsl:when test="$connectorTest_1"><!-- connectorID_1 ist vor connectorID_2 -->
						<xsl:if test="$connectorTest_3"><!-- connectorID_1 ist unterhalb der Activation des Receiver-Objects -->
							<xsl:attribute name="marker-start">url(#start2_black_arrowhead)</xsl:attribute>
						</xsl:if>
						<xsl:if test="not($connectorTest_3)"><!-- connectorID_1 ist nicht unterhalb der Activation des Receiver-Objects -->
							<xsl:attribute name="marker-end">url(#end_black_arrowhead)</xsl:attribute>
						</xsl:if>					
					</xsl:when>
					<xsl:when test="not($connectorTest_1)"><!-- connectorID_2 ist vor connectorID_1 -->
						<xsl:if test="$connectorTest_3"><!-- connectorID_1 ist unterhalb der Activation des Receiver-Objects -->
							<xsl:attribute name="marker-start">url(#start_black_arrowhead)</xsl:attribute>
						</xsl:if>
						<xsl:if test="not($connectorTest_3)"><!-- connectorID_1 ist nicht unterhalb der Activation des Receiver-Objects -->
							<xsl:attribute name="marker-end">url(#end_black_arrowhead)</xsl:attribute>
						</xsl:if>
					</xsl:when>				
				</xsl:choose>			
			</path>		

			<!-- Name des Stimulus -->
			<text style="font-size:11px">
				<xsl:attribute name="x"><xsl:value-of select="$stimPos_x + $shift_x + $arrow_width"/></xsl:attribute>
				<xsl:attribute name="y"><xsl:value-of select="$stimPos_y + 12 + $shift_y"/></xsl:attribute>
				<xsl:value-of select="$stimulusName"/>:<xsl:value-of select="$ActionName"/>
				<xsl:if test="$ActionName='' ">anonym</xsl:if>
			</text>
		</xsl:if>
	</xsl:template>

	<xsl:template name="getGlobal_x">
		<xsl:param name="DiagramID" />
		
		<xsl:variable name="temp">
			<xsl:for-each select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphNode/UML:GraphElement.position">
			      <xsl:sort select="XMI.field[1]" data-type="number" order="ascending" />
			      <xsl:if test="position() = 1">
			         <xsl:value-of select="XMI.field[1]" />
			      </xsl:if>
			</xsl:for-each>
		</xsl:variable>

		<xsl:variable name="temp_2">
			<xsl:for-each select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphEdge/UML:GraphEdge.waypoints/XMI.field">
			      <xsl:sort select="XMI.field[1]" data-type="number" order="ascending" />
			      <xsl:if test="position() = 1">
			         <xsl:value-of select="XMI.field[1]" />
			      </xsl:if>
			</xsl:for-each>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="number($temp) &lt; 0">
				<xsl:if test="number($temp) &lt; number($temp_2)"><xsl:value-of select="($temp * -1) + 40"/></xsl:if>
				<xsl:if test="number($temp_2) &lt; number($temp)"><xsl:value-of select="($temp_2 * -1) + 40"/></xsl:if>
			</xsl:when>
			<xsl:when test="number($temp_2) &lt; 0">
				<xsl:if test="number($temp) &lt; number($temp_2)"><xsl:value-of select="($temp * -1) + 40"/></xsl:if>
				<xsl:if test="number($temp_2) &lt; number($temp)"><xsl:value-of select="($temp_2 * -1) + 40"/></xsl:if>				
			</xsl:when>			
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>
	
	<xsl:template name="getGlobal_y">
		<xsl:param name="DiagramID" />

		<xsl:variable name="temp">
			<xsl:for-each select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphNode/UML:GraphElement.position">
			      <xsl:sort select="XMI.field[2]" data-type="number" order="ascending" />
			      <xsl:if test="position() = 1">
			      	<xsl:value-of select="XMI.field[2]" />
			      </xsl:if>
			   </xsl:for-each>		
		</xsl:variable>

		<xsl:variable name="temp_2">
			<xsl:for-each select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphEdge/UML:GraphEdge.waypoints/XMI.field">
			      <xsl:sort select="XMI.field[2]" data-type="number" order="ascending" />
			      <xsl:if test="position() = 1">
			         <xsl:value-of select="XMI.field[2]" />
			      </xsl:if>
			   </xsl:for-each>						
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="number($temp) &lt; 0">
				<xsl:if test="number($temp) &lt; number($temp_2)"><xsl:value-of select="(number($temp) * -1) + 40"/></xsl:if>
				<xsl:if test="number($temp_2) &lt; number($temp)"><xsl:value-of select="(number($temp_2) * -1) + 40"/></xsl:if>
			</xsl:when>
			<xsl:when test="number($temp_2) &lt; 0">
				<xsl:if test="number($temp) &lt; number($temp_2)"><xsl:value-of select="(number($temp) * -1) + 40"/></xsl:if>
				<xsl:if test="number($temp_2) &lt; number($temp)"><xsl:value-of select="(number($temp_2) * -1) + 40"/></xsl:if>				
			</xsl:when>			
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>
	
	<xsl:template name="getWert_1">
		<xsl:param name="nodes" />
		
		<xsl:choose>
			<xsl:when test="$nodes">
				<xsl:variable name="wert_1">
					<xsl:value-of select="$nodes[position()=last()]/UML:GraphElement.position/XMI.field[1]"/>
				</xsl:variable>
				<xsl:variable name="wert_2">
					<xsl:value-of select="$nodes[position()=last()]/UML:GraphNode.size/XMI.field[1]"/>
				</xsl:variable>
				<xsl:variable name="pruef"><xsl:value-of select="$wert_1 + $wert_2"/></xsl:variable>
	
				<xsl:variable name="large">
					<xsl:call-template name="getWert_1">
						<xsl:with-param name="nodes" select="$nodes[position()!=last()]" />
					</xsl:call-template>
				</xsl:variable>
	
				<xsl:choose>
					<xsl:when test="number($pruef) &gt; number($large)">
						<xsl:value-of select="number($pruef)"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="number($large)"/>
					</xsl:otherwise>
				</xsl:choose>			
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="0"/></xsl:otherwise>			
		</xsl:choose>
	</xsl:template>

	<xsl:template name="getWert_2">
		<xsl:param name="nodes" />
		
		<xsl:choose>
			<xsl:when test="$nodes">
				<xsl:variable name="wert_1">
					<xsl:value-of select="$nodes[position()=last()]/UML:GraphElement.position/XMI.field[2]"/>
				</xsl:variable>
				<xsl:variable name="wert_2">
					<xsl:value-of select="$nodes[position()=last()]/UML:GraphNode.size/XMI.field[2]"/>
				</xsl:variable>
				<xsl:variable name="pruef"><xsl:value-of select="number($wert_1) + number($wert_2)"/></xsl:variable>
	
				<xsl:variable name="large">
					<xsl:call-template name="getWert_2">
						<xsl:with-param name="nodes" select="$nodes[position()!=last()]" />
					</xsl:call-template>			
				</xsl:variable>
	
				<xsl:choose>
					<xsl:when test="number($pruef) &gt; number($large)">
						<xsl:value-of select="$pruef"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$large"/>
					</xsl:otherwise>
				</xsl:choose>			
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="0"/></xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="getWidth">
		<xsl:param name="DiagramID" />
		
		<!-- welcher Punkt der Klassen hat den höchsten X-Wert -->
		<xsl:variable name="temp">
			<xsl:call-template name="getWert_1">
				<xsl:with-param name="nodes" select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphNode" />
			</xsl:call-template>			  
		</xsl:variable>

		<!-- Welcher Wegpunkt hat den höchsten X-Wert -->
 		<xsl:variable name="temp_2">
			<xsl:for-each select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphEdge/UML:GraphEdge.waypoints/XMI.field">
				<xsl:sort select="XMI.field[1]" data-type="number" order="descending" />
				<xsl:if test="position() = 1">
					 <xsl:value-of select="XMI.field[1]" />
				 </xsl:if>
			 </xsl:for-each>
		</xsl:variable>

		<xsl:choose>
			<xsl:when test="number($temp_2)">
				<xsl:choose>
			 		<xsl:when test="number($temp) &gt; number($temp_2)"><xsl:value-of select="number($temp) + 20"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="number($temp_2) + 20"/></xsl:otherwise>
				</xsl:choose>			
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="number($temp)"/></xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="getHeight">
		<xsl:param name="DiagramID" />
		
		<xsl:variable name="temp">
			<xsl:call-template name="getWert_2">
				<xsl:with-param name="nodes" select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphNode" />			   	
			</xsl:call-template>			  			
		</xsl:variable>
		
		<!-- welcher Waypoint hat den höchsten y-Wert -->
		<xsl:variable name="temp_2">
			<xsl:for-each select="//UML:Diagram[attribute::xmi.id=$DiagramID]/UML:GraphElement.contained/UML:GraphEdge/UML:GraphEdge.waypoints/XMI.field">
			      <xsl:sort select="XMI.field[2]" data-type="number" order="descending" />
			      <xsl:if test="position() = 1">
			         <xsl:value-of select="XMI.field[2]" />
			      </xsl:if>
			   </xsl:for-each>
		</xsl:variable>
		
		<!-- Auswahl der Höhe -->
		<xsl:choose>
			<xsl:when test="number($temp_2)">
				<xsl:choose>
			 		<xsl:when test="number($temp) &gt; number($temp_2)"><xsl:value-of select="number($temp) + 20"/></xsl:when>
					<xsl:otherwise><xsl:value-of select="number($temp_2) + 20"/></xsl:otherwise>
				</xsl:choose>			
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="number($temp)"/></xsl:otherwise>
		</xsl:choose>

	</xsl:template>

	 <!-- The Stereotypes with Separator, Start and End -->
	<xsl:template match="UML:ModelElement.stereotype">
		<xsl:param name="object_x"/>
		<xsl:param name="object_y"/>
		<xsl:param name="package_x"/>
		<xsl:param name="package_y"/>
		<xsl:param name="graphNodeID"/>
		
		<xsl:variable name="stereotypeCompartment_x" select="//UML:GraphNode[attribute::xmi.id=$graphNodeID]/UML:GraphElement.position/XMI.field[position()=1]" />
		<xsl:variable name="stereotypeCompartment_y" select="//UML:GraphNode[attribute::xmi.id=$graphNodeID]/UML:GraphElement.position/XMI.field[position()=2]" />
		
		<xsl:for-each select="//UML:GraphNode[attribute::xmi.id=$graphNodeID]/UML:GraphElement.contained/UML:GraphNode">
			<xsl:variable name="stereotypeElement_x" select="UML:GraphElement.position/XMI.field[position()=1]" />
			<xsl:variable name="stereotypeElement_y" select="UML:GraphElement.position/XMI.field[position()=2]" />

			<xsl:variable name="stereotypeElement_typeInfo" select="UML:GraphElement.semanticModel/UML:SimpleSemanticModelElement/attribute::typeInfo" />
			<xsl:choose>
				<xsl:when test="$stereotypeElement_typeInfo = 'StereotypeStart'">
					<text x="{$object_x +$package_x + $stereotypeElement_x + $stereotypeCompartment_x + 12}" y="{$object_y +12 + $package_y + $stereotypeElement_y + $stereotypeCompartment_y}" font-weight="normal" style="font-size:11px">
						«
					</text>
				</xsl:when>
				<xsl:when test="$stereotypeElement_typeInfo = 'StereotypeEnd'">
					<text x="{$object_x +$package_x + $stereotypeElement_x  + $stereotypeCompartment_x}" y="{$object_y +12 + $package_y + $stereotypeElement_y + $stereotypeCompartment_y}" font-weight="normal" style="font-size:11px">
						»
					</text>
				</xsl:when>
				<xsl:when test="$stereotypeElement_typeInfo = 'StereotypeSeparator'">
					<text x="{$object_x +$package_x + $stereotypeElement_x + $stereotypeCompartment_x}" y="{$object_y +12 + $package_y + $stereotypeElement_y + $stereotypeCompartment_y}" font-weight="normal" style="font-size:11px">
						,
					</text>
				</xsl:when>
			</xsl:choose>
			
			<xsl:variable name="stereotypeID" select="UML:GraphElement.semanticModel/UML:Uml1SemanticModelBridge/UML:Uml1SemanticModelBridge.element/UML:Stereotype/attribute::xmi.idref" />

			<xsl:if test="$stereotypeID != '' ">
				<text x="{$object_x +$package_x + $stereotypeElement_x + $stereotypeCompartment_x}" y="{$object_y  + $stereotypeElement_y +12 + $package_y + $stereotypeCompartment_y}" font-weight="normal" style="font-size:11px">
					<xsl:if test="//UML:Stereotype[attribute::xmi.id=$stereotypeID]/attribute::isAbstract = 'true' ">
						<xsl:attribute name="font-style">oblique</xsl:attribute>
					</xsl:if>                    
					<xsl:if test="//UML:Stereotype[attribute::xmi.id=$stereotypeID]/attribute::name = '' "><xsl:text>anonym</xsl:text></xsl:if>
					<xsl:value-of select="//UML:Stereotype[attribute::xmi.id=$stereotypeID]/attribute::name"/>
				</text>            	
			</xsl:if>
		</xsl:for-each>
	</xsl:template>
	
	<!-- Baut den Pfad aus den Waypoints auf -->
	<xsl:template name="buildPath">
		<xsl:param name="waypoints" />
		<xsl:param name="pos" />
		<xsl:param name="package_x" />
		<xsl:param name="package_y" />
		<xsl:choose>
			<xsl:when test="$waypoints">
				<xsl:variable name="first" select="$waypoints[position()=last()]" />		
				<xsl:call-template name="buildPath">
					<xsl:with-param name="waypoints" select="$waypoints[position()!=last()]" />
					<xsl:with-param name="pos" select="$pos - 1" />
					<xsl:with-param name="package_x" select="$package_x" />
					<xsl:with-param name="package_y" select="$package_y" />										
				</xsl:call-template>
				<xsl:if test="($pos mod 3)=0">
					<xsl:text>L</xsl:text>			
					<xsl:value-of select="$first/XMI.field[position()=1] + $package_x"/><xsl:text> </xsl:text><xsl:value-of select="$first/XMI.field[position()=2]  + $package_y"/><xsl:text> </xsl:text>
				</xsl:if>
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<!-- Schreibt das richtige Zeichen der jeweiligen Sichtbarkeit auf -->
	<xsl:template name="getVisibility">
		<xsl:param name="visibility" />
		<xsl:choose>
			<xsl:when test="$visibility = 'public' ">+</xsl:when>
			<xsl:when test="$visibility = 'protected' ">#</xsl:when>
			<xsl:when test="$visibility = 'package' ">~</xsl:when>
			<xsl:when test="$visibility = 'private' ">-</xsl:when>                    
		</xsl:choose>                    
	</xsl:template>
	
	
</xsl:stylesheet>

