DescriptionXmlBuilder.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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>" + 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. }
  70. private void AppendIconList(StringBuilder builder)
  71. {
  72. builder.Append("<iconList>");
  73. foreach (var icon in GetIcons())
  74. {
  75. builder.Append("<icon>");
  76. builder.Append("<mimetype>" + SecurityElement.Escape(icon.MimeType ?? string.Empty) + "</mimetype>");
  77. builder.Append("<width>" + SecurityElement.Escape(icon.Width.ToString(_usCulture)) + "</width>");
  78. builder.Append("<height>" + SecurityElement.Escape(icon.Height.ToString(_usCulture)) + "</height>");
  79. builder.Append("<depth>" + SecurityElement.Escape(icon.Depth ?? string.Empty) + "</depth>");
  80. builder.Append("<url>" + SecurityElement.Escape(icon.Url ?? string.Empty) + "</url>");
  81. builder.Append("</icon>");
  82. }
  83. builder.Append("</iconList>");
  84. }
  85. private void AppendServiceList(StringBuilder builder)
  86. {
  87. builder.Append("<serviceList>");
  88. foreach (var service in GetServices())
  89. {
  90. builder.Append("<service>");
  91. builder.Append("<serviceType>" + SecurityElement.Escape(service.ServiceType ?? string.Empty) + "</serviceType>");
  92. builder.Append("<serviceId>" + SecurityElement.Escape(service.ServiceId ?? string.Empty) + "</serviceId>");
  93. builder.Append("<SCPDURL>" + SecurityElement.Escape(service.ScpdUrl ?? string.Empty) + "</SCPDURL>");
  94. builder.Append("<controlURL>" + SecurityElement.Escape(service.ControlUrl ?? string.Empty) + "</controlURL>");
  95. builder.Append("<eventSubURL>" + SecurityElement.Escape(service.EventSubUrl ?? string.Empty) + "</eventSubURL>");
  96. builder.Append("</service>");
  97. }
  98. builder.Append("</serviceList>");
  99. }
  100. private IEnumerable<DeviceIcon> GetIcons()
  101. {
  102. var list = new List<DeviceIcon>();
  103. list.Add(new DeviceIcon
  104. {
  105. MimeType = "image/png",
  106. Depth = "24",
  107. Width = 120,
  108. Height = 120,
  109. Url = "/mediabrowser/dlna/icons/logo120.png"
  110. });
  111. list.Add(new DeviceIcon
  112. {
  113. MimeType = "image/jpeg",
  114. Depth = "24",
  115. Width = 120,
  116. Height = 120,
  117. Url = "/mediabrowser/dlna/icons/logo120.jpg"
  118. });
  119. list.Add(new DeviceIcon
  120. {
  121. MimeType = "image/png",
  122. Depth = "24",
  123. Width = 48,
  124. Height = 48,
  125. Url = "/mediabrowser/dlna/icons/logo48.png"
  126. });
  127. list.Add(new DeviceIcon
  128. {
  129. MimeType = "image/jpeg",
  130. Depth = "24",
  131. Width = 48,
  132. Height = 48,
  133. Url = "/mediabrowser/dlna/icons/logo48.jpg"
  134. });
  135. return list;
  136. }
  137. private IEnumerable<DeviceService> GetServices()
  138. {
  139. var list = new List<DeviceService>();
  140. list.Add(new DeviceService
  141. {
  142. ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
  143. ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
  144. ScpdUrl = "/mediabrowser/dlna/contentdirectory.xml",
  145. ControlUrl = "/mediabrowser/dlna/" + _serverUdn + "/control"
  146. });
  147. return list;
  148. }
  149. public override string ToString()
  150. {
  151. return GetXml();
  152. }
  153. }
  154. }