using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace PSReleaseNotesGenerator
{
///
/// Class for working with overrides XML data.
///
public class Overrides
{
///
/// Parses the overrides XML content for the value for each C2JFilename in
/// VALUE then looks up the
/// ServiceNounPrefix for each service configuration.
///
/// A loaded overrides.xml file content
/// Callback to load the service configuration for the passed C2jFilename.
/// A HashSet containing each ServiceNounPrefix for each service in the overrides XML
public static HashSet ParseServiceNounPrefixes(string overridesXML, Func ServiceConfigLoader)
{
if(string.IsNullOrEmpty(overridesXML))
{
return new HashSet();
}
var xdoc = XDocument.Parse(overridesXML);
var c2jFilenames = xdoc.Descendants()
.Where(p => p.Name.LocalName == "C2jFilename")
.Select(element => element.Value)
.ToList();
var exceptions = new List();
var serviceNounsPrefixes = new HashSet();
foreach(var filetitle in c2jFilenames)
{
try
{
var serviceConfigXML = ServiceConfigLoader(filetitle);
xdoc = XDocument.Parse(serviceConfigXML);
serviceNounsPrefixes.Add(xdoc.Root.Element("ServiceNounPrefix").Value);
}
catch(Exception e)
{
exceptions.Add(new Exception($"Error processing '{filetitle}': {e.Message}", e));
}
}
if (exceptions.Any())
{
throw new AggregateException("Error(s) occurred while processing service configurations.", exceptions);
}
return serviceNounsPrefixes;
}
}
}