2
0

ContentDirectoryXmlBuilder.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using MediaBrowser.Dlna.Common;
  2. using MediaBrowser.Model.Dlna;
  3. using System.Collections.Generic;
  4. using System.Security;
  5. using System.Text;
  6. namespace MediaBrowser.Dlna.Server
  7. {
  8. public class ContentDirectoryXmlBuilder
  9. {
  10. private readonly DeviceProfile _profile;
  11. public ContentDirectoryXmlBuilder(DeviceProfile profile)
  12. {
  13. _profile = profile;
  14. }
  15. public string GetXml()
  16. {
  17. var builder = new StringBuilder();
  18. builder.Append("<?xml version=\"1.0\"?>");
  19. builder.Append("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">");
  20. builder.Append("<specVersion>");
  21. builder.Append("<major>1</major>");
  22. builder.Append("<minor>0</minor>");
  23. builder.Append("</specVersion>");
  24. AppendActionList(builder);
  25. AppendServiceStateTable(builder);
  26. builder.Append("</scpd>");
  27. return builder.ToString();
  28. }
  29. private void AppendActionList(StringBuilder builder)
  30. {
  31. builder.Append("<actionList>");
  32. foreach (var item in new ServiceActionListBuilder().GetActions())
  33. {
  34. builder.Append("<action>");
  35. builder.Append("<name>" + SecurityElement.Escape(item.Name ?? string.Empty) + "</name>");
  36. builder.Append("<argumentList>");
  37. foreach (var argument in item.ArgumentList)
  38. {
  39. builder.Append("<argument>");
  40. builder.Append("<name>" + SecurityElement.Escape(argument.Name ?? string.Empty) + "</name>");
  41. builder.Append("<direction>" + SecurityElement.Escape(argument.Direction ?? string.Empty) + "</direction>");
  42. builder.Append("<relatedStateVariable>" + SecurityElement.Escape(argument.RelatedStateVariable ?? string.Empty) + "</relatedStateVariable>");
  43. builder.Append("</argument>");
  44. }
  45. builder.Append("</argumentList>");
  46. builder.Append("</action>");
  47. }
  48. builder.Append("</actionList>");
  49. }
  50. private void AppendServiceStateTable(StringBuilder builder)
  51. {
  52. builder.Append("<serviceStateTable>");
  53. foreach (var item in GetStateVariables())
  54. {
  55. var sendEvents = item.SendsEvents ? "yes" : "no";
  56. builder.Append("<stateVariable sendEvents=\"" + sendEvents + "\">");
  57. builder.Append("<name>" + SecurityElement.Escape(item.Name ?? string.Empty) + "</name>");
  58. builder.Append("<dataType>" + SecurityElement.Escape(item.DataType ?? string.Empty) + "</dataType>");
  59. if (item.AllowedValues.Count > 0)
  60. {
  61. builder.Append("<allowedValueList>");
  62. foreach (var allowedValue in item.AllowedValues)
  63. {
  64. builder.Append("<allowedValue>" + SecurityElement.Escape(allowedValue) + "</allowedValue>");
  65. }
  66. builder.Append("</allowedValueList>");
  67. }
  68. builder.Append("</stateVariable>");
  69. }
  70. builder.Append("</serviceStateTable>");
  71. }
  72. private IEnumerable<StateVariable> GetStateVariables()
  73. {
  74. var list = new List<StateVariable>();
  75. list.Add(new StateVariable
  76. {
  77. Name = "A_ARG_TYPE_SortCriteria",
  78. DataType = "string",
  79. SendsEvents = false
  80. });
  81. list.Add(new StateVariable
  82. {
  83. Name = "A_ARG_TYPE_UpdateID",
  84. DataType = "ui4",
  85. SendsEvents = false
  86. });
  87. list.Add(new StateVariable
  88. {
  89. Name = "A_ARG_TYPE_SearchCriteria",
  90. DataType = "string",
  91. SendsEvents = false
  92. });
  93. list.Add(new StateVariable
  94. {
  95. Name = "A_ARG_TYPE_Filter",
  96. DataType = "string",
  97. SendsEvents = false
  98. });
  99. list.Add(new StateVariable
  100. {
  101. Name = "A_ARG_TYPE_Result",
  102. DataType = "string",
  103. SendsEvents = false
  104. });
  105. list.Add(new StateVariable
  106. {
  107. Name = "A_ARG_TYPE_Index",
  108. DataType = "ui4",
  109. SendsEvents = false
  110. });
  111. list.Add(new StateVariable
  112. {
  113. Name = "A_ARG_TYPE_ObjectID",
  114. DataType = "string",
  115. SendsEvents = false
  116. });
  117. list.Add(new StateVariable
  118. {
  119. Name = "SortCapabilities",
  120. DataType = "string",
  121. SendsEvents = false
  122. });
  123. list.Add(new StateVariable
  124. {
  125. Name = "SearchCapabilities",
  126. DataType = "string",
  127. SendsEvents = false
  128. });
  129. list.Add(new StateVariable
  130. {
  131. Name = "A_ARG_TYPE_Count",
  132. DataType = "ui4",
  133. SendsEvents = false
  134. });
  135. list.Add(new StateVariable
  136. {
  137. Name = "A_ARG_TYPE_BrowseFlag",
  138. DataType = "string",
  139. SendsEvents = false,
  140. AllowedValues = new List<string>
  141. {
  142. "BrowseMetadata",
  143. "BrowseDirectChildren"
  144. }
  145. });
  146. list.Add(new StateVariable
  147. {
  148. Name = "SystemUpdateID",
  149. DataType = "ui4",
  150. SendsEvents = true
  151. });
  152. list.Add(new StateVariable
  153. {
  154. Name = "A_ARG_TYPE_BrowseLetter",
  155. DataType = "string",
  156. SendsEvents = false
  157. });
  158. list.Add(new StateVariable
  159. {
  160. Name = "A_ARG_TYPE_CategoryType",
  161. DataType = "ui4",
  162. SendsEvents = false
  163. });
  164. list.Add(new StateVariable
  165. {
  166. Name = "A_ARG_TYPE_RID",
  167. DataType = "ui4",
  168. SendsEvents = false
  169. });
  170. list.Add(new StateVariable
  171. {
  172. Name = "A_ARG_TYPE_PosSec",
  173. DataType = "ui4",
  174. SendsEvents = false
  175. });
  176. list.Add(new StateVariable
  177. {
  178. Name = "A_ARG_TYPE_Featurelist",
  179. DataType = "string",
  180. SendsEvents = false
  181. });
  182. return list;
  183. }
  184. public override string ToString()
  185. {
  186. return GetXml();
  187. }
  188. }
  189. }