ServiceXmlBuilder.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma warning disable CS1591
  2. using System.Collections.Generic;
  3. using System.Security;
  4. using System.Text;
  5. using Emby.Dlna.Common;
  6. namespace Emby.Dlna.Service
  7. {
  8. public class ServiceXmlBuilder
  9. {
  10. public string GetXml(IEnumerable<ServiceAction> actions, IEnumerable<StateVariable> stateVariables)
  11. {
  12. var builder = new StringBuilder();
  13. builder.Append("<?xml version=\"1.0\"?>");
  14. builder.Append("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">");
  15. builder.Append("<specVersion>");
  16. builder.Append("<major>1</major>");
  17. builder.Append("<minor>0</minor>");
  18. builder.Append("</specVersion>");
  19. AppendActionList(builder, actions);
  20. AppendServiceStateTable(builder, stateVariables);
  21. builder.Append("</scpd>");
  22. return builder.ToString();
  23. }
  24. private static void AppendActionList(StringBuilder builder, IEnumerable<ServiceAction> actions)
  25. {
  26. builder.Append("<actionList>");
  27. foreach (var item in actions)
  28. {
  29. builder.Append("<action>");
  30. builder.Append("<name>")
  31. .Append(SecurityElement.Escape(item.Name ?? string.Empty))
  32. .Append("</name>");
  33. builder.Append("<argumentList>");
  34. foreach (var argument in item.ArgumentList)
  35. {
  36. builder.Append("<argument>");
  37. builder.Append("<name>")
  38. .Append(SecurityElement.Escape(argument.Name ?? string.Empty))
  39. .Append("</name>");
  40. builder.Append("<direction>")
  41. .Append(SecurityElement.Escape(argument.Direction ?? string.Empty))
  42. .Append("</direction>");
  43. builder.Append("<relatedStateVariable>")
  44. .Append(SecurityElement.Escape(argument.RelatedStateVariable ?? string.Empty))
  45. .Append("</relatedStateVariable>");
  46. builder.Append("</argument>");
  47. }
  48. builder.Append("</argumentList>");
  49. builder.Append("</action>");
  50. }
  51. builder.Append("</actionList>");
  52. }
  53. private static void AppendServiceStateTable(StringBuilder builder, IEnumerable<StateVariable> stateVariables)
  54. {
  55. builder.Append("<serviceStateTable>");
  56. foreach (var item in stateVariables)
  57. {
  58. var sendEvents = item.SendsEvents ? "yes" : "no";
  59. builder.Append("<stateVariable sendEvents=\"")
  60. .Append(sendEvents)
  61. .Append("\">");
  62. builder.Append("<name>")
  63. .Append(SecurityElement.Escape(item.Name ?? string.Empty))
  64. .Append("</name>");
  65. builder.Append("<dataType>")
  66. .Append(SecurityElement.Escape(item.DataType ?? string.Empty))
  67. .Append("</dataType>");
  68. if (item.AllowedValues.Count > 0)
  69. {
  70. builder.Append("<allowedValueList>");
  71. foreach (var allowedValue in item.AllowedValues)
  72. {
  73. builder.Append("<allowedValue>")
  74. .Append(SecurityElement.Escape(allowedValue))
  75. .Append("</allowedValue>");
  76. }
  77. builder.Append("</allowedValueList>");
  78. }
  79. builder.Append("</stateVariable>");
  80. }
  81. builder.Append("</serviceStateTable>");
  82. }
  83. }
  84. }