DescriptionXmlBuilder.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. private readonly string _serverAddress;
  16. public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress)
  17. {
  18. if (string.IsNullOrWhiteSpace(serverUdn))
  19. {
  20. throw new ArgumentNullException("serverUdn");
  21. }
  22. _profile = profile;
  23. _serverUdn = serverUdn;
  24. _serverAddress = serverAddress;
  25. }
  26. public string GetXml()
  27. {
  28. var builder = new StringBuilder();
  29. builder.Append("<?xml version=\"1.0\"?>");
  30. builder.Append("<root xmlns=\"urn:schemas-upnp-org:device-1-0\" xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\"");
  31. foreach (var att in _profile.XmlRootAttributes)
  32. {
  33. builder.AppendFormat(" {0}=\"{1}\"", att.Name, att.Value);
  34. }
  35. builder.Append(">");
  36. builder.Append("<specVersion>");
  37. builder.Append("<major>1</major>");
  38. builder.Append("<minor>0</minor>");
  39. builder.Append("</specVersion>");
  40. AppendDeviceInfo(builder);
  41. builder.Append("</root>");
  42. return builder.ToString();
  43. }
  44. private void AppendDeviceInfo(StringBuilder builder)
  45. {
  46. builder.Append("<device>");
  47. AppendDeviceProperties(builder);
  48. AppendIconList(builder);
  49. AppendServiceList(builder);
  50. builder.Append("</device>");
  51. }
  52. private void AppendDeviceProperties(StringBuilder builder)
  53. {
  54. builder.Append("<UDN>uuid:" + SecurityElement.Escape(_serverUdn) + "</UDN>");
  55. builder.Append("<dlna:X_DLNACAP>" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "</dlna:X_DLNACAP>");
  56. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">M-DMS-1.50</dlna:X_DLNADOC>");
  57. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + SecurityElement.Escape(_profile.XDlnaDoc ?? string.Empty) + "</dlna:X_DLNADOC>");
  58. builder.Append("<friendlyName>" + SecurityElement.Escape(_profile.FriendlyName ?? string.Empty) + "</friendlyName>");
  59. builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
  60. builder.Append("<manufacturer>" + SecurityElement.Escape(_profile.Manufacturer ?? string.Empty) + "</manufacturer>");
  61. builder.Append("<manufacturerURL>" + SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty) + "</manufacturerURL>");
  62. builder.Append("<modelName>" + SecurityElement.Escape(_profile.ModelName ?? string.Empty) + "</modelName>");
  63. builder.Append("<modelDescription>" + SecurityElement.Escape(_profile.ModelDescription ?? string.Empty) + "</modelDescription>");
  64. builder.Append("<modelNumber>" + SecurityElement.Escape(_profile.ModelNumber ?? string.Empty) + "</modelNumber>");
  65. builder.Append("<modelURL>" + SecurityElement.Escape(_profile.ModelUrl ?? string.Empty) + "</modelURL>");
  66. builder.Append("<serialNumber>" + SecurityElement.Escape(_profile.SerialNumber ?? string.Empty) + "</serialNumber>");
  67. //builder.Append("<URLBase>" + SecurityElement.Escape(_serverAddress) + "</URLBase>");
  68. if (!string.IsNullOrWhiteSpace(_profile.SonyAggregationFlags))
  69. {
  70. builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">" + SecurityElement.Escape(_profile.SonyAggregationFlags) + "</av:aggregationFlags>");
  71. }
  72. }
  73. private void AppendIconList(StringBuilder builder)
  74. {
  75. builder.Append("<iconList>");
  76. foreach (var icon in GetIcons())
  77. {
  78. builder.Append("<icon>");
  79. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  80. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  81. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  82. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  83. builder.Append("<url>" + SecurityElement.Escape(icon.Url ?? string.Empty) + "</url>");
  84. builder.Append("</icon>");
  85. }
  86. builder.Append("</iconList>");
  87. }
  88. private void AppendServiceList(StringBuilder builder)
  89. {
  90. builder.Append("<serviceList>");
  91. foreach (var service in GetServices())
  92. {
  93. builder.Append("<service>");
  94. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  95. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  96. builder.Append("<SCPDURL>" + SecurityElement.Escape(service.ScpdUrl ?? string.Empty) + "</SCPDURL>");
  97. builder.Append("<controlURL>" + SecurityElement.Escape(service.ControlUrl ?? string.Empty) + "</controlURL>");
  98. builder.Append("<eventSubURL>" + SecurityElement.Escape(service.EventSubUrl ?? string.Empty) + "</eventSubURL>");
  99. builder.Append("</service>");
  100. }
  101. builder.Append("</serviceList>");
  102. }
  103. private IEnumerable<DeviceIcon> GetIcons()
  104. {
  105. var list = new List<DeviceIcon>();
  106. list.Add(new DeviceIcon
  107. {
  108. MimeType = "image/png",
  109. Depth = "24",
  110. Width = 240,
  111. Height = 240,
  112. Url = "/mediabrowser/dlna/icons/logo240.png"
  113. });
  114. list.Add(new DeviceIcon
  115. {
  116. MimeType = "image/jpeg",
  117. Depth = "24",
  118. Width = 240,
  119. Height = 240,
  120. Url = "/mediabrowser/dlna/icons/logo240.jpg"
  121. });
  122. list.Add(new DeviceIcon
  123. {
  124. MimeType = "image/png",
  125. Depth = "24",
  126. Width = 120,
  127. Height = 120,
  128. Url = "/mediabrowser/dlna/icons/logo120.png"
  129. });
  130. list.Add(new DeviceIcon
  131. {
  132. MimeType = "image/jpeg",
  133. Depth = "24",
  134. Width = 120,
  135. Height = 120,
  136. Url = "/mediabrowser/dlna/icons/logo120.jpg"
  137. });
  138. list.Add(new DeviceIcon
  139. {
  140. MimeType = "image/png",
  141. Depth = "24",
  142. Width = 48,
  143. Height = 48,
  144. Url = "/mediabrowser/dlna/icons/logo48.png"
  145. });
  146. list.Add(new DeviceIcon
  147. {
  148. MimeType = "image/jpeg",
  149. Depth = "24",
  150. Width = 48,
  151. Height = 48,
  152. Url = "/mediabrowser/dlna/icons/logo48.jpg"
  153. });
  154. return list;
  155. }
  156. private IEnumerable<DeviceService> GetServices()
  157. {
  158. var list = new List<DeviceService>();
  159. list.Add(new DeviceService
  160. {
  161. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  162. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  163. ScpdUrl = "/mediabrowser/dlna/contentdirectory/contentdirectory.xml",
  164. ControlUrl = "/mediabrowser/dlna/contentdirectory/" + _serverUdn + "/control",
  165. EventSubUrl = "/mediabrowser/dlna/contentdirectory/" + _serverUdn + "/events"
  166. });
  167. list.Add(new DeviceService
  168. {
  169. ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
  170. ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
  171. ScpdUrl = "/mediabrowser/dlna/connectionmanager/connectionmanager.xml",
  172. ControlUrl = "/mediabrowser/dlna/connectionmanager/" + _serverUdn + "/control",
  173. EventSubUrl = "/mediabrowser/dlna/connectionmanager/" + _serverUdn + "/events"
  174. });
  175. return list;
  176. }
  177. public override string ToString()
  178. {
  179. return GetXml();
  180. }
  181. }
  182. }