Mercurial > pub > ModelGenerator
comparison xslt/generator.csharp.xsl @ 2:035de8b7b18e
Temporary commit, refactoring, added readme
author | cin |
---|---|
date | Sun, 25 Feb 2018 17:12:33 +0300 |
parents | |
children | 437127ab6a12 |
comparison
equal
deleted
inserted
replaced
1:7f803979305f | 2:035de8b7b18e |
---|---|
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 | |
12 <!-- entity --> | |
13 <xsl:template match="m:entity" mode="document"> | |
14 <xsl:apply-templates select="." mode="entity"/> | |
15 </xsl:template> | |
16 | |
17 <xsl:template match="m:entity" mode="entity"> | |
18 <cs:class name="{@name}" modifiers="partial"> | |
19 <xsl:attribute name="name"> | |
20 <xsl:apply-templates select="." mode="class-name"/> | |
21 </xsl:attribute> | |
22 <xsl:apply-templates mode="class-attributes" /> | |
23 <xsl:apply-templates mode="members" /> | |
24 </cs:class> | |
25 </xsl:template> | |
26 | |
27 <!-- class-name --> | |
28 <xsl:template match="*[@name]" mode="class-name"> | |
29 <xsl:value-of select="@name"/> | |
30 </xsl:template> | |
31 | |
32 <xsl:template match="*[@clr:name]" mode="class-name"> | |
33 <xsl:value-of select="@clr:name"/> | |
34 </xsl:template> | |
35 | |
36 <!-- class-attributes --> | |
37 <xsl:template match="*|text()" mode="class-attributes" /> | |
38 | |
39 <xsl:template match="sql:table" mode="class-attributes"> | |
40 <cs:attribute> | |
41 <cs:type name="Table" namespace="Linq2Db" /> | |
42 <cs:parameter><cs:string text="{@name}"/></cs:parameter> | |
43 </cs:attribute> | |
44 </xsl:template> | |
45 | |
46 <!-- generate members --> | |
47 | |
48 <xsl:template match="*|text()" mode="members" /> | |
49 | |
50 <xsl:template match="m:primaryKey | m:property | m:thisKey | clr:association" mode="members"> | |
51 <t:trace msg="{name()} {@name}"/> | |
52 <xsl:apply-templates select="." mode="property"/> | |
53 </xsl:template> | |
54 | |
55 <!-- hasA and hasMany doesn't generate members itself, they delegate this work to inner members --> | |
56 <xsl:template match="m:hasA | m:hasMany" mode="members"> | |
57 <t:trace msg="{name()} {@name}" /> | |
58 <xsl:apply-templates mode="members" /> | |
59 </xsl:template> | |
60 | |
61 <xsl:template match="m:hasA/clr:lazy" mode="members"> | |
62 <xsl:apply-templates select="." mode="field"/> | |
63 </xsl:template> | |
64 | |
65 <!-- member-name --> | |
66 <xsl:template match="*|text()|@*" mode="member-name" /> | |
67 | |
68 <xsl:template match="*[@name]" mode="member-name"> | |
69 <xsl:value-of select="@name"/> | |
70 </xsl:template> | |
71 <xsl:template match="*[@clr:name]" mode="member-name"> | |
72 <xsl:value-of select="@clr:name"/> | |
73 </xsl:template> | |
74 | |
75 <!-- member-type --> | |
76 <xsl:template match="*|text()|@*" mode="member-type" /> | |
77 | |
78 <xsl:template match="*[@type]" mode="member-type"> | |
79 <xsl:call-template name="getClrType"> | |
80 <xsl:with-param name="type" select="@type" /> | |
81 </xsl:call-template> | |
82 </xsl:template> | |
83 | |
84 <xsl:template match="*[clr:type]" mode="member-type"> | |
85 <xsl:apply-templates select="clr:type" mode="clr-type" /> | |
86 </xsl:template> | |
87 | |
88 <!-- member-attributes --> | |
89 <xsl:template match="*|text()" mode="member-attributes"/> | |
90 | |
91 <!-- member-extension --> | |
92 <xsl:template match="*|text()" mode="member-extension"/> | |
93 <xsl:template match="*" mode="member-extension"> | |
94 <xsl:apply-templates mode="member-extension-comments"/> | |
95 </xsl:template> | |
96 | |
97 <xsl:template match="m:hasA/* | m:hasMany/*" mode="member-extension"> | |
98 <xsl:variable name="comments" select="../m:description | m:description"/> | |
99 <xsl:apply-templates select="$comments[position() = count($comments)]" mode="member-extension-comments"/> | |
100 </xsl:template> | |
101 | |
102 <!-- member-extension-comments --> | |
103 <xsl:template match="*|text()" mode="member-extension-comments"/> | |
104 | |
105 <xsl:template match="m:description" mode="member-extension-comments"> | |
106 <cs:comments> | |
107 <xsl:apply-templates mode="comments" /> | |
108 </cs:comments> | |
109 </xsl:template> | |
110 | |
111 <!-- property --> | |
112 | |
113 <xsl:template match="*" mode="property"> | |
114 <cs:property modifiers="public"> | |
115 <xsl:attribute name="name"><xsl:apply-templates select="." mode="property-name"/></xsl:attribute> | |
116 <xsl:apply-templates select="." mode="property-type"/> | |
117 <xsl:apply-templates select="." mode="property-attributes"/> | |
118 <xsl:apply-templates select="." mode="property-extension" /> | |
119 <xsl:apply-templates select="." mode="property-accessors"/> | |
120 </cs:property> | |
121 </xsl:template> | |
122 | |
123 <!-- property-name --> | |
124 <xsl:template match="m:hasA/clr:association[not(@name|@clr:name)]" mode="property-name"> | |
125 <!-- if the association doesn't define a name, use it from the parent node --> | |
126 <xsl:apply-templates select=".." mode="property-name"/> | |
127 </xsl:template> | |
128 <xsl:template match="m:hasMany/clr:association[not(@name|@clr:name)]" mode="property-name"> | |
129 <!-- if the association doesn't define a name, use it from the parent node --> | |
130 <xsl:apply-templates select=".." mode="property-name"/> | |
131 </xsl:template> | |
132 | |
133 <xsl:template match="*" mode="property-name"> | |
134 <xsl:apply-templates select="." mode="member-name"/> | |
135 </xsl:template> | |
136 | |
137 <!-- property-type --> | |
138 <xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="property-type"> | |
139 <xsl:apply-templates select=".." mode="property-type"/> | |
140 </xsl:template> | |
141 | |
142 <xsl:template match="m:hasMany[@type]/clr:association[not(clr:type)]" mode="property-type"> | |
143 <cs:array> | |
144 <xsl:apply-templates select=".." mode="property-type"/> | |
145 </cs:array> | |
146 </xsl:template> | |
147 | |
148 <xsl:template match="m:hasA[@type]/m:thisKey" mode="property-type"> | |
149 <xsl:call-template name="getKeyType"> | |
150 <xsl:with-param name="type" select="../@type"/> | |
151 </xsl:call-template> | |
152 </xsl:template> | |
153 | |
154 <xsl:template match="m:hasA[@type and boolean(@optional)]/m:thisKey" mode="property-type"> | |
155 <cs:nullable> | |
156 <xsl:call-template name="getKeyType"> | |
157 <xsl:with-param name="type" select="../@type"/> | |
158 </xsl:call-template> | |
159 </cs:nullable> | |
160 </xsl:template> | |
161 | |
162 <xsl:template match="*" mode="property-type"> | |
163 <xsl:apply-templates select="." mode="member-type"/> | |
164 </xsl:template> | |
165 | |
166 <!-- property-attributes --> | |
167 <xsl:template match="m:primaryKey" mode="property-attributes"> | |
168 <cs:attribute> | |
169 <cs:type name="PrimaryKey" namespace="Linq2Db" /> | |
170 </cs:attribute> | |
171 </xsl:template> | |
172 | |
173 <xsl:template match="m:hasA/clr:association" mode="property-attributes"> | |
174 <cs:attribute> | |
175 <cs:type name="Association" namespace="Linq2Db"/> | |
176 <cs:parameter name="thisKey"> | |
177 <cs:nameOf> | |
178 <xsl:call-template name="getMember"> | |
179 <xsl:with-param name="memberName" select="../@name"/> | |
180 <xsl:with-param name="explicitMemberName" select="@thisKey"/> | |
181 <xsl:with-param name="type" select="../../@name"/> | |
182 <xsl:with-param name="short" select="true()"/> | |
183 </xsl:call-template> | |
184 </cs:nameOf> | |
185 </cs:parameter> | |
186 <cs:parameter name="otherKey"> | |
187 <cs:nameOf> | |
188 <xsl:call-template name="getKeyMember"> | |
189 <xsl:with-param name="explicitMemberName" select="@thisKey"/> | |
190 <xsl:with-param name="type" select="../@type"/> | |
191 </xsl:call-template> | |
192 </cs:nameOf> | |
193 </cs:parameter> | |
194 </cs:attribute> | |
195 </xsl:template> | |
196 | |
197 <xsl:template match="m:hasMany/clr:association" mode="property-attributes"> | |
198 <cs:attribute> | |
199 <cs:type name="Association" namespace="Linq2Db"/> | |
200 <!-- thisKey points to own primaryKey which may be inherited, using getKeyName to address such cases --> | |
201 <cs:parameter name="thisKey"> | |
202 <cs:nameOf> | |
203 <xsl:call-template name="getKeyMember"> | |
204 <xsl:with-param name="type" select="../../@name"/> | |
205 <xsl:with-param name="explicitMemberName" select="@thisKey"/> | |
206 <xsl:with-param name="short" select="true()"/> | |
207 </xsl:call-template> | |
208 </cs:nameOf> | |
209 </cs:parameter> | |
210 <cs:parameter name="otherKey"> | |
211 <cs:nameOf> | |
212 <xsl:call-template name="getMember"> | |
213 <xsl:with-param name="type" select="../@type"/> | |
214 <xsl:with-param name="memberName" select="../m:otherKey/@name"/> | |
215 <xsl:with-param name="explicitMemberName" select="@otherKey"/> | |
216 </xsl:call-template> | |
217 </cs:nameOf> | |
218 </cs:parameter> | |
219 </cs:attribute> | |
220 </xsl:template> | |
221 | |
222 <xsl:template match="*" mode="property-attributes"> | |
223 <xsl:apply-templates select="." mode="member-attributes"/> | |
224 </xsl:template> | |
225 | |
226 <!-- property-extension --> | |
227 <xsl:template match="*" mode="property-extension"> | |
228 <xsl:apply-templates select="." mode="member-extension"/> | |
229 </xsl:template> | |
230 | |
231 <!-- property-accessors --> | |
232 <xsl:template match="*" mode="property-accessors"> | |
233 <cs:get/> | |
234 <cs:set/> | |
235 </xsl:template> | |
236 | |
237 <xsl:template match="m:hasA[clr:lazy]/m:thisKey" mode="property-accessors"> | |
238 <cs:get> | |
239 <xsl:text>return </xsl:text> | |
240 <xsl:apply-templates select="../clr:lazy" mode="field-name"/> | |
241 <xsl:text>.Key;</xsl:text> | |
242 </cs:get> | |
243 <cs:set> | |
244 <xsl:apply-templates select="../clr:lazy" mode="field-name"/> | |
245 <xsl:text>.Key = value;</xsl:text> | |
246 </cs:set> | |
247 </xsl:template> | |
248 | |
249 <xsl:template match="m:hasA[clr:lazy]/clr:association" mode="property-accessors"> | |
250 <cs:get> | |
251 <xsl:text>return </xsl:text> | |
252 <xsl:apply-templates select="../clr:lazy" mode="field-name"/> | |
253 <xsl:text>.Instance;</xsl:text> | |
254 </cs:get> | |
255 <cs:set> | |
256 <xsl:apply-templates select="../clr:lazy" mode="field-name"/> | |
257 <xsl:text>.Instance = value;</xsl:text> | |
258 </cs:set> | |
259 </xsl:template> | |
260 | |
261 <!-- fields --> | |
262 <xsl:template match="*" mode="field"> | |
263 <cs:field> | |
264 <xsl:attribute name="name"><xsl:apply-templates select="." mode="field-name"/></xsl:attribute> | |
265 <xsl:apply-templates select="." mode="field-type"/> | |
266 <xsl:apply-templates select="." mode="field-initializer"/> | |
267 </cs:field> | |
268 </xsl:template> | |
269 | |
270 <!-- field-name --> | |
271 <xsl:template match="m:hasA/clr:lazy" mode="field-name"> | |
272 <xsl:text>m_lazy</xsl:text> | |
273 <xsl:apply-templates select=".." mode="property-name"/> | |
274 </xsl:template> | |
275 | |
276 <xsl:template match="clr:lazy[@field]" mode="field-name"> | |
277 <xsl:value-of select="@field"/> | |
278 </xsl:template> | |
279 | |
280 <xsl:template match="*" mode="field-name"> | |
281 <xsl:apply-templates select="." mode="member-name"/> | |
282 </xsl:template> | |
283 | |
284 <!-- field-type --> | |
285 <xsl:template match="m:hasA[@optional='true']/clr:lazy" mode="field-type"> | |
286 <cs:type name="NullableReference" namespace="Pallada.Data"> | |
287 <xsl:call-template name="getKeyType"> | |
288 <xsl:with-param name="type" select="../@type"/> | |
289 </xsl:call-template> | |
290 <xsl:apply-templates select=".." mode="property-type"/> | |
291 </cs:type> | |
292 </xsl:template> | |
293 <xsl:template match="m:hasA[@optional!='true']/clr:lazy" mode="field-type"> | |
294 <cs:type name="Reference" namespace="Pallada.Data"> | |
295 <xsl:apply-templates select="../m:thisKey" mode="property-type"/> | |
296 <xsl:apply-templates select=".." mode="property-type"/> | |
297 </cs:type> | |
298 </xsl:template> | |
299 | |
300 <xsl:template match="*" mode="field-type"> | |
301 <xsl:apply-templates select="." mode="member-type"/> | |
302 </xsl:template> | |
303 <!-- field-initializer --> | |
304 <xsl:template match="*|text()" mode="field-initializer"/> | |
305 | |
306 <xsl:template name="getKeyClrMemberName"> | |
307 <xsl:param name="explicitMemberName" select="''"/> | |
308 <xsl:param name="type"/> | |
309 <xsl:param name="short" select="false()"/> | |
310 <cs:member> | |
311 <xsl:choose> | |
312 <xsl:when test="$explicitMemberName"> | |
313 <xsl:attribute name="name"> | |
314 <xsl:value-of select="$explicitMemberName"/> | |
315 </xsl:attribute> | |
316 </xsl:when> | |
317 <xsl:otherwise> | |
318 <xsl:attribute name="name"> | |
319 <xsl:call-template name="getKeyName"> | |
320 <xsl:with-param name="type" select="$type"/> | |
321 </xsl:call-template> | |
322 </xsl:attribute> | |
323 </xsl:otherwise> | |
324 </xsl:choose> | |
325 <xsl:if test="not($short)"> | |
326 <xsl:call-template name="getClrType"> | |
327 <xsl:with-param name="type" select="$type"/> | |
328 </xsl:call-template> | |
329 </xsl:if> | |
330 </cs:member> | |
331 </xsl:template> | |
332 | |
333 <xsl:template name="getClrMemberName"> | |
334 <xsl:param name="memberName"/> | |
335 <xsl:param name="type"/> | |
336 <xsl:param name="explicitMemberName" select="''"/> | |
337 <xsl:param name="short" select="false()"/> | |
338 <cs:member> | |
339 <xsl:choose> | |
340 <xsl:when test="$explicitMemberName"> | |
341 <xsl:attribute name="name"> | |
342 <xsl:value-of select="$explicitMemberName"/> | |
343 </xsl:attribute> | |
344 </xsl:when> | |
345 <xsl:otherwise> | |
346 <xsl:attribute name="name"> | |
347 <xsl:call-template name="getClrPropertyName"> | |
348 <xsl:with-param name="member" select="$memberName"/> | |
349 <xsl:with-param name="type" select="$type"/> | |
350 </xsl:call-template> | |
351 </xsl:attribute> | |
352 </xsl:otherwise> | |
353 </xsl:choose> | |
354 <xsl:if test="not($short)"> | |
355 <xsl:call-template name="getClrType"> | |
356 <xsl:with-param name="type" select="$type"/> | |
357 </xsl:call-template> | |
358 </xsl:if> | |
359 </cs:member> | |
360 </xsl:template> | |
361 | |
362 <!-- lookups for the specified member for the specified type | |
363 and returns the CLR property name --> | |
364 <xsl:template name="getClrPropertyName"> | |
365 <xsl:param name="type"/> | |
366 <xsl:param name="member"/> | |
367 <!-- prevents cyclic references --> | |
368 <xsl:param name="seen" select="/.."/> | |
369 <xsl:for-each select="$module"> | |
370 <xsl:apply-templates select="key('type', $type)" | |
371 mode="member-lookup"> | |
372 <xsl:with-param name="member" select="$member"/> | |
373 <xsl:with-param name="seen" select="$seen"/> | |
374 </xsl:apply-templates> | |
375 </xsl:for-each> | |
376 </xsl:template> | |
377 | |
378 | |
379 </xsl:stylesheet> |