Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 660fdf759adfcc609726261ae05fdcb104b24a06 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Generates PHP code that works with the LoVullo ConceptOne import system

  This map expects that the data are available in the bucket provided by the
  quote and therefore validates against a provided Program UI source file. Data
  external to the bucket may be provided if it is indicated as such.

  Each map source file is independent; variables and values do not bleed into
  one-another, unless explicitly passed.
-->
<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:output
  indent="yes"
  omit-xml-declaration="yes"
  />

<!-- newline -->
<xsl:variable name="lvmp:nl" select="'&#10;'" />

<xsl:include href="c1map/c1nodes.xsl" />
<xsl:include href="c1map/valparse.xsl" />
<xsl:include href="c1map/render.xsl" />

<!--
  Represents the root of the source document that processing was initiated upon
-->
<xsl:variable name="orig-root" select="/" />


<!--
  The root node
-->
<xsl:template match="lvm:c1-map" priority="5">
  <!-- populated by includes, if any -->
  <xsl:param name="args" />

  <xsl:message select="$args" />

  <!-- get the name from the first C1 node -->
  <xsl:variable name="name" select="
      if ( @id ) then
        @id
      else
        concat( translate( c1:*[1]/name(), '_', '' ), 'Composer' )
    " />

  <!-- preprocessed result -->
  <xsl:variable name="pp-result">
    <lvmp:root program="{@program}" name="{$name}">
      <!-- introduce outer scope for variables -->
      <lvmp:scope id="">
        <xsl:apply-templates />
      </lvmp:scope>
    </lvmp:root>
  </xsl:variable>

  <!-- final processing -->
  <xsl:variable name="result">
    <xsl:apply-templates select="$pp-result/lvmp:root" mode="lvmp:render" />
  </xsl:variable>

  <!-- remove escapes -->
  <xsl:value-of disable-output-escaping="yes" select="$result" />
</xsl:template>


<!--
  Include another source map file relative to the path of the original source
  file

  The attributes @name and @for-each are special and cannot be used as
  arguments to the template. The special @for-each attribute will be copied
  into each of the children of the root node in the template as @lvm:for-each.
  For example, if the template consists of

    <c1-map>
      <Product>
      </Product>
    </c1-map>

  then <lvm:include for-each="foo" /> would produce

    <c1-map>
      <Product lvm:for-each="foo">
      </Product>
    </c1-map>
-->
<xsl:template match="lvm:include" priority="5">
  <xsl:message>[c1map] +<xsl:value-of select="@name" /></xsl:message>

  <xsl:variable name="src" select="
      document( concat( @name, '.xml' ), $orig-root )/lvm:c1-map
    " />

  <xsl:if test="not( $src )">
    <xsl:message terminate="yes">fatal: c1-map node not found</xsl:message>
  </xsl:if>

  <!-- process the body of the c1-map; we don't want to process the root node,
       as that would start processing from scratch, prematurely rendering the result -->
  <lvmp:scope id="/{@name}">
    <!-- arguments are included as attributes -->
    <xsl:variable name="args" select="
        @*[ not( local-name()='name' or local-name()='for-each' ) ]
      " />
    <xsl:variable name="for-each" select="@for-each" />

    <!-- augment the XML with our own mappings -->
    <xsl:variable name="augmented">
      <lvm:c1-map>
        <xsl:copy-of select="$src/@*" />

        <xsl:for-each select="$src/lvm:param">
          <xsl:call-template name="lvmp:param-to-map">
            <xsl:with-param name="args" select="$args" />
            <xsl:with-param name="param" select="." />
            <xsl:with-param name="context" select="/lvm:c1-map" />
          </xsl:call-template>
        </xsl:for-each>


        <xsl:choose>
          <!-- if @for-each was provided, then apply to all first-level
               children of the included template -->
          <xsl:when test="$for-each">
            <!-- we will need to expose the mapping -->
            <!-- TODO: need to validate that it actually exists -->
            <lvm:external name="{$for-each}" />

            <xsl:for-each select="$src/*">
              <xsl:copy>
                <xsl:copy-of select="@*" />
                <xsl:attribute name="lvm:for-each" select="$for-each" />
                <xsl:copy-of select="*|text()" />
              </xsl:copy>
            </xsl:for-each>
          </xsl:when>

          <!-- no @for-each; just do a quick copy of all the nodes -->
          <xsl:otherwise>
            <xsl:copy-of select="$src/*" />
          </xsl:otherwise>
        </xsl:choose>
      </lvm:c1-map>
    </xsl:variable>

    <xsl:apply-templates select="$augmented/lvm:c1-map/*" />
  </lvmp:scope>

  <xsl:message>[c1map] -<xsl:value-of select="@name" /></xsl:message>
</xsl:template>


<!--
  Processes a template param into mappings

  This will generate the mappings necessary to process the template as though
  it was hard-coded with the imported mappings.

  The {} brace syntax denotes a variable, but mixing values and inline
  variables are not supported.
-->
<xsl:template name="lvmp:param-to-map">
  <xsl:param name="args" />
  <xsl:param name="param" />
  <xsl:param name="context" />

  <xsl:variable name="name" select="$param/@name" />
  <xsl:variable name="arg" select="$args[ local-name()=$name ]" />
  <xsl:variable name="argvar" select="substring-after( $arg, '{' )" />

  <xsl:if test="$argvar and not( $argvar='' )">
    <xsl:variable name="varname" select="substring-before( $argvar, '}' )" />

    <lvmp:translate name="{$name}" to="{$varname}" />

    <xsl:variable name="predot" select="substring-before( $varname, '.' )" />

    <xsl:choose>
      <!-- no dot; output the entire thing -->
      <xsl:when test="$predot = ''">
        <lvm:external name="{$varname}" />
      </xsl:when>

      <!-- multi-level var -->
      <xsl:otherwise>
        <lvm:external name="{$predot}" dict="true" lvmp:no-validate="true" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:if>

  <!-- TODO: no need to do this if the above conditional matches -->
  <lvm:map to="{$name}" lvmp:allow-default="true">
    <xsl:copy-of select="$param/@dict" />
    <xsl:copy-of select="$param/@default" />

    <xsl:choose>
      <xsl:when test="$arg">
        <!-- determines if we have a variable -->

        <xsl:choose>
          <xsl:when test="$argvar and not( $argvar='' )">
            <xsl:attribute name="from"
              select="substring-before( $argvar, '}' )" />
          </xsl:when>

          <!-- static value -->
          <xsl:otherwise>
            <xsl:attribute name="value" select="$arg" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>

      <!-- required param -->
      <xsl:when test="$param/@required">
        <xsl:message terminate="yes">
          <xsl:text>error: missing required template argument `</xsl:text>
            <xsl:value-of select="$name" />
          <xsl:text>'</xsl:text>
        </xsl:message>
      </xsl:when>

      <!-- otherwise, we have no value -->
      <xsl:otherwise>
        <xsl:attribute name="value" select="''" />
      </xsl:otherwise>
    </xsl:choose>
  </lvm:map>
</xsl:template>


<!--
  Common actions performed by nearly every mapping
-->
<xsl:template name="lvmp:map-common">
  <!-- may or may not be set -->
  <xsl:copy-of select="@dict" />
  <xsl:copy-of select="@link" />
  <xsl:copy-of select="@transform" />

  <xsl:apply-templates select="@default" />
</xsl:template>


<xsl:template match="lvm:param" priority="4">
  <!-- processed above on import; no longer needed -->
</xsl:template>

<xsl:template match="lvm:map[@from]" priority="4">
  <lvmp:var name="{@to}" from="{@from}" src="map">
    <xsl:call-template name="lvmp:map-common" />
  </lvmp:var>
</xsl:template>

<xsl:template match="lvm:map[lvm:from]" priority="4">
  <lvmp:var name="{@to}" from="{lvm:from/@name}">
    <xsl:call-template name="lvmp:map-common" />
  </lvmp:var>
</xsl:template>

<xsl:template match="lvm:map[@value]" priority="4">
  <!-- it does not make sense to have a string value be a dictionary -->
  <xsl:if test="@dict">
    <xsl:message terminate="yes">
      <xsl:text>error: cannot have @dict on static mapping `</xsl:text>
        <xsl:value-of select="@to" />
      <xsl:text>'</xsl:text>
    </xsl:message>
  </xsl:if>

  <!-- nor does a default make sense -->
  <xsl:if test="@default and not( @lvmp:allow-default='true' )">
    <xsl:message terminate="yes">
      <xsl:text>error: cannot have @default on static mapping `</xsl:text>
        <xsl:value-of select="@to" />
      <xsl:text>'</xsl:text>
    </xsl:message>
  </xsl:if>

  <lvmp:var name="{@to}" value="{@value}">
    <!-- we may use defaults internally -->
    <xsl:call-template name="lvmp:map-common" />
  </lvmp:var>
</xsl:template>

<xsl:template match="lvm:pass" priority="4">
  <lvmp:var name="{@name}" from="{@name}" src="map">
    <xsl:call-template name="lvmp:map-common" />
  </lvmp:var>
</xsl:template>

<xsl:template match="lvm:external" priority="4">
  <lvmp:var name="{@name}" from="{@name}" src="external">
    <xsl:call-template name="lvmp:map-common" />
  </lvmp:var>
</xsl:template>

<xsl:template match="lvm:*/@default">
  <lvmp:default>
    <xsl:apply-templates select="." mode="lvm:valparse" />
  </lvmp:default>
</xsl:template>


<xsl:template match="lvmp:translate" priority="4">
  <!-- added by pre-processor during include; ignore -->
</xsl:template>


<!--
  Override default behavior of c1 nodes when iteration is requested
-->
<xsl:template match="c1:*[ @lvm:for-each ]"
  mode="lvmp:c1-node-result" priority="5">

  <lvmp:for-each name="{@lvm:for-each}">
    <!-- proceed with processing as normal -->
    <xsl:apply-templates select="@*|*" />
  </lvmp:for-each>
</xsl:template>


<xsl:template match="lvm:if" priority="4">
  <lvmp:condition>
    <lvmp:when>
      <xsl:call-template name="lvmp:gen-val">
        <xsl:with-param name="name" select="@name" />
      </xsl:call-template>
    </lvmp:when>

    <xsl:apply-templates />
  </lvmp:condition>
</xsl:template>


<!--
  Unhandled node character data

  Note that, if a node contains newlines, then there will be text preceding and
  following its children. For example:

    <foo>
      <bar>
    </foo>

  In the above, the `foo' node has the text "\n  ", followed by the node `bar',
  followed by the text "\n" (assuming that `foo' starts in column 1).
-->
<xsl:template match="text()" priority="1">
  <!-- do not output whitespace from source files -->
</xsl:template>


<!--
  Bail out on unhandled nodes.
-->
<xsl:template match="*" priority="1">
  <xsl:message>
    <xsl:text>[c1map] fatal: unexpected node </xsl:text>
    <xsl:apply-templates select="." mode="lvmp:node-out" />
    <xsl:text>:</xsl:text>
  </xsl:message>

  <xsl:message terminate="yes" select="." />
</xsl:template>

<xsl:template match="*" mode="lvmp:node-out">
  <xsl:variable name="parent" select="parent::*" />
  <xsl:if test="$parent">
    <xsl:apply-templates select="$parent" mode="lvmp:node-out" />
  </xsl:if>

  <xsl:text>/</xsl:text>
  <xsl:value-of select="name()" />
</xsl:template>

</xsl:stylesheet>