ServiceXmlBuilder.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma warning disable CS1591
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Emby.Dlna.Common;
  5. using Emby.Dlna.Server;
  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>" + DescriptionXmlBuilder.Escape(item.Name ?? string.Empty) + "</name>");
  31. builder.Append("<argumentList>");
  32. foreach (var argument in item.ArgumentList)
  33. {
  34. builder.Append("<argument>");
  35. builder.Append("<name>" + DescriptionXmlBuilder.Escape(argument.Name ?? string.Empty) + "</name>");
  36. builder.Append("<direction>" + DescriptionXmlBuilder.Escape(argument.Direction ?? string.Empty) + "</direction>");
  37. builder.Append("<relatedStateVariable>" + DescriptionXmlBuilder.Escape(argument.RelatedStateVariable ?? string.Empty) + "</relatedStateVariable>");
  38. builder.Append("</argument>");
  39. }
  40. builder.Append("</argumentList>");
  41. builder.Append("</action>");
  42. }
  43. builder.Append("</actionList>");
  44. }
  45. private static void AppendServiceStateTable(StringBuilder builder, IEnumerable<StateVariable> stateVariables)
  46. {
  47. builder.Append("<serviceStateTable>");
  48. foreach (var item in stateVariables)
  49. {
  50. var sendEvents = item.SendsEvents ? "yes" : "no";
  51. builder.Append("<stateVariable sendEvents=\"" + sendEvents + "\">");
  52. builder.Append("<name>" + DescriptionXmlBuilder.Escape(item.Name ?? string.Empty) + "</name>");
  53. builder.Append("<dataType>" + DescriptionXmlBuilder.Escape(item.DataType ?? string.Empty) + "</dataType>");
  54. if (item.AllowedValues.Length > 0)
  55. {
  56. builder.Append("<allowedValueList>");
  57. foreach (var allowedValue in item.AllowedValues)
  58. {
  59. builder.Append("<allowedValue>" + DescriptionXmlBuilder.Escape(allowedValue) + "</allowedValue>");
  60. }
  61. builder.Append("</allowedValueList>");
  62. }
  63. builder.Append("</stateVariable>");
  64. }
  65. builder.Append("</serviceStateTable>");
  66. }
  67. }
  68. }