Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 0e0030fe82097d78b5b306191cb38c8a766fd7e3 (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
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Utility templates
-->

<xsl:stylesheet version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

  xmlns:util="http://www.lovullo.com/util"
  xmlns:lv="http://www.lovullo.com/rater"
  xmlns:lvm="http://www.lovullo.com/rater/map"
  xmlns:c="http://www.lovullo.com/calc"
  xmlns:ext="http://www.lovullo.com/ext">


<xsl:variable name="_chrlower" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="_chrupper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<!--
  Path to map directory
-->
<xsl:param name="map-root" select="'../map/'" />
<xsl:param name="retmap-root" select="concat( $map-root, 'return/' )" />


<xsl:template name="util:load-package">
  <xsl:param name="package" />
  <xsl:param name="self" />

  <xsl:variable name="path" select="concat($package, '.xml')" />
  <xsl:variable name="pkg" select="document( $path, $self )/lv:package" />

  <ext:package name="{$pkg/@name}">
    <!-- path, including extension -->
    <xsl:attribute name="path">
      <xsl:value-of select="$path" />
    </xsl:attribute>

    <!-- path, excluding extension (as it appears in @package) -->
    <xsl:attribute name="import-path">
      <xsl:value-of select="@package" />
    </xsl:attribute>

    <xsl:copy-of select="$pkg" />
  </ext:package>
</xsl:template>


<xsl:template match="lvm:*" mode="util:map-expand" priority="1">
  <xsl:param name="path" select="'.'" />

  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates mode="util:map-expand">
      <xsl:with-param name="path" select="$path" />
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<!-- recursively inline imports -->
<xsl:template match="lvm:import" mode="util:map-expand" priority="5">
  <xsl:param name="path" select="'.'" />

  <xsl:apply-templates
    select="document( concat( @path, '.xml' ), . )/lvm:*/*"
    mode="util:map-expand">

    <xsl:with-param name="path" select="concat( $path, '/', @path )" />
  </xsl:apply-templates>
</xsl:template>

<!-- must be lower priority than lv:import -->
<xsl:template match="/lvm:*/lvm:*" mode="util:map-expand" priority="3">
  <xsl:param name="path" select="'.'" />

  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:attribute name="__src" select="$path" />

    <xsl:apply-templates mode="util:map-expand">
      <xsl:with-param name="path" select="$path" />
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>


<!--
  Converts first character to uppercase

  Functions like ucfirst in PHP

  @param string str string to ucfirst

  @return string provided string with first character converted to uppercase
-->
<xsl:template name="util:ucfirst">
  <xsl:param name="str" />

  <!-- convert first char to uppercase -->
  <xsl:value-of
    select="translate( substring( $str, 1, 1 ), $_chrlower, $_chrupper )" />

  <!-- remainder of string as it was provided -->
  <xsl:value-of select="substring( $str, 2 )" />
</xsl:template>


<!--
  Converts a string to uppercase
-->
<xsl:template name="util:uppercase">
  <xsl:param name="str" />
  <xsl:value-of select="translate( $str, $_chrlower, $_chrupper )" />
</xsl:template>

<!--
  Converts a string to lowercase
-->
<xsl:template name="util:lowercase">
  <xsl:param name="str" />
  <xsl:value-of select="translate( $str, $_chrupper, $_chrlower )" />
</xsl:template>


<xsl:template name="util:json">
  <xsl:param name="id" />
  <xsl:param name="value" />
  <xsl:param name="obj" />
  <xsl:param name="array" />

  <xsl:if test="$id">
    <xsl:text>"</xsl:text>
      <xsl:call-template name="util:json-escape">
        <xsl:with-param name="string" select="$id" />
      </xsl:call-template>
    <xsl:text>":</xsl:text>
  </xsl:if>

  <xsl:choose>
    <xsl:when test="$array">
      <xsl:text>[</xsl:text>
        <xsl:for-each select="$array/*">
          <xsl:if test="position() > 1">
            <xsl:text>,</xsl:text>
          </xsl:if>

          <xsl:value-of select="." />
        </xsl:for-each>
      <xsl:text>]</xsl:text>
    </xsl:when>

    <xsl:when test="$obj">
      <xsl:text>{</xsl:text>
        <xsl:for-each select="$obj/*">
          <xsl:if test="position() > 1">
            <xsl:text>,</xsl:text>
          </xsl:if>

          <xsl:value-of select="." />
        </xsl:for-each>
      <xsl:text>}</xsl:text>
    </xsl:when>

    <xsl:when test="$value">
      <xsl:text>"</xsl:text>
        <xsl:call-template name="util:json-escape">
          <xsl:with-param name="string" select="$value" />
        </xsl:call-template>
      <xsl:text>"</xsl:text>
    </xsl:when>

    <xsl:otherwise>
      <xsl:message terminate="yes">[util] !!! invalid util:json</xsl:message>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template name="util:json-escape">
  <xsl:param name="string" />

  <xsl:value-of select="$string" />
</xsl:template>

</xsl:stylesheet>