/// <summary>
 ///     Returns the entire data element dictionary as array of
 ///     <see cref="DataElementDictionaryEntry" />.
 /// </summary>
 public DataElementDictionaryEntry[] ToArray()
 {
     DataElementDictionaryEntry[] entryArray =
         new DataElementDictionaryEntry[hashTable.Count];
     hashTable.Values.CopyTo(entryArray, 0);
     Array.Sort(entryArray);
     return(entryArray);
 }
 /// <summary>
 ///     Adds a new data element dictionary entry to a data element
 ///     dictionary instance.
 /// </summary>
 public void Add(DataElementDictionaryEntry entry)
 {
     if (entry != null)
     {
         if (!Contains(entry.Tag))
         {
             hashTable.Add(entry.Tag.ToString(), entry);
         }
         else
         {
             throw new DicomException(
                       "Tag already exists in data element dictionary.",
                       "entry.Tag", entry.Tag.ToString());
         }
     }
     else
     {
         throw new DicomException("entry is null.", "entry");
     }
 }
        private void LoadFromBinary(StreamReader streamReader)
        {
            BinaryReader binaryReader = new BinaryReader(streamReader.BaseStream);

            while (streamReader.BaseStream.Position < streamReader.BaseStream.Length)
            {
                try
                {
                    int    group   = binaryReader.ReadInt32();
                    int    element = binaryReader.ReadInt32();
                    Tag    tag     = new Tag(group, element);
                    int    length  = binaryReader.ReadInt32();
                    byte[] buffer  = new byte[length];
                    binaryReader.Read(buffer, 0, length);
                    string description = ByteConvert.ToString(buffer,
                                                              CharacterRepertoire.Ascii);
                    buffer = new byte[2];
                    binaryReader.Read(buffer, 0, 2);
                    string vr = ByteConvert.ToString(buffer,
                                                     CharacterRepertoire.Ascii);
                    vr     = vr.Trim();
                    length = binaryReader.ReadInt32();
                    buffer = new byte[length];
                    binaryReader.Read(buffer, 0, length);
                    string vm = ByteConvert.ToString(buffer,
                                                     CharacterRepertoire.Ascii);
                    bool   retiredBool = binaryReader.ReadBoolean();
                    string retired     = retiredBool ? "RET" : null;
                    DataElementDictionaryEntry entry =
                        new DataElementDictionaryEntry(tag.ToString(),
                                                       description, vr, vm, retired);
                    Add(entry);
                }
                catch (Exception e)
                {
                    throw new DicomException("Wrong entry before index " +
                                             streamReader.BaseStream.Position + ": " + e.Message);
                }
            }
        }
        protected virtual void LoadFromXml(TextReader textReader)
        {
            XmlTextReader xmlTextReader = new XmlTextReader(textReader);
            string        tag           = null;
            string        description   = null;
            string        vr            = null;
            string        vm            = null;
            string        retired       = null;

            while (xmlTextReader.Read())
            {
                switch (xmlTextReader.Name)
                {
                case "DictionaryEntry":
                    retired = xmlTextReader["retired"];
                    break;

                case "Tag":
                    xmlTextReader.MoveToContent();
                    tag = xmlTextReader.ReadString();
                    break;

                case "Description":
                    xmlTextReader.MoveToContent();
                    description = xmlTextReader.ReadString();
                    break;

                case "VR":
                    xmlTextReader.MoveToContent();
                    vr = xmlTextReader.ReadString();
                    break;

                case "VM":
                    xmlTextReader.MoveToContent();
                    vm = xmlTextReader.ReadString();
                    break;
                }
                if (tag != null && description != null && vr != null &&
                    vm != null)
                {
                    try
                    {
                        if (Regex.IsMatch(tag.ToLower(), "(50xx|60xx)"))
                        {
                            // Dicom repeating groups
                            for (int i = 0; i <= 0x1E; i += 2)
                            {
                                string uniqueTag = tag.ToLower()
                                                   .Replace("xx", string.Format("{0:X2}", i));
                                DataElementDictionaryEntry entry =
                                    new DataElementDictionaryEntry(
                                        uniqueTag,
                                        description.Trim(),
                                        vr.Trim(), vm.Trim(), retired);
                                Add(entry);
                            }
                        }
                        else if (Regex.IsMatch(tag.ToLower(), "0020,31xx"))
                        {
                            // Not official, but useful!
                            for (int i = 0; i <= 0xFF; i++)
                            {
                                string uniqueTag = tag.ToLower()
                                                   .Replace("xx", string.Format("{0:X2}", i));
                                DataElementDictionaryEntry entry =
                                    new DataElementDictionaryEntry(
                                        uniqueTag,
                                        description.Trim(), vr.Trim(),
                                        vm.Trim(), retired);
                                Add(entry);
                            }
                        }
                        else
                        {
                            DataElementDictionaryEntry entry =
                                new DataElementDictionaryEntry(tag,
                                                               description.Trim(),
                                                               vr.Trim(), vm.Trim(), retired);
                            Add(entry);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new DicomException("Wrong entry at tag " +
                                                 tag + ": " + e.Message);
                    }
                    tag = description = vr = vm = retired = null;
                }
            }
            xmlTextReader.Close();
        }
        private void LoadFromCsv(TextReader textReader)
        {
            string line       = textReader.ReadLine();
            int    lineNumber = 1;

            string[] result = null;
            while (line != null)
            {
                string lineWithoutSpaces = line.Replace(" ", null);
                if (!lineWithoutSpaces.StartsWith("#") &&
                    !lineWithoutSpaces.Equals(""))
                {
                    if (Regex.IsMatch(lineWithoutSpaces,
                                      "^[^;]+;[^;]+;([A-Za-z]{2})?;[0-9\\-nN]+(;RET)?$"))
                    {
                        result = line.Split(';');
                        string tag     = result[0];
                        string retired = (result.Length == 5) ? "RET" : null;
                        try
                        {
                            if (Regex.IsMatch(tag.ToLower(), "(50xx|60xx)"))
                            {
                                // Dicom repeating groups
                                for (int i = 0; i <= 0x1E; i += 2)
                                {
                                    string uniqueTag = tag.ToLower()
                                                       .Replace("xx",
                                                                string.Format("{0:X2}", i));
                                    DataElementDictionaryEntry entry =
                                        new DataElementDictionaryEntry(
                                            uniqueTag,
                                            result[1].Trim(), result[2].Trim(),
                                            result[3].Trim(), retired);
                                    Add(entry);
                                }
                            }
                            else if (Regex.IsMatch(tag.ToLower(), "0020,31xx"))
                            {
                                // Not official, but useful!
                                for (int i = 0; i <= 0xFF; i++)
                                {
                                    string uniqueTag = tag.ToLower()
                                                       .Replace("xx",
                                                                string.Format("{0:X2}", i));
                                    DataElementDictionaryEntry entry =
                                        new DataElementDictionaryEntry(
                                            uniqueTag,
                                            result[1].Trim(), result[2].Trim(),
                                            result[3].Trim(), retired);
                                    Add(entry);
                                }
                            }
                            else
                            {
                                DataElementDictionaryEntry entry =
                                    new DataElementDictionaryEntry(tag,
                                                                   result[1].Trim(), result[2].Trim(),
                                                                   result[3].Trim(), retired);
                                Add(entry);
                            }
                        }
                        catch (Exception e)
                        {
                            throw new DicomException("Wrong entry in line " +
                                                     lineNumber.ToString() + ": " + e.Message);
                        }
                    }
                    else
                    {
                        throw new DicomException("Wrong entry in line " +
                                                 lineNumber.ToString() + ".");
                    }
                }
                line = textReader.ReadLine();
                lineNumber++;
            }
        }
 /// <summary>
 ///     Determines whether this instance is equal to another
 ///     data element dictionary entry instance.
 /// </summary>
 public bool Equals(DataElementDictionaryEntry dictionaryEntry)
 {
     return(CompareTo(dictionaryEntry) == 0);
 }