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
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:
13
* The above copyright notice and this permission notice shall be included in all copies or
14
* substantial portions of the Software.
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.
23
////////////////////////////////////////////////////////////
24
// THIS JAVA SOURCE IS GENERATED. AVOID EDITING DIRECTLY. //
25
////////////////////////////////////////////////////////////
27
package us.wxy.passe.fcgi;
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;
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.
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.
44
// The Type enum has additional utility flags indicating the direction
45
// (isFromServer), target level (isManagement), and streamingness (isStreaming)
47
final class Protocol {
50
<xsl:apply-templates />
54
<xsl:template name="type-a">
55
<xsl:param name="field"/>
56
<xsl:for-each select="$field">
58
<xsl:when test="@type"><xsl:value-of select="@type"/></xsl:when>
59
<xsl:when test="@unsigned and @size < 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>
67
<xsl:template name="type-o">
68
<xsl:param name="field"/>
69
<xsl:for-each select="$field">
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>
79
<xsl:template name="size-a">
80
<xsl:param name="field"/>
81
<xsl:for-each select="$field">
83
<xsl:when test="@type"><xsl:value-of select="@type"/>.STREAMED_SIZE</xsl:when>
84
<xsl:otherwise><xsl:value-of select="@size"/></xsl:otherwise>
88
<xsl:template name="type-b">
89
<xsl:param name="field"/>
90
<xsl:for-each select="$field">
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>
99
<xsl:template name="unsign-b">
100
<xsl:param name="field"/>
101
<xsl:for-each select="$field">
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>
109
<xsl:template name="resign-b">
110
<xsl:param name="field"/>
111
<xsl:for-each select="$field">
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>
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"/>
127
<xsl:text>"</xsl:text>
129
<xsl:template name="_z-string">
130
<xsl:param name="count"/>
131
<xsl:if test="$count > 0">
132
<xsl:text>\0</xsl:text>
133
<xsl:call-template name="_z-string">
134
<xsl:with-param name="count" select="$count - 1"/>
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="."/>
146
<xsl:value-of select="' '"/>
147
<xsl:value-of select="@name" />
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="."/>
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="."/>
168
<xsl:value-of select="' '"/>
169
<xsl:value-of select="@name" />
171
<xsl:text>) {</xsl:text>
172
<xsl:for-each select="field[@name]">
175
<xsl:value-of select="@name" />
176
<xsl:text> = </xsl:text>
177
<xsl:value-of select="@name" />
178
<xsl:text>;</xsl:text>
183
public static <xsl:value-of select="@name"/> readFrom(ByteBuffer in) throws IOException {
184
if(in.remaining() < STREAMED_SIZE)
185
throw new BufferUnderflowException();
187
<xsl:for-each select="field"><xsl:text/>
189
<xsl:when test="@name">
190
<xsl:call-template name="type-a">
191
<xsl:with-param name="field" select="."/>
193
<xsl:value-of select="concat(' ', @name, ' = ')" />
195
<xsl:when test="@transfer-type">
196
<xsl:value-of select="concat(@transfer-type, '.readFrom(in)')"/>
198
<xsl:when test="@type">
199
<xsl:value-of select="concat(@type, '.readFrom(in)')"/>
201
<xsl:when test="@unsigned">
202
<xsl:call-template name="unsign-b">
203
<xsl:with-param name="field" select="."/>
205
<xsl:text> & in.get</xsl:text>
206
<xsl:call-template name="type-b">
207
<xsl:with-param name="field" select="."/>
209
<xsl:text>()</xsl:text>
212
<xsl:text>in.get</xsl:text>
213
<xsl:call-template name="type-b">
214
<xsl:with-param name="field" select="."/>
216
<xsl:text>()</xsl:text>
223
in.position(in.position() + <xsl:value-of select="@size"/>);
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" />
235
<xsl:text>);</xsl:text>
238
public static void writeTo(ByteBuffer out, <xsl:value-of select="@name"/> data) throws IOException {
239
<xsl:for-each select="field"><xsl:text/>
241
<xsl:when test="@name">
243
<xsl:when test="@transfer-type">
244
<xsl:value-of select="concat(@transfer-type, '.writeTo(out, data.' , @name, ')')"/>
246
<xsl:when test="@type">
247
<xsl:value-of select="concat(@type, '.writeTo(out, data.' , @name, ')')"/>
250
<xsl:text>out.put</xsl:text>
251
<xsl:call-template name="type-b">
252
<xsl:with-param name="field" select="."/>
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="."/>
260
<xsl:text>)</xsl:text>
262
<xsl:text>data.</xsl:text>
263
<xsl:value-of select="@name"/>
264
<xsl:text>)</xsl:text>
271
out.position(out.position() + <xsl:value-of select="@size"/>);
279
<xsl:template match="enum">
280
<xsl:variable name="vt">
281
<xsl:call-template name="type-a">
282
<xsl:with-param name="field" select="."/>
285
<xsl:variable name="ovt">
286
<xsl:call-template name="type-o">
287
<xsl:with-param name="field" select="."/>
290
<xsl:variable name="xt">
291
<xsl:call-template name="type-b">
292
<xsl:with-param name="field" select="."/>
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]"/>
306
<xsl:text>)</xsl:text>
308
<xsl:when test="position() = last()">;</xsl:when>
309
<xsl:otherwise>,</xsl:otherwise>
316
public static final int STREAMED_SIZE = <xsl:call-template name="size-a">
317
<xsl:with-param name="field" select="."/>
318
</xsl:call-template>;
320
final <xsl:value-of select="$vt"/> value;
321
<xsl:for-each select="param">
324
<xsl:value-of select="@type"/>
325
<xsl:text> </xsl:text>
326
<xsl:value-of select="@name"/>
327
<xsl:text>;</xsl:text>
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"/>
340
<xsl:text>)</xsl:text>
343
<xsl:for-each select="param">
344
<xsl:value-of select="concat('this.', @name, ' = ', @name, ';')"/>
350
private static final HashMap<<xsl:value-of select="$ovt"/>,<xsl:value-of select="@name"/>> lookup = new HashMap<<xsl:value-of select="$ovt"/>,<xsl:value-of select="@name"/>>();
353
for(<xsl:value-of select="@name"/> v : values()) {
354
lookup.put(v.value, v);
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);
361
throw new InvalidValueException(value);
365
public static <xsl:value-of select="@name"/> readFrom(ByteBuffer in) throws IOException {
366
if(in.remaining() < STREAMED_SIZE)
367
throw new BufferUnderflowException();
369
return convertValue(in.get<xsl:value-of select="$xt"/>());
372
public void writeTo(ByteBuffer out) throws IOException {
373
if(out.remaining() < STREAMED_SIZE)
374
throw new BufferOverflowException();
376
out.put<xsl:value-of select="$xt"/>(value);
379
public static void writeTo(ByteBuffer out, <xsl:value-of select="@name"/> data) throws IOException {
383
static class InvalidValueException extends IOException {
384
private static final long serialVersionUID = 1L;
386
public final <xsl:value-of select="$vt"/> value;
388
public InvalidValueException(<xsl:value-of select="$vt"/> value) {
389
super("No member of <xsl:value-of select="@name"/> corresponds to the value " + value);
396
<xsl:template match="text()"/>