DescriptionXmlBuilder.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 name = _profile.FriendlyName ?? string.Empty;
  92. var characters = _serverName.Where(c => (char.IsLetterOrDigit(c) || c == '-')).ToArray();
  93. var serverName = new string(characters);
  94. name = name.Replace("${ServerName}", serverName, StringComparison.OrdinalIgnoreCase);
  95. return name;
  96. }
  97. private void AppendIconList(StringBuilder builder)
  98. {
  99. builder.Append("<iconList>");
  100. foreach (var icon in GetIcons())
  101. {
  102. builder.Append("<icon>");
  103. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  104. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  105. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  106. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  107. builder.Append("<url>" + BuildUrl(icon.Url) + "</url>");
  108. builder.Append("</icon>");
  109. }
  110. builder.Append("</iconList>");
  111. }
  112. private void AppendServiceList(StringBuilder builder)
  113. {
  114. builder.Append("<serviceList>");
  115. foreach (var service in GetServices())
  116. {
  117. builder.Append("<service>");
  118. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  119. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  120. builder.Append("<SCPDURL>" + BuildUrl(service.ScpdUrl) + "</SCPDURL>");
  121. builder.Append("<controlURL>" + BuildUrl(service.ControlUrl) + "</controlURL>");
  122. builder.Append("<eventSubURL>" + BuildUrl(service.EventSubUrl) + "</eventSubURL>");
  123. builder.Append("</service>");
  124. }
  125. builder.Append("</serviceList>");
  126. }
  127. private string BuildUrl(string url)
  128. {
  129. if (string.IsNullOrWhiteSpace(url))
  130. {
  131. return string.Empty;
  132. }
  133. url = url.TrimStart('/');
  134. url = "/dlna/" + _serverUdn + "/" + url;
  135. if (EnableAbsoluteUrls)
  136. {
  137. url = _serverAddress.TrimEnd('/') + url;
  138. }
  139. return SecurityElement.Escape(url);
  140. }
  141. private IEnumerable<DeviceIcon> GetIcons()
  142. {
  143. var list = new List<DeviceIcon>();
  144. list.Add(new DeviceIcon
  145. {
  146. MimeType = "image/png",
  147. Depth = "24",
  148. Width = 240,
  149. Height = 240,
  150. Url = "icons/logo240.png"
  151. });
  152. list.Add(new DeviceIcon
  153. {
  154. MimeType = "image/jpeg",
  155. Depth = "24",
  156. Width = 240,
  157. Height = 240,
  158. Url = "icons/logo240.jpg"
  159. });
  160. list.Add(new DeviceIcon
  161. {
  162. MimeType = "image/png",
  163. Depth = "24",
  164. Width = 120,
  165. Height = 120,
  166. Url = "icons/logo120.png"
  167. });
  168. list.Add(new DeviceIcon
  169. {
  170. MimeType = "image/jpeg",
  171. Depth = "24",
  172. Width = 120,
  173. Height = 120,
  174. Url = "icons/logo120.jpg"
  175. });
  176. list.Add(new DeviceIcon
  177. {
  178. MimeType = "image/png",
  179. Depth = "24",
  180. Width = 48,
  181. Height = 48,
  182. Url = "icons/logo48.png"
  183. });
  184. list.Add(new DeviceIcon
  185. {
  186. MimeType = "image/jpeg",
  187. Depth = "24",
  188. Width = 48,
  189. Height = 48,
  190. Url = "icons/logo48.jpg"
  191. });
  192. return list;
  193. }
  194. private IEnumerable<DeviceService> GetServices()
  195. {
  196. var list = new List<DeviceService>();
  197. list.Add(new DeviceService
  198. {
  199. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  200. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  201. ScpdUrl = "contentdirectory/contentdirectory.xml",
  202. ControlUrl = "contentdirectory/control",
  203. EventSubUrl = "contentdirectory/events"
  204. });
  205. list.Add(new DeviceService
  206. {
  207. ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
  208. ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
  209. ScpdUrl = "connectionmanager/connectionmanager.xml",
  210. ControlUrl = "connectionmanager/control",
  211. EventSubUrl = "connectionmanager/events"
  212. });
  213. list.Add(new DeviceService
  214. {
  215. ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
  216. ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
  217. ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
  218. ControlUrl = "mediareceiverregistrar/control",
  219. EventSubUrl = "mediareceiverregistrar/events"
  220. });
  221. return list;
  222. }
  223. public override string ToString()
  224. {
  225. return GetXml();
  226. }
  227. }
  228. }