Files
DP44/Common/DTS.Common/XMLUtils/TestMetadataXml.cs
2026-04-17 14:55:32 -04:00

99 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using DTS.Common.Utilities.Logging;
using DTS.Common.Utils;
// ReSharper disable UnusedVariable
namespace DTS.Common.XMLUtils
{
public static class TestMetadataXml
{
/// <summary>
/// Return list of files
/// </summary>
/// <param name="path"></param>
/// <param name="file"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static XDocument GetTestMetadataXml(string path, string file = "", string pattern = "")
{
if (string.IsNullOrEmpty(pattern)) pattern = ".dts";
FileUtils.FileList = new List<string>();
if (!String.IsNullOrEmpty(file))
{ FileUtils.FileList.Add(file); }
else
{ FileUtils.FindFiles(path, pattern); }
return SetTestMetadataType(FileUtils.FileList);
}
/// <summary>
/// Read all DTS xml files
/// </summary>
/// <param name="fileList">DTS xml file list</param>
/// <returns></returns>
private static XDocument SetTestMetadataType(IEnumerable<string> fileList)
{
try
{
var count = 0;
var x = new XDocument();
x.Add(new XElement("Tests"));
foreach (var file in fileList)
{
try
{
var xTestMetadata = new XElement("TestMetadata");
xTestMetadata.SetAttributeValue("Id", count++.ToString());
using (var reader = new StreamReader(file))
{
var allText = reader.ReadToEnd();
var delimiter = "<?xml version=";
var delimiterArray = new string[] { delimiter };
var allTextArray = allText.Split(delimiterArray, StringSplitOptions.RemoveEmptyEntries);
var xmlCounter = 0;
foreach (var fullstring in allTextArray)
{
var fullString = delimiter + allTextArray[xmlCounter];
xmlCounter++;
var sr = new StringReader(fullString);
var xDoc = XDocument.Load(sr);
if (xDoc.Element("Test") != null)
{
//remove file name and check folder
xDoc.Element("Test")
?.SetAttributeValue("DataType", file.Contains("ALL") ? "ALL" : "ROI");
xDoc.Element("Test")?.SetAttributeValue("FilePath", file);
xDoc.Element("Test")?.SetAttributeValue("FileDate", File.GetCreationTime(file));
xTestMetadata.Add(xDoc.Element("Test"));
}
else
{
xTestMetadata.Add(xDoc.Element("TestSetup"));
}
}
}
x.Root?.Add(xTestMetadata);
}
catch (Exception ex)
{
APILogger.Log("failed to process file:", file, ex);
}
}
return x;
}
catch (Exception ex)
{
var error = ex.Message;
return new XDocument();
}
}
}
}