diff xslt/generator.csharp.xsl @ 3:437127ab6a12

non-working version separate common model templates from csharp templates, plans to support complex primary and foreign keys
author cin
date Wed, 28 Feb 2018 02:10:29 +0300
parents 035de8b7b18e
children d240adc2ac70
line wrap: on
line diff
--- a/xslt/generator.csharp.xsl	Sun Feb 25 17:12:33 2018 +0300
+++ b/xslt/generator.csharp.xsl	Wed Feb 28 02:10:29 2018 +0300
@@ -9,13 +9,24 @@
 	
 	<xsl:output method="xml" indent="yes" />
 	
+	<!-- generate code for primary package -->
+	<xsl:template match="m:package[@primary='true' and @clr:namespace]"	mode="document">
+		<cs:namespace name="{@clr:namespace}">
+			<xsl:apply-templates mode="document" />
+		</cs:namespace>
+	</xsl:template>
+
+	<xsl:template match="m:package[@primary='true']" mode="document">
+		<xsl:apply-templates mode="document" />
+	</xsl:template>
+	
 	<!-- entity -->
 	<xsl:template match="m:entity" mode="document">
 		<xsl:apply-templates select="." mode="entity"/>
 	</xsl:template>
 	
 	<xsl:template match="m:entity" mode="entity">
-		<cs:class name="{@name}" modifiers="partial">
+		<cs:class modifiers="partial">
 			<xsl:attribute name="name">
 				<xsl:apply-templates select="." mode="class-name"/>
 			</xsl:attribute>
@@ -113,6 +124,7 @@
 	<xsl:template match="*" mode="property">
 		<cs:property modifiers="public">
 			<xsl:attribute name="name"><xsl:apply-templates select="." mode="property-name"/></xsl:attribute>
+			<xsl:attribute name="modifiers"><xsl:apply-templates select="." mode="property-modifiers"/></xsl:attribute>
 			<xsl:apply-templates select="." mode="property-type"/>
 			<xsl:apply-templates select="." mode="property-attributes"/>
 			<xsl:apply-templates select="." mode="property-extension" />
@@ -134,6 +146,11 @@
 		<xsl:apply-templates select="." mode="member-name"/>
 	</xsl:template>
 	
+	<!-- property-modifiers -->
+	<xsl:template match="*" mode="property-modifiers">
+		<xsl:text>public</xsl:text>
+	</xsl:template>
+	
 	<!-- property-type -->
 	<xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="property-type">
 		<xsl:apply-templates select=".." mode="property-type"/>
@@ -146,14 +163,14 @@
 	</xsl:template>
 	
 	<xsl:template match="m:hasA[@type]/m:thisKey" mode="property-type">
-		<xsl:call-template name="getKeyType">
+		<xsl:call-template name="getPrimaryKey">
 			<xsl:with-param name="type" select="../@type"/>
 		</xsl:call-template>
 	</xsl:template>
 	
 	<xsl:template match="m:hasA[@type and boolean(@optional)]/m:thisKey" mode="property-type">
 		<cs:nullable>
-			<xsl:call-template name="getKeyType">
+			<xsl:call-template name="getPrimaryKey">
 				<xsl:with-param name="type" select="../@type"/>
 			</xsl:call-template>
 		</cs:nullable>
@@ -375,5 +392,85 @@
 		</xsl:for-each>
 	</xsl:template>
 	
+	<!-- resolves CLR type for the given type -->
+	<xsl:template name="getClrType">
+		<xsl:param name="type" />
+		<xsl:param name="typeArgs" select="/.." />
+		<!-- <t:trace msg="resolveClrType {$type}"/> -->
+		<xsl:for-each select="$module">
+			<xsl:apply-templates select="key('type', $type)"
+				mode="clr-type">
+				<xsl:with-param name="typeArgs" select="$typeArgs"/>
+			</xsl:apply-templates>
+		</xsl:for-each>
+	</xsl:template>
+	
+	<!-- CLR type construction -->
+	<xsl:template match="*" mode="clr-type" />
+
+	<xsl:template match="m:type[clr:type]" mode="clr-type">
+		<xsl:param name="typeArgs" select="clr:type/*" />
+		<xsl:apply-templates select="clr:type" mode="clr-type">
+			<xsl:with-param name="typeArgs" select="$typeArgs" />
+		</xsl:apply-templates>
+	</xsl:template>
+
+	<xsl:template match="m:entity" mode="clr-type">
+		<cs:type name="{@clr:name | @name[not(../@clr:name)]}" namespace="{ancestor::*[@clr:namespace]/@clr:namespace}" />
+	</xsl:template>
+	
+	<xsl:template match="clr:type" mode="clr-type">
+		<xsl:apply-templates mode="clr-type"/>
+	</xsl:template>
+
+	<xsl:template match="clr:type[@ref]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<xsl:call-template name="getClrType">
+			<xsl:with-param name="type" select="@ref" />
+			<xsl:with-param name="typeArgs" select="$typeArgs" />
+		</xsl:call-template>
+	</xsl:template>
+
+	<xsl:template match="clr:type[@cs:name or @name]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<xsl:variable name="ns"
+			select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
+		<cs:type name="{@cs:name | @name[not(../@cs:name)]}">
+			<xsl:if test="$ns">
+				<xsl:attribute name="namespace"><xsl:value-of select="$ns" /></xsl:attribute>
+			</xsl:if>
+			<xsl:copy-of select="@struct"/>
+			
+			<xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
+				mode="clr-type" />
+		</cs:type>
+	</xsl:template>
+
+	<xsl:template match="clr:arrayOf[@type]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<cs:array>
+			<xsl:call-template name="getClrType">
+				<xsl:with-param name="type" select="@type" />
+				<xsl:with-param name="typeArgs" select="$typeArgs" />
+			</xsl:call-template>
+		</cs:array>
+	</xsl:template>
+
+	<xsl:template match="clr:arrayOf[@cs:name or @name]" mode="clr-type">
+		<xsl:param name="typeArgs" select="*" />
+		<xsl:variable name="ns"
+			select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
+		<cs:array>
+			<cs:type name="{@cs:name | @name[not(../@cs:name)]}">
+				<xsl:if test="$ns">
+					<xsl:attribute name="namespace"><xsl:value-of
+						select="$ns" /></xsl:attribute>
+				</xsl:if>
+				<xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
+					mode="clr-type" />
+			</cs:type>
+		</cs:array>
+	</xsl:template>
+	
 	
 </xsl:stylesheet>
\ No newline at end of file