main
  1<?xml version="1.0" encoding="utf-8"?>
  2<xsl:stylesheet version="1.0"
  3    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="xsl msxsl"
  5>
  6  <xsl:import href="common.xslt"/>
  7  <xsl:param name="help"/>
  8  <xsl:param name="xml"/>
  9  <xsl:param name="datacontract"/>
 10  <xsl:param name="binary"/>
 11  <xsl:param name="protoRpc"/>
 12  <xsl:param name="observable"/>
 13  <xsl:param name="preObservable"/>
 14  <xsl:param name="partialMethods"/>
 15  <xsl:param name="detectMissing"/>
 16  <xsl:param name="lightFramework"/>
 17  <xsl:param name="asynchronous"/>
 18  <xsl:param name="clientProxy"/>
 19  <xsl:param name="defaultNamespace"/>
 20  <xsl:param name="import"/>
 21  
 22  <xsl:key name="fieldNames" match="//FieldDescriptorProto" use="name"/>
 23  <xsl:output method="text" indent="no" omit-xml-declaration="yes"/>
 24
 25  <xsl:variable name="optionXml" select="$xml='true'"/>
 26  <xsl:variable name="optionDataContract" select="$datacontract='true'"/>
 27  <xsl:variable name="optionBinary" select="$binary='true'"/>
 28  <xsl:variable name="optionProtoRpc" select="$protoRpc='true'"/>
 29  <xsl:variable name="optionObservable" select="$observable='true'"/>
 30  <xsl:variable name="optionPreObservable" select="$preObservable='true'"/>
 31  <xsl:variable name="optionPartialMethods" select="$partialMethods='true'"/>
 32  <xsl:variable name="optionDetectMissing" select="$detectMissing='true'"/>
 33  <xsl:variable name="optionFullFramework" select="not($lightFramework='true')"/>
 34  <xsl:variable name="optionAsynchronous" select="$asynchronous='true'"/>
 35  <xsl:variable name="optionClientProxy" select="$clientProxy='true'"/>
 36
 37  <xsl:template match="/">
 38    <xsl:text disable-output-escaping="yes">//------------------------------------------------------------------------------
 39// &lt;auto-generated&gt;
 40//     This code was generated by a tool.
 41//
 42//     Changes to this file may cause incorrect behavior and will be lost if
 43//     the code is regenerated.
 44// &lt;/auto-generated&gt;
 45//------------------------------------------------------------------------------
 46</xsl:text><!--
 47    --><xsl:apply-templates select="*"/><!--
 48  --></xsl:template>
 49
 50  <xsl:template name="WriteUsings">
 51    <xsl:param name="ns"/>
 52    <xsl:if test="$ns != ''"><xsl:choose>
 53   <xsl:when test="contains($ns,';')">
 54using <xsl:value-of select="substring-before($ns,';')"/>;<!--
 55 --><xsl:call-template name="WriteUsings">
 56       <xsl:with-param name="ns" select="substring-after($ns,';')"/>
 57  </xsl:call-template>
 58  </xsl:when>
 59   <xsl:otherwise>
 60using <xsl:value-of select="$ns"/>;
 61   </xsl:otherwise>
 62 </xsl:choose></xsl:if></xsl:template>
 63  
 64  <xsl:template match="FileDescriptorSet">
 65    <xsl:if test="$help='true'">
 66      <xsl:message terminate="yes">
 67        CSharp template for protobuf-net.
 68        Options:
 69        General:
 70          "help" - this page
 71        Additional serializer support:
 72          "xml" - enable explicit xml support (XmlSerializer)
 73          "datacontract" - enable data-contract support (DataContractSerializer; requires .NET 3.0)
 74          "binary" - enable binary support (BinaryFormatter; not supported on Silverlight)
 75        Other:
 76          "protoRpc" - enable proto-rpc client
 77          "observable" - change notification (observer pattern) support
 78          "preObservable" - pre-change notification (observer pattern) support (requires .NET 3.5)
 79          "partialMethods" - provide partial methods for changes (requires C# 3.0)
 80          "detectMissing" - provide *Specified properties to indicate whether fields are present
 81          "lightFramework" - omit additional attributes not included in CF/Silverlight
 82          "asynchronous" - emit asynchronous methods for use with WCF
 83          "clientProxy" - emit asynchronous client proxy class
 84          "import" - additional namespaces to import (semicolon delimited)
 85          "fixCase" - change type/member names (types/properties become PascalCase; fields become camelCase)
 86      </xsl:message>
 87    </xsl:if>
 88
 89    <xsl:if test="$optionXml and $optionDataContract">
 90      <xsl:message terminate="yes">
 91        Invalid options: xml and data-contract serialization are mutually exclusive.
 92      </xsl:message>
 93    </xsl:if>
 94    <xsl:if test="$optionXml">
 95// Option: xml serialization ([XmlType]/[XmlElement]) enabled
 96    </xsl:if><xsl:if test="$optionDataContract">
 97// Option: data-contract serialization ([DataContract]/[DataMember]) enabled
 98    </xsl:if><xsl:if test="$optionBinary">
 99// Option: binary serialization (ISerializable) enabled
100    </xsl:if><xsl:if test="$optionObservable">
101// Option: observable (OnPropertyChanged) enabled
102    </xsl:if><xsl:if test="$optionPreObservable">
103// Option: pre-observable (OnPropertyChanging) enabled
104    </xsl:if><xsl:if test="$partialMethods">
105// Option: partial methods (On*Changing/On*Changed) enabled
106    </xsl:if><xsl:if test="$detectMissing">
107// Option: missing-value detection (*Specified/ShouldSerialize*/Reset*) enabled
108    </xsl:if><xsl:if test="not($optionFullFramework)">
109// Option: light framework (CF/Silverlight) enabled
110    </xsl:if><xsl:if test="$optionProtoRpc">
111// Option: proto-rpc enabled
112  </xsl:if>
113    <xsl:call-template name="WriteUsings">
114      <xsl:with-param name="ns" select="$import"/>
115    </xsl:call-template>
116    <xsl:apply-templates select="file/FileDescriptorProto"/>
117  </xsl:template>
118
119  
120  <xsl:template match="FileDescriptorProto">
121// Generated from: <xsl:value-of select="name"/>
122    
123    <xsl:apply-templates select="dependency/string[.!='']"/>
124    <xsl:variable name="namespace"><xsl:call-template name="PickNamespace">
125      <xsl:with-param name="defaultNamespace" select="$defaultNamespace"/>
126        </xsl:call-template>
127      </xsl:variable>
128    <xsl:if test="string($namespace) != ''">
129namespace <xsl:value-of select="translate($namespace,':-/\','__..')"/>
130{</xsl:if>
131    <xsl:apply-templates select="message_type | enum_type | service"/>
132    <xsl:if test="string($namespace) != ''">
133}</xsl:if></xsl:template>
134  
135  <xsl:template match="FileDescriptorProto/dependency/string">
136// Note: requires additional types generated from: <xsl:value-of select="."/></xsl:template>
137
138
139  <xsl:template name="camel">
140    <xsl:param name="value" select="name"/>
141    <xsl:param name="delimiter" select="'_'"/>
142    <xsl:choose>
143      <xsl:when test="$optionFixCase"><xsl:call-template name="toCamelCase">
144          <xsl:with-param name="value" select="$value"/>
145          <xsl:with-param name="delimiter" select="$delimiter"/>
146        </xsl:call-template></xsl:when>
147      <xsl:otherwise><xsl:value-of select="$value"/></xsl:otherwise>
148    </xsl:choose>
149  </xsl:template>
150  
151  <xsl:template match="DescriptorProto">
152  [<xsl:if test="$optionFullFramework">global::System.Serializable, </xsl:if>global::ProtoBuf.ProtoContract(Name=@"<xsl:value-of select="name"/>")]
153  <xsl:if test="$optionDataContract">[global::System.Runtime.Serialization.DataContract(Name=@"<xsl:value-of select="name"/>")]
154  </xsl:if><xsl:if test="$optionXml">[global::System.Xml.Serialization.XmlType(TypeName=@"<xsl:value-of select="name"/>")]
155  </xsl:if><!--
156  -->public partial class <xsl:call-template name="pascal"/> : global::ProtoBuf.IExtensible<!--
157  --><xsl:if test="$optionBinary">, global::System.Runtime.Serialization.ISerializable</xsl:if><!--
158  --><xsl:if test="$optionObservable">, global::System.ComponentModel.INotifyPropertyChanged</xsl:if><!--
159  --><xsl:if test="$optionPreObservable">, global::System.ComponentModel.INotifyPropertyChanging</xsl:if>
160  {
161    public <xsl:call-template name="pascal"/>() {}
162    <xsl:apply-templates select="*"/><xsl:if test="$optionBinary">
163    protected <xsl:call-template name="pascal"/>(global::System.Runtime.Serialization.SerializationInfo info, global::System.Runtime.Serialization.StreamingContext context)
164      : this() { global::ProtoBuf.Serializer.Merge(info, this); }
165    void global::System.Runtime.Serialization.ISerializable.GetObjectData(global::System.Runtime.Serialization.SerializationInfo info, global::System.Runtime.Serialization.StreamingContext context)
166      { global::ProtoBuf.Serializer.Serialize(info, this); }
167    </xsl:if><xsl:if test="$optionObservable">
168    public event global::System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
169    protected virtual void OnPropertyChanged(string propertyName)
170      { if(PropertyChanged != null) PropertyChanged(this, new global::System.ComponentModel.PropertyChangedEventArgs(propertyName)); }
171    </xsl:if><xsl:if test="$optionPreObservable">
172    public event global::System.ComponentModel.PropertyChangingEventHandler PropertyChanging;
173    protected virtual void OnPropertyChanging(string propertyName)
174    { if(PropertyChanging != null) PropertyChanging(this, new global::System.ComponentModel.PropertyChangingEventArgs(propertyName)); }
175    </xsl:if>
176    private global::ProtoBuf.IExtension extensionObject;
177    global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
178      { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
179  }
180  </xsl:template>
181
182  <xsl:template match="DescriptorProto/name | DescriptorProto/extension_range | DescriptorProto/extension"/>
183  
184  <xsl:template match="
185                FileDescriptorProto/message_type | FileDescriptorProto/enum_type | FileDescriptorProto/service
186                | DescriptorProto/enum_type | DescriptorProto/message_type
187                | DescriptorProto/nested_type | EnumDescriptorProto/value | ServiceDescriptorProto/method">
188    <xsl:apply-templates select="*"/>
189  </xsl:template>
190
191  <xsl:template match="DescriptorProto/field">
192    <xsl:apply-templates select="*"/>
193    <xsl:variable name="extName" select="concat('.',(ancestor::FileDescriptorProto/package)[1],'.',../name)"/>
194    <xsl:apply-templates select="//FieldDescriptorProto[extendee=$extName]"/>
195  </xsl:template>
196
197  <xsl:template match="EnumDescriptorProto">
198    [global::ProtoBuf.ProtoContract(Name=@"<xsl:value-of select="name"/>")]
199    <xsl:if test="$optionDataContract">[global::System.Runtime.Serialization.DataContract(Name=@"<xsl:value-of select="name"/>")]
200    </xsl:if>
201    <xsl:if test="$optionXml">[global::System.Xml.Serialization.XmlType(TypeName=@"<xsl:value-of select="name"/>")]
202    </xsl:if><!--
203    -->public enum <xsl:call-template name="pascal"/>
204    {
205      <xsl:apply-templates select="value"/>
206    }
207  </xsl:template>
208
209  <xsl:template match="EnumValueDescriptorProto">
210      <xsl:variable name="value"><xsl:choose>
211        <xsl:when test="number"><xsl:value-of select="number"/></xsl:when>
212        <xsl:otherwise>0</xsl:otherwise>
213      </xsl:choose></xsl:variable>      
214      [global::ProtoBuf.ProtoEnum(Name=@"<xsl:value-of select="name"/>", Value=<xsl:value-of select="$value"/>)]<!--
215      --><xsl:if test="$optionDataContract">
216      [global::System.Runtime.Serialization.EnumMember(Value=@"<xsl:value-of select="name"/>")]</xsl:if><!--
217      --><xsl:if test="$optionXml">
218      [global::System.Xml.Serialization.XmlEnum(@"<xsl:value-of select="name"/>")]</xsl:if><!--
219      --><xsl:text disable-output-escaping="yes">
220      </xsl:text><xsl:call-template name="pascal"/><xsl:text xml:space="preserve"> = </xsl:text><xsl:value-of select="$value"/><xsl:if test="position()!=last()">,
221      </xsl:if>
222  </xsl:template>
223
224  <xsl:template match="FieldDescriptorProto" mode="field">
225    <xsl:variable name="field"><xsl:choose>
226      <xsl:when test="$optionFixCase"><xsl:call-template name="toCamelCase">
227          <xsl:with-param name="value" select="name"/>
228        </xsl:call-template></xsl:when>
229      <xsl:otherwise><xsl:value-of select="name"/></xsl:otherwise>
230    </xsl:choose></xsl:variable>
231    <xsl:call-template name="escapeKeyword">
232      <xsl:with-param name="value"><xsl:choose>
233      <xsl:when test="not(key('fieldNames',concat('_',$field)))"><xsl:value-of select="concat('_',$field)"/></xsl:when>
234      <xsl:when test="not(key('fieldNames',concat($field,'Field')))"><xsl:value-of select="concat($field,'Field')"/></xsl:when>
235      <xsl:otherwise><xsl:value-of select="concat('_',generate-id())"/></xsl:otherwise>
236    </xsl:choose></xsl:with-param>
237    </xsl:call-template>
238  </xsl:template>
239
240  <xsl:template name="escapeKeyword">
241    <xsl:param name="value"/>
242    <xsl:if test="contains($keywords,concat('|',$value,'|'))">@</xsl:if><xsl:value-of select="$value"/>
243  </xsl:template>
244  <xsl:variable name="keywords">|abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|void|volatile|while|</xsl:variable>
245
246  <xsl:template match="FieldDescriptorProto" mode="format">
247    <xsl:choose>
248      <xsl:when test="type='TYPE_DOUBLE' or type='TYPE_FLOAT'
249                or type='TYPE_FIXED32' or type='TYPE_FIXED64'
250                or type='TYPE_SFIXED32' or type='TYPE_SFIXED64'">FixedSize</xsl:when>
251      <xsl:when test="type='TYPE_GROUP'">Group</xsl:when>
252      <xsl:when test="not(type) or type='TYPE_INT32' or type='TYPE_INT64'
253                or type='TYPE_UINT32' or type='TYPE_UINT64'
254                or type='TYPE_ENUM'">TwosComplement</xsl:when>
255      <xsl:when test="type='TYPE_SINT32' or type='TYPE_SINT64'">ZigZag</xsl:when>
256      <xsl:otherwise>Default</xsl:otherwise>
257    </xsl:choose>
258  </xsl:template>
259  <xsl:template match="FieldDescriptorProto" mode="primitiveType">
260    <xsl:choose>
261      <xsl:when test="not(type)">struct</xsl:when>
262      <xsl:when test="type='TYPE_DOUBLE'">struct</xsl:when>
263      <xsl:when test="type='TYPE_FLOAT'">struct</xsl:when>
264      <xsl:when test="type='TYPE_INT64'">struct</xsl:when>
265      <xsl:when test="type='TYPE_UINT64'">struct</xsl:when>
266      <xsl:when test="type='TYPE_INT32'">struct</xsl:when>
267      <xsl:when test="type='TYPE_FIXED64'">struct</xsl:when>
268      <xsl:when test="type='TYPE_FIXED32'">struct</xsl:when>
269      <xsl:when test="type='TYPE_BOOL'">struct</xsl:when>
270      <xsl:when test="type='TYPE_STRING'">class</xsl:when>
271      <xsl:when test="type='TYPE_BYTES'">class</xsl:when>
272      <xsl:when test="type='TYPE_UINT32'">struct</xsl:when>
273      <xsl:when test="type='TYPE_SFIXED32'">struct</xsl:when>
274      <xsl:when test="type='TYPE_SFIXED64'">struct</xsl:when>
275      <xsl:when test="type='TYPE_SINT32'">struct</xsl:when>
276      <xsl:when test="type='TYPE_SINT64'">struct</xsl:when>
277      <xsl:when test="type='TYPE_ENUM'">struct</xsl:when>
278      <xsl:when test="type='TYPE_GROUP' or type='TYPE_MESSAGE'">none</xsl:when>
279      <xsl:otherwise>
280        <xsl:message terminate="yes">
281          Field type not implemented: <xsl:value-of select="type"/> (<xsl:value-of select="../../name"/>.<xsl:value-of select="name"/>)
282        </xsl:message>
283      </xsl:otherwise>
284    </xsl:choose>
285  </xsl:template>
286  <xsl:template match="FieldDescriptorProto" mode="type">
287    <xsl:choose>
288      <xsl:when test="not(type)">double</xsl:when>
289      <xsl:when test="type='TYPE_DOUBLE'">double</xsl:when>
290      <xsl:when test="type='TYPE_FLOAT'">float</xsl:when>
291      <xsl:when test="type='TYPE_INT64'">long</xsl:when>
292      <xsl:when test="type='TYPE_UINT64'">ulong</xsl:when>
293      <xsl:when test="type='TYPE_INT32'">int</xsl:when>
294      <xsl:when test="type='TYPE_FIXED64'">ulong</xsl:when>
295      <xsl:when test="type='TYPE_FIXED32'">uint</xsl:when>
296      <xsl:when test="type='TYPE_BOOL'">bool</xsl:when>
297      <xsl:when test="type='TYPE_STRING'">string</xsl:when>
298      <xsl:when test="type='TYPE_BYTES'">byte[]</xsl:when>
299      <xsl:when test="type='TYPE_UINT32'">uint</xsl:when>
300      <xsl:when test="type='TYPE_SFIXED32'">int</xsl:when>
301      <xsl:when test="type='TYPE_SFIXED64'">long</xsl:when>
302      <xsl:when test="type='TYPE_SINT32'">int</xsl:when>
303      <xsl:when test="type='TYPE_SINT64'">long</xsl:when>
304      <xsl:when test="type='TYPE_GROUP' or type='TYPE_MESSAGE' or type='TYPE_ENUM'"><xsl:call-template name="pascal">
305        <xsl:with-param name="value" select="substring-after(type_name,'.')"/>
306      </xsl:call-template></xsl:when>
307      <xsl:otherwise>
308        <xsl:message terminate="yes">
309          Field type not implemented: <xsl:value-of select="type"/> (<xsl:value-of select="../../name"/>.<xsl:value-of select="name"/>)
310        </xsl:message>
311      </xsl:otherwise>
312    </xsl:choose>
313    
314  </xsl:template>
315
316  <xsl:template match="FieldDescriptorProto[default_value]" mode="defaultValue">
317    <xsl:choose>
318      <xsl:when test="type='TYPE_STRING'">@"<xsl:value-of select="default_value"/>"</xsl:when>
319      <xsl:when test="type='TYPE_ENUM'"><xsl:apply-templates select="." mode="type"/>.<xsl:call-template name="pascal">
320        <xsl:with-param name="value" select="default_value"/>
321      </xsl:call-template></xsl:when>
322      <xsl:when test="type='TYPE_BYTES'"> /* 
323        <xsl:value-of select="default_value"/>
324        */ null </xsl:when>
325      <xsl:otherwise>(<xsl:apply-templates select="." mode="type"/>)<xsl:value-of select="default_value"/></xsl:otherwise>
326    </xsl:choose>
327  </xsl:template>
328
329  <!--
330    We need to find the first enum value given .foo.bar.SomeEnum - but the enum itself
331    only knows about SomeEnum; we need to look at all parent DescriptorProto nodes, and
332    the FileDescriptorProto for the namespace.
333    
334    This does an annoying up/down recursion... a bit expensive, but *generally* OK.
335    Could perhaps index the last part of the enum name to reduce overhead?
336  -->
337  <xsl:template name="GetFirstEnumValue">
338    <xsl:variable name="hunt" select="type_name"/>
339    <xsl:for-each select="//EnumDescriptorProto">
340      <xsl:variable name="fullName">
341        <xsl:for-each select="ancestor::FileDescriptorProto">.<xsl:value-of select="package"/></xsl:for-each>
342        <xsl:for-each select="ancestor::DescriptorProto">.<xsl:value-of select="name"/></xsl:for-each>
343        <xsl:value-of select="'.'"/>
344        <xsl:call-template name="pascal"/>
345      </xsl:variable>
346      <xsl:if test="$fullName=$hunt"><xsl:value-of select="(value/EnumValueDescriptorProto)[1]/name"/></xsl:if>
347    </xsl:for-each>
348  </xsl:template>
349  
350  <xsl:template match="FieldDescriptorProto[not(default_value)]" mode="defaultValue">
351    <xsl:choose>
352      <xsl:when test="type='TYPE_STRING'">""</xsl:when>
353      <xsl:when test="type='TYPE_MESSAGE'">null</xsl:when>
354      <xsl:when test="type='TYPE_BYTES'">null</xsl:when>
355      <xsl:when test="type='TYPE_ENUM'"><xsl:apply-templates select="." mode="type"/>.<xsl:call-template name="GetFirstEnumValue"/></xsl:when>
356      <xsl:otherwise>default(<xsl:apply-templates select="." mode="type"/>)</xsl:otherwise>
357    </xsl:choose>
358  </xsl:template>
359
360  <xsl:template match="FieldDescriptorProto" mode="checkDeprecated"><!--
361    --><xsl:if test="options/deprecated='true'">global::System.Obsolete, </xsl:if><!--
362  --></xsl:template>
363  <xsl:template match="FieldDescriptorProto[label='LABEL_OPTIONAL' or not(label)]">
364    <xsl:variable name="propType"><xsl:apply-templates select="." mode="type"/></xsl:variable>
365    <xsl:variable name="format"><xsl:apply-templates select="." mode="format"/></xsl:variable>
366    <xsl:variable name="primitiveType"><xsl:apply-templates select="." mode="primitiveType"/></xsl:variable>
367    <xsl:variable name="defaultValue"><xsl:apply-templates select="." mode="defaultValue"/></xsl:variable>
368    <xsl:variable name="field"><xsl:apply-templates select="." mode="field"/></xsl:variable>
369    <xsl:variable name="specified" select="$optionDetectMissing and ($primitiveType='struct' or $primitiveType='class')"/>
370    <xsl:variable name="fieldType"><xsl:value-of select="$propType"/><xsl:if test="$specified and $primitiveType='struct'">?</xsl:if></xsl:variable>
371
372    private <xsl:value-of select="concat($fieldType,' ',$field)"/><xsl:if test="not($specified)"> = <xsl:value-of select="$defaultValue"/></xsl:if>;
373    [<xsl:apply-templates select="." mode="checkDeprecated"/>global::ProtoBuf.ProtoMember(<xsl:value-of select="number"/>, IsRequired = false, Name=@"<xsl:value-of select="name"/>", DataFormat = global::ProtoBuf.DataFormat.<xsl:value-of select="$format"/>)]<!--
374    --><xsl:if test="not($specified)">
375    [global::System.ComponentModel.DefaultValue(<xsl:value-of select="$defaultValue"/>)]</xsl:if><!--
376    --><xsl:if test="$optionXml">
377    [global::System.Xml.Serialization.XmlElement(@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>)]
378    </xsl:if><xsl:if test="$optionDataContract">
379    [global::System.Runtime.Serialization.DataMember(Name=@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>, IsRequired = false)]
380    </xsl:if><xsl:call-template name="WriteGetSet">
381      <xsl:with-param name="fieldType" select="$fieldType"/>
382      <xsl:with-param name="propType" select="$propType"/>
383      <xsl:with-param name="name"><xsl:call-template name="pascal"/></xsl:with-param>
384      <xsl:with-param name="field" select="$field"/>
385      <xsl:with-param name="defaultValue" select="$defaultValue"/>
386      <xsl:with-param name="specified" select="$specified"/>
387    </xsl:call-template>
388  </xsl:template>
389  
390  <xsl:template match="FieldDescriptorProto[label='LABEL_REQUIRED']">
391    <xsl:variable name="type"><xsl:apply-templates select="." mode="type"/></xsl:variable>
392    <xsl:variable name="format"><xsl:apply-templates select="." mode="format"/></xsl:variable>
393    <xsl:variable name="field"><xsl:apply-templates select="." mode="field"/></xsl:variable>
394    private <xsl:value-of select="concat($type, ' ', $field)"/>;
395    [<xsl:apply-templates select="." mode="checkDeprecated"/>global::ProtoBuf.ProtoMember(<xsl:value-of select="number"/>, IsRequired = true, Name=@"<xsl:value-of select="name"/>", DataFormat = global::ProtoBuf.DataFormat.<xsl:value-of select="$format"/>)]<!--
396    --><xsl:if test="$optionXml">
397    [global::System.Xml.Serialization.XmlElement(@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>)]
398    </xsl:if><xsl:if test="$optionDataContract">
399    [global::System.Runtime.Serialization.DataMember(Name=@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>, IsRequired = true)]
400    </xsl:if><xsl:call-template name="WriteGetSet">
401      <xsl:with-param name="fieldType" select="$type"/>
402      <xsl:with-param name="propType" select="$type"/>
403      <xsl:with-param name="name"><xsl:call-template name="pascal"/></xsl:with-param>
404      <xsl:with-param name="field" select="$field"/>
405    </xsl:call-template>    
406  </xsl:template>
407
408  <xsl:template name="stripKeyword">
409    <xsl:param name="value"/>
410    <xsl:choose>
411      <xsl:when test="starts-with($value,'@')"><xsl:value-of select="substring-after($value,'@')"/></xsl:when>
412      <xsl:otherwise><xsl:value-of select="$value"/></xsl:otherwise>
413    </xsl:choose>
414  </xsl:template>
415  
416  <xsl:template name="WriteGetSet">
417    <xsl:param name="fieldType"/>
418    <xsl:param name="propType"/>
419    <xsl:param name="name"/>
420    <xsl:param name="field"/>
421    <xsl:param name="specified" select="false()"/>
422    <xsl:param name="defaultValue"/>
423    <xsl:variable name="nameNoKeyword">
424      <xsl:call-template name="stripKeyword">
425        <xsl:with-param name="value" select="$name"/>
426      </xsl:call-template></xsl:variable>
427    public <xsl:value-of select="concat($propType,' ',$name)"/>
428    {
429      get { return <xsl:value-of select="$field"/> <xsl:if test="$specified">?? <xsl:value-of select="$defaultValue"/></xsl:if>; }
430      set { <xsl:if test="$optionPartialMethods">On<xsl:value-of select="$nameNoKeyword"/>Changing(value); </xsl:if><xsl:if test="$optionPreObservable">OnPropertyChanging(@"<xsl:value-of select="$nameNoKeyword"/>"); </xsl:if><xsl:value-of select="$field"/> = value; <xsl:if test="$optionObservable">OnPropertyChanged(@"<xsl:value-of select="$nameNoKeyword"/>"); </xsl:if><xsl:if test="$optionPartialMethods">On<xsl:value-of select="$nameNoKeyword"/>Changed();</xsl:if>}
431    }<xsl:if test="$optionPartialMethods">
432    partial void On<xsl:value-of select="$nameNoKeyword"/>Changing(<xsl:value-of select="$propType"/> value);
433    partial void On<xsl:value-of select="$nameNoKeyword"/>Changed();</xsl:if><xsl:if test="$specified">
434    [global::System.Xml.Serialization.XmlIgnore]
435    <xsl:if test="$optionFullFramework">[global::System.ComponentModel.Browsable(false)]</xsl:if>
436    public bool <xsl:value-of select="$nameNoKeyword"/>Specified
437    {
438      get { return <xsl:value-of select="$field"/> != null; }
439      set { if (value == (<xsl:value-of select="$field"/>== null)) <xsl:value-of select="$field"/> = value ? <xsl:value-of select="$name"/> : (<xsl:value-of select="$fieldType"/>)null; }
440    }
441    private bool ShouldSerialize<xsl:value-of select="$nameNoKeyword"/>() { return <xsl:value-of select="$nameNoKeyword"/>Specified; }
442    private void Reset<xsl:value-of select="$nameNoKeyword"/>() { <xsl:value-of select="$nameNoKeyword"/>Specified = false; }
443    </xsl:if>
444  </xsl:template>
445  <xsl:template match="FieldDescriptorProto[label='LABEL_REPEATED']">
446    <xsl:variable name="type"><xsl:apply-templates select="." mode="type"/></xsl:variable>
447    <xsl:variable name="format"><xsl:apply-templates select="." mode="format"/></xsl:variable>
448    <xsl:variable name="field"><xsl:apply-templates select="." mode="field"/></xsl:variable>
449    private <xsl:if test="not($optionXml)">readonly</xsl:if> global::System.Collections.Generic.List&lt;<xsl:value-of select="$type" />&gt; <xsl:value-of select="$field"/> = new global::System.Collections.Generic.List&lt;<xsl:value-of select="$type"/>&gt;();
450    [<xsl:apply-templates select="." mode="checkDeprecated"/>global::ProtoBuf.ProtoMember(<xsl:value-of select="number"/>, Name=@"<xsl:value-of select="name"/>", DataFormat = global::ProtoBuf.DataFormat.<xsl:value-of select="$format"/><xsl:if test="options/packed='true'">, Options = global::ProtoBuf.MemberSerializationOptions.Packed</xsl:if>)]<!--
451    --><xsl:if test="$optionDataContract">
452    [global::System.Runtime.Serialization.DataMember(Name=@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>, IsRequired = false)]
453    </xsl:if><xsl:if test="$optionXml">
454    [global::System.Xml.Serialization.XmlElement(@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>)]
455    </xsl:if>
456    public global::System.Collections.Generic.List&lt;<xsl:value-of select="$type" />&gt; <xsl:call-template name="pascal"/>
457    {
458      get { return <xsl:value-of select="$field"/>; }<!--
459      --><xsl:if test="$optionXml">
460      set { <xsl:value-of select="$field"/> = value; }</xsl:if>
461    }
462  </xsl:template>
463
464  <xsl:template match="ServiceDescriptorProto">
465    <xsl:if test="($optionClientProxy or $optionDataContract)">
466    [global::System.ServiceModel.ServiceContract(Name = @"<xsl:value-of select="name"/>")]</xsl:if>
467    public interface I<xsl:value-of select="name"/>
468    {
469      <xsl:apply-templates select="method"/>
470    }
471    
472    <xsl:if test="$optionProtoRpc">
473    public class <xsl:value-of select="name"/>Client : global::ProtoBuf.ServiceModel.RpcClient
474    {
475      public <xsl:value-of select="name"/>Client() : base(typeof(I<xsl:value-of select="name"/>)) { }
476      <xsl:apply-templates select="method/MethodDescriptorProto" mode="protoRpc"/>
477    }
478    </xsl:if>
479    <xsl:apply-templates select="." mode="clientProxy"/>
480    
481  </xsl:template>
482
483  <xsl:template match="MethodDescriptorProto">
484    <xsl:if test="($optionClientProxy or $optionDataContract)">
485        [global::System.ServiceModel.OperationContract(Name = @"<xsl:value-of select="name"/>")]
486        <xsl:if test="$optionFullFramework">[global::ProtoBuf.ServiceModel.ProtoBehavior]</xsl:if>
487    </xsl:if>
488        <xsl:apply-templates select="output_type"/><xsl:text xml:space="preserve"> </xsl:text><xsl:value-of select="name"/>(<xsl:apply-templates select="input_type"/> request);
489    <xsl:if test="$optionAsynchronous and ($optionClientProxy or $optionDataContract)">
490        [global::System.ServiceModel.OperationContract(AsyncPattern = true, Name = @"<xsl:value-of select="name"/>")]
491        global::System.IAsyncResult Begin<xsl:value-of select="name"/>(<xsl:apply-templates select="input_type"/> request, global::System.AsyncCallback callback, object state);
492    <xsl:apply-templates select="output_type"/> End<xsl:value-of select="name"/>(global::System.IAsyncResult ar);
493    </xsl:if>
494  </xsl:template>
495
496  <xsl:template match="MethodDescriptorProto" mode="protoRpc">
497        <xsl:apply-templates select="output_type"/><xsl:text xml:space="preserve"> </xsl:text><xsl:value-of select="name"/>(<xsl:apply-templates select="input_type"/> request)
498        {
499            return (<xsl:apply-templates select="output_type"/>) Send(@"<xsl:value-of select="name"/>", request);
500        }
501  </xsl:template>
502
503  <xsl:template match="MethodDescriptorProto/input_type | MethodDescriptorProto/output_type">
504    <xsl:value-of select="substring-after(.,'.')"/>
505  </xsl:template>
506
507  <xsl:template match="MethodDescriptorProto" mode="CompleteEvent">
508  <xsl:if test="$optionAsynchronous and $optionDataContract">
509    public partial class <xsl:value-of select="name"/>CompletedEventArgs : global::System.ComponentModel.AsyncCompletedEventArgs
510    {
511        private object[] results;
512
513        public <xsl:value-of select="name"/>CompletedEventArgs(object[] results, global::System.Exception exception, bool cancelled, object userState)
514            : base(exception, cancelled, userState) 
515        {
516            this.results = results;
517        }
518        
519        public <xsl:apply-templates select="output_type"/> Result
520        {
521            get { 
522                base.RaiseExceptionIfNecessary();
523                return (<xsl:apply-templates select="output_type"/>)(this.results[0]); 
524            }
525        }
526    }
527  </xsl:if>
528  </xsl:template>
529
530  <xsl:template match="ServiceDescriptorProto" mode="clientProxy">
531  <xsl:if test="$optionAsynchronous and $optionDataContract and $optionClientProxy">
532    <xsl:apply-templates select="method/MethodDescriptorProto" mode="CompleteEvent"/>
533    
534    [global::System.Diagnostics.DebuggerStepThroughAttribute()]
535    public partial class <xsl:value-of select="name"/>Client : global::System.ServiceModel.ClientBase&lt;I<xsl:value-of select="name"/>&gt;, I<xsl:value-of select="name"/>
536    {
537
538        public <xsl:value-of select="name"/>Client()
539        {}
540        public <xsl:value-of select="name"/>Client(string endpointConfigurationName) 
541            : base(endpointConfigurationName) 
542        {}
543        public <xsl:value-of select="name"/>Client(string endpointConfigurationName, string remoteAddress) 
544            : base(endpointConfigurationName, remoteAddress)
545        {}
546        public <xsl:value-of select="name"/>Client(string endpointConfigurationName, global::System.ServiceModel.EndpointAddress remoteAddress)
547            : base(endpointConfigurationName, remoteAddress)
548        {}
549        public <xsl:value-of select="name"/>Client(global::System.ServiceModel.Channels.Binding binding, global::System.ServiceModel.EndpointAddress remoteAddress)
550            : base(binding, remoteAddress)
551        {}
552
553        <xsl:apply-templates select="method/MethodDescriptorProto" mode="clientProxy"/>
554    }  
555  </xsl:if>
556  </xsl:template>
557
558  <xsl:template match="MethodDescriptorProto" mode="clientProxy">
559  <xsl:if test="$optionAsynchronous and $optionDataContract and $optionClientProxy">
560        private BeginOperationDelegate onBegin<xsl:value-of select="name"/>Delegate;
561        private EndOperationDelegate onEnd<xsl:value-of select="name"/>Delegate;
562        private global::System.Threading.SendOrPostCallback on<xsl:value-of select="name"/>CompletedDelegate;
563
564        public event global::System.EventHandler&lt;<xsl:value-of select="name"/>CompletedEventArgs&gt; <xsl:value-of select="name"/>Completed;
565
566        public <xsl:apply-templates select="output_type"/><xsl:text xml:space="preserve"> </xsl:text><xsl:value-of select="name"/>(<xsl:apply-templates select="input_type"/> request)
567        {
568            return base.Channel.<xsl:value-of select="name"/>(request);
569        }
570
571        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
572        public global::System.IAsyncResult Begin<xsl:value-of select="name"/>(<xsl:apply-templates select="input_type"/> request, global::System.AsyncCallback callback, object asyncState)
573        {
574            return base.Channel.Begin<xsl:value-of select="name"/>(request, callback, asyncState);
575        }
576
577        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
578        public <xsl:apply-templates select="output_type"/> End<xsl:value-of select="name"/>(global::System.IAsyncResult result)
579        {
580            return base.Channel.End<xsl:value-of select="name"/>(result);
581        }
582
583        private global::System.IAsyncResult OnBegin<xsl:value-of select="name"/>(object[] inValues, global::System.AsyncCallback callback, object asyncState)
584        {
585            <xsl:apply-templates select="input_type"/> request = ((<xsl:apply-templates select="input_type"/>)(inValues[0]));
586            return this.Begin<xsl:value-of select="name"/>(request, callback, asyncState);
587        }
588
589        private object[] OnEnd<xsl:value-of select="name"/>(global::System.IAsyncResult result)
590        {
591            <xsl:apply-templates select="output_type"/> retVal = this.End<xsl:value-of select="name"/>(result);
592            return new object[] {
593                retVal};
594        }
595
596        private void On<xsl:value-of select="name"/>Completed(object state)
597        {
598            if ((this.<xsl:value-of select="name"/>Completed != null))
599            {
600                InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
601                this.<xsl:value-of select="name"/>Completed(this, new <xsl:value-of select="name"/>CompletedEventArgs(e.Results, e.Error, e.Cancelled, e.UserState));
602            }
603        }
604
605        public void <xsl:value-of select="name"/>Async(<xsl:apply-templates select="input_type"/> request)
606        {
607            this.<xsl:value-of select="name"/>Async(request, null);
608        }
609
610        public void <xsl:value-of select="name"/>Async(<xsl:apply-templates select="input_type"/> request, object userState)
611        {
612            if ((this.onBegin<xsl:value-of select="name"/>Delegate == null))
613            {
614                this.onBegin<xsl:value-of select="name"/>Delegate = new BeginOperationDelegate(this.OnBegin<xsl:value-of select="name"/>);
615            }
616            if ((this.onEnd<xsl:value-of select="name"/>Delegate == null))
617            {
618                this.onEnd<xsl:value-of select="name"/>Delegate = new EndOperationDelegate(this.OnEnd<xsl:value-of select="name"/>);
619            }
620            if ((this.on<xsl:value-of select="name"/>CompletedDelegate == null))
621            {
622                this.on<xsl:value-of select="name"/>CompletedDelegate = new global::System.Threading.SendOrPostCallback(this.On<xsl:value-of select="name"/>Completed);
623            }
624            base.InvokeAsync(this.onBegin<xsl:value-of select="name"/>Delegate, new object[] {
625                    request}, this.onEnd<xsl:value-of select="name"/>Delegate, this.on<xsl:value-of select="name"/>CompletedDelegate, userState);
626        }
627    </xsl:if>
628    </xsl:template>
629</xsl:stylesheet>