// Copyright (C) Stichting Deltares 2024. All rights reserved.
//
// This file is part of the Dam Engine.
//
// The Dam Engine is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System.Linq;
using System.Text.RegularExpressions;
using Deltares.DamEngine.Io.XmlInput;
namespace Deltares.DamEngine.TestHelpers;
///
/// Helper class for adapting xml in tests
///
public static class XmlAdapter
{
///
/// Changes in an XML the value of all elements with the specified key to the specified value.
/// The key will be found only if it is a whole word.
///
/// The input.
/// The key.
/// The value.
/// The modified input
public static string ChangeValueInXml(string input, string key, string value)
{
string searchString = "\\b" + key + "=" + "\"([^\"]*)\""; // This means match any pattern which matches =""
string replacement = key + "=\"" + value + "\""; // Replace with =""
string result = Regex.Replace(input, searchString, replacement, RegexOptions.IgnorePatternWhitespace);
return result;
}
///
/// Selects locations from an XML based on the location names by removing the other locations.
///
/// The input.
/// The names of the locations that need to be selected.
/// The modified input
public static string SelectLocations(string input, string[] locationNames)
{
string result = input;
const string searchString = @"";
MatchCollection locationBlocks = Regex.Matches(result, searchString, RegexOptions.Singleline);
for (int i = locationBlocks.Count - 1; i > -1; i--)
{
bool isMatch = locationNames.Any(locationName => Regex.IsMatch(locationBlocks[i].Value, "Name=\"" + locationName.Replace("+", "\\+") + "\""));
if (!isMatch)
{
result = result.Remove(locationBlocks[i].Index, locationBlocks[i].Length);
}
}
return result;
}
///
/// Changes the stability input model.
///
/// The input.
/// The stability input model
/// The modified input
public static string ChangeStabilityInputModel(string input, InputStabilityModelType stabilityModelType)
{
const string pattern = "StabilityModelType=\"[a-zA-Z]+\"";
string replacement = stabilityModelType switch
{
InputStabilityModelType.Bishop => "StabilityModelType=\"Bishop\"",
InputStabilityModelType.UpliftVan => "StabilityModelType=\"UpliftVan\"",
InputStabilityModelType.BishopUpliftVan => "StabilityModelType=\"BishopUpliftVan\"",
_ => pattern
};
string result = Regex.Replace(input, pattern, replacement);
return result;
}
///
/// Changes the piping input model.
///
/// The input.
/// The piping input model
/// The modified input
public static string ChangePipingInputModel(string input, InputPipingModelType pipingModelType)
{
const string pattern = "PipingModelType=\"[a-zA-Z]+\"";
string replacement = pipingModelType switch
{
InputPipingModelType.Bligh => "PipingModelType=\"Bligh\"",
InputPipingModelType.WtiSellmeijerRevised => "PipingModelType=\"WtiSellmeijerRevised\"",
_ => pattern
};
string result = Regex.Replace(input, pattern, replacement);
return result;
}
///
/// Changes the stability model type to piping model type.
///
/// The input.
/// The modified input
public static string ChangeStabilityToPiping(string input)
{
const string pattern = "StabilityModelType=";
const string replacement = "PipingModelType=";
string result = Regex.Replace(input, pattern, replacement);
return result;
}
}