Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 40df4ea40c9007b285692205ad696ef2af5e0a29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Renders the final PHP code

  Copyright (C) 2016 LoVullo Associates, Inc.

    This file is part of TAME.

    TAME is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see
    <http://www.gnu.org/licenses/>.
-->
<xsl:stylesheet version="2.0"
  xmlns:c1="http://www.epic-premier.com/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:lvm="http://www.lovullo.com/rater/map/c1"
  xmlns:lvmp="http://www.lovullo.com/rater/map/c1/pp">


<xsl:import href="transform.xsl" />


<xsl:template match="lvmp:root" mode="lvmp:render" priority="5">
  <xsl:text>&lt;?php </xsl:text>
  <xsl:value-of select="$lvmp:nl" />

  <!-- TODO: add program id to namespace -->
  <xsl:text>namespace lovullo\c1\interfaces\c1\contract\</xsl:text>
    <xsl:value-of select="@program" />
    <xsl:text>;</xsl:text>
  <xsl:value-of select="$lvmp:nl" />

  <xsl:text>class </xsl:text>
    <xsl:value-of select="@name" />
  <xsl:text> {</xsl:text>
  <xsl:value-of select="$lvmp:nl" />

    <xsl:text>public function compose( $contract ) {</xsl:text>
    <xsl:value-of select="$lvmp:nl" />
      <xsl:text>    return array(</xsl:text>
      <xsl:value-of select="$lvmp:nl" />
        <!-- render the preprocessed content -->
        <xsl:apply-templates mode="lvmp:render" />
      <xsl:value-of select="$lvmp:nl" />
      <xsl:text>    );</xsl:text>
      <xsl:value-of select="$lvmp:nl" />
    <xsl:text>  }</xsl:text>
    <xsl:value-of select="$lvmp:nl" />

  <xsl:text>}</xsl:text>
  <xsl:value-of select="$lvmp:nl" />
  <xsl:text>?&gt;</xsl:text>
</xsl:template>


<xsl:template match="text()" mode="lvmp:render" priority="5">
  <xsl:value-of select="." />
</xsl:template>


<xsl:template name="lvmp:value" match="lvmp:value" mode="lvmp:render" priority="5">
  <xsl:param name="name" select="@ref" />
  <xsl:param name="scope" select="ancestor::lvmp:scope[1]" />
  <xsl:param name="var" select="$scope/lvmp:var[ @name=$name ][1]" />
  <xsl:param name="from" select="$var/@from" />
  <xsl:param name="value" select="$var/@value" />
  <xsl:param name="default" select="$var/lvmp:default" />

  <!-- provide error if the variable could not be found in the current scope -->
  <xsl:call-template name="lvmp:check-var">
    <xsl:with-param name="name" select="$name" />
    <xsl:with-param name="scope" select="$scope" />
  </xsl:call-template>

  <xsl:choose>
    <!-- mapping was provided -->
    <xsl:when test="$from and not( $from='' )">
      <xsl:apply-templates select="$var" mode="lvmp:transform">
        <xsl:with-param name="value">
          <xsl:apply-templates select="." mode="lvmp:render-value">
            <xsl:with-param name="var" select="$var" />
            <xsl:with-param name="from" select="$from" />
            <xsl:with-param name="scope" select="$scope" />
            <xsl:with-param name="default" select="$default" />
          </xsl:apply-templates>
        </xsl:with-param>
      </xsl:apply-templates>
    </xsl:when>

    <!-- static string mapping -->
    <xsl:when test="$value and not( $value='' )">
      <!-- TODO: escape single quote -->
      <xsl:text>'</xsl:text>
        <xsl:value-of select="$value" />
      <xsl:text>'</xsl:text>
    </xsl:when>

    <!-- if no @from was provided, then it forces the use of the default
         value -->
    <xsl:when test="$default">
      <xsl:text>$contract-&gt;checkDefaultValue( '</xsl:text>
        <xsl:value-of select="$name" />
      <xsl:text>', </xsl:text>
      <xsl:apply-templates select="$default" mode="lvmp:render" />
      <xsl:text> )</xsl:text>
    </xsl:when>

    <!-- if no default is available, then this node shall never be set -->
    <xsl:otherwise>
      <xsl:text>null</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template mode="lvmp:render" priority="7"
  match="lvmp:value[ lvmp:value ]">

  <xsl:param name="name" select="@ref" />
  <xsl:param name="scope" select="ancestor::lvmp:scope[1]" />
  <xsl:param name="var" select="$scope/lvmp:var[ @name=$name ][1]" />
  <xsl:param name="from" select="$var/@from" />
  <xsl:param name="value" select="$var/@value" />
  <xsl:param name="default" select="$var/lvmp:default" />

  <xsl:text>$contract-&gt;getValueByContext( </xsl:text>
    <!-- recursive process contexts -->
    <xsl:apply-templates select="lvmp:value" mode="lvmp:render">
      <xsl:with-param name="scope" select="$scope" />
      <xsl:with-param name="default" select="$default" />
    </xsl:apply-templates>
  <xsl:text>, '</xsl:text>
    <!-- TODO: validate existence of key in dict -->
    <xsl:value-of select="$name" />
  <xsl:text>' </xsl:text>

  <xsl:text> )</xsl:text>
</xsl:template>


<xsl:template match="lvmp:value" mode="lvmp:render-value" priority="1">
  <xsl:param name="var" />
  <xsl:param name="scope" />
  <xsl:param name="default" />
  <xsl:param name="from" />
  <!-- optional -->
  <xsl:param name="value-node" select="." />
  <xsl:param name="context" />

  <!-- the name of the index we reference for our value defaults to our own
       name, but can be overridden in the mapping -->
  <xsl:param name="index-name">
    <xsl:choose>
      <!-- link provided; override -->
      <xsl:when test="$var/@link">
        <!-- parse the link and replace it with the var it maps to -->
        <xsl:call-template name="lvmp:var-from">
          <xsl:with-param name="name" select="$var/@link" />
          <xsl:with-param name="scope" select="$scope" />
        </xsl:call-template>
      </xsl:when>

      <!-- default to our own index -->
      <xsl:otherwise>
        <xsl:value-of select="$from" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:param>

  <xsl:text>$contract-&gt;getValue( '</xsl:text>
    <xsl:value-of select="$from" />
  <xsl:text>', $contract->getValueIndex( '</xsl:text>
    <xsl:value-of select="$index-name" />
  <xsl:text>' )</xsl:text>

  <xsl:if test="$default">
    <xsl:text>, </xsl:text>
    <xsl:apply-templates select="$default" mode="lvmp:render" />
  </xsl:if>

  <xsl:text> )</xsl:text>
</xsl:template>


<!--
  Generates an error in the event that the given var name cannot be found in
  the given scope.
-->
<xsl:template name="lvmp:check-var">
  <xsl:param name="name" />
  <xsl:param name="scope" select="ancestor::lvmp:scope[1]" />

  <!-- look up variable in parent scope -->
  <xsl:variable name="var" select="$scope/lvmp:var[ @name=$name ][1]" />

  <!-- provide error if the variable could not be found in the current scope -->
  <xsl:if test="not( $var )">
    <xsl:message terminate="yes">
      <xsl:text>error: variable not within scope: </xsl:text>
      <xsl:value-of select="$scope/@id" />
      <xsl:text>/</xsl:text>
      <xsl:value-of select="$name" />
    </xsl:message>
  </xsl:if>
</xsl:template>


<!--
  Validates and then returns the source mapping of the given variable within
  the given (or current) scope
-->
<xsl:template name="lvmp:var-from">
  <xsl:param name="name" />
  <xsl:param name="scope" select="ancestor::lvmp:scope[1]" />

  <!-- undefined/scoping check -->
  <xsl:call-template name="lvmp:check-var">
    <xsl:with-param name="name" select="$name" />
    <xsl:with-param name="scope" select="$scope" />
  </xsl:call-template>

  <xsl:value-of select="$scope/lvmp:var[ @name=$name ][1]/@from" />
</xsl:template>


<xsl:template match="lvmp:translate" mode="lvmp:render" priority="5">
  <!-- no longer needed -->
</xsl:template>

<xsl:template match="lvmp:var" mode="lvmp:render" priority="5">
  <!-- no longer needed -->
</xsl:template>

<xsl:template match="lvmp:var/lvmp:default" mode="lvmp:render" priority="5">
  <!-- render contents -->
  <xsl:apply-templates mode="lvmp:render" />
</xsl:template>


<!--
  Scope boundary; no longer needed in output
-->
<xsl:template match="lvmp:scope" mode="lvmp:render" priority="2">
  <xsl:apply-templates mode="lvmp:render" />
</xsl:template>


<!--
  Conditional boundary; not needed in output
-->
<xsl:template match="lvmp:condition" mode="lvmp:render" priority="2">
  <xsl:apply-templates mode="lvmp:render" />
</xsl:template>

<xsl:template match="lvmp:condition/lvmp:when" mode="lvmp:render" priority="2">
  <!-- will be processed as part of sibling output -->
</xsl:template>


<!--
  Iteration boundary
-->
<xsl:template match="lvmp:for-each" mode="lvmp:render" priority="5">
  <xsl:variable name="from">
    <xsl:call-template name="lvmp:var-from">
      <xsl:with-param name="name" select="@name" />
    </xsl:call-template>
  </xsl:variable>

  <xsl:text>$contract->iterateValues( '</xsl:text>
      <xsl:value-of select="$from" />
    <xsl:text>', </xsl:text>
    <xsl:text>function( $contract ) {</xsl:text>
      <xsl:text>  return array(</xsl:text>
      <xsl:apply-templates mode="lvmp:render" />
      <xsl:text>);</xsl:text>
    <xsl:text>}</xsl:text>
  <xsl:text>)</xsl:text>
</xsl:template>


<!--
  The C1 import system recognizes the following formats:

    '>Node'
    'N>Node'

  where N is any number; the latter is used for creating unique indexes for
  multiple siblings of the same name.
-->
<xsl:template match="lvmp:node" mode="lvmp:render" priority="5">
  <xsl:variable name="name" select="@name" />

  <xsl:text>'</xsl:text>

  <!-- generate a unique key to avoid conflicts -->
  <xsl:value-of select="generate-id(.)" />

  <xsl:text>&gt;</xsl:text>
  <xsl:value-of select="@name" />

  <xsl:text>'</xsl:text>
</xsl:template>


<!--
  Node boundary; not needed in output

  The boundary dictates the context of a conditional. This template has a lower
  priority and will be the default if there is no parent conditional.
-->
<xsl:template match="lvmp:node-boundary" mode="lvmp:render" priority="2">
  <xsl:apply-templates mode="lvmp:render" />

  <!-- unconditional delimiter; nothing funky going on -->
  <xsl:text>, </xsl:text>
</xsl:template>

<xsl:template mode="lvmp:render" priority="5" match="
  lvmp:node-boundary[
    parent::lvmp:condition
    or parent::lvmp:scope/parent::lvmp:condition
  ]
">
  <!-- select the nearest condition -->
  <xsl:variable name="cond" select="ancestor::lvmp:condition[1]" />

  <xsl:text>( ( </xsl:text>
    <xsl:text>$contract->isTruthy( </xsl:text>
      <xsl:apply-templates select="$cond/lvmp:when/lvmp:*" mode="lvmp:render" />
    <xsl:text>)</xsl:text>
  <xsl:text> ) ? </xsl:text>
    <xsl:apply-templates mode="lvmp:render">
      <xsl:with-param name="no-trailing-sep" select="true()" />
    </xsl:apply-templates>
  <xsl:text> : null ), </xsl:text>
</xsl:template>


<xsl:template match="*" mode="lvmp:render" priority="1">
  <xsl:message terminate="yes">
    <xsl:text>[c1map] fatal: unexpected node during render: </xsl:text>
    <xsl:apply-templates select="." mode="lvmp:node-out" />
  </xsl:message>
</xsl:template>

</xsl:stylesheet>