~psmay/+junk/passe

« back to all changes in this revision

Viewing changes to fcgi-data/fcgi-data-java.xsl

  • Committer: Peter S. May
  • Date: 2013-04-25 01:57:46 UTC
  • Revision ID: peter_s._may_httppsmay.com-20130425015746-15if4x15pe21rpm7
Generated Protocol class, with supporting materials.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="utf-8" ?>
 
2
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 
3
        <xsl:output method="text" encoding="UTF-8"/>
 
4
        <xsl:template match="/">/*
 
5
 * Copyright (c) 2013 Peter S. May
 
6
 *
 
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 
8
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 
9
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 
10
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 
11
 * furnished to do so, subject to the following conditions:
 
12
 *
 
13
 * The above copyright notice and this permission notice shall be included in all copies or
 
14
 * substantial portions of the Software.
 
15
 *
 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 
17
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 
19
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
21
 */
 
22
 
 
23
////////////////////////////////////////////////////////////
 
24
// THIS JAVA SOURCE IS GENERATED. AVOID EDITING DIRECTLY. //
 
25
////////////////////////////////////////////////////////////
 
26
 
 
27
package us.wxy.passe.fcgi;
 
28
 
 
29
import java.io.IOException;
 
30
import java.nio.BufferOverflowException;
 
31
import java.nio.BufferUnderflowException;
 
32
import java.nio.ByteBuffer;
 
33
import java.util.HashMap;
 
34
 
 
35
// Selected structures, enums, and values from the FastCGI spec. The contents
 
36
// may be non-exhaustive in places where it makes more sense to manually
 
37
// implement than to attempt to automate. Most class-level elements are
 
38
// package-scoped and not intended to face outside code.
 
39
//
 
40
// These classes provide static readFrom() and writeTo() methods. The read
 
41
// methods on the enums throw an exception if the value read in is not an
 
42
// available value; the exception contains the attempted value.
 
43
//
 
44
// The Type enum has additional utility flags indicating the direction
 
45
// (isFromServer), target level (isManagement), and streamingness (isStreaming)
 
46
// of the type.
 
47
final class Protocol {
 
48
        private Protocol() {}
 
49
 
 
50
        <xsl:apply-templates />
 
51
}
 
52
</xsl:template>
 
53
 
 
54
        <xsl:template name="type-a">
 
55
                <xsl:param name="field"/>
 
56
                <xsl:for-each select="$field">
 
57
                <xsl:choose>
 
58
                        <xsl:when test="@type"><xsl:value-of select="@type"/></xsl:when>
 
59
                        <xsl:when test="@unsigned and @size &lt; 4">int</xsl:when>
 
60
                        <xsl:when test="@size = 1">byte</xsl:when>
 
61
                        <xsl:when test="@size = 2">short</xsl:when>
 
62
                        <xsl:when test="@size = 4">int</xsl:when>
 
63
                        <xsl:otherwise>[unknown type for size]</xsl:otherwise>
 
64
                </xsl:choose>
 
65
                </xsl:for-each>
 
66
        </xsl:template>
 
67
        <xsl:template name="type-o">
 
68
                <xsl:param name="field"/>
 
69
                <xsl:for-each select="$field">
 
70
                <xsl:choose>
 
71
                        <xsl:when test="@type"><xsl:value-of select="@type"/></xsl:when>
 
72
                        <xsl:when test="@size = 1">Byte</xsl:when>
 
73
                        <xsl:when test="@size = 2">Short</xsl:when>
 
74
                        <xsl:when test="@size = 4">Integer</xsl:when>
 
75
                        <xsl:otherwise>[unknown type for size]</xsl:otherwise>
 
76
                </xsl:choose>
 
77
                </xsl:for-each>
 
78
        </xsl:template>
 
79
        <xsl:template name="size-a">
 
80
                <xsl:param name="field"/>
 
81
                <xsl:for-each select="$field">
 
82
                <xsl:choose>
 
83
                        <xsl:when test="@type"><xsl:value-of select="@type"/>.STREAMED_SIZE</xsl:when>
 
84
                        <xsl:otherwise><xsl:value-of select="@size"/></xsl:otherwise>
 
85
                </xsl:choose>
 
86
                </xsl:for-each>
 
87
        </xsl:template>
 
88
        <xsl:template name="type-b">
 
89
                <xsl:param name="field"/>
 
90
                <xsl:for-each select="$field">
 
91
                <xsl:choose>
 
92
                        <xsl:when test="@size = 1"><!-- Byte --></xsl:when>
 
93
                        <xsl:when test="@size = 2">Short</xsl:when>
 
94
                        <xsl:when test="@size = 4">Int</xsl:when>
 
95
                        <xsl:otherwise>[unknown type for size]</xsl:otherwise>
 
96
                </xsl:choose>
 
97
                </xsl:for-each>
 
98
        </xsl:template>
 
99
        <xsl:template name="unsign-b">
 
100
                <xsl:param name="field"/>
 
101
                <xsl:for-each select="$field">
 
102
                <xsl:choose>
 
103
                        <xsl:when test="@size = 1">0xFF</xsl:when>
 
104
                        <xsl:when test="@size = 2">0xFFFF</xsl:when>
 
105
                        <xsl:otherwise>[unknown unsign mask for size]</xsl:otherwise>
 
106
                </xsl:choose>
 
107
                </xsl:for-each>
 
108
        </xsl:template>
 
109
        <xsl:template name="resign-b">
 
110
                <xsl:param name="field"/>
 
111
                <xsl:for-each select="$field">
 
112
                <xsl:choose>
 
113
                        <xsl:when test="@type"><xsl:value-of select="@type"/></xsl:when>
 
114
                        <xsl:when test="@size = 1">byte</xsl:when>
 
115
                        <xsl:when test="@size = 2">short</xsl:when>
 
116
                        <xsl:when test="@size = 4">int</xsl:when>
 
117
                        <xsl:otherwise>[unknown type for size]</xsl:otherwise>
 
118
                </xsl:choose>
 
119
                </xsl:for-each>
 
120
        </xsl:template>
 
121
        <xsl:template name="z-string">
 
122
                <xsl:param name="count"/>
 
123
                <xsl:text>"</xsl:text>
 
124
                <xsl:call-template name="_z-string">
 
125
                        <xsl:with-param name="count" select="$count"/>
 
126
                </xsl:call-template>
 
127
                <xsl:text>"</xsl:text>
 
128
        </xsl:template>
 
129
        <xsl:template name="_z-string">
 
130
                <xsl:param name="count"/>
 
131
                <xsl:if test="$count &gt; 0">
 
132
                        <xsl:text>\0</xsl:text>
 
133
                        <xsl:call-template name="_z-string">
 
134
                                <xsl:with-param name="count" select="$count - 1"/>
 
135
                        </xsl:call-template>
 
136
                </xsl:if>
 
137
        </xsl:template>
 
138
 
 
139
        <xsl:template match="struct">
 
140
        static class <xsl:value-of select="@name"/> {
 
141
                <xsl:for-each select="field[@name]">
 
142
                <xsl:text>final </xsl:text>
 
143
                <xsl:call-template name="type-a">
 
144
                        <xsl:with-param name="field" select="."/>
 
145
                </xsl:call-template>
 
146
                <xsl:value-of select="' '"/>
 
147
                <xsl:value-of select="@name" />
 
148
                <xsl:text>;
 
149
                </xsl:text>
 
150
                </xsl:for-each>
 
151
 
 
152
                <xsl:text>
 
153
                </xsl:text>
 
154
 
 
155
                public static final int STREAMED_SIZE = <xsl:for-each select="field">
 
156
                        <xsl:if test="position() != 1"> + </xsl:if>
 
157
                        <xsl:call-template name="size-a">
 
158
                                <xsl:with-param name="field" select="."/>
 
159
                        </xsl:call-template>
 
160
                </xsl:for-each>;
 
161
 
 
162
                <xsl:value-of select="@name"/><xsl:text>(</xsl:text>
 
163
                <xsl:for-each select="field[@name]">
 
164
                <xsl:if test="position() != 1">, </xsl:if>
 
165
                <xsl:call-template name="type-a">
 
166
                        <xsl:with-param name="field" select="."/>
 
167
                </xsl:call-template>
 
168
                <xsl:value-of select="' '"/>
 
169
                <xsl:value-of select="@name" />
 
170
                </xsl:for-each>
 
171
                <xsl:text>) {</xsl:text>
 
172
                <xsl:for-each select="field[@name]">
 
173
                <xsl:text>
 
174
                        this.</xsl:text>
 
175
                <xsl:value-of select="@name" />
 
176
                <xsl:text> = </xsl:text>
 
177
                <xsl:value-of select="@name" />
 
178
                <xsl:text>;</xsl:text>
 
179
                </xsl:for-each>
 
180
                <xsl:text>
 
181
                }</xsl:text>
 
182
 
 
183
                public static <xsl:value-of select="@name"/> readFrom(ByteBuffer in) throws IOException {
 
184
                        if(in.remaining() &lt; STREAMED_SIZE)
 
185
                                throw new BufferUnderflowException();
 
186
 
 
187
                        <xsl:for-each select="field"><xsl:text/>
 
188
                                <xsl:choose>
 
189
                                        <xsl:when test="@name">
 
190
                                                <xsl:call-template name="type-a">
 
191
                                                        <xsl:with-param name="field" select="."/>
 
192
                                                </xsl:call-template>
 
193
                                                <xsl:value-of select="concat(' ', @name, ' = ')" />
 
194
                                                <xsl:choose>
 
195
                                                        <xsl:when test="@transfer-type">
 
196
                                                                <xsl:value-of select="concat(@transfer-type, '.readFrom(in)')"/>
 
197
                                                        </xsl:when>
 
198
                                                        <xsl:when test="@type">
 
199
                                                                <xsl:value-of select="concat(@type, '.readFrom(in)')"/>
 
200
                                                        </xsl:when>
 
201
                                                        <xsl:when test="@unsigned">
 
202
                                                                <xsl:call-template name="unsign-b">
 
203
                                                                        <xsl:with-param name="field" select="."/>
 
204
                                                                </xsl:call-template>
 
205
                                                                <xsl:text> &amp; in.get</xsl:text>
 
206
                                                                <xsl:call-template name="type-b">
 
207
                                                                        <xsl:with-param name="field" select="."/>
 
208
                                                                </xsl:call-template>
 
209
                                                                <xsl:text>()</xsl:text>
 
210
                                                        </xsl:when>
 
211
                                                        <xsl:otherwise>
 
212
                                                                <xsl:text>in.get</xsl:text>
 
213
                                                                <xsl:call-template name="type-b">
 
214
                                                                        <xsl:with-param name="field" select="."/>
 
215
                                                                </xsl:call-template>
 
216
                                                                <xsl:text>()</xsl:text>
 
217
                                                        </xsl:otherwise>
 
218
                                                </xsl:choose>
 
219
                                                <xsl:text>;
 
220
                        </xsl:text>
 
221
                                        </xsl:when>
 
222
                                        <xsl:otherwise>
 
223
                        in.position(in.position() + <xsl:value-of select="@size"/>);
 
224
                                        </xsl:otherwise>
 
225
                                </xsl:choose>
 
226
                        </xsl:for-each>
 
227
 
 
228
                        <xsl:text>
 
229
                        return new </xsl:text>
 
230
                        <xsl:value-of select="@name"/><xsl:text>(</xsl:text>
 
231
                        <xsl:for-each select="field[@name]">
 
232
                        <xsl:if test="position() != 1">, </xsl:if>
 
233
                        <xsl:value-of select="@name" />
 
234
                        </xsl:for-each>
 
235
                        <xsl:text>);</xsl:text>
 
236
                }
 
237
 
 
238
                public static void writeTo(ByteBuffer out, <xsl:value-of select="@name"/> data) throws IOException {
 
239
                        <xsl:for-each select="field"><xsl:text/>
 
240
                                <xsl:choose>
 
241
                                        <xsl:when test="@name">
 
242
                                                <xsl:choose>
 
243
                                                        <xsl:when test="@transfer-type">
 
244
                                                                <xsl:value-of select="concat(@transfer-type, '.writeTo(out, data.' , @name, ')')"/>
 
245
                                                        </xsl:when>
 
246
                                                        <xsl:when test="@type">
 
247
                                                                <xsl:value-of select="concat(@type, '.writeTo(out, data.' , @name, ')')"/>
 
248
                                                        </xsl:when>
 
249
                                                        <xsl:otherwise>
 
250
                                                                <xsl:text>out.put</xsl:text>
 
251
                                                                <xsl:call-template name="type-b">
 
252
                                                                        <xsl:with-param name="field" select="."/>
 
253
                                                                </xsl:call-template>
 
254
                                                                <xsl:text>(</xsl:text>
 
255
                                                                <xsl:if test="@unsigned">
 
256
                                                                        <xsl:text>(</xsl:text>
 
257
                                                                                <xsl:call-template name="resign-b">
 
258
                                                                                        <xsl:with-param name="field" select="."/>
 
259
                                                                                </xsl:call-template>
 
260
                                                                        <xsl:text>)</xsl:text>
 
261
                                                                </xsl:if>
 
262
                                                                <xsl:text>data.</xsl:text>
 
263
                                                                <xsl:value-of select="@name"/>
 
264
                                                                <xsl:text>)</xsl:text>
 
265
                                                        </xsl:otherwise>
 
266
                                                </xsl:choose>
 
267
                                                <xsl:text>;
 
268
                        </xsl:text>
 
269
                                        </xsl:when>
 
270
                                        <xsl:otherwise>
 
271
                        out.position(out.position() + <xsl:value-of select="@size"/>);
 
272
                                        </xsl:otherwise>
 
273
                                </xsl:choose>
 
274
                        </xsl:for-each>
 
275
                }
 
276
        }
 
277
        </xsl:template>
 
278
 
 
279
        <xsl:template match="enum">
 
280
        <xsl:variable name="vt">
 
281
                <xsl:call-template name="type-a">
 
282
                        <xsl:with-param name="field" select="."/>
 
283
                </xsl:call-template>
 
284
        </xsl:variable>
 
285
        <xsl:variable name="ovt">
 
286
                <xsl:call-template name="type-o">
 
287
                        <xsl:with-param name="field" select="."/>
 
288
                </xsl:call-template>
 
289
        </xsl:variable>
 
290
        <xsl:variable name="xt">
 
291
                <xsl:call-template name="type-b">
 
292
                        <xsl:with-param name="field" select="."/>
 
293
                </xsl:call-template>
 
294
        </xsl:variable>
 
295
 
 
296
        enum <xsl:value-of select="@name"/> {
 
297
                <xsl:variable name="ep" select="param"/>
 
298
                <xsl:for-each select="define">
 
299
                        <xsl:variable name="def" select="."/>
 
300
                        <xsl:value-of select="@name"/>((<xsl:value-of select="$vt"/>)<xsl:value-of select="."/>
 
301
                        <xsl:for-each select="$ep">
 
302
                                <xsl:variable name="pname" select="@name"/>
 
303
                                <xsl:text>, </xsl:text>
 
304
                                <xsl:value-of select="$def/@*[name()=$pname]"/>
 
305
                        </xsl:for-each>
 
306
                        <xsl:text>)</xsl:text>
 
307
                        <xsl:choose>
 
308
                                <xsl:when test="position() = last()">;</xsl:when>
 
309
                                <xsl:otherwise>,</xsl:otherwise>
 
310
                        </xsl:choose>
 
311
                        <xsl:text>
 
312
                </xsl:text>
 
313
                </xsl:for-each>
 
314
 
 
315
 
 
316
                public static final int STREAMED_SIZE = <xsl:call-template name="size-a">
 
317
                        <xsl:with-param name="field" select="."/>
 
318
                </xsl:call-template>;
 
319
 
 
320
                final <xsl:value-of select="$vt"/> value;
 
321
                <xsl:for-each select="param">
 
322
                        <xsl:text>
 
323
                final </xsl:text>
 
324
                        <xsl:value-of select="@type"/>
 
325
                        <xsl:text> </xsl:text>
 
326
                        <xsl:value-of select="@name"/>
 
327
                        <xsl:text>;</xsl:text>
 
328
                </xsl:for-each>
 
329
 
 
330
                private <xsl:value-of select="@name"/>
 
331
                        <xsl:text>(</xsl:text>
 
332
                        <xsl:value-of select="$vt"/>
 
333
                        <xsl:text> value</xsl:text>
 
334
                        <xsl:for-each select="param">
 
335
                                <xsl:text>, </xsl:text>
 
336
                                <xsl:value-of select="@type"/>
 
337
                                <xsl:text> </xsl:text>
 
338
                                <xsl:value-of select="@name"/>
 
339
                        </xsl:for-each>
 
340
                        <xsl:text>)</xsl:text>
 
341
                {
 
342
                        this.value = value;
 
343
                        <xsl:for-each select="param">
 
344
                        <xsl:value-of select="concat('this.', @name, ' = ', @name, ';')"/>
 
345
                        <xsl:text>
 
346
                        </xsl:text>
 
347
                        </xsl:for-each>
 
348
                }
 
349
 
 
350
                private static final HashMap&lt;<xsl:value-of select="$ovt"/>,<xsl:value-of select="@name"/>&gt; lookup = new HashMap&lt;<xsl:value-of select="$ovt"/>,<xsl:value-of select="@name"/>&gt;();
 
351
 
 
352
                static {
 
353
                        for(<xsl:value-of select="@name"/> v : values()) {
 
354
                                lookup.put(v.value, v);
 
355
                        }
 
356
                }
 
357
 
 
358
                static <xsl:value-of select="@name"/> convertValue(<xsl:value-of select="$vt"/> value) throws InvalidValueException {
 
359
                        <xsl:value-of select="@name"/> r = lookup.get(value);
 
360
                        if(r == null)
 
361
                                throw new InvalidValueException(value);
 
362
                        return r;
 
363
                }
 
364
 
 
365
                public static <xsl:value-of select="@name"/> readFrom(ByteBuffer in) throws IOException {
 
366
                        if(in.remaining() &lt; STREAMED_SIZE)
 
367
                                throw new BufferUnderflowException();
 
368
 
 
369
                        return convertValue(in.get<xsl:value-of select="$xt"/>());
 
370
                }
 
371
 
 
372
                public void writeTo(ByteBuffer out) throws IOException {
 
373
                        if(out.remaining() &lt; STREAMED_SIZE)
 
374
                                throw new BufferOverflowException();
 
375
 
 
376
                        out.put<xsl:value-of select="$xt"/>(value);
 
377
                }
 
378
 
 
379
                public static void writeTo(ByteBuffer out, <xsl:value-of select="@name"/> data) throws IOException {
 
380
                        data.writeTo(out);
 
381
                }
 
382
 
 
383
                static class InvalidValueException extends IOException {
 
384
                        private static final long serialVersionUID = 1L;
 
385
 
 
386
                        public final <xsl:value-of select="$vt"/> value;
 
387
 
 
388
                        public InvalidValueException(<xsl:value-of select="$vt"/> value) {
 
389
                                super("No member of <xsl:value-of select="@name"/> corresponds to the value " + value);
 
390
                                this.value = value;
 
391
                        }
 
392
                }
 
393
        }
 
394
        </xsl:template>
 
395
 
 
396
        <xsl:template match="text()"/>
 
397
</xsl:stylesheet>
 
398