SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
StringConversion.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Linq;
19 using System.Security;
20 using erminas.SmartAPI.CMS;
21 using erminas.SmartAPI.CMS.Project;
22 
23 namespace erminas.SmartAPI.Utils
24 {
25  public static class StringConversion
26  {
27  public static string RQLFormat(this string value, params object[] args)
28  {
29  IEnumerable<object> newArgs = from x in args select ConvertRQL(x);
30  return string.Format(value, newArgs.ToArray());
31  }
32 
33  public static string SecureRQLFormat(this string value, params object[] args)
34  {
35  IEnumerable<object> newArgs = from x in args select SecureConvertRQL(x);
36  return string.Format(value, newArgs.ToArray());
37  }
38 
39  public static T ToEnum<T>(this string value) where T : struct, IConvertible
40  {
41  return (T) Enum.Parse(typeof (T), value);
42  }
43 
49  public static string ToRQLString(this Guid guid)
50  {
51  //uppercase conversion is needed for SOME queries (RedDot server will show strange behaviour on them otherwise).
52  return guid.ToString("N").ToUpperInvariant();
53  }
54 
58  public static string ToRQLString(this Boolean value)
59  {
60  return value ? "1" : "0";
61  }
62 
63  //TODO automatically include enum converters?
64  private static object ConvertRQL(object o)
65  {
66  if (o is Guid)
67  {
68  return ((Guid) o).ToRQLString();
69  }
70 
71  if (o is Boolean)
72  {
73  return ((Boolean) o).ToRQLString();
74  }
75 
76  var session = o as Session;
77  if (session != null)
78  {
79  return session.SessionKey;
80  }
81 
82  var languageVariant = o as ILanguageVariant;
83  if (languageVariant != null)
84  {
85  return languageVariant.Abbreviation;
86  }
87 
88  var locale = o as Locale;
89  if (locale != null)
90  {
91  return locale.LCID;
92  }
93 
94  var variants = o as IEnumerable<ILanguageVariant>;
95  if (variants != null)
96  {
97  const string SINGLE_LANGUAGE = @"<LANGUAGEVARIANT language=""{0}""/>";
98  string languages = variants.Aggregate("",
99  (s, variant) =>
100  s + SINGLE_LANGUAGE.RQLFormat(variant.Abbreviation));
101  return languages;
102  }
103 
104  return o is IRedDotObject ? ((IRedDotObject) o).Guid.ToRQLString() : o;
105  }
106 
107  private static object SecureConvertRQL(object o)
108  {
109  var s = o as string;
110  return s != null ? SecurityElement.Escape(s) : ConvertRQL(o);
111  }
112 
113  public static string ReplaceFirst(this string text, string search, string replace)
114  {
115  var pos = text.IndexOf(search, StringComparison.InvariantCulture);
116  if (pos < 0)
117  {
118  return text;
119  }
120  return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
121  }
122  }
123 }