Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 8c54a4e8a1b726a6f69307eb307e15709e3ec30b (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
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Inline value parser

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

  Will parse all attributes and text of the form "a{b}c", where `b' is some
  variable.
-->
<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:template match="@*|text()" mode="lvm:valparse">
  <xsl:call-template name="lvm:valparse">
    <xsl:with-param name="str" select="." />
  </xsl:call-template>
</xsl:template>


<xsl:template name="lvm:valparse">
  <xsl:param name="str" />

  <xsl:variable name="pre" select="substring-before( $str, '{' )" />
  <xsl:variable name="post" select="substring-after( $str, '}' )" />

  <!-- get stuff between the two -->
  <xsl:variable name="postpre" select="substring-after( $str, '{' )" />
  <xsl:variable name="val" select="substring-before( $postpre, '}' )" />


  <xsl:if test="$pre">
    <xsl:text>'</xsl:text>
      <!-- TODO: escape -->
      <xsl:value-of select="$pre" />
    <xsl:text>' . </xsl:text>
  </xsl:if>

  <xsl:choose>
    <!-- variable reference -->
    <xsl:when test="$val">
      <xsl:call-template name="lvmp:gen-val">
        <xsl:with-param name="name" select="$val" />
      </xsl:call-template>
    </xsl:when>

    <!-- static value; no variable -->
    <xsl:otherwise>
      <xsl:text>'</xsl:text>
        <xsl:value-of select="$str" />
      <xsl:text>'</xsl:text>
    </xsl:otherwise>
  </xsl:choose>

  <xsl:if test="$post">
    <xsl:text> . '</xsl:text>
      <!-- TODO: escape -->
      <xsl:value-of select="$post" />
    <xsl:text>'</xsl:text>
  </xsl:if>
</xsl:template>


<!--
  Generate a variable reference for later rendering

  If a variable of the form x.y is found, it is recursively processed with `y'
  as the variable and `x' as the context: a dictionary lookup.
-->
<xsl:template name="lvmp:gen-val">
  <xsl:param name="name" />

  <xsl:variable name="trans" select="
      ancestor::lvm:c1-map/lvmp:translate[ @name=$name ]
    " />

  <xsl:choose>
      <!-- name translation requested -->
    <xsl:when test="$trans">
      <xsl:call-template name="lvmp:do-gen-val">
        <xsl:with-param name="name" select="$trans/@to" />
      </xsl:call-template>
    </xsl:when>

    <!-- no translation; use name as-is -->
    <xsl:otherwise>
      <xsl:call-template name="lvmp:do-gen-val">
        <xsl:with-param name="name" select="$name" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template name="lvmp:do-gen-val">
  <xsl:param name="name" />

  <!-- we may have a variable of the form `x.y', in which case we should
       process `y' withint he context of `x' -->
  <xsl:variable name="rightmost" select="
      substring-after( $name, '.' )
    " />

  <xsl:choose>
    <!-- no more key references -->
    <xsl:when test="$rightmost = ''">
      <lvmp:value ref="{$name}" />
    </xsl:when>

    <!-- recursively process key reference -->
    <xsl:otherwise>
      <!-- determine the key context, which is the entire base sans the
           rightmost variable name -->
      <xsl:variable name="context" select="
          substring( $name, 1, (
            string-length( $name ) - string-length( $rightmost ) - 1
          ) )
        " />

      <lvmp:value ref="{$rightmost}" index-key="{$context}">
        <xsl:call-template name="lvmp:gen-val">
          <xsl:with-param name="name" select="$context" />
        </xsl:call-template>
      </lvmp:value>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>