17 using System.Collections.Generic;
19 using System.Security;
20 using erminas.SmartAPI.CMS;
21 using erminas.SmartAPI.CMS.Project;
23 namespace erminas.SmartAPI.Utils
25 public static class StringConversion
27 public static string RQLFormat(
this string value, params
object[] args)
29 IEnumerable<object> newArgs = from x in args select ConvertRQL(x);
30 return string.Format(value, newArgs.ToArray());
33 public static string SecureRQLFormat(
this string value, params
object[] args)
35 IEnumerable<object> newArgs = from x in args select SecureConvertRQL(x);
36 return string.Format(value, newArgs.ToArray());
39 public static T ToEnum<T>(
this string value) where T :
struct, IConvertible
41 return (T) Enum.Parse(typeof (T), value);
49 public static string ToRQLString(
this Guid guid)
52 return guid.ToString(
"N").ToUpperInvariant();
58 public static string ToRQLString(
this Boolean value)
60 return value ?
"1" :
"0";
64 private static object ConvertRQL(
object o)
68 return ((Guid) o).ToRQLString();
73 return ((Boolean) o).ToRQLString();
76 var session = o as Session;
79 return session.SessionKey;
83 if (languageVariant != null)
88 var locale = o as Locale;
94 var variants = o as IEnumerable<ILanguageVariant>;
97 const string SINGLE_LANGUAGE =
@"<LANGUAGEVARIANT language=""{0}""/>";
98 string languages = variants.Aggregate(
"",
100 s + SINGLE_LANGUAGE.RQLFormat(variant.Abbreviation));
107 private static object SecureConvertRQL(
object o)
110 return s != null ? SecurityElement.Escape(s) : ConvertRQL(o);
113 public static string ReplaceFirst(
this string text,
string search,
string replace)
115 var pos = text.IndexOf(search, StringComparison.InvariantCulture);
120 return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);