DescriptionXmlBuilder.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn)
  16. {
  17. if (string.IsNullOrWhiteSpace(serverUdn))
  18. {
  19. throw new ArgumentNullException("serverUdn");
  20. }
  21. _profile = profile;
  22. _serverUdn = serverUdn;
  23. }
  24. public string GetXml()
  25. {
  26. var builder = new StringBuilder();
  27. builder.Append("<?xml version=\"1.0\"?>");
  28. builder.Append("<root xmlns=\"urn:schemas-upnp-org:device-1-0\" xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\" xmlns:sec=\"http://www.sec.co.kr/dlna\">");
  29. builder.Append("<specVersion>");
  30. builder.Append("<major>1</major>");
  31. builder.Append("<minor>0</minor>");
  32. builder.Append("</specVersion>");
  33. AppendDeviceInfo(builder);
  34. builder.Append("</root>");
  35. return builder.ToString();
  36. }
  37. private void AppendDeviceInfo(StringBuilder builder)
  38. {
  39. builder.Append("<device>");
  40. AppendDeviceProperties(builder);
  41. AppendIconList(builder);
  42. AppendServiceList(builder);
  43. builder.Append("</device>");
  44. }
  45. private void AppendDeviceProperties(StringBuilder builder)
  46. {
  47. builder.Append("<UDN>uuid:" + SecurityElement.Escape(_serverUdn) + "</UDN>");
  48. builder.Append("<dlna:X_DLNACAP>" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "</dlna:X_DLNACAP>");
  49. if (!string.IsNullOrWhiteSpace(_profile.XDlnaDoc))
  50. {
  51. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" +
  52. SecurityElement.Escape(_profile.XDlnaDoc) + "</dlna:X_DLNADOC>");
  53. }
  54. else
  55. {
  56. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">DMS-1.50</dlna:X_DLNADOC>");
  57. }
  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("<sec:ProductCap>DCM10,getMediaInfo.sec</sec:ProductCap>");
  68. builder.Append("<sec:X_ProductCap>DCM10,getMediaInfo.sec</sec:X_ProductCap>");
  69. if (!string.IsNullOrWhiteSpace(_profile.SonyAggregationFlags))
  70. {
  71. builder.Append("<av:aggregationFlags xmlns:av=\"urn:schemas-sony-com:av\">" + SecurityElement.Escape(_profile.SonyAggregationFlags) + "</av:aggregationFlags>");
  72. }
  73. }
  74. private void AppendIconList(StringBuilder builder)
  75. {
  76. builder.Append("<iconList>");
  77. foreach (var icon in GetIcons())
  78. {
  79. builder.Append("<icon>");
  80. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  81. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  82. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  83. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  84. builder.Append("<url>" + SecurityElement.Escape(icon.Url ?? string.Empty) + "</url>");
  85. builder.Append("</icon>");
  86. }
  87. builder.Append("</iconList>");
  88. }
  89. private void AppendServiceList(StringBuilder builder)
  90. {
  91. builder.Append("<serviceList>");
  92. foreach (var service in GetServices())
  93. {
  94. builder.Append("<service>");
  95. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  96. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  97. builder.Append("<SCPDURL>" + SecurityElement.Escape(service.ScpdUrl ?? string.Empty) + "</SCPDURL>");
  98. builder.Append("<controlURL>" + SecurityElement.Escape(service.ControlUrl ?? string.Empty) + "</controlURL>");
  99. builder.Append("<eventSubURL>" + SecurityElement.Escape(service.EventSubUrl ?? string.Empty) + "</eventSubURL>");
  100. builder.Append("</service>");
  101. }
  102. builder.Append("</serviceList>");
  103. }
  104. private IEnumerable<DeviceIcon> GetIcons()
  105. {
  106. var list = new List<DeviceIcon>();
  107. list.Add(new DeviceIcon
  108. {
  109. MimeType = "image/png",
  110. Depth = "24",
  111. Width = 120,
  112. Height = 120,
  113. Url = "/mediabrowser/dlna/icons/logo120.png"
  114. });
  115. list.Add(new DeviceIcon
  116. {
  117. MimeType = "image/jpeg",
  118. Depth = "24",
  119. Width = 120,
  120. Height = 120,
  121. Url = "/mediabrowser/dlna/icons/logo120.jpg"
  122. });
  123. list.Add(new DeviceIcon
  124. {
  125. MimeType = "image/png",
  126. Depth = "24",
  127. Width = 48,
  128. Height = 48,
  129. Url = "/mediabrowser/dlna/icons/logo48.png"
  130. });
  131. list.Add(new DeviceIcon
  132. {
  133. MimeType = "image/jpeg",
  134. Depth = "24",
  135. Width = 48,
  136. Height = 48,
  137. Url = "/mediabrowser/dlna/icons/logo48.jpg"
  138. });
  139. return list;
  140. }
  141. private IEnumerable<DeviceService> GetServices()
  142. {
  143. var list = new List<DeviceService>();
  144. list.Add(new DeviceService
  145. {
  146. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  147. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  148. ScpdUrl = "/mediabrowser/dlna/contentdirectory/contentdirectory.xml",
  149. ControlUrl = "/mediabrowser/dlna/contentdirectory/" + _serverUdn + "/control",
  150. EventSubUrl = "/mediabrowser/dlna/contentdirectory/" + _serverUdn + "/events"
  151. });
  152. return list;
  153. }
  154. public override string ToString()
  155. {
  156. return GetXml();
  157. }
  158. }
  159. }