Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
295 lines (273 sloc) 12 KB
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2017 Aleksei Kharlov (aka halex2005)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:import href="common.xslt"/>
<xsl:param name="defaultNamespace"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:comment>
<xsl:text disable-output-escaping="yes">
This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
</xsl:text>
</xsl:comment>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="FileDescriptorSet">
<xs:schema>
<xsl:apply-templates select="file/FileDescriptorProto"/>
</xs:schema>
</xsl:template>
<xsl:template match="FileDescriptorProto">
<xsl:comment>Generated from: <xsl:value-of select="name"/></xsl:comment>
<xsl:variable name="namespace">
<xsl:call-template name="PickNamespace">
<xsl:with-param name="defaultNamespace" select="$defaultNamespace"/>
</xsl:call-template>
</xsl:variable>
<xsl:comment>Namespace: <xsl:value-of select="$namespace"/></xsl:comment>
<xsl:apply-templates select="message_type/DescriptorProto | enum_type/EnumDescriptorProto" />
</xsl:template>
<xsl:template match="DescriptorProto">
<xsl:variable name="fullType">
<xsl:call-template name="GetFullType">
<xsl:with-param name="name" select="name" />
</xsl:call-template>
</xsl:variable>
<xs:complexType name="{$fullType}">
<xs:all>
<xsl:apply-templates select="field/FieldDescriptorProto[normalize-space(type) = 'TYPE_MESSAGE']" mode="elements"/>
<xsl:apply-templates select="field/FieldDescriptorProto[normalize-space(type) != 'TYPE_MESSAGE']" mode="elements"/>
</xs:all>
<xsl:apply-templates select="field/FieldDescriptorProto[normalize-space(label) != 'LABEL_REPEATED']" mode="attributes"/>
</xs:complexType>
<xsl:apply-templates select="enum_type/EnumDescriptorProto" />
<xsl:apply-templates select="nested_type/DescriptorProto" />
</xsl:template>
<xsl:template match="FieldDescriptorProto[normalize-space(label) != 'LABEL_REPEATED']" mode="attributes">
<xsl:choose>
<xsl:when test="normalize-space(type) != 'TYPE_MESSAGE'">
<xs:attribute name="{name}">
<xsl:attribute name="use">
<xsl:choose>
<xsl:when test="label='LABEL_REQUIRED'">required</xsl:when>
<xsl:otherwise>optional</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="default_value">
<xsl:variable name="fullType">
<xsl:value-of select="substring-after(type_name, '.')" />
</xsl:variable>
<xsl:variable name="defaultValue">
<xsl:value-of select="default_value"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="type='TYPE_ENUM'">
<xsl:variable name="enumDescriptor">
<xsl:call-template name="GetEnumDescriptorProtoByName">
<xsl:with-param name="name" select="$fullType"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$enumDescriptor and msxsl:node-set($enumDescriptor)/value/EnumValueDescriptorProto[name=$defaultValue]">
<xsl:attribute name="default">
<xsl:choose>
<xsl:when test="msxsl:node-set($enumDescriptor)/value/EnumValueDescriptorProto[name=$defaultValue]/number">
<xsl:value-of select="msxsl:node-set($enumDescriptor)/value/EnumValueDescriptorProto[name=$defaultValue]/number" />
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="default">
<xsl:value-of select="default_value"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:attribute name="type">
<xsl:call-template name="GetPrimitiveType" />
</xsl:attribute>
</xs:attribute>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="GetEnumDescriptorProtoByName">
<xsl:param name="name"/>
<xsl:for-each select="//enum_type/EnumDescriptorProto">
<xsl:variable name="descriptorName">
<xsl:call-template name="GetFullType">
<xsl:with-param name="name" select="name" />
</xsl:call-template>
</xsl:variable>
<xsl:if test="$descriptorName=$name">
<xsl:copy-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="FieldDescriptorProto[normalize-space(label) != 'LABEL_REPEATED']" mode="elements">
<xsl:choose>
<xsl:when test="type='TYPE_MESSAGE'">
<xs:element name="{name}" type="{substring-after(type_name, '.')}">
<xsl:if test="not(label) or label='LABEL_OPTIONAL'">
<xsl:attribute name="minOccurs">0</xsl:attribute>
</xsl:if>
</xs:element>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="FieldDescriptorProto[normalize-space(type) != 'TYPE_MESSAGE' and normalize-space(label) = 'LABEL_REPEATED']" mode="elements">
<xsl:variable name="primitiveType">
<xsl:call-template name="GetPrimitiveType" />
</xsl:variable>
<xs:element name="{name}s">
<xs:complexType>
<xs:sequence>
<xs:element name="{name}" type="{$primitiveType}" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xsl:template>
<xsl:template match="FieldDescriptorProto[type='TYPE_MESSAGE' and label='LABEL_REPEATED']" mode="elements">
<xsl:variable name="primitiveType">
<xsl:call-template name="ConvertProtobufTypeToXsdType">
<xsl:with-param name="type" select="type"/>
</xsl:call-template>
</xsl:variable>
<xs:element name="{name}s">
<xs:complexType>
<xs:sequence>
<xsl:choose>
<xsl:when test="type='TYPE_MESSAGE'">
<xs:element name="{name}" type="{substring-after(type_name, '.')}" maxOccurs="unbounded" />
</xsl:when>
<xsl:otherwise>
<xs:element name="{name}" type="{$primitiveType}" maxOccurs="unbounded" />
</xsl:otherwise>
</xsl:choose>
</xs:sequence>
</xs:complexType>
</xs:element>
</xsl:template>
<xsl:template match="EnumDescriptorProto">
<xsl:variable name="fullType">
<xsl:call-template name="GetFullType">
<xsl:with-param name="name" select="name"/>
</xsl:call-template>
</xsl:variable>
<xs:simpleType name="{$fullType}">
<xs:restriction base="xs:string">
<xsl:apply-templates select="value/EnumValueDescriptorProto"/>
</xs:restriction>
</xs:simpleType>
</xsl:template>
<xsl:template match="EnumValueDescriptorProto">
<xsl:variable name="value">
<xsl:choose>
<xsl:when test="number">
<xsl:value-of select="number"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:comment><xsl:value-of select="name"/>:</xsl:comment>
<xs:enumeration value="{$value}" />
</xsl:template>
<xsl:template name="GetPrimitiveType">
<xsl:choose>
<xsl:when test="type_name">
<xsl:value-of select="substring-after(type_name, '.')" />
</xsl:when>
<xsl:when test="type">
<xsl:call-template name="ConvertProtobufTypeToXsdType">
<xsl:with-param name="type" select="type"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="ConvertProtobufTypeToXsdType">
<xsl:param name="type"/>
<xsl:choose>
<xsl:when test="not($type)"></xsl:when>
<xsl:when test="$type='TYPE_DOUBLE'">xs:double</xsl:when>
<xsl:when test="$type='TYPE_FLOAT'">xs:float</xsl:when>
<xsl:when test="$type='TYPE_INT64'">xs:long</xsl:when>
<xsl:when test="$type='TYPE_UINT64'">xs:unsignedLong</xsl:when>
<xsl:when test="$type='TYPE_INT32'">xs:int</xsl:when>
<xsl:when test="$type='TYPE_UINT32'">xs:unsignedInt</xsl:when>
<xsl:when test="$type='TYPE_FIXED64'">xs:unsignedLong</xsl:when>
<xsl:when test="$type='TYPE_FIXED32'">xs:unsignedInt</xsl:when>
<xsl:when test="$type='TYPE_BOOL'">xs:boolean</xsl:when>
<xsl:when test="$type='TYPE_STRING'">xs:string</xsl:when>
<xsl:when test="$type='TYPE_BYTES'">xs:base64Binary</xsl:when>
<xsl:when test="$type='TYPE_UINT32'">xs:unsignedInt</xsl:when>
<xsl:when test="$type='TYPE_SFIXED32'">xs:int</xsl:when>
<xsl:when test="$type='TYPE_SFIXED64'">xs:long</xsl:when>
<xsl:when test="$type='TYPE_SINT32'">xs:int</xsl:when>
<xsl:when test="$type='TYPE_SINT64'">xs:long</xsl:when>
<xsl:when test="$type='TYPE_ENUM'">xs:string</xsl:when>
<xsl:when test="$type='TYPE_GROUP' or $type='TYPE_MESSAGE'"></xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">Field type not implemented: <xsl:value-of select="$type"/></xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="GetFullType">
<xsl:param name="name" />
<xsl:variable name="namespace">
<xsl:call-template name="GetFullTypeForContext">
<xsl:with-param name="context" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="normalize-space($namespace) != ''">
<xsl:value-of select="$namespace" />
<xsl:text>.</xsl:text>
</xsl:if>
<xsl:value-of select="$name"/>
</xsl:template>
<xsl:template name="GetFullTypeForContext">
<xsl:param name="context" />
<xsl:choose>
<xsl:when test="msxsl:node-set($context)/ancestor::DescriptorProto[1]">
<xsl:call-template name="GetFullTypeForContext">
<xsl:with-param name="context" select="$context/ancestor::DescriptorProto[1]"/>
</xsl:call-template>
<xsl:text>.</xsl:text>
<xsl:value-of select="msxsl:node-set($context)/ancestor::DescriptorProto[1]/name" />
</xsl:when>
<xsl:when test="msxsl:node-set($context)/ancestor::FileDescriptorProto/package">
<xsl:value-of select="msxsl:node-set($context)/ancestor::FileDescriptorProto/package" />
</xsl:when>
<xsl:when test="$defaultNamespace != ''">
<xsl:value-of select="$defaultNamespace"/>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="ServiceDescriptorProto">
</xsl:template>
<xsl:template match="MethodDescriptorProto">
</xsl:template>
<xsl:template match="MethodDescriptorProto" mode="protoRpc">
</xsl:template>
<xsl:template match="ServiceDescriptorProto" mode="clientProxy">
</xsl:template>
<xsl:template match="MethodDescriptorProto" mode="clientProxy">
</xsl:template>
</xsl:stylesheet>