DescriptionXmlBuilder.cs 9.2 KB

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