PageRenderTime 78ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/DICOM/Generators/DicomUIDGenerator.cs

https://github.com/petnet/fo-dicom
C# | 118 lines | 104 code | 14 blank | 0 comment | 14 complexity | 8c5f2e534d36ece39fcda30f7742d95e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using System.Xml.Linq;
  8. namespace Dicom.Generators {
  9. public class DicomUIDGenerator {
  10. public static string Process(string file) {
  11. StringBuilder list = new StringBuilder();
  12. StringBuilder uids = new StringBuilder();
  13. XDocument doc = XDocument.Load(file);
  14. XElement xdict = doc.Element("dictionary");
  15. if (xdict == null)
  16. throw new DicomDataException("Expected <dictionary> root node in DICOM dictionary.");
  17. foreach (XElement xuid in xdict.Elements("uid")) {
  18. string name = xuid.Value;
  19. if (xuid.Attribute("uid") == null)
  20. continue;
  21. string uid = xuid.Attribute("uid").Value;
  22. if (xuid.Attribute("keyword") == null)
  23. continue;
  24. string keyword = xuid.Attribute("keyword").Value;
  25. bool retired = false;
  26. XAttribute xretired = xuid.Attribute("retired");
  27. if (xretired != null && !String.IsNullOrEmpty(xretired.Value) && Boolean.Parse(xretired.Value))
  28. retired = true;
  29. if (retired)
  30. keyword += "RETIRED";
  31. if (xuid.Attribute("type") == null)
  32. continue;
  33. string type = xuid.Attribute("type").Value;
  34. DicomUidType uidType = DicomUidType.Unknown;
  35. switch (type) {
  36. case "Transfer":
  37. case "Transfer Syntax":
  38. uidType = DicomUidType.TransferSyntax;
  39. break;
  40. case "SOP Class":
  41. case "Query/Retrieve":
  42. uidType = DicomUidType.SOPClass;
  43. break;
  44. case "Meta SOP Class":
  45. uidType = DicomUidType.MetaSOPClass;
  46. break;
  47. case "Service Class":
  48. uidType = DicomUidType.ServiceClass;
  49. break;
  50. case "Well-known frame of reference":
  51. case "Synchronization Frame of Reference":
  52. uidType = DicomUidType.FrameOfReference;
  53. break;
  54. case "Well-known SOP Instance":
  55. case "Well-known Printer SOP Instance":
  56. case "Well-known Print Queue SOP Instance":
  57. uidType = DicomUidType.SOPInstance;
  58. break;
  59. case "Coding Scheme":
  60. case "DICOM UIDs as a Coding Scheme":
  61. uidType = DicomUidType.CodingScheme;
  62. break;
  63. case "Application Context Name":
  64. uidType = DicomUidType.ApplicationContextName;
  65. break;
  66. case "LDAP":
  67. case "LDAP OID":
  68. uidType = DicomUidType.LDAP;
  69. break;
  70. case "Context Group Name":
  71. uidType = DicomUidType.ContextGroupName;
  72. break;
  73. case "Application Hosting Model":
  74. uidType = DicomUidType.ApplicationHostingModel;
  75. break;
  76. case "":
  77. uidType = DicomUidType.Unknown;
  78. break;
  79. default:
  80. throw new DicomDataException("Unkown UID type: {0}", type);
  81. }
  82. list.AppendFormat("\t\t\t_uids.Add(DicomUID.{0}.UID, DicomUID.{0});", keyword).AppendLine();
  83. uids.AppendLine();
  84. uids.AppendFormat("\t\t/// <summary>{0}: {1}</summary>", type, name).AppendLine();
  85. uids.AppendFormat("\t\tpublic readonly static DicomUID {0} = new DicomUID(\"{1}\", \"{2}\", DicomUidType.{3}, {4});", keyword, uid, name, uidType, retired ? "true" : "false").AppendLine();
  86. }
  87. string code = @"using System;
  88. using System.Collections.Generic;
  89. using System.Linq;
  90. using System.Text;
  91. namespace Dicom {
  92. public partial class DicomUID {
  93. private static void LoadInternalUIDs() {
  94. ";
  95. code += list.ToString();
  96. code += " }";
  97. code += uids.ToString();
  98. code += @" }
  99. }";
  100. return code;
  101. }
  102. }
  103. }