DescriptionXmlBuilder.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. AppendDeviceProperties(builder);
  40. AppendIconList(builder);
  41. AppendServiceList(builder);
  42. }
  43. private void AppendDeviceProperties(StringBuilder builder)
  44. {
  45. builder.Append("<UDN>" + SecurityElement.Escape(_serverUdn) + "</UDN>");
  46. builder.Append("<dlna:X_DLNACAP>" + SecurityElement.Escape(_profile.XDlnaCap ?? string.Empty) + "</dlna:X_DLNACAP>");
  47. if (!string.IsNullOrWhiteSpace(_profile.XDlnaDoc))
  48. {
  49. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">" +
  50. SecurityElement.Escape(_profile.XDlnaDoc) + "</dlna:X_DLNADOC>");
  51. }
  52. else
  53. {
  54. builder.Append("<dlna:X_DLNADOC xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">DMS-1.50</dlna:X_DLNADOC>");
  55. }
  56. builder.Append("<friendlyName>" + SecurityElement.Escape(_profile.FriendlyName ?? string.Empty) + "</friendlyName>");
  57. builder.Append("<deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>");
  58. builder.Append("<manufacturer>" + SecurityElement.Escape(_profile.Manufacturer ?? string.Empty) + "</manufacturer>");
  59. builder.Append("<manufacturerURL>" + SecurityElement.Escape(_profile.ManufacturerUrl ?? string.Empty) + "</manufacturerURL>");
  60. builder.Append("<modelName>" + SecurityElement.Escape(_profile.ModelName ?? string.Empty) + "</modelName>");
  61. builder.Append("<modelDescription>" + SecurityElement.Escape(_profile.ModelDescription ?? string.Empty) + "</modelDescription>");
  62. builder.Append("<modelNumber>" + SecurityElement.Escape(_profile.ModelNumber ?? string.Empty) + "</modelNumber>");
  63. builder.Append("<modelURL>" + SecurityElement.Escape(_profile.ModelUrl ?? string.Empty) + "</modelURL>");
  64. builder.Append("<serialNumber>" + SecurityElement.Escape(_profile.SerialNumber ?? string.Empty) + "</serialNumber>");
  65. builder.Append("<sec:ProductCap>DCM10,getMediaInfo.sec</sec:ProductCap>");
  66. builder.Append("<sec:X_ProductCap>DCM10,getMediaInfo.sec</sec:X_ProductCap>");
  67. }
  68. private void AppendIconList(StringBuilder builder)
  69. {
  70. builder.Append("<iconList>");
  71. foreach (var icon in GetIcons())
  72. {
  73. builder.Append("<icon>");
  74. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  75. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  76. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  77. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  78. builder.Append("<url>" + SecurityElement.Escape(icon.Url ?? string.Empty) + "</url>");
  79. builder.Append("</icon>");
  80. }
  81. builder.Append("</iconList>");
  82. }
  83. private void AppendServiceList(StringBuilder builder)
  84. {
  85. builder.Append("<serviceList>");
  86. foreach (var service in GetServices())
  87. {
  88. builder.Append("<service>");
  89. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  90. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  91. builder.Append("<SCPDURL>" + SecurityElement.Escape(service.ScpdUrl ?? string.Empty) + "</SCPDURL>");
  92. builder.Append("<controlURL>" + SecurityElement.Escape(service.ControlUrl ?? string.Empty) + "</controlURL>");
  93. builder.Append("<eventSubURL>" + SecurityElement.Escape(service.EventSubUrl ?? string.Empty) + "</eventSubURL>");
  94. builder.Append("</service>");
  95. }
  96. builder.Append("</serviceList>");
  97. }
  98. private IEnumerable<DeviceIcon> GetIcons()
  99. {
  100. var list = new List<DeviceIcon>();
  101. list.Add(new DeviceIcon
  102. {
  103. MimeType = "image/jpeg",
  104. Depth = "24",
  105. Width = 48,
  106. Height = 48,
  107. Url = "/icons/small.jpg"
  108. });
  109. list.Add(new DeviceIcon
  110. {
  111. MimeType = "image/jpeg",
  112. Depth = "24",
  113. Width = 120,
  114. Height = 120,
  115. Url = "/icons/large.jpg"
  116. });
  117. list.Add(new DeviceIcon
  118. {
  119. MimeType = "image/png",
  120. Depth = "24",
  121. Width = 48,
  122. Height = 48,
  123. Url = "/icons/small.png"
  124. });
  125. list.Add(new DeviceIcon
  126. {
  127. MimeType = "image/png",
  128. Depth = "24",
  129. Width = 120,
  130. Height = 120,
  131. Url = "/icons/large.png"
  132. });
  133. return list;
  134. }
  135. private IEnumerable<DeviceService> GetServices()
  136. {
  137. var list = new List<DeviceService>();
  138. list.Add(new DeviceService
  139. {
  140. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  141. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  142. ScpdUrl = "/contentdirectory.xml",
  143. ControlUrl = "/servicecontrol"
  144. });
  145. return list;
  146. }
  147. public override string ToString()
  148. {
  149. return GetXml();
  150. }
  151. }
  152. }