2009-06-11 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / tools / c-constants-generator.xsl
1 <!-- Stylesheet to extract C enumerations from the AT-SPI spec.
2
3 Copyright (C) 2006, 2007 Collabora Limited
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 -->
19
20 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
21   xmlns:tp="http://telepathy.freedesktop.org/wiki/DbusSpec#extensions-v0"
22   xmlns:str="http://exslt.org/strings"
23   exclude-result-prefixes="str tp">
24
25   <xsl:output method="text" indent="no" encoding="ascii"/>
26
27   <xsl:param name="mixed-case-prefix" select="''"/>
28
29   <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
30   <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
31
32   <xsl:variable name="lower-case-prefix" select="translate($mixed-case-prefix, $upper, $lower)"/>
33
34
35   <xsl:template match="tp:flags">
36     <xsl:variable name="name">
37       <xsl:choose>
38         <xsl:when test="@plural">
39           <xsl:value-of select="@plural"/>
40         </xsl:when>
41         <xsl:otherwise>
42           <xsl:value-of select="@name"/>
43         </xsl:otherwise>
44       </xsl:choose>
45     </xsl:variable>
46     <xsl:variable name="value-prefix">
47       <xsl:choose>
48         <xsl:when test="@singular">
49           <xsl:value-of select="@singular"/>
50         </xsl:when>
51         <xsl:when test="@value-prefix">
52           <xsl:value-of select="@value-prefix"/>
53         </xsl:when>
54         <xsl:otherwise>
55           <xsl:value-of select="@name"/>
56         </xsl:otherwise>
57       </xsl:choose>
58     </xsl:variable>
59     <xsl:text>/**&#10;</xsl:text>
60     <xsl:text> *&#10;</xsl:text>
61     <xsl:value-of select="concat($mixed-case-prefix, $name)"/>
62     <xsl:text>:&#10;</xsl:text>
63     <xsl:apply-templates mode="flag-or-enumvalue-gtkdoc">
64       <xsl:with-param name="value-prefix" select="$value-prefix"/>
65     </xsl:apply-templates>
66     <xsl:text> *&#10;</xsl:text>
67     <xsl:if test="tp:docstring">
68       <xsl:text> * &lt;![CDATA[</xsl:text>
69       <xsl:value-of select="translate(string (tp:docstring), '&#13;&#10;', '  ')"/>
70       <xsl:text>]]&gt;&#10;</xsl:text>
71       <xsl:text> *&#10;</xsl:text>
72     </xsl:if>
73     <xsl:text> * Bitfield/set of flags generated from the AT-SPI specification.&#10;</xsl:text>
74     <xsl:text> */&#10;</xsl:text>
75     <xsl:text>typedef enum {&#10;</xsl:text>
76     <xsl:apply-templates>
77       <xsl:with-param name="value-prefix" select="$value-prefix"/>
78     </xsl:apply-templates>
79     <xsl:text>} </xsl:text>
80     <xsl:value-of select="concat($mixed-case-prefix, $name)"/>
81     <xsl:text>;&#10;</xsl:text>
82     <xsl:text>&#10;</xsl:text>
83   </xsl:template>
84
85   <xsl:template match="text()" mode="flag-or-enumvalue-gtkdoc"/>
86
87   <xsl:template match="tp:enumvalue" mode="flag-or-enumvalue-gtkdoc">
88     <xsl:param name="value-prefix"/>
89     <xsl:variable name="suffix">
90       <xsl:choose>
91         <xsl:when test="ancestor::interface">
92           <xsl:value-of select="concat(str:replace(ancestor::interface/@name, 'org.freedesktop.atspi.', ''), '_', @suffix)"/>
93         </xsl:when>
94         <xsl:otherwise>
95           <xsl:value-of select="@suffix"/>
96         </xsl:otherwise>
97       </xsl:choose>
98     </xsl:variable>
99     <xsl:text> * @</xsl:text>
100     <xsl:value-of select="concat($mixed-case-prefix, $suffix)"/>
101     <xsl:text>: &lt;![CDATA[</xsl:text>
102     <xsl:value-of select="translate(string(tp:docstring), '&#13;&#10;', '  ')"/>
103     <xsl:text>]]&gt;&#10;</xsl:text>
104   </xsl:template>
105
106   <xsl:template match="tp:flag" mode="flag-or-enumvalue-gtkdoc">
107     <xsl:param name="value-prefix"/>
108     <xsl:text> * @</xsl:text>
109     <xsl:value-of select="translate(concat($mixed-case-prefix, $value-prefix, '_', @suffix), $lower, $upper)"/>
110     <xsl:text>: &lt;![CDATA[</xsl:text>
111     <xsl:value-of select="translate(string(tp:docstring), '&#13;&#10;', '  ')"/>
112     <xsl:text>]]&gt;&#10;</xsl:text>
113   </xsl:template>
114
115   <xsl:template match="tp:enum">
116     <xsl:variable name="name">
117       <xsl:choose>
118         <xsl:when test="ancestor::interface">
119           <xsl:value-of select="concat(str:replace(ancestor::interface/@name, 'org.freedesktop.atspi.', ''), '_', @name)"/>
120         </xsl:when>
121         <xsl:otherwise>
122           <xsl:value-of select="@name"/>
123         </xsl:otherwise>
124       </xsl:choose>
125     </xsl:variable>
126     <xsl:variable name="value-prefix">
127       <xsl:choose>
128         <xsl:when test="@singular">
129           <xsl:value-of select="@singular"/>
130         </xsl:when>
131         <xsl:when test="@value-prefix">
132           <xsl:value-of select="@value-prefix"/>
133         </xsl:when>
134         <xsl:otherwise>
135           <xsl:value-of select="@name"/>
136         </xsl:otherwise>
137       </xsl:choose>
138     </xsl:variable>
139     <xsl:variable name="name-plural">
140       <xsl:choose>
141         <xsl:when test="@plural">
142           <xsl:value-of select="@plural"/>
143         </xsl:when>
144         <xsl:otherwise>
145           <xsl:value-of select="@name"/><xsl:text>s</xsl:text>
146         </xsl:otherwise>
147       </xsl:choose>
148     </xsl:variable>
149     <xsl:text>/**&#10;</xsl:text>
150     <xsl:text> *&#10;</xsl:text>
151     <xsl:value-of select="concat($mixed-case-prefix, $name)"/>
152     <xsl:text>:&#10;</xsl:text>
153     <xsl:apply-templates mode="flag-or-enumvalue-gtkdoc">
154       <xsl:with-param name="value-prefix" select="$value-prefix"/>
155     </xsl:apply-templates>
156     <xsl:text> *&#10;</xsl:text>
157     <xsl:if test="tp:docstring">
158       <xsl:text> * &lt;![CDATA[</xsl:text>
159       <xsl:value-of select="translate(string (tp:docstring), '&#13;&#10;', '  ')"/>
160       <xsl:text>]]&gt;&#10;</xsl:text>
161       <xsl:text> *&#10;</xsl:text>
162     </xsl:if>
163     <xsl:text> * Bitfield/set of flags generated from the AT-SPI specification.&#10;</xsl:text>
164     <xsl:text> */&#10;</xsl:text>
165     <xsl:text>typedef enum {&#10;</xsl:text>
166     <xsl:apply-templates>
167       <xsl:with-param name="value-prefix" select="$value-prefix"/>
168     </xsl:apply-templates>
169     <xsl:text>} </xsl:text>
170     <xsl:value-of select="concat($mixed-case-prefix, $name)"/>
171     <xsl:text>;&#10;</xsl:text>
172     <xsl:text>&#10;</xsl:text>
173     <xsl:text>/**&#10;</xsl:text>
174     <xsl:text> * NUM_</xsl:text>
175     <xsl:value-of select="translate(concat($mixed-case-prefix, $name-plural), $lower, $upper)"/>
176     <xsl:text>:&#10;</xsl:text>
177     <xsl:text> *&#10;</xsl:text>
178     <xsl:text> * 1 higher than the highest valid value of #</xsl:text>
179     <xsl:value-of select="concat($mixed-case-prefix, $name)"/>
180     <xsl:text>.&#10;</xsl:text>
181     <xsl:text> */&#10;</xsl:text>
182     <xsl:text>#define NUM_</xsl:text>
183     <xsl:value-of select="translate(concat($mixed-case-prefix, $name-plural), $lower, $upper)"/>
184     <xsl:text> (</xsl:text>
185     <xsl:value-of select="tp:enumvalue[position() = last()]/@value"/>
186     <xsl:text>+1)&#10;</xsl:text>
187     <xsl:text>&#10;</xsl:text>
188   </xsl:template>
189
190   <xsl:template match="tp:flags/tp:flag">
191     <xsl:param name="value-prefix"/>
192     <xsl:variable name="suffix">
193       <xsl:choose>
194         <xsl:when test="@suffix">
195           <xsl:value-of select="@suffix"/>
196         </xsl:when>
197         <xsl:otherwise>
198           <xsl:value-of select="@name"/>
199         </xsl:otherwise>
200       </xsl:choose>
201     </xsl:variable>
202     <xsl:variable name="name" select="translate(concat($mixed-case-prefix, $value-prefix, '_', $suffix), $lower, $upper)"/>
203
204     <xsl:if test="@name and @suffix and @name != @suffix">
205       <xsl:message terminate="yes">
206         <xsl:text>Flag name </xsl:text>
207         <xsl:value-of select="@name"/>
208         <xsl:text> != suffix </xsl:text>
209         <xsl:value-of select="@suffix"/>
210         <xsl:text>&#10;</xsl:text>
211       </xsl:message>
212     </xsl:if>
213     <xsl:text>    </xsl:text>
214     <xsl:value-of select="$name"/>
215     <xsl:text> = </xsl:text>
216     <xsl:value-of select="@value"/>
217     <xsl:text>,&#10;</xsl:text>
218   </xsl:template>
219
220   <xsl:template match="tp:enum/tp:enumvalue">
221     <xsl:param name="value-prefix"/>
222     <xsl:variable name="suffix">
223       <xsl:choose>
224         <xsl:when test="ancestor::interface">
225           <xsl:value-of select="concat(str:replace(ancestor::interface/@name, 'org.freedesktop.atspi.', ''), '_', @suffix)"/>
226         </xsl:when>
227         <xsl:otherwise>
228           <xsl:value-of select="@suffix"/>
229         </xsl:otherwise>
230       </xsl:choose>
231     </xsl:variable>
232     <xsl:variable name="name" select="concat($mixed-case-prefix, $suffix)"/>
233
234     <xsl:if test="@name and @suffix and @name != @suffix">
235       <xsl:message terminate="yes">
236         <xsl:text>Enumvalue name </xsl:text>
237         <xsl:value-of select="@name"/>
238         <xsl:text> != suffix </xsl:text>
239         <xsl:value-of select="@suffix"/>
240         <xsl:text>&#10;</xsl:text>
241       </xsl:message>
242     </xsl:if>
243
244     <xsl:if test="preceding-sibling::tp:enumvalue and number(preceding-sibling::tp:enumvalue[1]/@value) > number(@value)">
245       <xsl:message terminate="yes">
246         <xsl:text>Enum values must be in ascending numeric order, but </xsl:text>
247         <xsl:value-of select="$name"/>
248         <xsl:text> is less than the previous value</xsl:text>
249       </xsl:message>
250     </xsl:if>
251
252     <xsl:text>    </xsl:text>
253     <xsl:value-of select="$name"/>
254     <xsl:text>,&#10;</xsl:text>
255   </xsl:template>
256
257   <xsl:template match="tp:flag">
258     <xsl:message terminate="yes">tp:flag found outside tp:flags&#10;</xsl:message>
259   </xsl:template>
260
261   <xsl:template match="tp:enumvalue">
262     <xsl:message terminate="yes">tp:enumvalue found outside tp:enum&#10;</xsl:message>
263   </xsl:template>
264
265   <xsl:template match="text()"/>
266
267   <xsl:template match="/tp:spec">
268     <xsl:if test="$mixed-case-prefix = ''">
269       <xsl:message terminate="yes">
270         <xsl:text>mixed-case-prefix param must be set&#10;</xsl:text>
271       </xsl:message>
272     </xsl:if>
273
274     <xsl:text>/* Generated from </xsl:text>
275     <xsl:value-of select="tp:title"/>
276     <xsl:if test="tp:version">
277       <xsl:text>, version </xsl:text>
278       <xsl:value-of select="tp:version"/>
279     </xsl:if>
280     <xsl:text>&#10;</xsl:text>
281     <xsl:text>&#10;</xsl:text>
282     <xsl:for-each select="tp:copyright">
283       <xsl:value-of select="."/>
284       <xsl:text>&#10;</xsl:text>
285     </xsl:for-each>
286     <xsl:value-of select="tp:license"/>
287     <xsl:text>&#10;</xsl:text>
288     <xsl:value-of select="tp:docstring"/>
289     <xsl:text>&#10;</xsl:text>
290     <xsl:text> */&#10;</xsl:text>
291     <xsl:text>&#10;</xsl:text>
292     <xsl:text>#ifndef _ACCESSIBILITY_H_&#10;</xsl:text>
293     <xsl:text>#define _ACCESSIBILITY_H_&#10;</xsl:text>
294     <xsl:text>#ifdef __cplusplus&#10;</xsl:text>
295     <xsl:text>extern "C" {&#10;</xsl:text>
296     <xsl:text>#endif&#10;</xsl:text>
297     <xsl:text>&#10;</xsl:text>
298     <xsl:apply-templates/>
299     <xsl:text>&#10;</xsl:text>
300     <xsl:text>#ifdef __cplusplus&#10;</xsl:text>
301     <xsl:text>}&#10;</xsl:text>
302     <xsl:text>#endif&#10;</xsl:text>
303     <xsl:text>#endif&#10;</xsl:text>
304   </xsl:template>
305
306 </xsl:stylesheet>
307
308 <!-- vim:set sw=2 sts=2 et noai noci: -->