PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/package/app/app/generator/CSharpClientGenerator.php

https://bitbucket.org/pandaos/kaltura
PHP | 906 lines | 780 code | 98 blank | 28 comment | 113 complexity | 274a66aca9a13c32cda98fd2830172d0 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-2.0, LGPL-3.0, JSON, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. class CSharpClientGenerator extends ClientGeneratorFromXml
  3. {
  4. private $_doc = null;
  5. private $_csprojIncludes = array();
  6. private $_classInheritance = array();
  7. private $_enums = array();
  8. function CSharpClientGenerator($xmlPath)
  9. {
  10. parent::ClientGeneratorFromXml($xmlPath, realpath("sources/csharp"));
  11. $this->_doc = new DOMDocument();
  12. $this->_doc->load($this->_xmlFile);
  13. }
  14. function generate()
  15. {
  16. parent::generate();
  17. $this->removeFilesFromSource();
  18. $xpath = new DOMXPath($this->_doc);
  19. $this->loadClassInheritance($xpath->query("/xml/classes/class"));
  20. $this->loadEnums($xpath->query("/xml/enums/enum"));
  21. // enumes $ types
  22. $enumNodes = $xpath->query("/xml/enums/enum");
  23. foreach($enumNodes as $enumNode)
  24. {
  25. $this->writeEnum($enumNode);
  26. }
  27. $classNodes = $xpath->query("/xml/classes/class");
  28. foreach($classNodes as $classNode)
  29. {
  30. $this->writeClass($classNode);
  31. }
  32. $this->writeObjectFactoryClass($classNodes);
  33. $serviceNodes = $xpath->query("/xml/services/service");
  34. $this->startNewTextBlock();
  35. foreach($serviceNodes as $serviceNode)
  36. {
  37. $this->writeService($serviceNode);
  38. }
  39. $this->writeMainClient($serviceNodes);
  40. $this->writeCsproj();
  41. }
  42. function writeEnum(DOMElement $enumNode)
  43. {
  44. $enumName = $enumNode->getAttribute("name");
  45. $s = "";
  46. $s .= "namespace Kaltura"."\n";
  47. $s .= "{"."\n";
  48. if ($enumNode->getAttribute("enumType") == "string")
  49. {
  50. $s .= " public sealed class $enumName : KalturaStringEnum"."\n";
  51. $s .= " {"."\n";
  52. foreach($enumNode->childNodes as $constNode)
  53. {
  54. if ($constNode->nodeType != XML_ELEMENT_NODE)
  55. continue;
  56. $propertyName = $constNode->getAttribute("name");
  57. $propertyValue = $constNode->getAttribute("value");
  58. $s .= " public static readonly $enumName $propertyName = new $enumName(\"$propertyValue\");"."\n";
  59. }
  60. $s .= "\n";
  61. $s .= " private $enumName(string name) : base(name) { }"."\n";
  62. $s .= " }"."\n";
  63. $s .= "}"."\n";
  64. }
  65. else
  66. {
  67. $s .= " public enum $enumName"."\n";
  68. $s .= " {"."\n";
  69. foreach($enumNode->childNodes as $constNode)
  70. {
  71. if ($constNode->nodeType != XML_ELEMENT_NODE)
  72. continue;
  73. $propertyName = $constNode->getAttribute("name");
  74. $propertyValue = $constNode->getAttribute("value");
  75. $s .= " $propertyName = $propertyValue,"."\n";
  76. }
  77. $s .= " }"."\n";
  78. $s .= "}"."\n";
  79. }
  80. $file = "Enums/$enumName.cs";
  81. $this->addFile("KalturaClient/".$file, $s);
  82. $this->_csprojIncludes[] = $file;
  83. }
  84. function writeClass(DOMElement $classNode)
  85. {
  86. $this->startNewTextBlock();
  87. $this->appendLine("using System;");
  88. $this->appendLine("using System.Xml;");
  89. $this->appendLine("using System.Collections.Generic;");
  90. $this->appendLine();
  91. $this->appendLine("namespace Kaltura");
  92. $this->appendLine("{");
  93. $type = $classNode->getAttribute("name");
  94. // class definition
  95. if ($classNode->hasAttribute("base"))
  96. {
  97. $this->appendLine(" public class $type : ".$classNode->getAttribute("base"));
  98. }
  99. else
  100. {
  101. $this->appendLine(" public class $type : KalturaObjectBase");
  102. }
  103. $this->appendLine(" {");
  104. // we want to make the orderBy property strongly typed with the corresponding string enum
  105. $isFilter = false;
  106. if ($this->isClassInherit($type, "KalturaFilter"))
  107. {
  108. $orderByType = str_replace("Filter", "OrderBy", $type);
  109. if ($this->enumExists($orderByType))
  110. {
  111. $orderByElement = $classNode->ownerDocument->createElement("property");
  112. $orderByElement->setAttribute("name", "orderBy");
  113. $orderByElement->setAttribute("type", "string");
  114. $orderByElement->setAttribute("enumType", $orderByType);
  115. $classNode->appendChild($orderByElement);
  116. $isFilter = true;
  117. }
  118. }
  119. $properties = array();
  120. foreach($classNode->childNodes as $propertyNode)
  121. {
  122. if ($propertyNode->nodeType != XML_ELEMENT_NODE)
  123. continue;
  124. $property = array(
  125. "name" => null,
  126. "type" => null,
  127. "default" => null,
  128. "isNew" => false
  129. );
  130. $propType = $propertyNode->getAttribute("type");
  131. $propName = $propertyNode->getAttribute("name");
  132. $isEnum = $propertyNode->hasAttribute("enumType");
  133. $dotNetPropName = $this->upperCaseFirstLetter($propName);
  134. $property["name"] = $dotNetPropName;
  135. if ($isEnum)
  136. $dotNetPropType = $propertyNode->getAttribute("enumType");
  137. else if ($propType == "array")
  138. $dotNetPropType = "IList<".$propertyNode->getAttribute("arrayType").">";
  139. else if ($propType == "bool")
  140. $dotNetPropType = "bool?";
  141. else
  142. $dotNetPropType = $propType;
  143. $property["type"] = $dotNetPropType;
  144. if ($isFilter && $dotNetPropName == "OrderBy")
  145. $property["isNew"] = true;
  146. switch($propType)
  147. {
  148. case "int":
  149. if ($isEnum)
  150. $property["default"] = "($dotNetPropType)Int32.MinValue";
  151. else
  152. $property["default"] = "Int32.MinValue";
  153. break;
  154. case "string":
  155. $property["default"] = "null";
  156. break;
  157. case "bool":
  158. $property["default"] = "false";
  159. break;
  160. case "float":
  161. $property["default"] = "Single.MinValue";
  162. break;
  163. }
  164. $properties[] = $property;
  165. }
  166. // private fields
  167. $this->appendLine(" #region Private Fields");
  168. foreach($properties as $property)
  169. {
  170. $propertyLine = "private";
  171. $propertyLine .= " {$property['type']} _{$property['name']}";
  172. if (!is_null($property["default"]))
  173. $propertyLine .= " = {$property['default']}";
  174. $propertyLine .= ";";
  175. $this->appendLine(" " . $propertyLine);
  176. }
  177. $this->appendLine(" #endregion");
  178. $this->appendLine();
  179. // properties
  180. $this->appendLine(" #region Properties");
  181. foreach($properties as $property)
  182. {
  183. $propertyLine = "public";
  184. if ($property['isNew'])
  185. $propertyLine .= " new";
  186. $propertyLine .= " {$property['type']} {$property['name']}";
  187. $this->appendLine(" " . $propertyLine);
  188. $this->appendLine(" {");
  189. $this->appendLine(" get { return _{$property['name']}; }");
  190. $this->appendLine(" set ");
  191. $this->appendLine(" { ");
  192. $this->appendLine(" _{$property['name']} = value;");
  193. $this->appendLine(" OnPropertyChanged(\"{$property['name']}\");");
  194. $this->appendLine(" }");
  195. $this->appendLine(" }");
  196. }
  197. $this->appendLine(" #endregion");
  198. $this->appendLine();
  199. $this->appendLine(" #region CTor");
  200. // CTor
  201. $this->appendLine(" public $type()");
  202. $this->appendLine(" {");
  203. $this->appendLine(" }");
  204. $this->appendLine("");
  205. // CTor For XmlElement
  206. if ($classNode->hasAttribute("base"))
  207. {
  208. $this->appendLine(" public $type(XmlElement node) : base(node)");
  209. }
  210. else
  211. {
  212. $this->appendLine(" public $type(XmlElement node)");
  213. }
  214. $this->appendLine(" {");
  215. if ($classNode->childNodes->length)
  216. {
  217. $this->appendLine(" foreach (XmlElement propertyNode in node.ChildNodes)");
  218. $this->appendLine(" {");
  219. $this->appendLine(" string txt = propertyNode.InnerText;");
  220. $this->appendLine(" switch (propertyNode.Name)");
  221. $this->appendLine(" {");
  222. foreach($classNode->childNodes as $propertyNode)
  223. {
  224. if ($propertyNode->nodeType != XML_ELEMENT_NODE)
  225. continue;
  226. $propType = $propertyNode->getAttribute("type");
  227. $propName = $propertyNode->getAttribute("name");
  228. $isEnum = $propertyNode->hasAttribute("enumType");
  229. $dotNetPropName = $this->upperCaseFirstLetter($propName);
  230. $this->appendLine(" case \"$propName\":");
  231. switch($propType)
  232. {
  233. case "int":
  234. if ($isEnum)
  235. {
  236. $enumType = $propertyNode->getAttribute("enumType");
  237. $this->appendLine(" this.$dotNetPropName = ($enumType)ParseEnum(typeof($enumType), txt);");
  238. }
  239. else
  240. $this->appendLine(" this.$dotNetPropName = ParseInt(txt);");
  241. break;
  242. case "string":
  243. if ($isEnum)
  244. {
  245. $enumType = $propertyNode->getAttribute("enumType");
  246. $this->appendLine(" this.$dotNetPropName = ($enumType)KalturaStringEnum.Parse(typeof($enumType), txt);");
  247. }
  248. else
  249. $this->appendLine(" this.$dotNetPropName = txt;");
  250. break;
  251. case "bool":
  252. $this->appendLine(" this.$dotNetPropName = ParseBool(txt);");
  253. break;
  254. case "float":
  255. $this->appendLine(" this.$dotNetPropName = ParseFloat(txt);");
  256. break;
  257. case "array":
  258. $arrayType = $propertyNode->getAttribute("arrayType");
  259. $this->appendLine(" this.$dotNetPropName = new List<$arrayType>();");
  260. $this->appendLine(" foreach(XmlElement arrayNode in propertyNode.ChildNodes)");
  261. $this->appendLine(" {");
  262. $this->appendLine(" this.$dotNetPropName.Add(($arrayType)KalturaObjectFactory.Create(arrayNode));");
  263. $this->appendLine(" }");
  264. break;
  265. default: // sub object
  266. $this->appendLine(" this.$dotNetPropName = ($propType)KalturaObjectFactory.Create(propertyNode);");
  267. break;
  268. }
  269. $this->appendLine(" continue;");
  270. }
  271. $this->appendLine(" }");
  272. $this->appendLine(" }");
  273. }
  274. $this->appendLine(" }");
  275. $this->appendLine(" #endregion");
  276. $this->appendLine("");
  277. $this->appendLine(" #region Methods");
  278. // ToParams method
  279. $this->appendLine(" public override KalturaParams ToParams()");
  280. $this->appendLine(" {");
  281. $this->appendLine(" KalturaParams kparams = base.ToParams();");
  282. foreach($classNode->childNodes as $propertyNode)
  283. {
  284. if ($propertyNode->nodeType != XML_ELEMENT_NODE)
  285. continue;
  286. $propType = $propertyNode->getAttribute("type");
  287. $propName = $propertyNode->getAttribute("name");
  288. $isEnum = $propertyNode->hasAttribute("enumType");
  289. $dotNetPropName = $this->upperCaseFirstLetter($propName);
  290. switch($propType)
  291. {
  292. case "int":
  293. if ($isEnum)
  294. $this->appendLine(" kparams.AddEnumIfNotNull(\"$propName\", this.$dotNetPropName);");
  295. else
  296. $this->appendLine(" kparams.AddIntIfNotNull(\"$propName\", this.$dotNetPropName);");
  297. break;
  298. case "string":
  299. if ($isEnum)
  300. $this->appendLine(" kparams.AddStringEnumIfNotNull(\"$propName\", this.$dotNetPropName);");
  301. else
  302. $this->appendLine(" kparams.AddStringIfNotNull(\"$propName\", this.$dotNetPropName);");
  303. break;
  304. case "bool":
  305. $this->appendLine(" kparams.AddBoolIfNotNull(\"$propName\", this.$dotNetPropName);");
  306. break;
  307. case "float":
  308. $this->appendLine(" kparams.AddFloatIfNotNull(\"$propName\", this.$dotNetPropName);");
  309. break;
  310. case "enum":
  311. $this->appendLine(" kparams.AddEnumIfNotNull(\"$propName\", this.$dotNetPropName);");
  312. break;
  313. case "array":
  314. $arrayType = $propertyNode->getAttribute("arrayType");
  315. $this->appendLine(" if (this.$dotNetPropName != null)");
  316. $this->appendLine(" {");
  317. $this->appendLine(" if (this.$dotNetPropName.Count == 0)");
  318. $this->appendLine(" {");
  319. $this->appendLine(" kparams.Add(\"".$propName.":-\", \"\");");
  320. $this->appendLine(" }");
  321. $this->appendLine(" else");
  322. $this->appendLine(" {");
  323. $this->appendLine(" int i = 0;");
  324. $this->appendLine(" foreach ($arrayType item in this.$dotNetPropName)");
  325. $this->appendLine(" {");
  326. $this->appendLine(" kparams.Add(\"".$propName.":\" + i + \":objectType\", item.GetType().Name);");
  327. $this->appendLine(" kparams.Add(\"".$propName.":\" + i, item.ToParams());");
  328. $this->appendLine(" i++;");
  329. $this->appendLine(" }");
  330. $this->appendLine(" }");
  331. $this->appendLine(" }");
  332. break;
  333. default: // for objects
  334. $this->appendLine(" if (this.$dotNetPropName != null)");
  335. $this->appendLine(" kparams.Add(\"$propName\", this.$dotNetPropName.ToParams());");
  336. break;
  337. }
  338. }
  339. $this->appendLine(" return kparams;");
  340. $this->appendLine(" }");
  341. $this->appendLine(" #endregion");
  342. // close class
  343. $this->appendLine(" }");
  344. $this->appendLine("}");
  345. $this->appendLine();
  346. $file = "Types/$type.cs";
  347. $this->addFile("KalturaClient/".$file, $this->getTextBlock());
  348. $this->_csprojIncludes[] = $file;
  349. }
  350. function writeObjectFactoryClass(DOMNodeList $classNodes)
  351. {
  352. $this->startNewTextBlock();
  353. $this->appendLine("using System;");
  354. $this->appendLine("using System.Text;");
  355. $this->appendLine("using System.Xml;");
  356. $this->appendLine("using System.Runtime.Serialization;");
  357. $this->appendLine();
  358. $this->appendLine("namespace Kaltura");
  359. $this->appendLine("{");
  360. $this->appendLine(" public static class KalturaObjectFactory");
  361. $this->appendLine(" {");
  362. $this->appendLine(" public static object Create(XmlElement xmlElement)");
  363. $this->appendLine(" {");
  364. $this->appendLine(" if (xmlElement[\"objectType\"] == null)");
  365. $this->appendLine(" {");
  366. $this->appendLine(" return null;");
  367. $this->appendLine(" }");
  368. $this->appendLine(" switch (xmlElement[\"objectType\"].InnerText)");
  369. $this->appendLine(" {");
  370. foreach($classNodes as $classNode)
  371. {
  372. $this->appendLine(" case \"".$classNode->getAttribute("name")."\":");
  373. $this->appendLine(" return new ".$classNode->getAttribute("name")."(xmlElement);");
  374. }
  375. $this->appendLine(" }");
  376. $this->appendLine(" throw new SerializationException(\"Invalid object type\");");
  377. $this->appendLine(" }");
  378. $this->appendLine(" }");
  379. $this->appendLine("}");
  380. $file = "KalturaObjectFactory.cs";
  381. $this->addFile("KalturaClient/".$file, $this->getTextBlock());
  382. $this->_csprojIncludes[] = $file;
  383. }
  384. function writeCsproj()
  385. {
  386. $csprojDoc = new DOMDocument();
  387. $csprojDoc->formatOutput = true;
  388. $csprojDoc->load($this->_sourcePath."/KalturaClient/KalturaClient.csproj");
  389. $csprojXPath = new DOMXPath($csprojDoc);
  390. $csprojXPath->registerNamespace("m", "http://schemas.microsoft.com/developer/msbuild/2003");
  391. $compileNodes = $csprojXPath->query("//m:ItemGroup/m:Compile/..");
  392. $compileItemGroupElement = $compileNodes->item(0);
  393. foreach($this->_csprojIncludes as $include)
  394. {
  395. $compileElement = $csprojDoc->createElement("Compile");
  396. $compileElement->setAttribute("Include", str_replace("/","\\", $include));
  397. $compileItemGroupElement->appendChild($compileElement);
  398. }
  399. $this->addFile("KalturaClient/KalturaClient.csproj", $csprojDoc->saveXML());
  400. }
  401. function writeService(DOMElement $serviceNode)
  402. {
  403. $this->startNewTextBlock();
  404. $this->appendLine("using System;");
  405. $this->appendLine("using System.Xml;");
  406. $this->appendLine("using System.Collections.Generic;");
  407. $this->appendLine("using System.IO;");
  408. $this->appendLine();
  409. $this->appendLine("namespace Kaltura");
  410. $this->appendLine("{");
  411. $serviceName = $serviceNode->getAttribute("name");
  412. $serviceId = $serviceNode->getAttribute("id");
  413. $dotNetServiceName = $this->upperCaseFirstLetter($serviceName)."Service";
  414. $dotNetServiceType = "Kaltura" . $dotNetServiceName;
  415. $this->appendLine();
  416. $this->appendLine(" public class $dotNetServiceType : KalturaServiceBase");
  417. $this->appendLine(" {");
  418. $this->appendLine(" public $dotNetServiceType(KalturaClient client)");
  419. $this->appendLine(" : base(client)");
  420. $this->appendLine(" {");
  421. $this->appendLine(" }");
  422. $actionNodes = $serviceNode->childNodes;
  423. foreach($actionNodes as $actionNode)
  424. {
  425. if ($actionNode->nodeType != XML_ELEMENT_NODE)
  426. continue;
  427. $this->writeAction($serviceId, $actionNode);
  428. }
  429. $this->appendLine(" }");
  430. $this->appendLine("}");
  431. $file = "Services/".$dotNetServiceName.".cs";
  432. $this->addFile("KalturaClient/".$file, $this->getTextBlock());
  433. $this->_csprojIncludes[] = $file;
  434. }
  435. function writeAction($serviceId, DOMElement $actionNode)
  436. {
  437. $action = $actionNode->getAttribute("name");
  438. $resultNode = $actionNode->getElementsByTagName("result")->item(0);
  439. $resultType = $resultNode->getAttribute("type");
  440. if($resultType == 'file')
  441. return;
  442. switch($resultType)
  443. {
  444. case null:
  445. $dotNetOutputType = "void";
  446. break;
  447. case "array":
  448. $arrayType = $resultNode->getAttribute("arrayType");
  449. $dotNetOutputType = "IList<".$arrayType.">";
  450. break;
  451. default:
  452. $dotNetOutputType = $resultType;
  453. break;
  454. }
  455. $signaturePrefix = "public $dotNetOutputType ".$this->upperCaseFirstLetter($action)."(";
  456. $paramNodes = $actionNode->getElementsByTagName("param");
  457. // check for needed overloads
  458. $mandatoryParams = array();
  459. $optionalParams = array();
  460. foreach($paramNodes as $paramNode)
  461. {
  462. $optional = $paramNode->getAttribute("optional");
  463. if ($optional == "1")
  464. $optionalParams[] = $paramNode;
  465. else
  466. $mandatoryParams[] = $paramNode;
  467. }
  468. for($overloadNumber = 0; $overloadNumber < count($optionalParams); $overloadNumber++)
  469. {
  470. $currentOptionalParams = array_slice($optionalParams, 0, $overloadNumber);
  471. $defaultParams = array_slice(array_merge($mandatoryParams, $optionalParams), 0, count($mandatoryParams) + $overloadNumber + 1);
  472. $signature = $this->getSignature(array_merge($mandatoryParams, $currentOptionalParams));
  473. // write the overload
  474. $this->appendLine();
  475. $this->appendLine(" $signaturePrefix$signature");
  476. $this->appendLine(" {");
  477. $paramsStr = "";
  478. foreach($defaultParams as $paramNode)
  479. {
  480. $optional = $paramNode->getAttribute("optional");
  481. if ($optional == "1" && ! in_array ( $paramNode, $currentOptionalParams, true ))
  482. {
  483. $type = $paramNode->getAttribute("type");
  484. if ($type == "string")
  485. {
  486. $value = $paramNode->getAttribute("default");
  487. if($value == 'null')
  488. $paramsStr .= "null";
  489. else
  490. $paramsStr .= "\"".$paramNode->getAttribute("default")."\"";
  491. }
  492. else if ($type == "int" && $paramNode->hasAttribute("enumType"))
  493. {
  494. $value = $paramNode->getAttribute("default");
  495. if ($value == "null")
  496. $value = "Int32.MinValue";
  497. $paramsStr .= "(".$paramNode->getAttribute("enumType").")(".$value.")";
  498. }
  499. elseif ($type == "int" && $paramNode->getAttribute("default") == "null") // because Partner.GetUsage has an int field with empty default value
  500. $paramsStr .= "Int32.MinValue";
  501. else
  502. $paramsStr .= $paramNode->getAttribute("default");
  503. }
  504. else
  505. {
  506. $paramName = $paramNode->getAttribute("name");
  507. $paramsStr .= $this->fixParamName($paramName);
  508. }
  509. $paramsStr .= ", ";
  510. }
  511. if ($this->endsWith($paramsStr, ", "))
  512. $paramsStr = substr($paramsStr, 0, strlen($paramsStr) - 2);
  513. if($resultType)
  514. $this->appendLine(" return this.".$this->upperCaseFirstLetter($action)."($paramsStr);");
  515. else
  516. $this->appendLine(" this.".$this->upperCaseFirstLetter($action)."($paramsStr);");
  517. $this->appendLine(" }");
  518. }
  519. $signature = $this->getSignature(array_merge($mandatoryParams, $optionalParams));
  520. $this->appendLine();
  521. $this->appendLine(" $signaturePrefix$signature");
  522. $this->appendLine(" {");
  523. $this->appendLine(" KalturaParams kparams = new KalturaParams();");
  524. $haveFiles = false;
  525. foreach($paramNodes as $paramNode)
  526. {
  527. $paramType = $paramNode->getAttribute("type");
  528. $paramName = $paramNode->getAttribute("name");
  529. $isEnum = $paramNode->hasAttribute("enumType");
  530. if ($haveFiles === false && $paramType === "file")
  531. {
  532. $haveFiles = true;
  533. $this->appendLine(" KalturaFiles kfiles = new KalturaFiles();");
  534. }
  535. switch ($paramType)
  536. {
  537. case "string":
  538. if ($isEnum)
  539. $this->appendLine(" kparams.AddStringEnumIfNotNull(\"$paramName\", ".$this->fixParamName($paramName).");");
  540. else
  541. $this->appendLine(" kparams.AddStringIfNotNull(\"$paramName\", ".$this->fixParamName($paramName).");");
  542. break;
  543. case "float":
  544. $this->appendLine(" kparams.AddFloatIfNotNull(\"$paramName\", ".$this->fixParamName($paramName).");");
  545. break;
  546. case "int":
  547. if ($isEnum)
  548. $this->appendLine(" kparams.AddEnumIfNotNull(\"$paramName\", ".$this->fixParamName($paramName).");");
  549. else
  550. $this->appendLine(" kparams.AddIntIfNotNull(\"$paramName\", ".$this->fixParamName($paramName).");");
  551. break;
  552. case "bool":
  553. $this->appendLine(" kparams.AddBoolIfNotNull(\"$paramName\", ".$this->fixParamName($paramName).");");
  554. break;
  555. case "array":
  556. $this->appendLine(" foreach(".$paramNode->getAttribute("arrayType")." obj in ".$this->fixParamName($paramName).")");
  557. $this->appendLine(" {");
  558. $this->appendLine(" kparams.Add(\"$paramName\", obj.ToParams());");
  559. $this->appendLine(" }");
  560. break;
  561. case "file":
  562. $this->appendLine(" kfiles.Add(\"$paramName\", ".$this->fixParamName($paramName).");");
  563. break;
  564. default: // for objects
  565. $this->appendLine(" if (".$this->fixParamName($paramName)." != null)");
  566. $this->appendLine(" kparams.Add(\"$paramName\", ".$this->fixParamName($paramName).".ToParams());");
  567. break;
  568. }
  569. }
  570. if ($haveFiles)
  571. $this->appendLine(" _Client.QueueServiceCall(\"$serviceId\", \"$action\", kparams, kfiles);");
  572. else
  573. $this->appendLine(" _Client.QueueServiceCall(\"$serviceId\", \"$action\", kparams);");
  574. $this->appendLine(" if (this._Client.IsMultiRequest)");
  575. if (!$resultType)
  576. $this->appendLine(" return;");
  577. else if ($resultType == "int" || $resultNode == "float")
  578. $this->appendLine(" return 0;");
  579. else if ($resultType == "bool")
  580. $this->appendLine(" return false;");
  581. else
  582. $this->appendLine(" return null;");
  583. $this->appendLine(" XmlElement result = _Client.DoQueue();");
  584. if ($resultType)
  585. {
  586. switch ($resultType)
  587. {
  588. case "array":
  589. $arrayType = $resultNode->getAttribute("arrayType");
  590. $this->appendLine(" IList<$arrayType> list = new List<$arrayType>();");
  591. $this->appendLine(" foreach(XmlElement node in result.ChildNodes)");
  592. $this->appendLine(" {");
  593. $this->appendLine(" list.Add(($arrayType)KalturaObjectFactory.Create(node));");
  594. $this->appendLine(" }");
  595. $this->appendLine(" return list;");
  596. break;
  597. case "int":
  598. $this->appendLine(" return int.Parse(result.InnerText);");
  599. break;
  600. case "float":
  601. $this->appendLine(" return Single.Parse(result.InnerText);");
  602. break;
  603. case "bool":
  604. $this->appendLine(" if (result.InnerText == \"1\")");
  605. $this->appendLine(" return true;");
  606. $this->appendLine(" return false;");
  607. break;
  608. case "string":
  609. $this->appendLine(" return result.InnerText;");
  610. break;
  611. default:
  612. $this->appendLine(" return ($resultType)KalturaObjectFactory.Create(result);");
  613. break;
  614. }
  615. }
  616. $this->appendLine(" }");
  617. }
  618. function getSignature($paramNodes)
  619. {
  620. $signature = "";
  621. foreach($paramNodes as $paramNode)
  622. {
  623. $paramType = $paramNode->getAttribute("type");
  624. $paramName = $paramNode->getAttribute("name");
  625. $isEnum = $paramNode->hasAttribute("enumType");
  626. switch($paramType)
  627. {
  628. case "array":
  629. $dotNetType = "IList<".$paramNode->getAttribute("arrayType").">";
  630. break;
  631. case "file":
  632. $dotNetType = "FileStream";
  633. break;
  634. case "int":
  635. if ($isEnum)
  636. $dotNetType = $paramNode->getAttribute("enumType");
  637. else
  638. $dotNetType = $paramType;
  639. break;
  640. default:
  641. if ($isEnum)
  642. $dotNetType = $paramNode->getAttribute("enumType");
  643. else
  644. $dotNetType = $paramType;
  645. break;
  646. }
  647. $signature .= "$dotNetType ".$this->fixParamName($paramName).", ";
  648. }
  649. if ($this->endsWith($signature, ", "))
  650. $signature = substr($signature, 0, strlen($signature) - 2);
  651. $signature .= ")";
  652. return $signature;
  653. }
  654. function writeMainClient(DOMNodeList $serviceNodes)
  655. {
  656. $apiVersion = $this->_doc->documentElement->getAttribute('apiVersion');
  657. $this->startNewTextBlock();
  658. $this->appendLine("using System;");
  659. $this->appendLine();
  660. $this->appendLine("namespace Kaltura");
  661. $this->appendLine("{");
  662. $this->appendLine(" public class KalturaClient : KalturaClientBase");
  663. $this->appendLine(" {");
  664. $this->appendLine(" public KalturaClient(KalturaConfiguration config)");
  665. $this->appendLine(" : base(config)");
  666. $this->appendLine(" {");
  667. $this->appendLine(" _ApiVersion = \"$apiVersion\";");
  668. $this->appendLine(" }");
  669. foreach($serviceNodes as $serviceNode)
  670. {
  671. $serviceName = $serviceNode->getAttribute("name");
  672. $dotNetServiceName = $this->upperCaseFirstLetter($serviceName)."Service";
  673. $dotNetServiceType = "Kaltura" . $dotNetServiceName;
  674. $this->appendLine();
  675. $this->appendLine(" $dotNetServiceType _$dotNetServiceName;");
  676. $this->appendLine(" public $dotNetServiceType $dotNetServiceName");
  677. $this->appendLine(" {");
  678. $this->appendLine(" get");
  679. $this->appendLine(" {");
  680. $this->appendLine(" if (_$dotNetServiceName == null)");
  681. $this->appendLine(" _$dotNetServiceName = new $dotNetServiceType(this);");
  682. $this->appendLine("");
  683. $this->appendLine(" return _$dotNetServiceName;");
  684. $this->appendLine(" }");
  685. $this->appendLine(" }");
  686. }
  687. $this->appendLine(" }");
  688. $this->appendLine("}");
  689. $this->addFile("KalturaClient/KalturaClient.cs", $this->getTextBlock());
  690. // not needed because it is included in the sources
  691. //$this->_csprojIncludes[] = "KalturaClient.cs";
  692. }
  693. private function loadEnums(DOMNodeList $enums)
  694. {
  695. foreach($enums as $item)
  696. {
  697. $this->_enums[$item->getAttribute("name")] = null;
  698. }
  699. }
  700. private function loadClassInheritance(DOMNodeList $classes)
  701. {
  702. // first fill the base classes
  703. foreach($classes as $item)
  704. {
  705. $class = $item->getAttribute("name");
  706. if (!$item->hasAttribute("base"))
  707. {
  708. $this->_classInheritance[$class] = array();
  709. }
  710. }
  711. // now fill recursively the childs
  712. foreach($this->_classInheritance as $class => $null)
  713. {
  714. $this->loadChildsForInheritance($classes, $class, $this->_classInheritance);
  715. }
  716. }
  717. private function loadChildsForInheritance(DOMNodeList $classes, $baseClass, array &$baseClassChilds)
  718. {
  719. $baseClassChilds[$baseClass] = $this->getChildsForParentClass($classes, $baseClass);
  720. foreach($baseClassChilds[$baseClass] as $childClass => $null)
  721. {
  722. $this->loadChildsForInheritance($classes, $childClass, $baseClassChilds[$baseClass]);
  723. }
  724. }
  725. private function getChildsForParentClass(DOMNodeList $classes, $parentClass)
  726. {
  727. $childs = array();
  728. foreach($classes as $item2)
  729. {
  730. $currentParentClass = $item2->getAttribute("base");
  731. $class = $item2->getAttribute("name");
  732. if ($currentParentClass === $parentClass)
  733. {
  734. $childs[$class] = array();
  735. }
  736. }
  737. return $childs;
  738. }
  739. private function isClassInherit($class, $baseClass)
  740. {
  741. $classTree = $this->getClassChildsTree($this->_classInheritance, $baseClass);
  742. if (is_null($classTree))
  743. return false;
  744. else
  745. {
  746. if (is_null($this->getClassChildsTree($classTree, $class)))
  747. return false;
  748. else
  749. return true;
  750. }
  751. }
  752. /**
  753. * Finds the class in the multidimensional array and returns a multidimensional array with its child classes
  754. * Null if not found
  755. *
  756. * @param array $classes
  757. * @param string $class
  758. */
  759. private function getClassChildsTree(array $classes, $class)
  760. {
  761. foreach($classes as $tempClass => $null)
  762. {
  763. if ($class === $tempClass)
  764. {
  765. return $classes[$class];
  766. }
  767. else
  768. {
  769. $subArray = $this->getClassChildsTree($classes[$tempClass], $class);
  770. if (!is_null($subArray))
  771. return $subArray;
  772. }
  773. }
  774. return null;
  775. }
  776. private function enumExists($enum)
  777. {
  778. return array_key_exists($enum, $this->_enums);
  779. }
  780. private function removeFilesFromSource()
  781. {
  782. $files = array_keys($this->_files);
  783. foreach($files as $file)
  784. {
  785. if ($file == "KalturaClient.suo")
  786. unset($this->_files["KalturaClient.suo"]);
  787. $dirname = pathinfo($file, PATHINFO_DIRNAME);
  788. if ($this->endsWith($dirname, "Debug"))
  789. unset($this->_files[$file]);
  790. if ($this->endsWith($dirname, "Release"))
  791. unset($this->_files[$file]);
  792. }
  793. }
  794. /**
  795. * Fix .net reserved words
  796. *
  797. * @param string $param
  798. * @return string
  799. */
  800. private function fixParamName($param)
  801. {
  802. if ($param == "event")
  803. return "kevent";
  804. else
  805. return $param;
  806. }
  807. }
  808. ?>