DescriptionXmlBuilder.cs 10 KB

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