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
|
|
9 <xsl:import href="preprocess.xsl" />
|
|
10 <xsl:include href="text-tools.xsl"/>
|
|
11
|
|
12 <xsl:output method="xml" indent="yes" />
|
|
13
|
|
14 <xsl:key name="type" match="m:package/m:*" use="@name" />
|
|
15
|
|
16 <xsl:template match="/">
|
|
17 <cs:document>
|
|
18 <xsl:apply-templates select="$module" mode="document" />
|
|
19 </cs:document>
|
|
20 </xsl:template>
|
|
21
|
|
22 <!-- code generation -->
|
|
23
|
|
24 <!-- disable default template -->
|
|
25 <xsl:template match="*|text()" mode="document" />
|
|
26
|
|
27 <xsl:template match="t:module" mode="document">
|
|
28 <xsl:apply-templates mode="document" />
|
|
29 </xsl:template>
|
|
30
|
3
|
31
|
2
|
32
|
|
33 <!-- member resolution traits -->
|
|
34
|
|
35 <!-- Resolves members using given criteria
|
|
36 @param type
|
|
37 the name of the type whose members are inspected
|
|
38 @param memberName
|
|
39 the name of the member to lookup
|
|
40 @param scope
|
|
41 the scope where to look (members, properties, keys, relations)
|
|
42
|
|
43 This template recursively inspects the given type down to it's inheritance
|
|
44 hierarchy and outputs members matching criteria.
|
|
45 -->
|
|
46 <xsl:template name="getMembers">
|
|
47 <xsl:param name="type"/>
|
|
48 <xsl:param name="memberName" select="''"/>
|
|
49 <xsl:param name="scope" select="'members'"/>
|
|
50 <!-- prevents cyclic references -->
|
|
51 <xsl:param name="seen" select="/.."/>
|
|
52 <xsl:for-each select="$module">
|
|
53 <xsl:apply-templates select="key('type', $type)"
|
|
54 mode="lookup-members">
|
|
55 <xsl:with-param name="memberName" select="$memberName"/>
|
|
56 <xsl:with-param name="scope" select="$scope"/>
|
|
57 <xsl:with-param name="seen" select="$seen"/>
|
|
58 </xsl:apply-templates>
|
|
59 </xsl:for-each>
|
|
60 </xsl:template>
|
|
61
|
|
62 <xsl:template match="*" mode="lookup-members"/>
|
|
63
|
|
64 <xsl:template match="m:entity" mode="lookup-members">
|
|
65 <xsl:param name="memberName" select="''"/>
|
|
66 <xsl:param name="scope" select="'members'"/>
|
|
67 <xsl:param name="seen"/>
|
|
68
|
|
69 <xsl:variable name="self" select="."/>
|
|
70
|
|
71 <xsl:if test="not($seen[generate-id() = generate-id($self)])">
|
|
72 <xsl:variable name="scopeMembers">
|
|
73 <xsl:call-template name="selectScopeMembers">
|
|
74 <xsl:with-param name="scope" select="$scope"/>
|
|
75 </xsl:call-template>
|
|
76 </xsl:variable>
|
|
77
|
6
|
78 <xsl:variable name="members" select="exsl:node-set($scopeMembers)/*[not($memberName) or @name=$memberName]"/>
|
2
|
79
|
|
80 <xsl:choose>
|
|
81 <xsl:when test="$members">
|
|
82 <xsl:copy-of select="$members"/>
|
|
83 </xsl:when>
|
|
84 <xsl:when test="m:extends">
|
|
85 <xsl:call-template name="getMembers">
|
|
86 <xsl:with-param name="type" select="m:extends/@type"/>
|
|
87 <xsl:with-param name="memberName" select="$memberName"/>
|
|
88 <xsl:with-param name="scope" select="$scope"/>
|
|
89 <xsl:with-param name="seen" select="$seen | $self"/>
|
|
90 </xsl:call-template>
|
|
91 </xsl:when>
|
|
92 <xsl:otherwise>
|
|
93 <xsl:call-template name="warn">
|
|
94 <xsl:with-param name="msg">
|
6
|
95 <t:trace msg="failed to resolve {$memberName}, scope={$scope}"/>
|
2
|
96 <t:trace msg="inspected classes">
|
|
97 <xsl:for-each select="$seen | $self">
|
|
98 <t:trace msg="{name()} {@name}"/>
|
|
99 </xsl:for-each>
|
|
100 </t:trace>
|
|
101 </xsl:with-param>
|
|
102 </xsl:call-template>
|
|
103 </xsl:otherwise>
|
|
104 </xsl:choose>
|
|
105 </xsl:if>
|
|
106 </xsl:template>
|
|
107
|
|
108 <!-- this template implements scope filtering -->
|
|
109 <xsl:template name="selectScopeMembers">
|
|
110 <xsl:param name="scope" select="'members'"/>
|
|
111 <xsl:choose>
|
|
112 <xsl:when test="$scope='members'">
|
|
113 <xsl:apply-templates mode="filter-members"/>
|
|
114 </xsl:when>
|
|
115 <xsl:when test="$scope='keys'">
|
6
|
116 <xsl:apply-templates mode="filter-keys"/>
|
2
|
117 </xsl:when>
|
|
118 <xsl:when test="$scope='properties'">
|
|
119 <xsl:apply-templates mode="fiflter-properties"/>
|
|
120 </xsl:when>
|
|
121 <xsl:when test="$scope='relations'">
|
|
122 <xsl:apply-templates mode="fiflter-relations"/>
|
|
123 </xsl:when>
|
|
124 </xsl:choose>
|
|
125 </xsl:template>
|
|
126
|
|
127 <!-- filter-members -->
|
|
128 <xsl:template match="*|text()" mode="filter-members"/>
|
|
129 <xsl:template match="*[@name]" mode="filter-members">
|
|
130 <xsl:copy>
|
|
131 <xsl:copy-of select="@*" />
|
|
132 <xsl:attribute name="declaringType"><xsl:value-of select="../@name" /></xsl:attribute>
|
|
133 <xsl:copy-of select="*" />
|
|
134 </xsl:copy>
|
|
135 </xsl:template>
|
|
136
|
|
137 <!-- filter-keys -->
|
|
138 <xsl:template match="*|text()" mode="filter-keys"/>
|
|
139 <xsl:template match="m:primaryKey" mode="filter-keys">
|
|
140 <xsl:apply-templates select="." mode="filter-members"/>
|
|
141 </xsl:template>
|
|
142
|
|
143 <!-- filter-properties -->
|
|
144 <xsl:template match="*|text()" mode="filter-properties"/>
|
|
145 <xsl:template match="m:property" mode="filter-properties">
|
|
146 <xsl:apply-templates select="." mode="filter-members"/>
|
|
147 </xsl:template>
|
|
148
|
|
149 <!-- filter-relations -->
|
|
150 <xsl:template match="*|text()" mode="filter-relations"/>
|
|
151 <xsl:template match="m:hasA | m:hasMany" mode="filter-relations">
|
|
152 <xsl:apply-templates select="." mode="filter-members"/>
|
|
153 </xsl:template>
|
|
154
|
|
155
|
|
156
|
|
157 <!-- Resolves primaryKey information for the given type name.
|
|
158 @returns m:primaryKey node copy with additional attribute
|
|
159 @declaringType which points to the type where this primaryKey
|
|
160 was defined.
|
|
161 -->
|
|
162 <xsl:template name="getPrimaryKey">
|
|
163 <xsl:param name="type" />
|
|
164 <xsl:call-template name="getMembers">
|
|
165 <xsl:with-param name="type" select="$type"/>
|
6
|
166 <xsl:with-param name="scope" select="'keys'"/>
|
2
|
167 </xsl:call-template>
|
|
168 </xsl:template>
|
|
169
|
7
|
170 <!-- expand-member templates are used to process relations
|
9
|
171 and keys and provide useful information. This mode is designed
|
8
|
172 to recursively traverse members expanding details like special
|
|
173 properties generated by relations.
|
|
174
|
|
175 expand-member-reference is special mode used to traverse inside
|
|
176 members which are referenced from relations. By default this mode
|
|
177 does nothing.
|
|
178
|
|
179 Use this templates to produce intermediate model for further
|
|
180 processing by specific code-generators.
|
7
|
181
|
|
182 @special - means that the member is a composite part and isn't
|
|
183 accessible directly
|
|
184 -->
|
|
185
|
8
|
186 <xsl:template match="*" mode="expand-member">
|
9
|
187 <xsl:copy>
|
|
188 <xsl:copy-of select="@*"/>
|
|
189 <xsl:apply-templates mode="expand-member"/>
|
|
190 </xsl:copy>
|
8
|
191 </xsl:template>
|
|
192
|
|
193
|
7
|
194 <!-- short form of primaryKey{@name, @type} -->
|
|
195 <xsl:template match="m:primaryKey" mode="expand-member">
|
9
|
196 <xsl:variable name="pkName" select="concat(@declaringType, 'Pk')"/>
|
|
197 <xsl:variable name="propName" select="@name" />
|
|
198
|
8
|
199 <xsl:variable name="specialProperty">
|
9
|
200 <m:property name="{$propName}" special="true">
|
|
201 <xsl:copy-of select="@*[not(self::name)]" />
|
|
202 <m:hiddenBy name="{$pkName}" type="{@declaringType}"/>
|
7
|
203 <xsl:copy-of select="*" />
|
|
204 </m:property>
|
8
|
205 </xsl:variable>
|
9
|
206
|
8
|
207 <xsl:apply-templates select="exsl:node-set($specialProperty)" mode="expand-member"/>
|
9
|
208 <xsl:copy>
|
|
209 <xsl:attribute name="name"><xsl:value-of select="$pkName"/></xsl:attribute>
|
|
210 <m:member name="{@name}" type="{@declaringType}"/>
|
|
211 </xsl:copy>
|
7
|
212 </xsl:template>
|
|
213
|
9
|
214 <!-- long form of primaryKey{ member{@name}+ } -->
|
|
215 <xsl:template match="m:primaryKey[m:member]" mode="expand-member">
|
8
|
216 <xsl:apply-templates mode="expand-member"/>
|
7
|
217 </xsl:template>
|
|
218
|
9
|
219 <!-- short form of hasA -->
|
|
220 <xsl:template match="m:hasA" mode="expand-member">
|
|
221 <xsl:variable name="foreignPk">
|
|
222 <xsl:call-template name="getPrimaryKey">
|
|
223 <xsl:with-param name="type" select="@type"/>
|
|
224 </xsl:call-template>
|
8
|
225 </xsl:variable>
|
9
|
226 <xsl:variable name="expandedPk">
|
|
227 <xsl:apply-templates select="exsl:node-set($foreignPk)" mode="expand-member"/>
|
|
228 </xsl:variable>
|
|
229 <xsl:variable name="resolvedPk">
|
|
230 <xsl:apply-templates select="exsl:node-set($expandedPk)/m:primaryKey" mode="resolve-members"/>
|
|
231 </xsl:variable>
|
7
|
232 </xsl:template>
|
|
233
|
9
|
234 <!-- short form of hasA with matched keys specified -->
|
|
235 <xsl:template match="m:hasA[m:thisKey]">
|
|
236 <xsl:apply-templates select="m:thisKey" mode="generate-fk-properties"/>
|
|
237 <xsl:copy>
|
|
238 <xsl:copy-of select="@*"/>
|
|
239 <xsl:apply-templates mode="expand-member"/>
|
|
240 </xsl:copy>
|
7
|
241 </xsl:template>
|
|
242
|
8
|
243 <xsl:template match="m:hasA/m:thisKey" mode="expand-member">
|
9
|
244 <m:member name="{@name}" matches="{@matches}"/>
|
7
|
245 </xsl:template>
|
|
246
|
9
|
247 <!-- long form of hasA -->
|
|
248 <xsl:template match="m:hasA[m:member]" mode="expand-member">
|
7
|
249 <xsl:apply-templates mode="expand-member"/>
|
|
250 </xsl:template>
|
|
251
|
9
|
252 <!-- resolve expands -->
|
|
253
|
|
254 <xsl:template match="m:member" mode="resolve-members">
|
|
255 <xsl:variable name="$name" select="@name"/>
|
|
256 <xsl:variable name="$declaringType" select="@declaringType" />
|
|
257
|
|
258 <xsl:choose>
|
|
259 <xsl:when test="/*[@name=$name and @declaringType=$declaringType]">
|
|
260 <xsl:apply-templates select="/*[@name=$name and @declaringType=$declaringType]" mode="resolve-members"/>
|
|
261 </xsl:when>
|
|
262 <xsl:otherwise>
|
|
263 <xsl:variable name="fragment">
|
|
264 <xsl:call-template name="getMembers">
|
|
265 <xsl:with-param name="memberName" select="$name"/>
|
|
266 <xsl:with-param name="type" select="$declaringType"/>
|
|
267 </xsl:call-template>
|
|
268 </xsl:variable>
|
|
269 <xsl:apply-templates select="exsl:node-set($fragment)/*[not(@special)]" mode="resolve-members"/>
|
|
270 </xsl:otherwise>
|
|
271 </xsl:choose>
|
|
272 </xsl:template>
|
|
273
|
|
274 <xsl:template match="m:property" mode="resolve-members">
|
|
275 <xsl:copy-of select="."/>
|
|
276 </xsl:template>
|
|
277
|
2
|
278 </xsl:stylesheet>
|