Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: bdb6282c2861fc6091fe6c3fde7ef05845b7809a (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
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Compiles domains from typedefs

  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/>.

  The ultime goal is to implement typedefs as macros and move to a generic
  domain system that is much more powerful.
-->
<xsl:stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

  xmlns:preproc="http://www.lovullo.com/rater/preproc"
  xmlns:lv="http://www.lovullo.com/rater"
  xmlns:c="http://www.lovullo.com/calc">


<!--
  Base typedefs (internal) are declarations only

  TODO: We still want a domain generated.
-->
<xsl:template match="lv:typedef[ lv:base-type ]"
  mode="preproc:mkdomain" priority="9">
</xsl:template>


<!--
  Union typedefs

  This generates not only the contained typedefs, but also a denormalized
  domain containing the union of all the subdomain elements. This is
  intended to improve lookup performance and reduce algorithmic complexity.
-->
<xsl:template match="
    lv:typedef[
      lv:union
    ]
  "
  mode="preproc:mkdomain" priority="5">

  <!-- generate all contained domains -->
  <xsl:variable name="subdomains">
    <xsl:variable name="union-types" select="
      lv:union/lv:typedef" />

    <!-- if a union is empty, then somebody probably made an oopsie or wrote
    a defective/deficient template -->
    <xsl:if test="count( $union-types ) = 0">
      <xsl:message>
        <xsl:text>warning: union `</xsl:text>
          <xsl:value-of select="@name" />
        <xsl:text>' has no subdomains; something is probably wrong</xsl:text>
      </xsl:message>
    </xsl:if>

    <xsl:apply-templates mode="preproc:mkdomain"
      select="$union-types" />
  </xsl:variable>

  <!-- provide a denormalized domain for performance and to reduce
       algorithmic complexity-->
  <xsl:call-template name="preproc:mkdomain-union">
    <xsl:with-param name="name" select="@name" />
    <xsl:with-param name="subdomains" select="$subdomains" />
  </xsl:call-template>

  <xsl:copy-of select="$subdomains" />
</xsl:template>


<!--
  Enumerated typedefs
-->
<xsl:template match="
    lv:typedef[
      lv:enum
    ]
  "
  mode="preproc:mkdomain" priority="5">

  <lv:domain name="{@name}">
    <xsl:variable name="items" select="lv:enum/lv:item" />

    <!-- if a typedef is empty, then somebody probably made an oopsie or
         wrote a defective/deficient template -->
    <xsl:if test="count( $items ) = 0">
      <xsl:message>
        <xsl:text>warning: typedef `</xsl:text>
          <xsl:value-of select="@name" />
        <xsl:text>' is empty; something is probably wrong</xsl:text>
      </xsl:message>
    </xsl:if>

    <xsl:apply-templates mode="preproc:mkdomain"
      select="$items" />
  </lv:domain>
</xsl:template>


<!--
  Prohibit mixing explicit and auto-generated values

  For the time being at least.
-->
<xsl:template match="
  lv:typedef[
    lv:enum/lv:item[ @value ]
    and lv:enum/lv:item[ not( @value ) ]
  ]"
  mode="preproc:mkdomain" priority="2">

  <xsl:message terminate="yes">
    <xsl:text>error: typedef `</xsl:text>
      <xsl:value-of select="@name" />
    <xsl:text>' must not contain both @value and non-@value items</xsl:text>
  </xsl:message>
</xsl:template>


<!--
  Unsupported typedef

  Well, we know that it is a typedef, but its format is unknown. This
  wouldn't be surprising, since it is presently very limited.
-->
<xsl:template match="lv:typedef"
  mode="preproc:mkdomain" priority="2">

  <xsl:message terminate="yes">
    <xsl:text>error: malformed typedef `</xsl:text>
      <xsl:value-of select="@name" />
    <xsl:text>'</xsl:text>
  </xsl:message>
</xsl:template>



<!--
  Generate a denormalized domain consisting of the union of its subdomains'
  elements

  As this is a union, duplicate elements will be removed; the user will not
  be notified of this fact, as this allows domains to overlap in order to
  interpret the same data in different manners.
-->
<xsl:template name="preproc:mkdomain-union">
  <xsl:param name="name" />
  <xsl:param name="subdomains" />

  <xsl:variable name="union">
    <preproc:elements>
      <xsl:copy-of select="$subdomains/lv:domain/lv:element" />
    </preproc:elements>
  </xsl:variable>

  <!-- remove duplicate values (yes, this will take the first description if
       there are duplicates; whatever, for now) -->
  <lv:domain name="{@name}">
    <xsl:copy-of select="
      $union/preproc:elements/lv:element[
        not( @value = preceding-sibling::lv:element/@value )
      ]" />
  </lv:domain>
</xsl:template>


<!--
  Enumerated items without values require calculation

  Note the above validation that ensures that our value generation is
  sufficient.
-->
<xsl:template match="lv:enum/lv:item[ not( @value ) ]"
  mode="preproc:mkdomain" priority="5">

  <xsl:variable name="augmented">
    <xsl:copy>
      <xsl:attribute name="value" select="position()" />
    </xsl:copy>
  </xsl:variable>

  <!-- re-process using an augmented item with the value calculated -->
  <xsl:apply-templates mode="preproc:mkdomain"
    select="$augmented/lv:item" />
</xsl:template>


<!--
  Convert typedef item into a domain element

  This is a straightforward rename with sanity checking. Note that the
  element may have an empty description.

  We do not care about the name, since we use that to generate constants
  elsewhere.
-->
<xsl:template match="lv:item"
  mode="preproc:mkdomain" priority="4">

  <!-- previous templates should have prevented this, but just in case -->
  <xsl:if test="not( @value )">
    <xsl:message terminate="yes">
      <xsl:text>internal error: preproc:mkdomain on non-value item: </xsl:text>
      <xsl:copy-of select="." />
    </xsl:message>
  </xsl:if>

  <lv:element value="{@value}" desc="{@desc}" />
</xsl:template>


<!--
  Unexpected node; terminate
-->
<xsl:template match="*"
  mode="preproc:mkdomain" priority="1">

  <xsl:message terminate="yes">
    <xsl:text>internal error: unknown domain source: </xsl:text>
    <xsl:copy-of select="." />
  </xsl:message>
</xsl:template>

</xsl:stylesheet>