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