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