Ejemplo n.º 1
0
        public InsertStudyXmlCommand(DicomFile file, StudyXml studyXml, StudyLocation storageLocation, bool writeFile)
            : base("Insert Study Xml", true)
        {
            _file = file;
            _studyXml = studyXml;
            _studyStorageLocation = storageLocation;
            _writeFile = writeFile;

            _settings = new StudyXmlOutputSettings
                            {
                                IncludePrivateValues = StudyXmlTagInclusion.IgnoreTag,
                                IncludeUnknownTags = StudyXmlTagInclusion.IgnoreTag,
                                IncludeLargeTags = StudyXmlTagInclusion.IncludeTagExclusion,
                                MaxTagLength = 2048,
                                IncludeSourceFileName = true
                            };
        }
Ejemplo n.º 2
0
		public void Create()
		{
			
			SelectFolderDialogCreationArgs args = new SelectFolderDialogCreationArgs();
			args.Path = _lastFolder ?? Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

			FileDialogResult result = base.Context.DesktopWindow.ShowSelectFolderDialogBox(args);
			if (result.Action == DialogBoxAction.Ok)
			{
				_lastFolder = result.FileName;

				StudyLoaderExtensionPoint xp = new StudyLoaderExtensionPoint();
				IStudyLoader loader = (IStudyLoader)CollectionUtils.SelectFirst(xp.CreateExtensions(),
					delegate(object extension) { return ((IStudyLoader) extension).Name == "DICOM_LOCAL";});

				var selected = base.Context.SelectedStudy;

				loader.Start(new StudyLoaderArgs(selected.StudyInstanceUid, selected.Server, null));
				StudyXml xml = new StudyXml();
				Sop sop;
				
				while (null != (sop = loader.LoadNextSop()))
				{
					xml.AddFile(((ILocalSopDataSource) sop.DataSource).File);
				}

				StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
				settings.IncludePrivateValues = StudyXmlTagInclusion.IgnoreTag;
				settings.IncludeUnknownTags = StudyXmlTagInclusion.IgnoreTag;
				settings.MaxTagLength = 100 * 1024;
				settings.IncludeSourceFileName = true;

				XmlDocument doc = xml.GetMemento(settings);
				string fileName = System.IO.Path.Combine(result.FileName, "studyxml.xml");

				XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.UTF8);
				writer.Formatting = Formatting.Indented;
				writer.Indentation = 5;
				
				doc.Save(writer);
			}
		}
Ejemplo n.º 3
0
        internal XmlElement GetMemento(XmlDocument theDocument, StudyXmlOutputSettings settings)
        {
            if (_cachedElement != null)
            {
                return(_cachedElement);
            }

            if (_baseInstance != null)
            {
                _cachedElement = GetMementoForCollection(theDocument, _baseInstance.Collection, Collection, settings);

                // Only keep around the cached xml data, free the collection to reduce memory usage
                SwitchToCachedXml();

                return(_cachedElement);
            }

            _cachedElement = GetMementoForCollection(theDocument, null, Collection, settings);
            return(_cachedElement);
        }
Ejemplo n.º 4
0
        internal XmlElement GetMemento(XmlDocument theDocument, StudyXmlOutputSettings settings)
        {
            _dirty = false;
            // Calc the base attributes
            CalculateBaseCollectionForSeries();

            XmlElement series = theDocument.CreateElement("Series");

            XmlAttribute seriesInstanceUid = theDocument.CreateAttribute("UID");

            seriesInstanceUid.Value = _seriesInstanceUid;
            series.Attributes.Append(seriesInstanceUid);

            XmlElement baseElement = theDocument.CreateElement("BaseInstance");

            // If there's only 1 total image in the series, leave an empty base instance
            // and just have the entire image be stored.
            if (_instanceList.Count > 1)
            {
                XmlElement baseInstance = _seriesTagsStream.GetMemento(theDocument, settings);
                baseElement.AppendChild(baseInstance);
            }
            else
            {
                _seriesTagsStream = null;
            }

            series.AppendChild(baseElement);

            foreach (InstanceXml instance in _instanceList.Values)
            {
                instance.SetBaseInstance(_seriesTagsStream);
                XmlElement instanceElement = instance.GetMemento(theDocument, settings);

                series.AppendChild(instanceElement);
            }

            return(series);
        }
Ejemplo n.º 5
0
        private InstanceXml GetInstanceXml(StudyXmlOutputSettings outputSettings, out DicomFile real)
        {
            var xml = new StudyXml();
            real = new DicomFile();
            SetupMR(real.DataSet);
            real.MediaStorageSopClassUid = real.DataSet[DicomTags.SopClassUid].ToString();
            real.MetaInfo[DicomTags.SopClassUid].SetString(0, real.DataSet[DicomTags.SopClassUid].ToString());
            
            var bytes = new Byte[2048];
            real.DataSet[DicomTags.RedPaletteColorLookupTableData].Values = bytes;

            var privateTag = new DicomTag(0x00111301, "Private Tag", "PrivateTag", DicomVr.CSvr, false, 1, 1, false);
            real.DataSet[privateTag].SetString(0, "Private");

            var sequences = (DicomSequenceItem[])real.DataSet[DicomTags.RequestAttributesSequence].Values;
            var firstItem = sequences.First();
            firstItem[DicomTags.RedPaletteColorLookupTableData].Values = bytes;

            var attr = real.DataSet[DicomTags.ReferencedStudySequence];
            var sequenceItem = new DicomSequenceItem();
            attr.AddSequenceItem(sequenceItem);
            sequenceItem[privateTag].SetString(0, "Private");

            xml.AddFile(real);
            
            var memento = xml.GetMemento(outputSettings ?? new StudyXmlOutputSettings
            {
                IncludeLargeTags = StudyXmlTagInclusion.IncludeTagExclusion,
                IncludePrivateValues = StudyXmlTagInclusion.IgnoreTag,
                MaxTagLength = 1024
            });

            xml = new StudyXml();
            xml.SetMemento(memento);
            return xml.First().First();
        }
Ejemplo n.º 6
0
        public void Test_Excluded_PrivateSequenceAttribute()
        {
            DicomFile real;
            var outputSettings = new StudyXmlOutputSettings
            {
                //If this were enabled, it would work.
                //IncludePrivateValues = StudyXmlTagInclusion.IncludeTagExclusion
            };

            var instanceXml = GetInstanceXml(outputSettings, out real);
            using (var instance = new TestInstance(instanceXml, real))
            {
                Assert.AreEqual("MR", instance[DicomTags.Modality].ToString());
                Assert.AreEqual(false, instance._fullHeaderLoaded);

                var sequences = (DicomSequenceItem[])instance[DicomTags.ReferencedStudySequence].Values;
                Assert.AreEqual(true, instance._fullHeaderLoaded);

                var sequenceItem = sequences.First();
                var privateAttribute = sequenceItem[0x00111301];
                Assert.AreEqual("Private", privateAttribute.ToString());
            }
        }
Ejemplo n.º 7
0
        private XmlElement GetMementoForCollection(XmlDocument theDocument, DicomAttributeCollection baseCollection,
                                                   DicomAttributeCollection collection, StudyXmlOutputSettings settings)
        {
            XmlElement instance;

            if (collection is DicomSequenceItem)
            {
                instance = theDocument.CreateElement("Item");
            }
            else
            {
                instance = theDocument.CreateElement("Instance");

                XmlAttribute sopInstanceUid = theDocument.CreateAttribute("UID");
                sopInstanceUid.Value = _sopInstanceUid;
                instance.Attributes.Append(sopInstanceUid);

                if (_sopClass != null)
                {
                    XmlAttribute sopClassAttribute = theDocument.CreateAttribute("SopClassUID");
                    sopClassAttribute.Value = _sopClass.Uid;
                    instance.Attributes.Append(sopClassAttribute);
                }

                if (_transferSyntax != null && !(this is BaseInstanceXml))
                {
                    XmlAttribute transferSyntaxAttribute = theDocument.CreateAttribute("TransferSyntaxUID");
                    transferSyntaxAttribute.Value = _transferSyntax.UidString;
                    instance.Attributes.Append(transferSyntaxAttribute);
                }

                if (_sourceAETitle != null)
                {
                    XmlAttribute sourceAEAttribute = theDocument.CreateAttribute("SourceAETitle");
                    sourceAEAttribute.Value = XmlEscapeString(_sourceAETitle);
                    instance.Attributes.Append(sourceAEAttribute);
                }

                if (_sourceFileName != null && settings.IncludeSourceFileName)
                {
                    XmlAttribute sourceFileNameAttribute = theDocument.CreateAttribute("SourceFileName");
                    string       fileName = SecurityElement.Escape(_sourceFileName);
                    sourceFileNameAttribute.Value = fileName;
                    instance.Attributes.Append(sourceFileNameAttribute);
                }

                if (_fileSize != 0)
                {
                    XmlAttribute fileSize = theDocument.CreateAttribute("FileSize");
                    fileSize.Value = _fileSize.ToString();
                    instance.Attributes.Append(fileSize);
                }
            }

            IPrivateInstanceXmlDicomAttributeCollection thisCollection = null;

            if (collection is IPrivateInstanceXmlDicomAttributeCollection)
            {
                thisCollection = (IPrivateInstanceXmlDicomAttributeCollection)collection;
                foreach (DicomTag tag in thisCollection.ExcludedTags)
                {
                    //Artificially seed the collection with empty attributes from this
                    //instance and the base instance so we can add them in the right order.
                    //Note in the case of the base instance, this will never alter
                    //the collection because the empty attribute is already there (see ParseAttribute).
                    DicomAttribute attribute = collection[tag];
                }
            }

            IEnumerator <DicomAttribute> baseIterator = null;
            IPrivateInstanceXmlDicomAttributeCollection privateBaseCollection = null;

            if (baseCollection != null)
            {
                privateBaseCollection = baseCollection as IPrivateInstanceXmlDicomAttributeCollection;
                if (privateBaseCollection != null)
                {
                    foreach (DicomTag tag in privateBaseCollection.ExcludedTags)
                    {
                        //Artificially seed the collection with empty attributes from this
                        //instance and the base instance so we can add them in the right order.
                        //Note in the case of the base instance, this will never alter
                        //the collection because the empty attribute is already there (see ParseAttribute).
                        DicomAttribute attribute = collection[tag];
                    }
                }

                baseIterator = baseCollection.GetEnumerator();
                if (!baseIterator.MoveNext())
                {
                    baseIterator = null;
                }
            }

            List <DicomTag> newlyExcludedTags = new List <DicomTag>();

            foreach (DicomAttribute attribute in collection)
            {
                bool isExcludedFromThisCollection = thisCollection != null &&
                                                    thisCollection.IsTagExcluded(attribute.Tag.TagValue);

                bool isExcludedFromBase = privateBaseCollection != null &&
                                          privateBaseCollection.ExcludedTagsHelper.IsTagExcluded(attribute.Tag.TagValue);

                bool isInBase       = isExcludedFromBase;
                bool isSameAsInBase = isExcludedFromThisCollection && isExcludedFromBase;

                if (baseIterator != null)
                {
                    while (baseIterator != null && baseIterator.Current.Tag < attribute.Tag)
                    {
                        if (!baseIterator.Current.IsEmpty)
                        {
                            XmlElement emptyAttributeElement = CreateDicomAttributeElement(theDocument, baseIterator.Current, "EmptyAttribute");
                            instance.AppendChild(emptyAttributeElement);
                        }

                        if (!baseIterator.MoveNext())
                        {
                            baseIterator = null;
                        }
                    }

                    if (baseIterator != null)
                    {
                        if (baseIterator.Current.Tag == attribute.Tag)
                        {
                            isInBase = !baseIterator.Current.IsEmpty || isExcludedFromBase;

                            bool isEmpty       = attribute.IsEmpty && !isExcludedFromThisCollection;
                            bool isEmptyInBase = baseIterator.Current.IsEmpty && !isExcludedFromBase;

                            isSameAsInBase = (isExcludedFromThisCollection && isExcludedFromBase) ||
                                             (isEmpty && isEmptyInBase);

                            if (!baseIterator.Current.IsEmpty && !isExcludedFromBase)
                            {
                                if (!(attribute is DicomAttributeOB) &&
                                    !(attribute is DicomAttributeOW) &&
                                    !(attribute is DicomAttributeOF) &&
                                    !(attribute is DicomFragmentSequence))
                                {
                                    if (attribute.Equals(baseIterator.Current))
                                    {
                                        isSameAsInBase = true;
                                    }
                                }
                            }

                            // Move to the next attribute for the next time through the loop
                            if (!baseIterator.MoveNext())
                            {
                                baseIterator = null;
                            }
                        }
                    }
                }

                //by this point, equality has been covered for both attributes with values, empty attributes and excluded attributes.
                if (isSameAsInBase)
                {
                    continue;
                }

                if (isExcludedFromThisCollection)
                {
                    XmlElement excludedAttributeElement = CreateDicomAttributeElement(theDocument, attribute, "ExcludedAttribute");
                    instance.AppendChild(excludedAttributeElement);
                    continue;
                }

                if (attribute.IsEmpty)
                {
                    //Only store an empty attribute if it is in the base (either non-empty or excluded).
                    if (isInBase)
                    {
                        XmlElement emptyAttributeElement = CreateDicomAttributeElement(theDocument, attribute, "EmptyAttribute");
                        instance.AppendChild(emptyAttributeElement);
                    }
                    continue;
                }

                StudyXmlTagInclusion inclusion = AttributeShouldBeIncluded(attribute, settings);
                if (inclusion == StudyXmlTagInclusion.IncludeTagExclusion)
                {
                    newlyExcludedTags.Add(attribute.Tag);
                    if (!isExcludedFromBase)
                    {
                        XmlElement excludedAttributeElement = CreateDicomAttributeElement(theDocument, attribute, "ExcludedAttribute");
                        instance.AppendChild(excludedAttributeElement);
                    }
                    continue;
                }
                if (inclusion == StudyXmlTagInclusion.IgnoreTag)
                {
                    continue;
                }

                XmlElement instanceElement = CreateDicomAttributeElement(theDocument, attribute, "Attribute");
                if (attribute is DicomAttributeSQ)
                {
                    DicomSequenceItem[] items = (DicomSequenceItem[])attribute.Values;
                    foreach (DicomSequenceItem item in items)
                    {
                        XmlElement itemElement = GetMementoForCollection(theDocument, null, item, settings);

                        instanceElement.AppendChild(itemElement);
                    }
                }
                else if (attribute is DicomAttributeOW || attribute is DicomAttributeUN)
                {
                    byte[] val = (byte[])attribute.Values;

                    StringBuilder str = null;
                    foreach (byte i in val)
                    {
                        if (str == null)
                        {
                            str = new StringBuilder(i.ToString());
                        }
                        else
                        {
                            str.AppendFormat("\\{0}", i);
                        }
                    }
                    if (str != null)
                    {
                        instanceElement.InnerText = str.ToString();
                    }
                }
                else if (attribute is DicomAttributeOF)
                {
                    float[]       val = (float[])attribute.Values;
                    StringBuilder str = null;
                    foreach (float i in val)
                    {
                        if (str == null)
                        {
                            str = new StringBuilder(i.ToString());
                        }
                        else
                        {
                            str.AppendFormat("\\{0}", i);
                        }
                    }
                    if (str != null)
                    {
                        instanceElement.InnerText = str.ToString();
                    }
                }
                else
                {
                    instanceElement.InnerText = XmlEscapeString(attribute);
                }

                instance.AppendChild(instanceElement);
            }

            //fill in empty attributes past the end of this collection
            while (baseIterator != null)
            {
                if (!baseIterator.Current.IsEmpty)
                {
                    XmlElement emptyAttributeElement = CreateDicomAttributeElement(theDocument, baseIterator.Current, "EmptyAttribute");
                    instance.AppendChild(emptyAttributeElement);
                }

                if (!baseIterator.MoveNext())
                {
                    baseIterator = null;
                }
            }

            if (thisCollection != null)
            {
                //when this is the base collection, 'thisCollection' will never be null.
                //when this is not the base collection, we switch to 'cached xml' right after this, anyway,
                //so the fact that this won't occur is ok.
                foreach (DicomTag tag in newlyExcludedTags)
                {
                    collection[tag].SetEmptyValue();
                    thisCollection.ExcludedTagsHelper.Add(tag);
                }
            }

            return(instance);
        }
Ejemplo n.º 8
0
        private static StudyXmlTagInclusion AttributeShouldBeIncluded(DicomAttribute attribute, StudyXmlOutputSettings settings)
        {
            if (settings == null)
            {
                return(StudyXmlTagInclusion.IncludeTagValue);
            }

            if (attribute is DicomAttributeSQ)
            {
                if (attribute.Tag.IsPrivate)
                {
                    return(settings.IncludePrivateValues);
                }
                return(StudyXmlTagInclusion.IncludeTagValue);
            }


            // private tag
            if (attribute.Tag.IsPrivate)
            {
                if (settings.IncludePrivateValues != StudyXmlTagInclusion.IncludeTagValue)
                {
                    return(settings.IncludePrivateValues);
                }
            }

            // check type
            if (attribute is DicomAttributeUN)
            {
                if (settings.IncludeUnknownTags != StudyXmlTagInclusion.IncludeTagValue)
                {
                    return(settings.IncludeUnknownTags);
                }
            }

            // This check isn't needed, but it bypasses the StreamLength calculation if its not needed
            if (settings.IncludeLargeTags == StudyXmlTagInclusion.IncludeTagValue)
            {
                return(settings.IncludeLargeTags);
            }

            // check the size
            ulong length = attribute.StreamLength;

            if (length <= settings.MaxTagLength)
            {
                return(StudyXmlTagInclusion.IncludeTagValue);
            }

            // Move here, such that we first check if the tag should be excluded
            if ((attribute is DicomAttributeOB) ||
                (attribute is DicomAttributeOW) ||
                (attribute is DicomAttributeOF) ||
                (attribute is DicomFragmentSequence))
            {
                return(StudyXmlTagInclusion.IncludeTagExclusion);
            }

            return(settings.IncludeLargeTags);
        }
Ejemplo n.º 9
0
		private XmlElement GetMementoForCollection(XmlDocument theDocument, DicomAttributeCollection baseCollection,
												   DicomAttributeCollection collection, StudyXmlOutputSettings settings)
		{
			XmlElement instance;

			if (collection is DicomSequenceItem)
			{
				instance = theDocument.CreateElement("Item");
			}
			else
			{
				instance = theDocument.CreateElement("Instance");

				XmlAttribute sopInstanceUid = theDocument.CreateAttribute("UID");
				sopInstanceUid.Value = _sopInstanceUid;
				instance.Attributes.Append(sopInstanceUid);

				if (_sopClass != null)
				{
					XmlAttribute sopClassAttribute = theDocument.CreateAttribute("SopClassUID");
					sopClassAttribute.Value = _sopClass.Uid;
					instance.Attributes.Append(sopClassAttribute);
				}

				if (_transferSyntax != null && !(this is BaseInstanceXml))
				{
					XmlAttribute transferSyntaxAttribute = theDocument.CreateAttribute("TransferSyntaxUID");
					transferSyntaxAttribute.Value = _transferSyntax.UidString;
					instance.Attributes.Append(transferSyntaxAttribute);
				}

				if (_sourceFileName != null && settings.IncludeSourceFileName)
				{
					XmlAttribute sourceFileNameAttribute = theDocument.CreateAttribute("SourceFileName");
					string fileName = SecurityElement.Escape(_sourceFileName);
					sourceFileNameAttribute.Value = fileName;
					instance.Attributes.Append(sourceFileNameAttribute);
				}

				if (_fileSize != 0)
				{
					XmlAttribute fileSize = theDocument.CreateAttribute("FileSize");
					fileSize.Value = _fileSize.ToString();
					instance.Attributes.Append(fileSize);
				}
			}

			IPrivateInstanceXmlDicomAttributeCollection thisCollection = null;
			if (collection is IPrivateInstanceXmlDicomAttributeCollection)
			{
				thisCollection = (IPrivateInstanceXmlDicomAttributeCollection)collection;
				foreach (DicomTag tag in thisCollection.ExcludedTags)
				{
					//Artificially seed the collection with empty attributes from this
					//instance and the base instance so we can add them in the right order.
					//Note in the case of the base instance, this will never alter
					//the collection because the empty attribute is already there (see ParseAttribute).
					DicomAttribute attribute = collection[tag];
				}
			}

			IEnumerator<DicomAttribute> baseIterator = null;
			IPrivateInstanceXmlDicomAttributeCollection privateBaseCollection = null;
			if (baseCollection != null)
			{
				privateBaseCollection = baseCollection as IPrivateInstanceXmlDicomAttributeCollection;
				if (privateBaseCollection != null)
				{
					foreach (DicomTag tag in privateBaseCollection.ExcludedTags)
					{
						//Artificially seed the collection with empty attributes from this
						//instance and the base instance so we can add them in the right order.
						//Note in the case of the base instance, this will never alter
						//the collection because the empty attribute is already there (see ParseAttribute).
						DicomAttribute attribute = collection[tag];
					}
				}

				baseIterator = baseCollection.GetEnumerator();
				if (!baseIterator.MoveNext())
					baseIterator = null;
			}

			List<DicomTag> newlyExcludedTags = new List<DicomTag>();

			foreach (DicomAttribute attribute in collection)
			{
				bool isExcludedFromThisCollection = thisCollection != null && 
					thisCollection.IsTagExcluded(attribute.Tag.TagValue);

				bool isExcludedFromBase = privateBaseCollection != null && 
					privateBaseCollection.ExcludedTagsHelper.IsTagExcluded(attribute.Tag.TagValue);

				bool isInBase = isExcludedFromBase;
				bool isSameAsInBase = isExcludedFromThisCollection && isExcludedFromBase;

				if (baseIterator != null)
				{
					while (baseIterator != null && baseIterator.Current.Tag < attribute.Tag)
					{
						if (!baseIterator.Current.IsEmpty)
						{
							XmlElement emptyAttributeElement = CreateDicomAttributeElement(theDocument, baseIterator.Current, "EmptyAttribute");
							instance.AppendChild(emptyAttributeElement);
						}

						if (!baseIterator.MoveNext())
							baseIterator = null;
					}

					if (baseIterator != null)
					{
						if (baseIterator.Current.Tag == attribute.Tag)
						{
							isInBase = !baseIterator.Current.IsEmpty || isExcludedFromBase;

							bool isEmpty = attribute.IsEmpty && !isExcludedFromThisCollection;
							bool isEmptyInBase = baseIterator.Current.IsEmpty && !isExcludedFromBase;

							isSameAsInBase = (isExcludedFromThisCollection && isExcludedFromBase)
							                 || (isEmpty && isEmptyInBase);

							if (!baseIterator.Current.IsEmpty && !isExcludedFromBase)
							{
								if (!(attribute is DicomAttributeOB)
								    && !(attribute is DicomAttributeOW)
								    && !(attribute is DicomAttributeOF)
								    && !(attribute is DicomFragmentSequence))
								{
									if (attribute.Equals(baseIterator.Current))
										isSameAsInBase = true;
								}
							}

							// Move to the next attribute for the next time through the loop
							if (!baseIterator.MoveNext())
								baseIterator = null;
						}
					}
				}
				
				//by this point, equality has been covered for both attributes with values, empty attributes and excluded attributes.
				if (isSameAsInBase)
					continue;

				if (isExcludedFromThisCollection)
				{
					XmlElement excludedAttributeElement = CreateDicomAttributeElement(theDocument, attribute, "ExcludedAttribute");
					instance.AppendChild(excludedAttributeElement);
					continue;
				}

				if (attribute.IsEmpty)
				{
					//Only store an empty attribute if it is in the base (either non-empty or excluded).
					if (isInBase)
					{
						XmlElement emptyAttributeElement = CreateDicomAttributeElement(theDocument, attribute, "EmptyAttribute");
						instance.AppendChild(emptyAttributeElement);
					}
					continue;
				}

				StudyXmlTagInclusion inclusion = AttributeShouldBeIncluded(attribute, settings);
				if (inclusion == StudyXmlTagInclusion.IncludeTagExclusion)
				{
					newlyExcludedTags.Add(attribute.Tag);
					if (!isExcludedFromBase)
					{
						XmlElement excludedAttributeElement = CreateDicomAttributeElement(theDocument, attribute, "ExcludedAttribute");
						instance.AppendChild(excludedAttributeElement);
					}
					continue;
				}
				if (inclusion == StudyXmlTagInclusion.IgnoreTag)
				{
					continue;
				}

				XmlElement instanceElement = CreateDicomAttributeElement(theDocument, attribute, "Attribute");
				if (attribute is DicomAttributeSQ)
				{
					DicomSequenceItem[] items = (DicomSequenceItem[])attribute.Values;
					foreach (DicomSequenceItem item in items)
					{
						XmlElement itemElement = GetMementoForCollection(theDocument, null, item, settings);

						instanceElement.AppendChild(itemElement);
					}
				}
				else if (attribute is DicomAttributeOW || attribute is DicomAttributeUN)
				{
					byte[] val = (byte[])attribute.Values;

					StringBuilder str = null;
					foreach (byte i in val)
					{
						if (str == null)
							str = new StringBuilder(i.ToString());
						else
							str.AppendFormat("\\{0}", i);
					}
					if (str != null)
						instanceElement.InnerText = str.ToString();
				}
				else if (attribute is DicomAttributeOF)
				{
					float[] val = (float[])attribute.Values;
					StringBuilder str = null;
					foreach (float i in val)
					{
						if (str == null)
							str = new StringBuilder(i.ToString());
						else
							str.AppendFormat("\\{0}", i);
					}
					if (str != null)
						instanceElement.InnerText = str.ToString();
				}
				else
				{
					instanceElement.InnerText = XmlEscapeString(attribute);
				}

				instance.AppendChild(instanceElement);
			}

			//fill in empty attributes past the end of this collection
			while(baseIterator != null)
			{
				if (!baseIterator.Current.IsEmpty)
				{
					XmlElement emptyAttributeElement = CreateDicomAttributeElement(theDocument, baseIterator.Current, "EmptyAttribute");
					instance.AppendChild(emptyAttributeElement);
				}

				if (!baseIterator.MoveNext())
					baseIterator = null;
			}

			if (thisCollection != null)
			{
				//when this is the base collection, 'thisCollection' will never be null.
				//when this is not the base collection, we switch to 'cached xml' right after this, anyway,
				//so the fact that this won't occur is ok.
				foreach (DicomTag tag in newlyExcludedTags)
				{
					collection[tag].SetEmptyValue();
					thisCollection.ExcludedTagsHelper.Add(tag);
				}
			}

			return instance;
		}
Ejemplo n.º 10
0
		private static StudyXmlTagInclusion AttributeShouldBeIncluded(DicomAttribute attribute, StudyXmlOutputSettings settings)
		{
			if (settings == null)
				return StudyXmlTagInclusion.IncludeTagValue;

			if (attribute is DicomAttributeSQ)
			{
				if (attribute.Tag.IsPrivate)
					return settings.IncludePrivateValues;
				return StudyXmlTagInclusion.IncludeTagValue;
			}


			// private tag
			if (attribute.Tag.IsPrivate)
			{
				if (settings.IncludePrivateValues != StudyXmlTagInclusion.IncludeTagValue)
					return settings.IncludePrivateValues;
			}

			// check type
			if (attribute is DicomAttributeUN)
			{
				if (settings.IncludeUnknownTags != StudyXmlTagInclusion.IncludeTagValue)
					return settings.IncludeUnknownTags;
			}

			// This check isn't needed, but it bypasses the StreamLength calculation if its not needed
			if (settings.IncludeLargeTags == StudyXmlTagInclusion.IncludeTagValue)
				return settings.IncludeLargeTags;

			// check the size
			ulong length = attribute.StreamLength;
			if (length <= settings.MaxTagLength)
				return StudyXmlTagInclusion.IncludeTagValue;

			// Move here, such that we first check if the tag should be excluded
			if ((attribute is DicomAttributeOB)
			 || (attribute is DicomAttributeOW)
			 || (attribute is DicomAttributeOF)
			 || (attribute is DicomFragmentSequence))
				return StudyXmlTagInclusion.IncludeTagExclusion;

			return settings.IncludeLargeTags;
		}
Ejemplo n.º 11
0
		internal XmlElement GetMemento(XmlDocument theDocument, StudyXmlOutputSettings settings)
		{
			if (_cachedElement != null)
			{
				return _cachedElement;
			}

			if (_baseInstance != null)
			{
				_cachedElement = GetMementoForCollection(theDocument, _baseInstance.Collection, Collection, settings);

				// Only keep around the cached xml data, free the collection to reduce memory usage
				SwitchToCachedXml();

				return _cachedElement;
			}

			_cachedElement = GetMementoForCollection(theDocument, null, Collection, settings);
			return _cachedElement;
		}
Ejemplo n.º 12
0
 /// <summary>
 /// Add a <see cref="DicomFile"/> to the StudyXml.
 /// </summary>
 /// <param name="theFile">The <see cref="DicomFile"/> to add.</param>
 /// <param name="fileSize">The size in bytes of the file being added.</param>
 /// <param name="settings">The settings used when writing out the file.</param>
 /// <returns>true on scuccess.</returns>
 public bool AddFile(DicomFile theFile, long fileSize, StudyXmlOutputSettings settings)
 {
     return(AddFile(theFile, null, fileSize, settings));
 }
Ejemplo n.º 13
0
        private void _buttonGenerateXml_Click(object sender, EventArgs e)
        {
            saveFileDialog.DefaultExt = "xml";
            saveFileDialog.ShowDialog();

			StudyXmlOutputSettings settings = new StudyXmlOutputSettings();
        	settings.IncludeSourceFileName = false;
            XmlDocument doc = _theStream.GetMemento(settings);

            Stream fileStream = saveFileDialog.OpenFile();

            StudyXmlIo.Write(doc, fileStream);

            fileStream.Close();
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Save the <see cref="StudyXml"/> file for a study.
        /// </summary>
        /// <param name="studyXml">The <see cref="StudyXml"/> file to save.</param>
        /// <param name="fileCreated">flag set to true if the file was created</param>
        public void SaveStudyXml(StudyXml studyXml, out bool fileCreated)
        {
            var settings = new StudyXmlOutputSettings
            {
                IncludePrivateValues = StudyXmlTagInclusion.IgnoreTag,
                IncludeUnknownTags = StudyXmlTagInclusion.IgnoreTag,
                IncludeLargeTags = StudyXmlTagInclusion.IncludeTagExclusion,
                MaxTagLength = 2048,
                IncludeSourceFileName = true
            };

            var doc = studyXml.GetMemento(settings);
            string streamFile = GetStudyXmlPath();

            // allocate the random number generator here, in case we need it below
            var rand = new Random();
            string tmpStreamFile = streamFile + "_tmp";
            for (int i = 0; ; i++)
                try
                {
                    if (File.Exists(tmpStreamFile))
                        FileUtils.Delete(tmpStreamFile);

                    using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(tmpStreamFile, FileMode.CreateNew))
                    {
                        StudyXmlIo.Write(doc, xmlStream);
                        xmlStream.Close();
                    }

                    File.Copy(tmpStreamFile, streamFile, true);
                    fileCreated = true;
                    FileUtils.Delete(tmpStreamFile);
                    return;
                }
                catch (IOException)
                {
                    if (i < 5)
                    {
                        Thread.Sleep(rand.Next(5, 50)); // Sleep 5-50 milliseconds
                        continue;
                    }

                    throw;
                }
        }
Ejemplo n.º 15
0
        internal StudyXmlMemento.StudyXmlNode GetMemento(StudyXmlMemento theDocument, StudyXmlOutputSettings settings)
        {
            _dirty = false;
            // Calc the base attributes
            CalculateBaseCollectionForSeries();

            var seriesElement = new StudyXmlMemento.StudyXmlNode {
                ElementName = "Series"
            };

            seriesElement.AddAttribute("UID", _seriesInstanceUid);


            // If there's only 1 total image in the series, leave an empty base instance
            // and just have the entire image be stored.
            if (_instanceList.Count > 1)
            {
                if (!string.IsNullOrEmpty(_seriesTagsStream.XmlFragment))
                {
                    seriesElement.AddChild(new StudyXmlMemento.StudyXmlNode(_seriesTagsStream.XmlFragment));
                }
                else
                {
                    XmlElement baseElement  = theDocument.Document.CreateElement("BaseInstance");
                    XmlElement baseInstance = _seriesTagsStream.GetMemento(theDocument.Document, settings);
                    baseElement.AppendChild(baseInstance);

                    var baseNode = new StudyXmlMemento.StudyXmlNode(baseElement);
                    _seriesTagsStream.XmlFragment = baseNode.XmlElementFragment;
                    seriesElement.AddChild(baseNode);
                }
            }
            else
            {
                _seriesTagsStream = null;
                seriesElement.AddChild(new StudyXmlMemento.StudyXmlNode("<BaseInstance/>"));
            }

            foreach (InstanceXml instance in _instanceList.Values)
            {
                instance.SetBaseInstance(_seriesTagsStream);

                if (!string.IsNullOrEmpty(instance.XmlFragment))
                {
                    seriesElement.AddChild(new StudyXmlMemento.StudyXmlNode(instance.XmlFragment));
                }
                else
                {
                    XmlElement instanceElement = instance.GetMemento(theDocument.Document, settings);

                    var node = new StudyXmlMemento.StudyXmlNode(instanceElement);
                    instance.XmlFragment = node.XmlElementFragment;
                    seriesElement.AddChild(node);
                }
            }

            return(seriesElement);
        }