summaryrefslogtreecommitdiffstats
path: root/secret-service/tools/spec-to-introspect.xsl
blob: b2682b10971cb83e9be2a69551e26dae973a3500 (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
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"
        exclude-result-prefixes="tp">

<!--
    Telepathy D-Bus Introspection to EggDBus Introspection format translator.

    Copyright 2009  Michael Leupold <lemma@confuego.org>

    This program 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 2 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/>.
-->

<!--
    TODO:
     - Enable conversion of dictionary element types (eg. "{ss}") and
       struct types (eg. "(sayay)")
     - unhandled: tp:simple-type, tp:enum, tp:flags, tp:external-type
     - tp:docstring may contain XHTML which this template doesn't handle
-->

    <!-- main template -->
    <xsl:template match="tp:spec">
        <node>
            <xsl:apply-templates select="tp:errors"/>
            <xsl:apply-templates select="tp:struct"/>
            <!-- TODO: <xsl:apply-templates select="tp:mapping"/> -->
            <xsl:apply-templates select="node/interface"/>
        </node>
    </xsl:template>

    <!-- Resolve the type a node has. This will first look at tp:type and
         - if not found - use the type attribute -->
    <xsl:template name="ResolveType">
        <xsl:param name="node"/>
        <xsl:choose>
            <xsl:when test="$node//@tp:type">
                <xsl:call-template name="TpType">
                    <xsl:with-param name="type" select="$node//@tp:type"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="$node//@type">
                <xsl:call-template name="DBusType">
                    <xsl:with-param name="type" select="$node//@type"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:message terminate="yes">
                    Node doesn't contain a type.
                </xsl:message>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Map a D-Bus type to its EggDBus counterpart -->
    <xsl:template name="DBusType">
        <xsl:param name="type"/>
        <xsl:choose>
            <xsl:when test="$type='o'">ObjectPath</xsl:when>
            <xsl:when test="$type='s'">String</xsl:when>
            <xsl:when test="$type='y'">Byte</xsl:when>
            <xsl:when test="$type='b'">Boolean</xsl:when>
            <xsl:when test="$type='n'">Int16</xsl:when>
            <xsl:when test="$type='q'">UInt16</xsl:when>
            <xsl:when test="$type='i'">Int32</xsl:when>
            <xsl:when test="$type='u'">UInt32</xsl:when>
            <xsl:when test="$type='x'">Int64</xsl:when>
            <xsl:when test="$type='t'">UInt64</xsl:when>
            <xsl:when test="$type='d'">Double</xsl:when>
            <xsl:when test="$type='g'">Signature</xsl:when>
            <xsl:when test="starts-with($type, 'a')">
                Array&lt;
                <xsl:call-template name="DBusType">
                    <xsl:with-param name="type" select="substring($type, 2)"/>
                </xsl:call-template>
                &gt;
            </xsl:when>
            <!-- TODO: doesn't implement dict-entries and structs -->
            <xsl:otherwise>
                <xsl:message terminate="yes">
                    Unknown DBus Type <xsl:value-of select="$type"/>
                </xsl:message>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Resolve tp:type attributes by searching for matching tp:struct
         and tp:mapping elements -->
    <xsl:template name="TpType">
        <xsl:param name="type"/>
        <xsl:choose>
            <xsl:when test="/tp:spec/tp:struct[@name=$type]">
                <xsl:value-of select="$type"/>
            </xsl:when>
            <xsl:when test="/tp:spec/tp:mapping[@name=$type]">
                Dict&lt;
                <xsl:call-template name="ResolveType">
                    <xsl:with-param name="node" select="/tp:spec/tp:mapping[@name=$type]/tp:member[@name='Key']"/>
                </xsl:call-template>,
                <xsl:call-template name="ResolveType">
                    <xsl:with-param name="node" select="/tp:spec/tp:mapping[@name=$type]/tp:member[@name='Value']"/>
                </xsl:call-template>
                &gt;
            </xsl:when>
            <xsl:otherwise>
                <xsl:message terminate="yes">
                    Unspecified type <xsl:value-of select="$type"/>.
                </xsl:message>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- handle most of the D-Bus introspection elements -->
    <xsl:template match="interface|annotation|method|signal">
        <xsl:copy>
            <xsl:for-each select="@*">
                <xsl:if test="not(starts-with(name(), 'tp:'))">
                    <xsl:copy/>
                </xsl:if>
            </xsl:for-each>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <!-- handle the arg and property D-Bus introspection elements.
         They get special handling because they may contain a tp:type
         attribute -->
    <xsl:template match="arg|property">
        <xsl:copy>
            <xsl:for-each select="@*">
                <xsl:choose>
                    <xsl:when test="not(starts-with(name(), 'tp:'))">
                        <xsl:copy/>
                    </xsl:when>
                    <xsl:when test="name() = 'tp:type'">
                        <xsl:variable name="type">
                            <xsl:call-template name="TpType">
                                <xsl:with-param name="type" select="."/>
                            </xsl:call-template>
                        </xsl:variable>
                        <annotation name="org.gtk.EggDBus.Type">
                            <xsl:attribute name="value">
                                <xsl:value-of select="normalize-space($type)"/>
                            </xsl:attribute>
                        </annotation>
                    </xsl:when>
                    <xsl:otherwise/>
                </xsl:choose>
            </xsl:for-each>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <!-- tp:docstring to org.gtk.EggDBus.DocString -->
    <xsl:template match="tp:docstring">
        <annotation name="org.gtk.EggDBus.DocString">
            <xsl:attribute name="value">
                <xsl:value-of select="normalize-space(text())"/>
            </xsl:attribute>
        </annotation>
    </xsl:template>

    <!-- tp:errors to org.gtk.EggDBus.DeclareErrorDomain -->
    <xsl:template match="tp:errors">
        <annotation value="Error" name="org.gtk.EggDBus.DeclareErrorDomain">
            <xsl:apply-templates select="tp:docstring"/>
            <xsl:apply-templates select="tp:error"/>
        </annotation>
    </xsl:template>

    <!-- tp:error to org.gtk.EggDBus.ErrorDomain.Member -->
    <xsl:template match="tp:error">
        <annotation name="org.gtk.EggDBus.ErrorDomain.Member">
            <xsl:attribute name="value">
                <xsl:value-of select="concat(../@namespace, '.', @name)"/>
            </xsl:attribute>
            <xsl:apply-templates select="tp:docstring"/>
        </annotation>
    </xsl:template>

    <!-- tp:struct to org.gtk.EggDBus.DeclareStruct -->
    <xsl:template match="tp:struct">
        <annotation name="org.gtk.EggDBus.DeclareStruct">
            <xsl:attribute name="value">
                <xsl:value-of select="@name"/>
            </xsl:attribute>
            <xsl:apply-templates select="tp:docstring"/>
            <xsl:apply-templates select="tp:member"/>
        </annotation>
    </xsl:template>

    <!-- tp:member to org.gtk.EggDBus.Struct.Member -->
    <xsl:template match="tp:member">
        <xsl:variable name="type">
            <xsl:call-template name="ResolveType">
                <xsl:with-param name="node" select="."/>
            </xsl:call-template>
        </xsl:variable>
        <annotation name="org.gtk.EggDBus.Struct.Member">
            <xsl:attribute name="value">
                <xsl:value-of select="concat(normalize-space($type), ':', @name)"/>
            </xsl:attribute>
            <xsl:apply-templates select="tp:docstring"/>
        </annotation>
    </xsl:template>

    <xsl:template match="text()"/>

    <xsl:output method="xml" indent="yes" encoding="UTF-8"
                omit-xml-declaration="no"
                doctype-system="http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"
                doctype-public="-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"/>

</xsl:stylesheet>