JD2022-TU1/main/tools/framework/JD.StringParserService/StringParserService.cs

156 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace JD.StringParserService
{
/// <summary>
/// Application-wide service that handles the parsing of internal ${xyz} tags.
/// All environment variables are available under the name ${env:[NAME]}
/// where [NAME] represents the string under which it is avaiable in
/// the environment.
/// </summary>
public static class StringParserService
{
#region Variables
private static readonly StringParser m_stringParser = new StringParser();
#endregion
#region Constructor
static StringParserService()
{
Assembly entryAssembly = Assembly.GetEntryAssembly();
// entryAssembly == null might happen in unit test mode
if (entryAssembly != null)
{
string exeName = entryAssembly.Location;
m_stringParser.PropertyObjects["exe"] = FileVersionInfo.GetVersionInfo(exeName);
}
m_stringParser.Properties["USER"] = Environment.UserName;
}
#endregion
#region Properties
/// <summary>
/// Name-Value property pairs, used in the parsing process.
/// </summary>
public static Dictionary<string, string> Properties
{
get
{
return m_stringParser.Properties;
}
}
/// <summary>
/// Name-value property pairs to objects, used in the parsing process.
/// </summary>
/// <example>
/// If some instance MyInstance has a property called 'Length', you can assign the propertyObject
/// StringParser.PropertyObjects["myObj"] = MyInstance. If you then call StringParser.Parse("${myObj:Length}"),
/// you will get the value of this property at the time that Parse was called.
/// </example>
public static Dictionary<string, object> PropertyObjects
{
get
{
return m_stringParser.PropertyObjects;
}
}
#endregion
#region Methods
#region Misc
/// <summary>
/// Clears the StringParserService (Removes all properties and custom tag providers).
/// </summary>
public static void Clear()
{
m_stringParser.Clear();
}
#endregion
#region Custom Tag Providers
/// <summary>
/// Gets a custom tag provider corresponding to the given <paramref name="tag"/>.
/// </summary>
/// <param name="tag"></param>
/// <returns></returns>
public static IStringTagProvider GetCustomTagProvider(string tag)
{
return m_stringParser.GetCustomTagProvider(tag);
}
/// <summary>
/// Registers a custom tag provider.
/// </summary>
/// <param name="provider"></param>
public static void RegisterCustomTagProvider(IStringTagProvider provider)
{
m_stringParser.RegisterCustomTagProvider(provider);
}
#endregion
#region Parse
/// <summary>
/// Parses an array of strings. The result is put back directly into the array.
/// </summary>
/// <see cref="Parse(string)"/>
/// <param name="inputs">String array to parse</param>
public static void Parse(string[] inputs)
{
m_stringParser.Parse(inputs);
}
/// <summary>
/// Parses an array of strings, using a set of custom tags. The result is put back directly into the array.
/// </summary>
/// <see cref="Parse(string, string[,])"/>
/// <param name="inputs">String array to parse</param>
/// <param name="customTags">Custom Tags to use in the parsing process</param>
public static void Parse(string[] inputs, string[,] customTags)
{
m_stringParser.Parse(inputs, customTags);
}
/// <summary>
/// Parses a string, replacing the tags it contains
/// </summary>
/// <param name="input">The text to parse</param>
/// <returns></returns>
public static string Parse(string input)
{
return m_stringParser.Parse(input);
}
/// <summary>
/// Parses a string, replacing the tags it contains. This overload also uses the custom tags that are passed with this method.
/// </summary>
/// <param name="input">The text to parse</param>
/// <param name="customTags">Custom Tags to use in the parsing process, in a 'Name-Value pairs' format</param>
/// <returns></returns>
public static string Parse(string input, string[,] customTags)
{
return m_stringParser.Parse(input, customTags);
}
#endregion
#endregion
}
}