DescriptionXmlBuilder.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using MediaBrowser.Dlna.Common;
  2. using MediaBrowser.Model.Dlna;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Security;
  7. using System.Text;
  8. namespace MediaBrowser.Dlna.Server
  9. {
  10. public class DescriptionXmlBuilder
  11. {
  12. private readonly DeviceProfile _profile;
  13. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  14. private readonly string _serverUdn;
  15. public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn)
  16. {
  17. if (string.IsNullOrWhiteSpace(serverUdn))
  18. {
  19. throw new ArgumentNullException("serverUdn");
  20. }
  21. _profile = profile;
  22. _serverUdn = serverUdn;
  23. }
  24. public string GetXml()
  25. {
  26. var builder = new StringBuilder();
  27. builder.Append("<?xml version=\"1.0\"?>");
  28. builder.Append("<root xmlns=\"urn:schemas-upnp-org:device-1-0\" xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\"");
  29. foreach (var att in _profile.XmlRootAttributes)
  30. {
  31. builder.AppendFormat(" {0}=\"{1}\"", att.Name, att.Value);
  32. }
  33. builder.Append(">");
  34. builder.Append("<specVersion>");
  35. builder.Append("<major>1</major>");
  36. builder.Append("<minor>0</minor>");
  37. builder.Append("</specVersion>");
  38. AppendDeviceInfo(builder);
  39. builder.Append("</root>");
  40. return builder.ToString();
  41. }
  42. private void AppendDeviceInfo(StringBuilder builder)
  43. {
  44. builder.Append("<device>");
  45. AppendDeviceProperties(builder);
  46. AppendIconList(builder);
  47. AppendServiceList(builder);
  48. builder.Append("</device>");
  49. }
  50. private void AppendDeviceProperties(StringBuilder builder)
  51. {
  52. builder.Append("<UDN>uuid:" + SecurityElement.Escape(_serverUdn) + "</UDN>");
  53. builder.Append("<dlna:X_DLNACAP>" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "</dlna:X_DLNACAP>");
  54. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + SecurityElement.Escape(_profile.XDlnaDoc ?? string.Empty) + "</dlna:X_DLNADOC>");
  55. builder.Append("<friendlyName>" + SecurityElement.Escape(_profile.FriendlyName ?? string.Empty) + "</friendlyName>");
  56. builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
  57. builder.Append("<manufacturer>" + SecurityElement.Escape(_profile.Manufacturer ?? string.Empty) + "</manufacturer>");
  58. builder.Append("<manufacturerURL>" + SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty) + "</manufacturerURL>");
  59. builder.Append("<modelName>" + SecurityElement.Escape(_profile.ModelName ?? string.Empty) + "</modelName>");
  60. builder.Append("<modelDescription>" + SecurityElement.Escape(_profile.ModelDescription ?? string.Empty) + "</modelDescription>");
  61. builder.Append("<modelNumber>" + SecurityElement.Escape(_profile.ModelNumber ?? string.Empty) + "</modelNumber>");
  62. builder.Append("<modelURL>" + SecurityElement.Escape(_profile.ModelUrl ?? string.Empty) + "</modelURL>");
  63. builder.Append("<serialNumber>" + SecurityElement.Escape(_profile.SerialNumber ?? string.Empty) + "</serialNumber>");
  64. if (!string.IsNullOrWhiteSpace(_profile.SonyAggregationFlags))
  65. {
  66. builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">" + SecurityElement.Escape(_profile.SonyAggregationFlags) + "</av:aggregationFlags>");
  67. }
  68. }
  69. private void AppendIconList(StringBuilder builder)
  70. {
  71. builder.Append("<iconList>");
  72. foreach (var icon in GetIcons())
  73. {
  74. builder.Append("<icon>");
  75. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  76. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  77. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  78. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  79. builder.Append("<url>" + SecurityElement.Escape(icon.Url ?? string.Empty) + "</url>");
  80. builder.Append("</icon>");
  81. }
  82. builder.Append("</iconList>");
  83. }
  84. private void AppendServiceList(StringBuilder builder)
  85. {
  86. builder.Append("<serviceList>");
  87. foreach (var service in GetServices())
  88. {
  89. builder.Append("<service>");
  90. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  91. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  92. builder.Append("<SCPDURL>" + SecurityElement.Escape(service.ScpdUrl ?? string.Empty) + "</SCPDURL>");
  93. builder.Append("<controlURL>" + SecurityElement.Escape(service.ControlUrl ?? string.Empty) + "</controlURL>");
  94. builder.Append("<eventSubURL>" + SecurityElement.Escape(service.EventSubUrl ?? string.Empty) + "</eventSubURL>");
  95. builder.Append("</service>");
  96. }
  97. builder.Append("</serviceList>");
  98. }
  99. private IEnumerable<DeviceIcon> GetIcons()
  100. {
  101. var list = new List<DeviceIcon>();
  102. list.Add(new DeviceIcon
  103. {
  104. MimeType = "image/png",
  105. Depth = "24",
  106. Width = 120,
  107. Height = 120,
  108. Url = "/mediabrowser/dlna/icons/logo120.png"
  109. });
  110. list.Add(new DeviceIcon
  111. {
  112. MimeType = "image/jpeg",
  113. Depth = "24",
  114. Width = 120,
  115. Height = 120,
  116. Url = "/mediabrowser/dlna/icons/logo120.jpg"
  117. });
  118. list.Add(new DeviceIcon
  119. {
  120. MimeType = "image/png",
  121. Depth = "24",
  122. Width = 48,
  123. Height = 48,
  124. Url = "/mediabrowser/dlna/icons/logo48.png"
  125. });
  126. list.Add(new DeviceIcon
  127. {
  128. MimeType = "image/jpeg",
  129. Depth = "24",
  130. Width = 48,
  131. Height = 48,
  132. Url = "/mediabrowser/dlna/icons/logo48.jpg"
  133. });
  134. return list;
  135. }
  136. private IEnumerable<DeviceService> GetServices()
  137. {
  138. var list = new List<DeviceService>();
  139. list.Add(new DeviceService
  140. {
  141. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  142. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  143. ScpdUrl = "/mediabrowser/dlna/contentdirectory/contentdirectory.xml",
  144. ControlUrl = "/mediabrowser/dlna/contentdirectory/" + _serverUdn + "/control",
  145. EventSubUrl = "/mediabrowser/dlna/contentdirectory/" + _serverUdn + "/events"
  146. });
  147. return list;
  148. }
  149. public override string ToString()
  150. {
  151. return GetXml();
  152. }
  153. }
  154. }