2
|
1 <?xml version="1.0" encoding="UTF-8"?>
|
|
2 <xsl:stylesheet version="1.0"
|
|
3 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://implab.org/schemas/data-model.v1.xsd"
|
|
4 xmlns:clr="http://implab.org/schemas/data-model/dotnet.v1.xsd"
|
|
5 xmlns:cs="http://implab.org/schemas/code-dom/csharp.v1.xsd" xmlns:sql="http://implab.org/schemas/data-model/sql.v1.xsd"
|
|
6 xmlns:t="http://implab.org/schemas/temp" xmlns:exsl="http://exslt.org/common"
|
|
7 exclude-result-prefixes="clr m xsl exsl sql">
|
|
8 <xsl:import href="model.xsl"/>
|
|
9
|
|
10 <xsl:output method="xml" indent="yes" />
|
|
11
|
3
|
12 <!-- generate code for primary package -->
|
|
13 <xsl:template match="m:package[@primary='true' and @clr:namespace]" mode="document">
|
|
14 <cs:namespace name="{@clr:namespace}">
|
|
15 <xsl:apply-templates mode="document" />
|
|
16 </cs:namespace>
|
|
17 </xsl:template>
|
|
18
|
|
19 <xsl:template match="m:package[@primary='true']" mode="document">
|
|
20 <xsl:apply-templates mode="document" />
|
|
21 </xsl:template>
|
|
22
|
2
|
23 <!-- entity -->
|
|
24 <xsl:template match="m:entity" mode="document">
|
|
25 <xsl:apply-templates select="." mode="entity"/>
|
|
26 </xsl:template>
|
|
27
|
|
28 <xsl:template match="m:entity" mode="entity">
|
3
|
29 <cs:class modifiers="partial">
|
2
|
30 <xsl:attribute name="name">
|
|
31 <xsl:apply-templates select="." mode="class-name"/>
|
|
32 </xsl:attribute>
|
|
33 <xsl:apply-templates mode="class-attributes" />
|
|
34 <xsl:apply-templates mode="members" />
|
|
35 </cs:class>
|
|
36 </xsl:template>
|
|
37
|
|
38 <!-- class-name -->
|
|
39 <xsl:template match="*[@name]" mode="class-name">
|
|
40 <xsl:value-of select="@name"/>
|
|
41 </xsl:template>
|
|
42
|
|
43 <xsl:template match="*[@clr:name]" mode="class-name">
|
|
44 <xsl:value-of select="@clr:name"/>
|
|
45 </xsl:template>
|
|
46
|
|
47 <!-- class-attributes -->
|
|
48 <xsl:template match="*|text()" mode="class-attributes" />
|
|
49
|
|
50 <xsl:template match="sql:table" mode="class-attributes">
|
|
51 <cs:attribute>
|
|
52 <cs:type name="Table" namespace="Linq2Db" />
|
|
53 <cs:parameter><cs:string text="{@name}"/></cs:parameter>
|
|
54 </cs:attribute>
|
|
55 </xsl:template>
|
|
56
|
|
57 <!-- generate members -->
|
|
58
|
|
59 <xsl:template match="*|text()" mode="members" />
|
|
60
|
|
61 <xsl:template match="m:primaryKey | m:property | m:thisKey | clr:association" mode="members">
|
|
62 <t:trace msg="{name()} {@name}"/>
|
|
63 <xsl:apply-templates select="." mode="property"/>
|
|
64 </xsl:template>
|
|
65
|
|
66 <!-- hasA and hasMany doesn't generate members itself, they delegate this work to inner members -->
|
|
67 <xsl:template match="m:hasA | m:hasMany" mode="members">
|
|
68 <t:trace msg="{name()} {@name}" />
|
|
69 <xsl:apply-templates mode="members" />
|
|
70 </xsl:template>
|
|
71
|
|
72 <xsl:template match="m:hasA/clr:lazy" mode="members">
|
|
73 <xsl:apply-templates select="." mode="field"/>
|
|
74 </xsl:template>
|
|
75
|
|
76 <!-- member-name -->
|
|
77 <xsl:template match="*|text()|@*" mode="member-name" />
|
|
78
|
|
79 <xsl:template match="*[@name]" mode="member-name">
|
|
80 <xsl:value-of select="@name"/>
|
|
81 </xsl:template>
|
|
82 <xsl:template match="*[@clr:name]" mode="member-name">
|
|
83 <xsl:value-of select="@clr:name"/>
|
|
84 </xsl:template>
|
|
85
|
|
86 <!-- member-type -->
|
|
87 <xsl:template match="*|text()|@*" mode="member-type" />
|
|
88
|
|
89 <xsl:template match="*[@type]" mode="member-type">
|
|
90 <xsl:call-template name="getClrType">
|
|
91 <xsl:with-param name="type" select="@type" />
|
|
92 </xsl:call-template>
|
|
93 </xsl:template>
|
|
94
|
|
95 <xsl:template match="*[clr:type]" mode="member-type">
|
|
96 <xsl:apply-templates select="clr:type" mode="clr-type" />
|
|
97 </xsl:template>
|
|
98
|
|
99 <!-- member-attributes -->
|
|
100 <xsl:template match="*|text()" mode="member-attributes"/>
|
|
101
|
|
102 <!-- member-extension -->
|
|
103 <xsl:template match="*|text()" mode="member-extension"/>
|
|
104 <xsl:template match="*" mode="member-extension">
|
|
105 <xsl:apply-templates mode="member-extension-comments"/>
|
|
106 </xsl:template>
|
|
107
|
|
108 <xsl:template match="m:hasA/* | m:hasMany/*" mode="member-extension">
|
|
109 <xsl:variable name="comments" select="../m:description | m:description"/>
|
|
110 <xsl:apply-templates select="$comments[position() = count($comments)]" mode="member-extension-comments"/>
|
|
111 </xsl:template>
|
|
112
|
|
113 <!-- member-extension-comments -->
|
|
114 <xsl:template match="*|text()" mode="member-extension-comments"/>
|
|
115
|
|
116 <xsl:template match="m:description" mode="member-extension-comments">
|
|
117 <cs:comments>
|
|
118 <xsl:apply-templates mode="comments" />
|
|
119 </cs:comments>
|
|
120 </xsl:template>
|
|
121
|
|
122 <!-- property -->
|
|
123
|
|
124 <xsl:template match="*" mode="property">
|
|
125 <cs:property modifiers="public">
|
|
126 <xsl:attribute name="name"><xsl:apply-templates select="." mode="property-name"/></xsl:attribute>
|
3
|
127 <xsl:attribute name="modifiers"><xsl:apply-templates select="." mode="property-modifiers"/></xsl:attribute>
|
2
|
128 <xsl:apply-templates select="." mode="property-type"/>
|
|
129 <xsl:apply-templates select="." mode="property-attributes"/>
|
|
130 <xsl:apply-templates select="." mode="property-extension" />
|
|
131 <xsl:apply-templates select="." mode="property-accessors"/>
|
|
132 </cs:property>
|
|
133 </xsl:template>
|
|
134
|
|
135 <!-- property-name -->
|
|
136 <xsl:template match="m:hasA/clr:association[not(@name|@clr:name)]" mode="property-name">
|
|
137 <!-- if the association doesn't define a name, use it from the parent node -->
|
|
138 <xsl:apply-templates select=".." mode="property-name"/>
|
|
139 </xsl:template>
|
|
140 <xsl:template match="m:hasMany/clr:association[not(@name|@clr:name)]" mode="property-name">
|
|
141 <!-- if the association doesn't define a name, use it from the parent node -->
|
|
142 <xsl:apply-templates select=".." mode="property-name"/>
|
|
143 </xsl:template>
|
|
144
|
|
145 <xsl:template match="*" mode="property-name">
|
|
146 <xsl:apply-templates select="." mode="member-name"/>
|
|
147 </xsl:template>
|
|
148
|
3
|
149 <!-- property-modifiers -->
|
|
150 <xsl:template match="*" mode="property-modifiers">
|
|
151 <xsl:text>public</xsl:text>
|
|
152 </xsl:template>
|
|
153
|
2
|
154 <!-- property-type -->
|
|
155 <xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="property-type">
|
|
156 <xsl:apply-templates select=".." mode="property-type"/>
|
|
157 </xsl:template>
|
|
158
|
|
159 <xsl:template match="m:hasMany[@type]/clr:association[not(clr:type)]" mode="property-type">
|
|
160 <cs:array>
|
|
161 <xsl:apply-templates select=".." mode="property-type"/>
|
|
162 </cs:array>
|
|
163 </xsl:template>
|
|
164
|
|
165 <xsl:template match="m:hasA[@type]/m:thisKey" mode="property-type">
|
3
|
166 <xsl:call-template name="getPrimaryKey">
|
2
|
167 <xsl:with-param name="type" select="../@type"/>
|
|
168 </xsl:call-template>
|
|
169 </xsl:template>
|
|
170
|
|
171 <xsl:template match="m:hasA[@type and boolean(@optional)]/m:thisKey" mode="property-type">
|
|
172 <cs:nullable>
|
3
|
173 <xsl:call-template name="getPrimaryKey">
|
2
|
174 <xsl:with-param name="type" select="../@type"/>
|
|
175 </xsl:call-template>
|
|
176 </cs:nullable>
|
|
177 </xsl:template>
|
|
178
|
|
179 <xsl:template match="*" mode="property-type">
|
|
180 <xsl:apply-templates select="." mode="member-type"/>
|
|
181 </xsl:template>
|
|
182
|
|
183 <!-- property-attributes -->
|
|
184 <xsl:template match="m:primaryKey" mode="property-attributes">
|
|
185 <cs:attribute>
|
|
186 <cs:type name="PrimaryKey" namespace="Linq2Db" />
|
|
187 </cs:attribute>
|
|
188 </xsl:template>
|
|
189
|
|
190 <xsl:template match="m:hasA/clr:association" mode="property-attributes">
|
|
191 <cs:attribute>
|
|
192 <cs:type name="Association" namespace="Linq2Db"/>
|
|
193 <cs:parameter name="thisKey">
|
|
194 <cs:nameOf>
|
|
195 <xsl:call-template name="getMember">
|
|
196 <xsl:with-param name="memberName" select="../@name"/>
|
|
197 <xsl:with-param name="explicitMemberName" select="@thisKey"/>
|
|
198 <xsl:with-param name="type" select="../../@name"/>
|
|
199 <xsl:with-param name="short" select="true()"/>
|
|
200 </xsl:call-template>
|
|
201 </cs:nameOf>
|
|
202 </cs:parameter>
|
|
203 <cs:parameter name="otherKey">
|
|
204 <cs:nameOf>
|
|
205 <xsl:call-template name="getKeyMember">
|
|
206 <xsl:with-param name="explicitMemberName" select="@thisKey"/>
|
|
207 <xsl:with-param name="type" select="../@type"/>
|
|
208 </xsl:call-template>
|
|
209 </cs:nameOf>
|
|
210 </cs:parameter>
|
|
211 </cs:attribute>
|
|
212 </xsl:template>
|
|
213
|
|
214 <xsl:template match="m:hasMany/clr:association" mode="property-attributes">
|
|
215 <cs:attribute>
|
|
216 <cs:type name="Association" namespace="Linq2Db"/>
|
|
217 <!-- thisKey points to own primaryKey which may be inherited, using getKeyName to address such cases -->
|
|
218 <cs:parameter name="thisKey">
|
|
219 <cs:nameOf>
|
|
220 <xsl:call-template name="getKeyMember">
|
|
221 <xsl:with-param name="type" select="../../@name"/>
|
|
222 <xsl:with-param name="explicitMemberName" select="@thisKey"/>
|
|
223 <xsl:with-param name="short" select="true()"/>
|
|
224 </xsl:call-template>
|
|
225 </cs:nameOf>
|
|
226 </cs:parameter>
|
|
227 <cs:parameter name="otherKey">
|
|
228 <cs:nameOf>
|
|
229 <xsl:call-template name="getMember">
|
|
230 <xsl:with-param name="type" select="../@type"/>
|
|
231 <xsl:with-param name="memberName" select="../m:otherKey/@name"/>
|
|
232 <xsl:with-param name="explicitMemberName" select="@otherKey"/>
|
|
233 </xsl:call-template>
|
|
234 </cs:nameOf>
|
|
235 </cs:parameter>
|
|
236 </cs:attribute>
|
|
237 </xsl:template>
|
|
238
|
|
239 <xsl:template match="*" mode="property-attributes">
|
|
240 <xsl:apply-templates select="." mode="member-attributes"/>
|
|
241 </xsl:template>
|
|
242
|
|
243 <!-- property-extension -->
|
|
244 <xsl:template match="*" mode="property-extension">
|
|
245 <xsl:apply-templates select="." mode="member-extension"/>
|
|
246 </xsl:template>
|
|
247
|
|
248 <!-- property-accessors -->
|
|
249 <xsl:template match="*" mode="property-accessors">
|
|
250 <cs:get/>
|
|
251 <cs:set/>
|
|
252 </xsl:template>
|
|
253
|
|
254 <xsl:template match="m:hasA[clr:lazy]/m:thisKey" mode="property-accessors">
|
|
255 <cs:get>
|
|
256 <xsl:text>return </xsl:text>
|
|
257 <xsl:apply-templates select="../clr:lazy" mode="field-name"/>
|
|
258 <xsl:text>.Key;</xsl:text>
|
|
259 </cs:get>
|
|
260 <cs:set>
|
|
261 <xsl:apply-templates select="../clr:lazy" mode="field-name"/>
|
|
262 <xsl:text>.Key = value;</xsl:text>
|
|
263 </cs:set>
|
|
264 </xsl:template>
|
|
265
|
|
266 <xsl:template match="m:hasA[clr:lazy]/clr:association" mode="property-accessors">
|
|
267 <cs:get>
|
|
268 <xsl:text>return </xsl:text>
|
|
269 <xsl:apply-templates select="../clr:lazy" mode="field-name"/>
|
|
270 <xsl:text>.Instance;</xsl:text>
|
|
271 </cs:get>
|
|
272 <cs:set>
|
|
273 <xsl:apply-templates select="../clr:lazy" mode="field-name"/>
|
|
274 <xsl:text>.Instance = value;</xsl:text>
|
|
275 </cs:set>
|
|
276 </xsl:template>
|
|
277
|
|
278 <!-- fields -->
|
|
279 <xsl:template match="*" mode="field">
|
|
280 <cs:field>
|
|
281 <xsl:attribute name="name"><xsl:apply-templates select="." mode="field-name"/></xsl:attribute>
|
|
282 <xsl:apply-templates select="." mode="field-type"/>
|
|
283 <xsl:apply-templates select="." mode="field-initializer"/>
|
|
284 </cs:field>
|
|
285 </xsl:template>
|
|
286
|
|
287 <!-- field-name -->
|
|
288 <xsl:template match="m:hasA/clr:lazy" mode="field-name">
|
|
289 <xsl:text>m_lazy</xsl:text>
|
|
290 <xsl:apply-templates select=".." mode="property-name"/>
|
|
291 </xsl:template>
|
|
292
|
|
293 <xsl:template match="clr:lazy[@field]" mode="field-name">
|
|
294 <xsl:value-of select="@field"/>
|
|
295 </xsl:template>
|
|
296
|
|
297 <xsl:template match="*" mode="field-name">
|
|
298 <xsl:apply-templates select="." mode="member-name"/>
|
|
299 </xsl:template>
|
|
300
|
|
301 <!-- field-type -->
|
|
302 <xsl:template match="m:hasA[@optional='true']/clr:lazy" mode="field-type">
|
|
303 <cs:type name="NullableReference" namespace="Pallada.Data">
|
|
304 <xsl:call-template name="getKeyType">
|
|
305 <xsl:with-param name="type" select="../@type"/>
|
|
306 </xsl:call-template>
|
|
307 <xsl:apply-templates select=".." mode="property-type"/>
|
|
308 </cs:type>
|
|
309 </xsl:template>
|
|
310 <xsl:template match="m:hasA[@optional!='true']/clr:lazy" mode="field-type">
|
|
311 <cs:type name="Reference" namespace="Pallada.Data">
|
|
312 <xsl:apply-templates select="../m:thisKey" mode="property-type"/>
|
|
313 <xsl:apply-templates select=".." mode="property-type"/>
|
|
314 </cs:type>
|
|
315 </xsl:template>
|
|
316
|
|
317 <xsl:template match="*" mode="field-type">
|
|
318 <xsl:apply-templates select="." mode="member-type"/>
|
|
319 </xsl:template>
|
|
320 <!-- field-initializer -->
|
|
321 <xsl:template match="*|text()" mode="field-initializer"/>
|
|
322
|
|
323 <xsl:template name="getKeyClrMemberName">
|
|
324 <xsl:param name="explicitMemberName" select="''"/>
|
|
325 <xsl:param name="type"/>
|
|
326 <xsl:param name="short" select="false()"/>
|
|
327 <cs:member>
|
|
328 <xsl:choose>
|
|
329 <xsl:when test="$explicitMemberName">
|
|
330 <xsl:attribute name="name">
|
|
331 <xsl:value-of select="$explicitMemberName"/>
|
|
332 </xsl:attribute>
|
|
333 </xsl:when>
|
|
334 <xsl:otherwise>
|
|
335 <xsl:attribute name="name">
|
|
336 <xsl:call-template name="getKeyName">
|
|
337 <xsl:with-param name="type" select="$type"/>
|
|
338 </xsl:call-template>
|
|
339 </xsl:attribute>
|
|
340 </xsl:otherwise>
|
|
341 </xsl:choose>
|
|
342 <xsl:if test="not($short)">
|
|
343 <xsl:call-template name="getClrType">
|
|
344 <xsl:with-param name="type" select="$type"/>
|
|
345 </xsl:call-template>
|
|
346 </xsl:if>
|
|
347 </cs:member>
|
|
348 </xsl:template>
|
|
349
|
|
350 <xsl:template name="getClrMemberName">
|
|
351 <xsl:param name="memberName"/>
|
|
352 <xsl:param name="type"/>
|
|
353 <xsl:param name="explicitMemberName" select="''"/>
|
|
354 <xsl:param name="short" select="false()"/>
|
|
355 <cs:member>
|
|
356 <xsl:choose>
|
|
357 <xsl:when test="$explicitMemberName">
|
|
358 <xsl:attribute name="name">
|
|
359 <xsl:value-of select="$explicitMemberName"/>
|
|
360 </xsl:attribute>
|
|
361 </xsl:when>
|
|
362 <xsl:otherwise>
|
|
363 <xsl:attribute name="name">
|
|
364 <xsl:call-template name="getClrPropertyName">
|
|
365 <xsl:with-param name="member" select="$memberName"/>
|
|
366 <xsl:with-param name="type" select="$type"/>
|
|
367 </xsl:call-template>
|
|
368 </xsl:attribute>
|
|
369 </xsl:otherwise>
|
|
370 </xsl:choose>
|
|
371 <xsl:if test="not($short)">
|
|
372 <xsl:call-template name="getClrType">
|
|
373 <xsl:with-param name="type" select="$type"/>
|
|
374 </xsl:call-template>
|
|
375 </xsl:if>
|
|
376 </cs:member>
|
|
377 </xsl:template>
|
|
378
|
|
379 <!-- lookups for the specified member for the specified type
|
|
380 and returns the CLR property name -->
|
|
381 <xsl:template name="getClrPropertyName">
|
|
382 <xsl:param name="type"/>
|
|
383 <xsl:param name="member"/>
|
|
384 <!-- prevents cyclic references -->
|
|
385 <xsl:param name="seen" select="/.."/>
|
|
386 <xsl:for-each select="$module">
|
|
387 <xsl:apply-templates select="key('type', $type)"
|
|
388 mode="member-lookup">
|
|
389 <xsl:with-param name="member" select="$member"/>
|
|
390 <xsl:with-param name="seen" select="$seen"/>
|
|
391 </xsl:apply-templates>
|
|
392 </xsl:for-each>
|
|
393 </xsl:template>
|
|
394
|
3
|
395 <!-- resolves CLR type for the given type -->
|
|
396 <xsl:template name="getClrType">
|
|
397 <xsl:param name="type" />
|
|
398 <xsl:param name="typeArgs" select="/.." />
|
|
399 <!-- <t:trace msg="resolveClrType {$type}"/> -->
|
|
400 <xsl:for-each select="$module">
|
|
401 <xsl:apply-templates select="key('type', $type)"
|
|
402 mode="clr-type">
|
|
403 <xsl:with-param name="typeArgs" select="$typeArgs"/>
|
|
404 </xsl:apply-templates>
|
|
405 </xsl:for-each>
|
|
406 </xsl:template>
|
|
407
|
|
408 <!-- CLR type construction -->
|
|
409 <xsl:template match="*" mode="clr-type" />
|
|
410
|
|
411 <xsl:template match="m:type[clr:type]" mode="clr-type">
|
|
412 <xsl:param name="typeArgs" select="clr:type/*" />
|
|
413 <xsl:apply-templates select="clr:type" mode="clr-type">
|
|
414 <xsl:with-param name="typeArgs" select="$typeArgs" />
|
|
415 </xsl:apply-templates>
|
|
416 </xsl:template>
|
|
417
|
|
418 <xsl:template match="m:entity" mode="clr-type">
|
|
419 <cs:type name="{@clr:name | @name[not(../@clr:name)]}" namespace="{ancestor::*[@clr:namespace]/@clr:namespace}" />
|
|
420 </xsl:template>
|
|
421
|
|
422 <xsl:template match="clr:type" mode="clr-type">
|
|
423 <xsl:apply-templates mode="clr-type"/>
|
|
424 </xsl:template>
|
|
425
|
|
426 <xsl:template match="clr:type[@ref]" mode="clr-type">
|
|
427 <xsl:param name="typeArgs" select="*" />
|
|
428 <xsl:call-template name="getClrType">
|
|
429 <xsl:with-param name="type" select="@ref" />
|
|
430 <xsl:with-param name="typeArgs" select="$typeArgs" />
|
|
431 </xsl:call-template>
|
|
432 </xsl:template>
|
|
433
|
|
434 <xsl:template match="clr:type[@cs:name or @name]" mode="clr-type">
|
|
435 <xsl:param name="typeArgs" select="*" />
|
|
436 <xsl:variable name="ns"
|
|
437 select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
|
|
438 <cs:type name="{@cs:name | @name[not(../@cs:name)]}">
|
|
439 <xsl:if test="$ns">
|
|
440 <xsl:attribute name="namespace"><xsl:value-of select="$ns" /></xsl:attribute>
|
|
441 </xsl:if>
|
|
442 <xsl:copy-of select="@struct"/>
|
|
443
|
|
444 <xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
|
|
445 mode="clr-type" />
|
|
446 </cs:type>
|
|
447 </xsl:template>
|
|
448
|
|
449 <xsl:template match="clr:arrayOf[@type]" mode="clr-type">
|
|
450 <xsl:param name="typeArgs" select="*" />
|
|
451 <cs:array>
|
|
452 <xsl:call-template name="getClrType">
|
|
453 <xsl:with-param name="type" select="@type" />
|
|
454 <xsl:with-param name="typeArgs" select="$typeArgs" />
|
|
455 </xsl:call-template>
|
|
456 </cs:array>
|
|
457 </xsl:template>
|
|
458
|
|
459 <xsl:template match="clr:arrayOf[@cs:name or @name]" mode="clr-type">
|
|
460 <xsl:param name="typeArgs" select="*" />
|
|
461 <xsl:variable name="ns"
|
|
462 select="@cs:namespace | @namespace[not(../@cs:namespace)]" />
|
|
463 <cs:array>
|
|
464 <cs:type name="{@cs:name | @name[not(../@cs:name)]}">
|
|
465 <xsl:if test="$ns">
|
|
466 <xsl:attribute name="namespace"><xsl:value-of
|
|
467 select="$ns" /></xsl:attribute>
|
|
468 </xsl:if>
|
|
469 <xsl:apply-templates select="$typeArgs | *[not($typeArgs)]"
|
|
470 mode="clr-type" />
|
|
471 </cs:type>
|
|
472 </cs:array>
|
|
473 </xsl:template>
|
|
474
|
2
|
475
|
|
476 </xsl:stylesheet> |