Mercurial > pub > ModelGenerator
changeset 5:6f67d3a6b950
sync
author | cin |
---|---|
date | Fri, 02 Mar 2018 18:34:19 +0300 |
parents | d240adc2ac70 |
children | 1f4009d4afb6 |
files | data/sample.xml xslt/generator.csharp.xsl |
diffstat | 2 files changed, 81 insertions(+), 84 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/sample.xml Fri Mar 02 18:34:19 2018 +0300 @@ -0,0 +1,19 @@ +<?xml version="1.0"?> +<package xmlns="http://implab.org/schemas/data-model.v1.xsd" + xmlns:f="http://implab.org/schemas/data-model/geo.v1.xsd" xmlns:clr="http://implab.org/schemas/data-model/dotnet.v1.xsd" + xmlns:sql="http://implab.org/schemas/data-model/sql.v1.xsd" xmlns:esri="http://geopallada.ru/schemas/data-model/esri.v1.xsd" + name="users" version="1.0" clr:namespace="Sample.Model"> + + <import href="types.xml"/> + + <entity name="Gallery"> + <primaryKey name="Id" type="uuid"/> + + </entity> + + <entity name="GalleryItemLink"> + <primaryKey> + </primaryKey> + <hasA /> + </entity> +</package> \ No newline at end of file
--- a/xslt/generator.csharp.xsl Thu Mar 01 16:53:23 2018 +0300 +++ b/xslt/generator.csharp.xsl Fri Mar 02 18:34:19 2018 +0300 @@ -26,13 +26,20 @@ </xsl:template> <xsl:template match="m:entity" mode="entity"> + <!-- select all members --> <xsl:variable name="members"> <xsl:apply-templates mode="fiter-members"/> </xsl:variable> + <cs:class modifiers="partial"> + <!-- generate class name --> <xsl:attribute name="name"> <xsl:apply-templates select="." mode="class-name"/> </xsl:attribute> + <xsl:attribute name="modifiers"> + <xsl:apply-templates select="." mode="class-modifiers"/> + </xsl:attribute> + <xsl:apply-templates mode="class-attributes" /> <xsl:apply-templates select="exsl:node-set($members)" mode="members" /> </cs:class> @@ -47,6 +54,11 @@ <xsl:value-of select="@clr:name"/> </xsl:template> + <!-- class-modifiers --> + <xsl:template match="*" mode="class-modifiers"> + <xsl:text>partial</xsl:text> + </xsl:template> + <!-- class-attributes --> <xsl:template match="*|text()" mode="class-attributes" /> @@ -66,12 +78,6 @@ <xsl:apply-templates select="." mode="property"/> </xsl:template> - <xsl:template match="m:thisKey" mode="members"> - <xsl:variable name="properties"> - - </xsl:variable> - </xsl:template> - <!-- hasA and hasMany doesn't generate members itself, they delegate this work to inner members --> <xsl:template match="m:hasA | m:hasMany" mode="members"> <t:trace msg="{name()} {@name}" /> @@ -141,6 +147,7 @@ </cs:property> </xsl:template> + <!-- property-name --> <xsl:template match="m:hasA/clr:association[not(@name|@clr:name)]" mode="property-name"> <!-- if the association doesn't define a name, use it from the parent node --> @@ -161,6 +168,10 @@ </xsl:template> <!-- property-type --> + <xsl:template match="*" mode="property-type"> + <xsl:apply-templates select="." mode="member-type"/> + </xsl:template> + <xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="property-type"> <xsl:apply-templates select=".." mode="property-type"/> </xsl:template> @@ -172,21 +183,21 @@ </xsl:template> <xsl:template match="m:hasA[@type]/m:thisKey" mode="property-type"> - <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:variable name="otherKey"> <xsl:call-template name="getPrimaryKey"> <xsl:with-param name="type" select="../@type"/> </xsl:call-template> - </cs:nullable> - </xsl:template> - - <xsl:template match="*" mode="property-type"> - <xsl:apply-templates select="." mode="member-type"/> + </xsl:variable> + <xsl:choose> + <xsl:when test="@optional"> + </xsl:when> + <cs:nullable> + <xsl:apply-templates select="exsl:node-set($otherKey)" mode="property-type"/> + </cs:nullable> + <xsl:otherwise> + <xsl:apply-templates select="exsl:node-set($otherKey)" mode="property-type"/> + </xsl:otherwise> + </xsl:choose> </xsl:template> <!-- property-attributes --> @@ -329,77 +340,44 @@ <!-- field-initializer --> <xsl:template match="*|text()" mode="field-initializer"/> - <xsl:template name="getKeyClrMemberName"> - <xsl:param name="explicitMemberName" select="''"/> + <xsl:template name="getClrKeyReference"> + <xsl:param name="type"/> + <xsl:variable name="primaryKey"> + <xsl:call-template name="getPrimaryKey"> + <xsl:with-param name="type" select="$type"/> + </xsl:call-template> + </xsl:variable> + + <xsl:apply-templates select="exsl:node-set($primaryKey)" mode="clr-member-reference"/> + </xsl:template> + + <!-- --> + <xsl:template name="getClrMemberReference"> <xsl:param name="type"/> - <xsl:param name="short" select="false()"/> + <xsl:param name="memberName"/> + <xsl:param name="scope" select="members"/> + <xsl:variable name="member"> + <xsl:call-template name="getMembers"> + <xsl:with-param name="memberName" select="$memberName"/> + <xsl:with-param name="type" select="$type"/> + </xsl:call-template> + </xsl:variable> + + <xsl:apply-templates select="exsl:node-set($member)" mode="clr-member-reference"/> + </xsl:template> + + <xsl:template match="*" mode="clr-member-reference"> + <xsl:param name="$type" select="@declaringType"/> <cs:member> - <xsl:choose> - <xsl:when test="$explicitMemberName"> - <xsl:attribute name="name"> - <xsl:value-of select="$explicitMemberName"/> - </xsl:attribute> - </xsl:when> - <xsl:otherwise> - <xsl:attribute name="name"> - <xsl:call-template name="getKeyName"> - <xsl:with-param name="type" select="$type"/> - </xsl:call-template> - </xsl:attribute> - </xsl:otherwise> - </xsl:choose> - <xsl:if test="not($short)"> - <xsl:call-template name="getClrType"> - <xsl:with-param name="type" select="$type"/> - </xsl:call-template> - </xsl:if> + <xsl:attribute name="name"> + <xsl:apply-templates select="." mode="member-name"/> + </xsl:attribute> + <xsl:call-template name="getClrType"> + <xsl:with-param name="type" select="$type"/> + </xsl:call-template> </cs:member> </xsl:template> - <xsl:template name="getClrMemberName"> - <xsl:param name="memberName"/> - <xsl:param name="type"/> - <xsl:param name="explicitMemberName" select="''"/> - <xsl:param name="short" select="false()"/> - <cs:member> - <xsl:choose> - <xsl:when test="$explicitMemberName"> - <xsl:attribute name="name"> - <xsl:value-of select="$explicitMemberName"/> - </xsl:attribute> - </xsl:when> - <xsl:otherwise> - <xsl:attribute name="name"> - <xsl:call-template name="getClrPropertyName"> - <xsl:with-param name="member" select="$memberName"/> - <xsl:with-param name="type" select="$type"/> - </xsl:call-template> - </xsl:attribute> - </xsl:otherwise> - </xsl:choose> - <xsl:if test="not($short)"> - <xsl:call-template name="getClrType"> - <xsl:with-param name="type" select="$type"/> - </xsl:call-template> - </xsl:if> - </cs:member> - </xsl:template> - - <!-- lookups for the specified member for the specified type - and returns the CLR property name --> - <xsl:template name="getClrPropertyName"> - <xsl:param name="type"/> - <xsl:param name="member"/> - <!-- prevents cyclic references --> - <xsl:param name="seen" select="/.."/> - <xsl:for-each select="$module"> - <xsl:apply-templates select="key('type', $type)" - mode="member-lookup"> - <xsl:with-param name="member" select="$member"/> - <xsl:with-param name="seen" select="$seen"/> - </xsl:apply-templates> - </xsl:for-each> - </xsl:template> <!-- resolves CLR type for the given type --> <xsl:template name="getClrType">