ServiceXmlBuilder.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Emby.Dlna.Common;
  2. using System.Collections.Generic;
  3. using System.Security;
  4. using System.Text;
  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 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 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.Count > 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. }