using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
namespace JD.StringParserService
{
///
/// 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.
///
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
///
/// Name-Value property pairs, used in the parsing process.
///
public static Dictionary Properties
{
get
{
return m_stringParser.Properties;
}
}
///
/// Name-value property pairs to objects, used in the parsing process.
///
///
/// 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.
///
public static Dictionary PropertyObjects
{
get
{
return m_stringParser.PropertyObjects;
}
}
#endregion
#region Methods
#region Misc
///
/// Clears the StringParserService (Removes all properties and custom tag providers).
///
public static void Clear()
{
m_stringParser.Clear();
}
#endregion
#region Custom Tag Providers
///
/// Gets a custom tag provider corresponding to the given .
///
///
///
public static IStringTagProvider GetCustomTagProvider(string tag)
{
return m_stringParser.GetCustomTagProvider(tag);
}
///
/// Registers a custom tag provider.
///
///
public static void RegisterCustomTagProvider(IStringTagProvider provider)
{
m_stringParser.RegisterCustomTagProvider(provider);
}
#endregion
#region Parse
///
/// Parses an array of strings. The result is put back directly into the array.
///
///
/// String array to parse
public static void Parse(string[] inputs)
{
m_stringParser.Parse(inputs);
}
///
/// Parses an array of strings, using a set of custom tags. The result is put back directly into the array.
///
///
/// String array to parse
/// Custom Tags to use in the parsing process
public static void Parse(string[] inputs, string[,] customTags)
{
m_stringParser.Parse(inputs, customTags);
}
///
/// Parses a string, replacing the tags it contains
///
/// The text to parse
///
public static string Parse(string input)
{
return m_stringParser.Parse(input);
}
///
/// Parses a string, replacing the tags it contains. This overload also uses the custom tags that are passed with this method.
///
/// The text to parse
/// Custom Tags to use in the parsing process, in a 'Name-Value pairs' format
///
public static string Parse(string input, string[,] customTags)
{
return m_stringParser.Parse(input, customTags);
}
#endregion
#endregion
}
}