comparison xslt/model.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
comparison
equal deleted inserted replaced
2:035de8b7b18e 3:437127ab6a12
26 26
27 <xsl:template match="t:module" mode="document"> 27 <xsl:template match="t:module" mode="document">
28 <xsl:apply-templates mode="document" /> 28 <xsl:apply-templates mode="document" />
29 </xsl:template> 29 </xsl:template>
30 30
31 <!-- generate code for primary package --> 31
32 <xsl:template match="m:package[@primary='true' and @clr:namespace]" mode="document">
33 <cs:namespace name="{@clr:namespace}">
34 <xsl:apply-templates mode="document" />
35 </cs:namespace>
36 </xsl:template>
37
38 <xsl:template match="m:package[@primary='true']" mode="document">
39 <xsl:apply-templates mode="document" />
40 </xsl:template>
41 32
42 <!-- member resolution traits --> 33 <!-- member resolution traits -->
43 34
44 <!-- Resolves members using given criteria 35 <!-- Resolves members using given criteria
45 @param type 36 @param type
161 <xsl:apply-templates select="." mode="filter-members"/> 152 <xsl:apply-templates select="." mode="filter-members"/>
162 </xsl:template> 153 </xsl:template>
163 154
164 155
165 156
166 <!-- primary key -->
167
168 <!-- Resolves primaryKey information for the given type name. 157 <!-- Resolves primaryKey information for the given type name.
169 @returns m:primaryKey node copy with additional attribute 158 @returns m:primaryKey node copy with additional attribute
170 @declaringType which points to the type where this primaryKey 159 @declaringType which points to the type where this primaryKey
171 was defined. 160 was defined.
172 --> 161 -->
176 <xsl:with-param name="type" select="$type"/> 165 <xsl:with-param name="type" select="$type"/>
177 <xsl:with-param name="scope" select="keys"/> 166 <xsl:with-param name="scope" select="keys"/>
178 </xsl:call-template> 167 </xsl:call-template>
179 </xsl:template> 168 </xsl:template>
180 169
181 <!-- --> 170 <xsl:template match="*|text()" mode="process-member"/>
182 <xsl:template name="getKeyType"> 171
183 <xsl:param name="type" /> 172 <xsl:template match="m:hasA" mode="process-member">
184 <xsl:variable name="otherKey"> 173 <xsl:apply-templates mode="process-member"/>
185 <xsl:call-template name="getKey"> 174 </xsl:template>
186 <xsl:with-param name="type" select="$type" /> 175
176 <xsl:template match="m:hasA[@type]/m:thisKey" mode="process-member">
177 <xsl:variable name="foreignPrimaryKey">
178 <xsl:call-template name="getPrimaryKey">
179 <xsl:with-param name="type" select="../@type"/>
187 </xsl:call-template> 180 </xsl:call-template>
188 </xsl:variable> 181 </xsl:variable>
189 <xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-type"/> 182 <m:property type="{exsl:node-set($foreignPrimaryKey)[1]/@type}">
183 <xsl:copy-of select="@*"/>
184 </m:property>
190 </xsl:template> 185 </xsl:template>
191 186
192 <xsl:template name="getKeyName"> 187 <xsl:template match="m:primaryKey" mode="process-member">
193 <xsl:param name="type" /> 188 <m:property>
194 <xsl:variable name="otherKey"> 189 <xsl:copy-of select="@*"/>
195 <xsl:call-template name="getKey"> 190 <xsl:copy-of select="*"/>
196 <xsl:with-param name="type" select="$type" /> 191 </m:property>
197 </xsl:call-template>
198 </xsl:variable>
199 <xsl:apply-templates select="exsl:node-set($otherKey)/m:primaryKey" mode="property-name"/>
200 </xsl:template>
201
202 <!-- internal. applied to the entity with a primaryKey node -->
203 <xsl:template match="m:entity[m:primaryKey]" mode="resolvePK">
204 <xsl:apply-templates select="m:primaryKey" mode="resolvePK" />
205 </xsl:template>
206
207 <!--
208 This template formats the result of 'resolvePk' template,
209 override this template to extend returned metadata, beware that
210 other templates rely on the resulting format of this template.
211 -->
212 <xsl:template match="m:primaryKey" mode="resolvePK">
213 <xsl:copy>
214 <xsl:copy-of select="@*" />
215 <xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
216 <xsl:copy-of select="*" />
217 </xsl:copy>
218 </xsl:template>
219
220 <!-- internal, used to traverse the hierarchy -->
221 <xsl:template match="m:entity" mode="resolvePK">
222 <xsl:apply-templates select="m:extends" mode="resolvePK" />
223 </xsl:template>
224
225 <!-- internal, used to traverse the hierarchy -->
226 <xsl:template match="m:extends" mode="resolvePK">
227 <xsl:apply-templates select="key('type', @type)"
228 mode="resolvePK" />
229 </xsl:template>
230
231 <!-- resolves CLR type for the given type -->
232 <xsl:template name="getClrType">
233 <xsl:param name="type" />
234 <xsl:param name="typeArgs" select="/.." />
235 <!-- <t:trace msg="resolveClrType {$type}"/> -->
236 <xsl:for-each select="$module">
237 <xsl:apply-templates select="key('type', $type)"
238 mode="clr-type">
239 <xsl:with-param name="typeArgs" select="$typeArgs"/>
240 </xsl:apply-templates>
241 </xsl:for-each>
242 </xsl:template>
243
244 <!-- CLR type construction -->
245 <xsl:template match="*" mode="clr-type" />
246
247 <xsl:template match="m:type[clr:type]" mode="clr-type">
248 <xsl:param name="typeArgs" select="clr:type/*" />
249 <xsl:apply-templates select="clr:type" mode="clr-type">
250 <xsl:with-param name="typeArgs" select="$typeArgs" />
251 </xsl:apply-templates>
252 </xsl:template>
253
254 <xsl:template match="m:entity" mode="clr-type">
255 <cs:type name="{@clr:name | @name[not(../@clr:name)]}" namespace="{ancestor::*[@clr:namespace]/@clr:namespace}" />
256 </xsl:template>
257
258 <xsl:template match="clr:type" mode="clr-type">
259 <xsl:apply-templates mode="clr-type"/>
260 </xsl:template>
261
262 <xsl:template match="clr:type[@ref]" mode="clr-type">
263 <xsl:param name="typeArgs" select="*" />
264 <xsl:call-template name="getClrType">
265 <xsl:with-param name="type" select="@ref" />
266 <xsl:with-param name="typeArgs" select="$typeArgs" />
267 </xsl:call-template>
268 </xsl:template>
269
270 <xsl:template match="clr:type[@cs:name or @name]" mode="clr-type">
271 <xsl:param name="typeArgs" select="*" />
272 <xsl:variable name="ns"
273 select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
274 <cs:type name="{@cs:name | @name[not(../@cs:name)]}">
275 <xsl:if test="$ns">
276 <xsl:attribute name="namespace"><xsl:value-of select="$ns" /></xsl:attribute>
277 </xsl:if>
278 <xsl:copy-of select="@struct"/>
279
280 <xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
281 mode="clr-type" />
282 </cs:type>
283 </xsl:template>
284
285 <xsl:template match="clr:arrayOf[@type]" mode="clr-type">
286 <xsl:param name="typeArgs" select="*" />
287 <cs:array>
288 <xsl:call-template name="getClrType">
289 <xsl:with-param name="type" select="@type" />
290 <xsl:with-param name="typeArgs" select="$typeArgs" />
291 </xsl:call-template>
292 </cs:array>
293 </xsl:template>
294
295 <xsl:template match="clr:arrayOf[@cs:name or @name]" mode="clr-type">
296 <xsl:param name="typeArgs" select="*" />
297 <xsl:variable name="ns"
298 select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
299 <cs:array>
300 <cs:type name="{@cs:name | @name[not(../@cs:name)]}">
301 <xsl:if test="$ns">
302 <xsl:attribute name="namespace"><xsl:value-of
303 select="$ns" /></xsl:attribute>
304 </xsl:if>
305 <xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
306 mode="clr-type" />
307 </cs:type>
308 </cs:array>
309 </xsl:template> 192 </xsl:template>
310 193
311 194
312 </xsl:stylesheet> 195 </xsl:stylesheet>