177 lines
6.2 KiB
C#
177 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Resources;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Eugen.NX
|
|
{
|
|
public class VaribleThroughString
|
|
{
|
|
const string repStart = "<§<§<§<";
|
|
const string repEnd = ">§>§>§>";
|
|
|
|
/// <summary>
|
|
/// Replaces variables with the pattern ${variable} or %variable% with their value.
|
|
/// </summary>
|
|
/// <param name="text">The environment variable containing text. If a variable was not found, then the variable name is not replaced.</param>
|
|
/// <returns>The edited text.</returns>
|
|
public static string Replace(string text)
|
|
{
|
|
if (!(text.Contains("${") || text.Contains("%")) || String.IsNullOrEmpty(text))
|
|
{
|
|
return text;
|
|
}
|
|
|
|
while (text.Contains("${"))
|
|
{
|
|
text = ReplaceVarible(text, "${", "}");
|
|
}
|
|
|
|
//Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen
|
|
if (text.Contains(repStart))
|
|
{
|
|
text.Replace(repStart, "${");
|
|
text.Replace(repEnd, "}");
|
|
}
|
|
|
|
while (text.Contains("%"))
|
|
{
|
|
text = ReplaceVarible(text, "%", "%");
|
|
}
|
|
|
|
//Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen
|
|
if (text.Contains(repStart))
|
|
{
|
|
text = text.Replace(repStart, "%");
|
|
text = text.Replace(repEnd, "%");
|
|
}
|
|
|
|
return text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Replaces variables with the pattern ${variable} or %variable% with their value.
|
|
/// </summary>
|
|
/// <param name="text">The environment variable containing text.</param>
|
|
/// <param name="messageWhenError">Behavior if variable was not found. True=An exception is thrown. False=The variable is not replaced.</param>
|
|
/// <returns>The edited text</returns>
|
|
public static string Replace(string text, bool messageWhenError)
|
|
{
|
|
if (!(text.Contains("${") || text.Contains("%")) || String.IsNullOrEmpty(text))
|
|
{
|
|
return text;
|
|
}
|
|
|
|
while (text.Contains("${"))
|
|
{
|
|
text = ReplaceVarible(text, "${", "}");
|
|
}
|
|
|
|
//Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen
|
|
if (text.Contains(repStart))
|
|
{
|
|
if (messageWhenError)
|
|
{
|
|
string varName = text.Remove(0, text.IndexOf(repStart) + 7);
|
|
varName = varName.Remove(varName.IndexOf(repEnd));
|
|
|
|
text.Replace(repStart, "${");
|
|
text.Replace(repEnd, "}");
|
|
|
|
throw new NoEnvironmentVariableFoundException(String.Concat(Language.message001seg01, varName, Language.message001seg02));
|
|
}
|
|
else
|
|
{
|
|
text.Replace(repStart, "${");
|
|
text.Replace(repEnd, "}");
|
|
}
|
|
}
|
|
|
|
while (text.Contains("%"))
|
|
{
|
|
text = ReplaceVarible(text, "%", "%");
|
|
}
|
|
|
|
//Wenn die Umgebungsvariable nicht gefunden wurde, dann die Ursprüngliche Umgebungsvariable wieder herstellen
|
|
if (text.Contains(repStart))
|
|
{
|
|
if (messageWhenError)
|
|
{
|
|
string varName = text.Remove(0, text.IndexOf(repStart) + 7);
|
|
varName = varName.Remove(varName.IndexOf(repEnd));
|
|
|
|
text = text.Replace(repStart, "%");
|
|
text = text.Replace(repEnd, "%");
|
|
|
|
throw new NoEnvironmentVariableFoundException(String.Concat(Language.message001seg01, varName, Language.message001seg02));
|
|
}
|
|
else
|
|
{
|
|
text = text.Replace(repStart, "%");
|
|
text = text.Replace(repEnd, "%");
|
|
}
|
|
}
|
|
|
|
return text;
|
|
}
|
|
|
|
private static string ReplaceVarible(string text, string startSign, string endSign)
|
|
{
|
|
//Wert vor der Variablen sichern
|
|
string temp = text.Remove(text.IndexOf(startSign));
|
|
|
|
//Variable extrahieren
|
|
text = text.Remove(0, text.IndexOf(startSign)); //'temp' entfernen
|
|
text = text.Remove(0, startSign.Length); //'startSign' entfernen
|
|
string variableName = text.Remove(text.IndexOf(endSign)); //'endSign' und nachfolgenden Text entfernen
|
|
|
|
//Restlicher Text (hinter 'endSign') erzeugen
|
|
text = text.Remove(0, text.IndexOf(endSign) + 1);
|
|
|
|
//Variable umwandeln und Text wieder zusammenbauen
|
|
string variable = ReplaceEnvironmentVariable(variableName);
|
|
text = temp + variable + text; //Text wieder zusammensetzen
|
|
|
|
return text;
|
|
}
|
|
|
|
private static string ReplaceEnvironmentVariable(string variableName)
|
|
{
|
|
string variableValue = "";
|
|
|
|
//Wenn im Text eine bekannte Umgebungsvariable enthalten ist, dann gegen den aktuellen Wert tauschen
|
|
variableValue = Environment.GetEnvironmentVariable(variableName);
|
|
|
|
if (String.IsNullOrEmpty(variableValue))
|
|
{
|
|
variableValue = String.Concat(repStart, variableName, repEnd); //Wird keine Umgebungsvariable gefunden, dann wieder den Variblen Namen zurückgeben
|
|
}
|
|
|
|
return variableValue;
|
|
}
|
|
}
|
|
|
|
#region Exceptions
|
|
class NoEnvironmentVariableFoundException : ApplicationException
|
|
{
|
|
public NoEnvironmentVariableFoundException()
|
|
{
|
|
}
|
|
|
|
public NoEnvironmentVariableFoundException(string message)
|
|
: base(message)
|
|
{
|
|
}
|
|
|
|
public NoEnvironmentVariableFoundException(string message, Exception inner)
|
|
: base(message, inner)
|
|
{
|
|
}
|
|
}
|
|
#endregion
|
|
}
|