esdl2cpp_srvbasecpp.xslt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. ##############################################################################
  4. # HPCC SYSTEMS software Copyright (C) 2015 HPCC Systems.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. ##############################################################################
  18. -->
  19. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  20. <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
  21. <xsl:template match="esxdl">
  22. <xsl:variable name="servicename"><xsl:value-of select="EsdlService/@name"/></xsl:variable>
  23. <xsl:variable name="servicebase"><xsl:value-of select="$servicename"/>ServiceBase</xsl:variable>
  24. #include "<xsl:value-of select="$servicename"/>ServiceBase.hpp"
  25. #include "jliball.hpp"
  26. #include "jlog.hpp"
  27. #include "jptree.hpp"
  28. <xsl:apply-templates select="EsdlService"/>
  29. </xsl:template>
  30. <xsl:template name="outputCppPrimitive">
  31. <xsl:param name="typename"/>
  32. <xsl:choose>
  33. <xsl:when test="$typename='bool'"><xsl:value-of select="'Boolean'"/></xsl:when>
  34. <xsl:when test="$typename='boolean'"><xsl:value-of select="'Boolean'"/></xsl:when>
  35. <xsl:when test="$typename='decimal'"><xsl:value-of select="'Double'"/></xsl:when>
  36. <xsl:when test="$typename='float'"><xsl:value-of select="'Float'"/></xsl:when>
  37. <xsl:when test="$typename='double'"><xsl:value-of select="'Double'"/></xsl:when>
  38. <xsl:when test="$typename='integer'"><xsl:value-of select="'Integer'"/></xsl:when>
  39. <xsl:when test="$typename='int64'"><xsl:value-of select="'Integer64'"/></xsl:when>
  40. <xsl:when test="$typename='uint64'"><xsl:value-of select="'Integer64'"/></xsl:when>
  41. <xsl:when test="$typename='long'"><xsl:value-of select="'Integer'"/></xsl:when>
  42. <xsl:when test="$typename='int'"><xsl:value-of select="'Integer'"/></xsl:when>
  43. <xsl:when test="$typename='short'"><xsl:value-of select="'Integer'"/></xsl:when>
  44. <xsl:when test="$typename='nonPositiveInteger'"><xsl:value-of select="'Integer'"/></xsl:when>
  45. <xsl:when test="$typename='negativeInteger'"><xsl:value-of select="'Integer'"/></xsl:when>
  46. <xsl:when test="$typename='nonNegativeInteger'"><xsl:value-of select="'Integer'"/></xsl:when>
  47. <xsl:when test="$typename='unsigned'"><xsl:value-of select="'Integer'"/></xsl:when>
  48. <xsl:when test="$typename='unsignedLong'"><xsl:value-of select="'Integer'"/></xsl:when>
  49. <xsl:when test="$typename='unsignedInt'"><xsl:value-of select="'Integer'"/></xsl:when>
  50. <xsl:when test="$typename='unsignedShort'"><xsl:value-of select="'Integer'"/></xsl:when>
  51. <xsl:when test="$typename='unsignedByte'"><xsl:value-of select="'Integer'"/></xsl:when>
  52. <xsl:when test="$typename='positiveInteger'"><xsl:value-of select="'Integer'"/></xsl:when>
  53. <xsl:when test="$typename='base64Binary'"><xsl:value-of select="'PString'"/></xsl:when>
  54. <xsl:when test="$typename='string'"><xsl:value-of select="'PString'"/></xsl:when>
  55. <xsl:when test="$typename='xsdString'"><xsl:value-of select="'PString'"/></xsl:when>
  56. <xsl:when test="$typename='normalizedString'"><xsl:value-of select="'PString'"/></xsl:when>
  57. <xsl:when test="$typename='binary'"><xsl:value-of select="'PString'"/></xsl:when>
  58. <xsl:otherwise><xsl:value-of select="$typename"/></xsl:otherwise>
  59. </xsl:choose>
  60. </xsl:template>
  61. <xsl:template match="EsdlService">
  62. <xsl:variable name="servicename" select="@name"/>
  63. <xsl:variable name="servicebase"><xsl:value-of select="$servicename"/>ServiceBase</xsl:variable>
  64. <xsl:for-each select="EsdlMethod">
  65. <xsl:text>extern "C" int on</xsl:text><xsl:value-of select="../@name"/><xsl:value-of select="@name"/>(const char* CtxStr, const char* ReqStr, StringBuffer&amp; RespStr)
  66. {
  67. Owned&lt;<xsl:value-of select="$servicebase"/>&gt; service = create<xsl:value-of select="$servicename"/>ServiceObj();
  68. return service-&gt;<xsl:value-of select="@name"/><xsl:text>(CtxStr, ReqStr, RespStr);
  69. }
  70. </xsl:text>
  71. </xsl:for-each>
  72. <xsl:text>
  73. class </xsl:text><xsl:value-of select="$servicename"/>UnSerializer : public CInterface, implements IInterface
  74. {
  75. public:
  76. IMPLEMENT_IINTERFACE;
  77. <xsl:value-of select="$servicename"/>UnSerializer()
  78. {
  79. }
  80. int unserialize(EsdlContext* obj, IPropertyTree* ptree)
  81. {
  82. if (!obj || !ptree)
  83. return 0;
  84. if (ptree->hasProp("username"))
  85. obj->username.setown(new PString(ptree->queryProp("username")));
  86. if (ptree->hasProp("clientMajorVersion"))
  87. obj->clientMajorVersion.setown(new Integer(ptree->getPropInt("clientMajorVersion")));
  88. if (ptree->hasProp("clientMinorVersion"))
  89. obj->clientMinorVersion.setown(new Integer(ptree->getPropInt("clientMinorVersion")));
  90. }<xsl:text>
  91. </xsl:text>
  92. <xsl:for-each select="/esxdl/EsdlRequest|/esxdl/EsdlResponse|/esxdl/EsdlStruct">
  93. <xsl:call-template name="generateUnserializeForStruct"/>
  94. </xsl:for-each>
  95. <xsl:for-each select="/esxdl/EsdlRequest|/esxdl/EsdlResponse|/esxdl/EsdlStruct">
  96. <xsl:call-template name="generateSerializeForStruct"/>
  97. </xsl:for-each>
  98. <xsl:text>
  99. };
  100. </xsl:text>
  101. <xsl:for-each select="EsdlMethod">
  102. <xsl:text>int </xsl:text><xsl:value-of select="$servicebase"/>::<xsl:value-of select="@name"/><xsl:text>(const char* CtxStr, const char* ReqStr, StringBuffer&amp; RespStr)
  103. {</xsl:text>
  104. Owned&lt;<xsl:value-of select="$servicename"/>UnSerializer&gt; UnSe = new <xsl:value-of select="$servicename"/>UnSerializer();
  105. Owned&lt;EsdlContext&gt; ctx = new EsdlContext();
  106. if (CtxStr &amp;&amp; *CtxStr)
  107. {
  108. Owned&lt;IPropertyTree&gt; ptree = createPTreeFromXMLString(CtxStr);
  109. UnSe-&gt;unserialize(ctx.get(), ptree.get());
  110. }
  111. Owned&lt;<xsl:value-of select="$servicebase"/>&gt; service = create<xsl:value-of select="$servicename"/>ServiceObj();
  112. Owned&lt;<xsl:value-of select="@request_type"/>&gt; req = new <xsl:value-of select="@request_type"/>();
  113. Owned&lt;IPropertyTree&gt; ptree;
  114. ptree.setown(createPTreeFromXMLString(ReqStr));
  115. UnSe->unserialize(req.get(), ptree.get());
  116. Owned&lt;<xsl:value-of select="@response_type"/>&gt; resp = service-&gt;<xsl:value-of select="@name"/>(ctx.get(), req.get());
  117. RespStr = "&lt;Response&gt;&lt;Results&gt;&lt;Result&gt;&lt;Dataset name=\"<xsl:value-of select="@response_type"/>\">&lt;Row&gt;";
  118. UnSe->serialize(resp.get(), RespStr);
  119. RespStr.append("&lt;/Row&gt;&lt;/Dataset&gt;&lt;/Result&gt;&lt;/Results&gt;&lt;/Response&gt;");
  120. return 0;
  121. }<xsl:text>
  122. </xsl:text>
  123. </xsl:for-each>
  124. </xsl:template>
  125. <xsl:template name="generateUnserializeForStruct">
  126. int unserialize(<xsl:value-of select="@name"/>* obj, IPropertyTree* ptree)
  127. {
  128. if (!obj || !ptree)
  129. return 0;
  130. IPropertyTree* subtree = nullptr;<xsl:text>
  131. </xsl:text>
  132. <xsl:for-each select="*">
  133. <xsl:variable name="fieldclass" select="name(.)"/>
  134. <xsl:variable name="fieldname" select="@name"/>
  135. <xsl:variable name="fieldvar" select="concat('m_', $fieldname)"/>
  136. <xsl:choose>
  137. <xsl:when test="$fieldclass='EsdlElement'">
  138. <xsl:choose>
  139. <xsl:when test="@complex_type">
  140. subtree = ptree->queryPropTree("<xsl:value-of select="$fieldname"/>");
  141. if (subtree != nullptr)
  142. {
  143. obj-&gt;<xsl:value-of select="$fieldvar"/>.setown(new <xsl:value-of select="@complex_type"/>());
  144. unserialize(obj-&gt;<xsl:value-of select="$fieldvar"/>.get(), subtree);
  145. }<xsl:text>
  146. </xsl:text>
  147. </xsl:when>
  148. <xsl:otherwise>
  149. <xsl:variable name="primitive">
  150. <xsl:call-template name="outputCppPrimitive">
  151. <xsl:with-param name="typename">
  152. <xsl:value-of select="@type"/>
  153. </xsl:with-param>
  154. </xsl:call-template>
  155. </xsl:variable>
  156. <xsl:choose>
  157. <xsl:when test="$primitive='PString'">
  158. if (ptree-&gt;hasProp("<xsl:value-of select="$fieldname"/>"))
  159. obj-&gt;<xsl:value-of select="$fieldvar"/>.setown(new PString(ptree-&gt;queryProp("<xsl:value-of select="$fieldname"/>")));<xsl:text>
  160. </xsl:text>
  161. </xsl:when>
  162. <xsl:when test="$primitive='Integer'">
  163. if (ptree-&gt;hasProp("<xsl:value-of select="$fieldname"/>"))
  164. obj-&gt;<xsl:value-of select="$fieldvar"/>.setown(new Integer(ptree-&gt;getPropInt("<xsl:value-of select="$fieldname"/>")));<xsl:text>
  165. </xsl:text>
  166. </xsl:when>
  167. <xsl:when test="$primitive='Integer64'">
  168. if (ptree-&gt;hasProp("<xsl:value-of select="$fieldname"/>"))
  169. obj-&gt;<xsl:value-of select="$fieldvar"/>.setown(new Integer64(ptree-&gt;getPropInt64("<xsl:value-of select="$fieldname"/>")));<xsl:text>
  170. </xsl:text>
  171. </xsl:when>
  172. <xsl:when test="$primitive='Boolean'">
  173. if (ptree-&gt;hasProp("<xsl:value-of select="$fieldname"/>"))
  174. obj-&gt;<xsl:value-of select="$fieldvar"/>.setown(new Boolean(ptree-&gt;getPropBool("<xsl:value-of select="$fieldname"/>")));<xsl:text>
  175. </xsl:text>
  176. </xsl:when>
  177. <xsl:when test="$primitive='Float'">
  178. if (ptree-&gt;hasProp("<xsl:value-of select="$fieldname"/>"))
  179. obj-&gt;<xsl:value-of select="$fieldvar"/>.setown(new Float(atof(ptree-&gt;queryProp("<xsl:value-of select="$fieldname"/>"))));<xsl:text>
  180. </xsl:text>
  181. </xsl:when>
  182. <xsl:when test="$primitive='Double'">
  183. if (ptree-&gt;hasProp("<xsl:value-of select="$fieldname"/>"))
  184. obj-&gt;<xsl:value-of select="$fieldvar"/>.setown(new Double(atof(ptree-&gt;queryProp("<xsl:value-of select="$fieldname"/>"))));<xsl:text>
  185. </xsl:text>
  186. </xsl:when>
  187. <xsl:otherwise>
  188. </xsl:otherwise>
  189. </xsl:choose>
  190. </xsl:otherwise>
  191. </xsl:choose>
  192. </xsl:when>
  193. <xsl:when test="$fieldclass='EsdlArray'">
  194. subtree = ptree->queryPropTree("<xsl:value-of select="$fieldname"/>");
  195. if (subtree != nullptr)
  196. {
  197. Owned&lt;IPropertyTreeIterator&gt; itr = subtree->getElements("<xsl:value-of select="@item_tag"/>");
  198. ForEach (*itr)
  199. {
  200. IPropertyTree* onetree = &amp;itr->query();<xsl:text>
  201. </xsl:text>
  202. <xsl:variable name="primitive">
  203. <xsl:call-template name="outputCppPrimitive">
  204. <xsl:with-param name="typename">
  205. <xsl:value-of select="@type"/>
  206. </xsl:with-param>
  207. </xsl:call-template>
  208. </xsl:variable>
  209. <xsl:choose>
  210. <xsl:when test="$primitive=@type">
  211. Owned&lt;<xsl:value-of select="@type"/>&gt; oneobj(new <xsl:value-of select="@type"/>());
  212. unserialize(oneobj.get(), onetree);
  213. obj-&gt;<xsl:value-of select="$fieldvar"/>.append(*oneobj.getClear());<xsl:text>
  214. </xsl:text>
  215. </xsl:when>
  216. <xsl:when test="$primitive='PString'">
  217. obj-&gt;<xsl:value-of select="$fieldvar"/>.append(*(new PString(onetree->queryProp("."))));<xsl:text>
  218. </xsl:text>
  219. </xsl:when>
  220. <xsl:when test="$primitive='Integer'">
  221. obj-&gt;<xsl:value-of select="$fieldvar"/>.append(*(new Integer(onetree->getPropInt("."))));<xsl:text>
  222. </xsl:text>
  223. </xsl:when>
  224. <xsl:when test="$primitive='Integer64'">
  225. obj-&gt;<xsl:value-of select="$fieldvar"/>.append(*(new Integer64(onetree->getPropInt64("."))));<xsl:text>
  226. </xsl:text>
  227. </xsl:when>
  228. <xsl:when test="$primitive='Boolean'">
  229. obj-&gt;<xsl:value-of select="$fieldvar"/>.append(*(new Boolean(onetree->getPropBool("."))));<xsl:text>
  230. </xsl:text>
  231. </xsl:when>
  232. <xsl:when test="$primitive='Float'">
  233. obj-&gt;<xsl:value-of select="$fieldvar"/>.append(*(new Float(atof(onetree->queryProp(".")))));<xsl:text>
  234. </xsl:text>
  235. </xsl:when>
  236. <xsl:when test="$primitive='Double'">
  237. obj-&gt;<xsl:value-of select="$fieldvar"/>.append(*(new Double(atof(onetree->queryProp(".")))));<xsl:text>
  238. </xsl:text>
  239. </xsl:when>
  240. </xsl:choose>
  241. }<xsl:text>
  242. </xsl:text>
  243. }<xsl:text>
  244. </xsl:text>
  245. </xsl:when>
  246. <xsl:when test="$fieldclass='EsdlEnum'">
  247. if (ptree-&gt;hasProp("<xsl:value-of select="$fieldname"/>"))
  248. obj-&gt;<xsl:value-of select="$fieldvar"/> = EnumHandler<xsl:value-of select="@enum_type"/>::fromString(ptree-&gt;queryProp("<xsl:value-of select="$fieldname"/>"));<xsl:text>
  249. </xsl:text>
  250. </xsl:when>
  251. <xsl:otherwise>
  252. </xsl:otherwise>
  253. </xsl:choose>
  254. </xsl:for-each>
  255. return 0;
  256. }
  257. </xsl:template>
  258. <xsl:template name="generateSerializeForStruct">
  259. int serialize(<xsl:value-of select="@name"/>* obj, StringBuffer&amp; buf)
  260. {
  261. if (!obj)
  262. return 0;<xsl:text>
  263. </xsl:text>
  264. <xsl:for-each select="*">
  265. <xsl:variable name="fieldclass" select="name(.)"/>
  266. <xsl:variable name="fieldname" select="@name"/>
  267. <xsl:variable name="fieldvar" select="concat('m_', $fieldname)"/>
  268. <xsl:choose>
  269. <xsl:when test="$fieldclass='EsdlElement'">
  270. if (obj-&gt;<xsl:value-of select="$fieldvar"/>)
  271. {
  272. buf.append("&lt;<xsl:value-of select="$fieldname"/>&gt;");<xsl:text>
  273. </xsl:text>
  274. <xsl:choose>
  275. <xsl:when test="@complex_type">
  276. <xsl:text> </xsl:text>serialize(obj-&gt;<xsl:value-of select="$fieldvar"/>, buf);<xsl:text>
  277. </xsl:text>
  278. </xsl:when>
  279. <xsl:otherwise>
  280. <xsl:variable name="primitive">
  281. <xsl:call-template name="outputCppPrimitive">
  282. <xsl:with-param name="typename">
  283. <xsl:value-of select="@type"/>
  284. </xsl:with-param>
  285. </xsl:call-template>
  286. </xsl:variable>
  287. <xsl:text> </xsl:text>buf.append(obj-&gt;<xsl:value-of select="$fieldvar"/>-&gt;str());<xsl:text>
  288. </xsl:text>
  289. </xsl:otherwise>
  290. </xsl:choose>
  291. <xsl:text> </xsl:text>buf.append("&lt;/<xsl:value-of select="$fieldname"/>&gt;");<xsl:text>
  292. }
  293. </xsl:text>
  294. </xsl:when>
  295. <xsl:when test="$fieldclass='EsdlArray'">
  296. if (obj-&gt;<xsl:value-of select="$fieldvar"/>.length() > 0)
  297. {
  298. buf.append("&lt;<xsl:value-of select="$fieldname"/>&gt;");
  299. ForEachItemIn(i, obj-&gt;<xsl:value-of select="$fieldvar"/>)
  300. {<xsl:text>
  301. </xsl:text>
  302. <xsl:variable name="primitive">
  303. <xsl:call-template name="outputCppPrimitive">
  304. <xsl:with-param name="typename">
  305. <xsl:value-of select="@type"/>
  306. </xsl:with-param>
  307. </xsl:call-template>
  308. </xsl:variable>
  309. <xsl:choose>
  310. <xsl:when test="$primitive=@type">
  311. <xsl:text> </xsl:text><xsl:value-of select="@type"/>&amp; oneitem = obj-&gt;<xsl:value-of select="$fieldvar"/>.item(i);
  312. buf.append("&lt;<xsl:value-of select="@item_tag"/>&gt;");
  313. serialize(&amp;oneitem, buf);
  314. buf.append("&lt;/<xsl:value-of select="@item_tag"/>&gt;");<xsl:text>
  315. </xsl:text>
  316. </xsl:when>
  317. <xsl:otherwise>
  318. <xsl:text> </xsl:text><xsl:value-of select="$primitive"/>&amp; oneitem = obj-&gt;<xsl:value-of select="$fieldvar"/>.item(i);
  319. buf.append("&lt;<xsl:value-of select="@item_tag"/>&gt;").append(oneitem.str()).append("&lt;/<xsl:value-of select="@item_tag"/>&gt;");<xsl:text>
  320. </xsl:text>
  321. </xsl:otherwise>
  322. </xsl:choose>
  323. }
  324. buf.append("&lt;/<xsl:value-of select="$fieldname"/>&gt;");
  325. }<xsl:text>
  326. </xsl:text>
  327. </xsl:when>
  328. <xsl:when test="$fieldclass='EsdlEnum'">
  329. if (obj-&gt;<xsl:value-of select="$fieldvar"/> != <xsl:value-of select="@enum_type"/>::UNSET)
  330. buf.append("&lt;<xsl:value-of select="$fieldname"/>&gt;").append(EnumHandler<xsl:value-of select="@enum_type"/>::toString(obj-&gt;<xsl:value-of select="$fieldvar"/>)).append("&lt;/<xsl:value-of select="$fieldname"/>&gt;");<xsl:text>
  331. </xsl:text>
  332. </xsl:when>
  333. <xsl:otherwise>
  334. </xsl:otherwise>
  335. </xsl:choose>
  336. </xsl:for-each>
  337. return 0;
  338. }<xsl:text>
  339. </xsl:text>
  340. </xsl:template>
  341. </xsl:stylesheet>