-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroll_out_into_sql.xsl
158 lines (151 loc) · 5.26 KB
/
roll_out_into_sql.xsl
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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="no" method="text" omit-xml-declaration="yes" media-type="text/plain"/>
<!--
roll out WURFL.xml into SQL
send comments, questions to roland guelle <[email protected]>
-->
<!--
##############################################################
parameters
##############################################################
-->
<!-- resolve wurfl fallbacks true/false -->
<xsl:param name="resolve_fallbacks">true</xsl:param>
<!-- file with capabilities that should be used -->
<xsl:param name="file_name">roll_out_capabilities.xml</xsl:param>
<!-- sql target table name -->
<xsl:param name="table_name">wurfl_devices</xsl:param>
<!-- add create statement -->
<xsl:param name="create_statement">true</xsl:param>
<xsl:variable name="caps" select="document($file_name)//cap"/>
<xsl:key name="device_id" match="device" use="@id"/>
<xsl:template match="/">
<xsl:apply-templates select="wurfl/devices"/>
</xsl:template>
<xsl:template match="wurfl/devices">
<xsl:if test="$create_statement = 'true'">
<xsl:text>CREATE TABLE `</xsl:text>
<xsl:value-of select="$table_name"/>
<xsl:text>` (</xsl:text>
<xsl:for-each select="$caps">
<xsl:text>`</xsl:text>
<xsl:value-of select="."/>
<xsl:text>` </xsl:text>
<xsl:choose>
<xsl:when test="@type">
<xsl:value-of select="@type"/>
</xsl:when>
<xsl:otherwise>varchar(255)</xsl:otherwise>
</xsl:choose>
<xsl:text> default NULL,</xsl:text>
</xsl:for-each>
<xsl:if test="$caps[text() = 'id']">
<xsl:text> PRIMARY KEY (`id`)</xsl:text>
</xsl:if>
<xsl:text>);</xsl:text>
</xsl:if>
<xsl:apply-templates select="device"/>
</xsl:template>
<xsl:template match="device">
<xsl:text>insert into </xsl:text>
<xsl:value-of select="$table_name"/>
<xsl:text>(</xsl:text>
<xsl:for-each select="$caps">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>) values (</xsl:text>
<xsl:variable name="device" select="."/>
<xsl:for-each select="$caps">
<xsl:choose>
<!-- select device attributes -->
<xsl:when test=". = 'id'">
<xsl:text>'</xsl:text>
<xsl:value-of select="$device/@id"/>
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:when test=". = 'fall_back'">
<xsl:text>'</xsl:text>
<xsl:value-of select="$device/@fall_back"/>
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:when test=". = 'actual_device_root'">
<xsl:choose>
<xsl:when test="$device[@actual_device_root = 'true']">
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>0</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test=". = 'user_agent'">
<xsl:text>'</xsl:text>
<xsl:value-of select="translate($device/@user_agent, "'", "\\'")"/>
<xsl:text>'</xsl:text>
</xsl:when>
<xsl:otherwise>
<!-- normal capabilities -->
<xsl:apply-templates select="$device" mode="CapOrNull">
<xsl:with-param name="cap" select="."/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>);</xsl:text>
</xsl:template>
<!-- change context from capability xml to device -->
<xsl:template match="device" mode="CapOrNull">
<xsl:param name="cap"/>
<xsl:call-template name="CapOrNull">
<xsl:with-param name="capability">
<xsl:value-of select="$cap"/>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<!-- change true/false into 1/0 -->
<xsl:template name="TorF">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="$value='true'">
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:when test="$value='false'">
<xsl:text>0</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>'</xsl:text>
<xsl:value-of select="$value"/>
<xsl:text>'</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="CapOrNull">
<xsl:param name="capability"/>
<xsl:param name="fall_back" select="@fall_back"/>
<xsl:choose>
<xsl:when test="group/capability[@name = $capability]">
<xsl:call-template name="TorF">
<xsl:with-param name="value" select="translate(group/capability[@name = $capability]/@value, "'", "\\'")"/>
</xsl:call-template>
</xsl:when>
<!-- check if resolving fallbacks is enabled, fall_back device is available and current id isn't the generic device -->
<xsl:when test="$resolve_fallbacks = 'true' and (key('device_id',$fall_back) and @id != 'generic')">
<xsl:for-each select="key('device_id',$fall_back)[1]">
<xsl:call-template name="CapOrNull">
<xsl:with-param name="capability" select="$capability"/>
<xsl:with-param name="fall_back" select="key('device_id',$fall_back)/@fall_back"/>
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<!-- this should never reatched, a capability that is not in fallback available -->
<xsl:otherwise>NULL</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>