DescriptionXmlBuilder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. using MediaBrowser.Dlna.Common;
  2. using MediaBrowser.Model.Dlna;
  3. using MediaBrowser.Model.Extensions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Security;
  9. using System.Text;
  10. namespace MediaBrowser.Dlna.Server
  11. {
  12. public class DescriptionXmlBuilder
  13. {
  14. private readonly DeviceProfile _profile;
  15. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  16. private readonly string _serverUdn;
  17. private readonly string _serverAddress;
  18. private readonly string _serverName;
  19. private readonly string _serverId;
  20. public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress, string serverName, string serverId)
  21. {
  22. if (string.IsNullOrWhiteSpace(serverUdn))
  23. {
  24. throw new ArgumentNullException("serverUdn");
  25. }
  26. if (string.IsNullOrWhiteSpace(serverAddress))
  27. {
  28. throw new ArgumentNullException("serverAddress");
  29. }
  30. _profile = profile;
  31. _serverUdn = serverUdn;
  32. _serverAddress = serverAddress;
  33. _serverName = serverName;
  34. _serverId = serverId;
  35. }
  36. private bool EnableAbsoluteUrls
  37. {
  38. get { return false; }
  39. }
  40. public string GetXml()
  41. {
  42. var builder = new StringBuilder();
  43. builder.Append("<?xml version=\"1.0\"?>");
  44. builder.Append("<root");
  45. var attributes = _profile.XmlRootAttributes.ToList();
  46. attributes.Insert(0, new XmlAttribute
  47. {
  48. Name = "xmlns:dlna",
  49. Value = "urn:schemas-dlna-org:device-1-0"
  50. });
  51. attributes.Insert(0, new XmlAttribute
  52. {
  53. Name = "xmlns",
  54. Value = "urn:schemas-upnp-org:device-1-0"
  55. });
  56. foreach (var att in attributes)
  57. {
  58. builder.AppendFormat(" {0}=\"{1}\"", att.Name, att.Value);
  59. }
  60. builder.Append(">");
  61. builder.Append("<specVersion>");
  62. builder.Append("<major>1</major>");
  63. builder.Append("<minor>0</minor>");
  64. builder.Append("</specVersion>");
  65. AppendDeviceInfo(builder);
  66. builder.Append("</root>");
  67. return builder.ToString();
  68. }
  69. private void AppendDeviceInfo(StringBuilder builder)
  70. {
  71. builder.Append("<device>");
  72. AppendDeviceProperties(builder);
  73. AppendIconList(builder);
  74. AppendServiceList(builder);
  75. builder.Append("</device>");
  76. }
  77. private void AppendDeviceProperties(StringBuilder builder)
  78. {
  79. builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
  80. builder.Append("<dlna:X_DLNACAP>" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "</dlna:X_DLNACAP>");
  81. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">M-DMS-1.50</dlna:X_DLNADOC>");
  82. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" + SecurityElement.Escape(_profile.XDlnaDoc ?? string.Empty) + "</dlna:X_DLNADOC>");
  83. builder.Append("<friendlyName>" + SecurityElement.Escape(GetFriendlyName()) + "</friendlyName>");
  84. builder.Append("<manufacturer>" + SecurityElement.Escape(_profile.Manufacturer ?? string.Empty) + "</manufacturer>");
  85. builder.Append("<manufacturerURL>" + SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty) + "</manufacturerURL>");
  86. builder.Append("<modelDescription>" + SecurityElement.Escape(_profile.ModelDescription ?? string.Empty) + "</modelDescription>");
  87. builder.Append("<modelName>" + SecurityElement.Escape(_profile.ModelName ?? string.Empty) + "</modelName>");
  88. builder.Append("<modelNumber>" + SecurityElement.Escape(_profile.ModelNumber ?? string.Empty) + "</modelNumber>");
  89. builder.Append("<modelURL>" + SecurityElement.Escape(_profile.ModelUrl ?? string.Empty) + "</modelURL>");
  90. if (string.IsNullOrWhiteSpace(_profile.SerialNumber))
  91. {
  92. builder.Append("<serialNumber>" + SecurityElement.Escape(_serverId) + "</serialNumber>");
  93. }
  94. else
  95. {
  96. builder.Append("<serialNumber>" + SecurityElement.Escape(_profile.SerialNumber) + "</serialNumber>");
  97. }
  98. builder.Append("<UDN>uuid:" + SecurityElement.Escape(_serverId) + "</UDN>");
  99. builder.Append("<presentationURL>" + SecurityElement.Escape(_serverAddress) + "</presentationURL>");
  100. if (!EnableAbsoluteUrls)
  101. {
  102. //builder.Append("<URLBase>" + SecurityElement.Escape(_serverAddress) + "</URLBase>");
  103. }
  104. if (!string.IsNullOrWhiteSpace(_profile.SonyAggregationFlags))
  105. {
  106. builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">" + SecurityElement.Escape(_profile.SonyAggregationFlags) + "</av:aggregationFlags>");
  107. }
  108. }
  109. private string GetFriendlyName()
  110. {
  111. var characters = _serverName.Where(c => (char.IsLetterOrDigit(c) || c == '-')).ToArray();
  112. var serverName = new string(characters);
  113. var name = (_profile.FriendlyName ?? string.Empty).Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
  114. return name;
  115. }
  116. private void AppendIconList(StringBuilder builder)
  117. {
  118. builder.Append("<iconList>");
  119. foreach (var icon in GetIcons())
  120. {
  121. builder.Append("<icon>");
  122. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  123. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  124. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  125. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  126. builder.Append("<url>" + BuildUrl(icon.Url) + "</url>");
  127. builder.Append("</icon>");
  128. }
  129. builder.Append("</iconList>");
  130. }
  131. private void AppendServiceList(StringBuilder builder)
  132. {
  133. builder.Append("<serviceList>");
  134. foreach (var service in GetServices())
  135. {
  136. builder.Append("<service>");
  137. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  138. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  139. builder.Append("<SCPDURL>" + BuildUrl(service.ScpdUrl) + "</SCPDURL>");
  140. builder.Append("<controlURL>" + BuildUrl(service.ControlUrl) + "</controlURL>");
  141. builder.Append("<eventSubURL>" + BuildUrl(service.EventSubUrl) + "</eventSubURL>");
  142. builder.Append("</service>");
  143. }
  144. builder.Append("</serviceList>");
  145. }
  146. private string BuildUrl(string url)
  147. {
  148. if (string.IsNullOrWhiteSpace(url))
  149. {
  150. return string.Empty;
  151. }
  152. url = url.TrimStart('/');
  153. url = "/dlna/" + _serverUdn + "/" + url;
  154. if (EnableAbsoluteUrls)
  155. {
  156. url = _serverAddress.TrimEnd('/') + url;
  157. }
  158. return SecurityElement.Escape(url);
  159. }
  160. private IEnumerable<DeviceIcon> GetIcons()
  161. {
  162. var list = new List<DeviceIcon>();
  163. list.Add(new DeviceIcon
  164. {
  165. MimeType = "image/png",
  166. Depth = "24",
  167. Width = 240,
  168. Height = 240,
  169. Url = "icons/logo240.png"
  170. });
  171. list.Add(new DeviceIcon
  172. {
  173. MimeType = "image/jpeg",
  174. Depth = "24",
  175. Width = 240,
  176. Height = 240,
  177. Url = "icons/logo240.jpg"
  178. });
  179. list.Add(new DeviceIcon
  180. {
  181. MimeType = "image/png",
  182. Depth = "24",
  183. Width = 120,
  184. Height = 120,
  185. Url = "icons/logo120.png"
  186. });
  187. list.Add(new DeviceIcon
  188. {
  189. MimeType = "image/jpeg",
  190. Depth = "24",
  191. Width = 120,
  192. Height = 120,
  193. Url = "icons/logo120.jpg"
  194. });
  195. list.Add(new DeviceIcon
  196. {
  197. MimeType = "image/png",
  198. Depth = "24",
  199. Width = 48,
  200. Height = 48,
  201. Url = "icons/logo48.png"
  202. });
  203. list.Add(new DeviceIcon
  204. {
  205. MimeType = "image/jpeg",
  206. Depth = "24",
  207. Width = 48,
  208. Height = 48,
  209. Url = "icons/logo48.jpg"
  210. });
  211. return list;
  212. }
  213. private IEnumerable<DeviceService> GetServices()
  214. {
  215. var list = new List<DeviceService>();
  216. list.Add(new DeviceService
  217. {
  218. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  219. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  220. ScpdUrl = "contentdirectory/contentdirectory.xml",
  221. ControlUrl = "contentdirectory/control",
  222. EventSubUrl = "contentdirectory/events"
  223. });
  224. list.Add(new DeviceService
  225. {
  226. ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
  227. ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
  228. ScpdUrl = "connectionmanager/connectionmanager.xml",
  229. ControlUrl = "connectionmanager/control",
  230. EventSubUrl = "connectionmanager/events"
  231. });
  232. if (_profile.EnableMSMediaReceiverRegistrar)
  233. {
  234. list.Add(new DeviceService
  235. {
  236. ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
  237. ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
  238. ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
  239. ControlUrl = "mediareceiverregistrar/control",
  240. EventSubUrl = "mediareceiverregistrar/events"
  241. });
  242. }
  243. return list;
  244. }
  245. public override string ToString()
  246. {
  247. return GetXml();
  248. }
  249. }
  250. }