view xslt/model.xsl @ 2:035de8b7b18e

Temporary commit, refactoring, added readme
author cin
date Sun, 25 Feb 2018 17:12:33 +0300
parents
children 437127ab6a12
line wrap: on
line source

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://implab.org/schemas/data-model.v1.xsd"
	xmlns:clr="http://implab.org/schemas/data-model/dotnet.v1.xsd"
	xmlns:cs="http://implab.org/schemas/code-dom/csharp.v1.xsd" xmlns:sql="http://implab.org/schemas/data-model/sql.v1.xsd"
	xmlns:t="http://implab.org/schemas/temp" xmlns:exsl="http://exslt.org/common"
	exclude-result-prefixes="clr m xsl exsl sql">
	
	<xsl:import href="preprocess.xsl" />
	<xsl:include href="text-tools.xsl"/>
	
	<xsl:output method="xml" indent="yes" />

	<xsl:key name="type" match="m:package/m:*" use="@name" />

	<xsl:template match="/">
		<cs:document>
			<xsl:apply-templates select="$module" mode="document" />
		</cs:document>
	</xsl:template>

	<!-- code generation -->

	<!-- disable default template -->
	<xsl:template match="*|text()" mode="document" />

	<xsl:template match="t:module" mode="document">
		<xsl:apply-templates mode="document" />
	</xsl:template>

	<!-- 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>
	
	<!-- member resolution traits -->
	
	<!-- Resolves members using given criteria
		@param type
			the name of the type whose members are inspected
		@param memberName
			the name of the member to lookup
		@param scope
			the scope where to look (members, properties, keys, relations)
			
		This template recursively inspects the given type down to it's inheritance
		hierarchy and outputs members matching criteria.
	 -->
	<xsl:template name="getMembers">
		<xsl:param name="type"/>
		<xsl:param name="memberName" select="''"/>
		<xsl:param name="scope" select="'members'"/>
		<!-- prevents cyclic references -->
		<xsl:param name="seen" select="/.."/>
		<xsl:for-each select="$module">
			<xsl:apply-templates select="key('type', $type)"
				mode="lookup-members">
				<xsl:with-param name="memberName" select="$memberName"/>
				<xsl:with-param name="scope" select="$scope"/>
				<xsl:with-param name="seen" select="$seen"/>
			</xsl:apply-templates>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template match="*" mode="lookup-members"/>
	
	<xsl:template match="m:entity" mode="lookup-members">
		<xsl:param name="memberName" select="''"/>
		<xsl:param name="scope" select="'members'"/>
		<xsl:param name="seen"/>
		
		<xsl:variable name="self" select="."/>
		
		<xsl:if test="not($seen[generate-id() = generate-id($self)])">
			<xsl:variable name="scopeMembers">
				<xsl:call-template name="selectScopeMembers">
					<xsl:with-param name="scope" select="$scope"/>
				</xsl:call-template>
			</xsl:variable>
			
			<xsl:variable name="members" select="exsl:node-set($scopeMembers)/*[not($memberName) or @name='$memberName']"/>
			
			<xsl:choose>
				<xsl:when test="$members">
					<xsl:copy-of select="$members"/>
				</xsl:when>
				<xsl:when test="m:extends">
					<xsl:call-template name="getMembers">
						<xsl:with-param name="type" select="m:extends/@type"/>
						<xsl:with-param name="memberName" select="$memberName"/>
						<xsl:with-param name="scope" select="$scope"/>
						<xsl:with-param name="seen" select="$seen | $self"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="warn">
						<xsl:with-param name="msg">
							<t:trace msg="failed to resolve {$member}"/>
							<t:trace msg="inspected classes">
							<xsl:for-each select="$seen | $self">
								<t:trace msg="{name()} {@name}"/>
							</xsl:for-each>
							</t:trace>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	<!-- this template implements scope filtering -->
	<xsl:template name="selectScopeMembers">
		<xsl:param name="scope" select="'members'"/>
		<xsl:choose>
			<xsl:when test="$scope='members'">
				<xsl:apply-templates mode="filter-members"/>
			</xsl:when>
			<xsl:when test="$scope='keys'">
				<xsl:apply-template mode="filter-keys"/>
			</xsl:when>
			<xsl:when test="$scope='properties'">
				<xsl:apply-templates mode="fiflter-properties"/>
			</xsl:when>
			<xsl:when test="$scope='relations'">
				<xsl:apply-templates mode="fiflter-relations"/>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<!-- filter-members -->
	<xsl:template match="*|text()" mode="filter-members"/>
	<xsl:template match="*[@name]" mode="filter-members">
		<xsl:copy>
			<xsl:copy-of select="@*" />
			<xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
			<xsl:copy-of select="*" />
		</xsl:copy>
	</xsl:template>
	
	<!-- filter-keys -->
	<xsl:template match="*|text()" mode="filter-keys"/>
	<xsl:template match="m:primaryKey" mode="filter-keys">
		<xsl:apply-templates select="." mode="filter-members"/>
	</xsl:template>
	
	<!-- filter-properties -->
	<xsl:template match="*|text()" mode="filter-properties"/>
	<xsl:template match="m:property" mode="filter-properties">
		<xsl:apply-templates select="." mode="filter-members"/>
	</xsl:template>
	
	<!-- filter-relations -->
	<xsl:template match="*|text()" mode="filter-relations"/>
	<xsl:template match="m:hasA | m:hasMany" mode="filter-relations">
		<xsl:apply-templates select="." mode="filter-members"/>
	</xsl:template>

	

	<!-- primary key -->
	
	<!-- Resolves primaryKey information for the given type name.
		@returns m:primaryKey node copy with additional attribute
		@declaringType which points to the type where this primaryKey
		was defined.
	-->
	<xsl:template name="getPrimaryKey">
		<xsl:param name="type" />
		<xsl:call-template name="getMembers">
			<xsl:with-param name="type" select="$type"/>
			<xsl:with-param name="scope" select="keys"/>
		</xsl:call-template>
	</xsl:template>
	
	<!--  -->
	<xsl:template name="getKeyType">
		<xsl:param name="type" />
		<xsl:variable name="otherKey">
			<xsl:call-template name="getKey">
				<xsl:with-param name="type" select="$type" />
			</xsl:call-template>
		</xsl:variable>
		<xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-type"/>
	</xsl:template>
	
	<xsl:template name="getKeyName">
		<xsl:param name="type" />
		<xsl:variable name="otherKey">
			<xsl:call-template name="getKey">
				<xsl:with-param name="type" select="$type" />
			</xsl:call-template>
		</xsl:variable>
		<xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-name"/>
	</xsl:template>

	<!-- internal. applied to the entity with a primaryKey node -->
	<xsl:template match="m:entity[m:primaryKey]" mode="resolvePK">
		<xsl:apply-templates select="m:primaryKey" mode="resolvePK" />
	</xsl:template>

	<!--
	This template formats the result of 'resolvePk' template,
	override this template to extend returned metadata, beware that
	other templates rely on the resulting format of this template.
	-->
	<xsl:template match="m:primaryKey" mode="resolvePK">
		<xsl:copy>
			<xsl:copy-of select="@*" />
			<xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
			<xsl:copy-of select="*" />
		</xsl:copy>
	</xsl:template>

	<!-- internal, used to traverse the hierarchy -->
	<xsl:template match="m:entity" mode="resolvePK">
		<xsl:apply-templates select="m:extends" mode="resolvePK" />
	</xsl:template>

	<!-- internal, used to traverse the hierarchy -->
	<xsl:template match="m:extends" mode="resolvePK">
		<xsl:apply-templates select="key('type', @type)"
			mode="resolvePK" />
	</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>