ServiceXmlBuilder.cs 3.2 KB

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