PageRenderTime 53ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Comdiv.Core/Extensions/AdvancedConvertExtensions.cs

http://comdivmvccontrib.googlecode.com/
C# | 219 lines | 202 code | 1 blank | 16 comment | 0 complexity | 738e128035c5c1bf974d2183e5352684 MD5 | raw file
  1. // Copyright 2007-2010 Comdiv (F. Sadykov) - http://code.google.com/u/fagim.sadykov/
  2. // Supported by Media Technology LTD
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // MODIFICATIONS HAVE BEEN MADE TO THIS FILE
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.IO.Compression;
  22. using System.Security.Cryptography;
  23. using System.Security.Principal;
  24. using System.Text;
  25. using System.Text.RegularExpressions;
  26. namespace Comdiv.Extensions{
  27. public static class AdvancedConvertExtensions{
  28. public static int DefaultRegexTimeout = 2000;
  29. public static object safeSync = new object();
  30. public const string lSlash = "\\";
  31. public const string rSlash = "/";
  32. public static string toZip(this string text)
  33. {
  34. // Converts to byte array
  35. byte[] bytes = Encoding.UTF8.GetBytes(text);
  36. var ms = new MemoryStream();
  37. int length = bytes.Length; // the length of the text
  38. using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
  39. {
  40. // Writes compressed bytes to the MemoryStream ojbect from the bytes
  41. zip.Write(bytes, 0, length);
  42. }
  43. var compressed = new byte[ms.Length];
  44. // Reads a block of bytes from the current stream and current position and writes the data to buffer (compressed)
  45. ms.Position = 0;
  46. ms.Read(compressed, 0, compressed.Length);
  47. // Saves the length of the compressed text
  48. byte[] buffer = new byte[compressed.Length + 4];
  49. Buffer.BlockCopy(compressed, 0, buffer, 4, compressed.Length);
  50. Buffer.BlockCopy(BitConverter.GetBytes(length), 0, buffer, 0, 4);
  51. // Returns base64 string
  52. return Convert.ToBase64String(buffer);
  53. }
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. /// <param name="compressed"></param>
  58. /// <exception cref="System.OverflowException"/>
  59. /// <returns></returns>
  60. public static string unZip(this string compressed)
  61. {
  62. // Converts the specified String (compressed), which encodes binary data as base 64 digits, to an equivalent 8-bit unsigned integer array.
  63. byte[] bytes = Convert.FromBase64String(compressed);
  64. using (var ms = new MemoryStream())
  65. {
  66. // Gets the number of bytes of the results, decompressed stiring
  67. var decompressedlength = BitConverter.ToInt32(bytes, 0); // 4 bytes only
  68. ms.Write(bytes, 4, bytes.Length - 4);
  69. byte[] buffer = new byte[decompressedlength];
  70. // Reads decompressed bytes to the buffer
  71. ms.Position = 0;
  72. using (var zip = new GZipStream(ms, CompressionMode.Decompress))
  73. {
  74. zip.Read(buffer, 0, buffer.Length);
  75. }
  76. return Encoding.UTF8.GetString(buffer);
  77. }
  78. }
  79. const string entityPrefix = "&#";
  80. private const string entitySuffix = ";";
  81. /// <summary>
  82. ///  ??????? ?????????? ?????? ? ???? ?????????????????? ????????, ??? ???
  83. /// ??????? ???????? ???????€?? Utf8 &amp;#CODE;
  84. /// </summary>
  85. public static string toHtmlUtf(this string str)
  86. {
  87. if (String.IsNullOrEmpty(str)) return "";
  88. var result = new StringBuilder();
  89. foreach (var c in str)
  90. {
  91. // var bytes = Encoding.UTF8.GetBytes(c.ToString());
  92. // Console.WriteLine( Encoding.UTF8.GetBytes(str).concat( ","));
  93. // int up = bytes[0];
  94. // if(bytes.Length>1){
  95. // up = (bytes[1] << 8) + bytes[0];
  96. // }
  97. //
  98. result.Append(entityPrefix);
  99. result.Append((int)c);
  100. result.Append(entitySuffix);
  101. }
  102. return result.ToString();
  103. }
  104. /// <summary>
  105. /// ??????????? HTML ? ??????????? ?????
  106. /// </summary>
  107. /// <param name="value"></param>
  108. /// <returns></returns>
  109. public static string toText(this string value)
  110. {
  111. if (value == null) return String.Empty;
  112. return value
  113. .replace(@"<(br|p|table|tr)[^>]*?>", Environment.NewLine)
  114. .replace(@"</td\s*>", "\t")
  115. .replace(@"<hr[^>]*?>", "\r\n----------\r\n")
  116. .replace(@"&([lr]a)?(quo)t?;", "\"")
  117. .replace(@"&apos;", "'")
  118. .replace(@"&ndash;", "-")
  119. .replace(@"&amp;", "&")
  120. .replace(@"&nbsp;", " ")
  121. .replace(@"<[^>]*>",String.Empty)
  122. .replace(@"[\r\n]\s+", Environment.NewLine)
  123. .replace(@"\x20+", " ")
  124. .Trim()
  125. ;
  126. }
  127. public static string toSqlDateString(this DateTime date)
  128. {
  129. return date.ToString("yyyy-MM-dd");
  130. }
  131. public static string toSqlDateString(this string date)
  132. {
  133. return toSqlDateString(date.toDate());
  134. }
  135. public static string toXmlDateString(this DateTime date)
  136. {
  137. return date.ToString("yyyy-MM-ddThh:mm:ss");
  138. }
  139. public static string toXmlDateString(this string date)
  140. {
  141. return toXmlDateString(date.toDate());
  142. }
  143. public static string toInternetDateString(this DateTime date)
  144. {
  145. return date.ToString("R", CultureInfo.GetCultureInfo("En-us"));
  146. }
  147. public static string cleanHtml(this string value)
  148. {
  149. return value.toText().toHtml();
  150. }
  151. public static string toSqlString(this string value)
  152. {
  153. if (null == value) return String.Empty;
  154. return value.Replace("'", "''");
  155. }
  156. public const string LocalDomainName = "(local)";
  157. /// <summary>
  158. /// ????? ?????? ? ?????? ?????? ???????????€ ? ?????????? ??????????????? ??€ ? ?????? ??? ??? ????? ??????
  159. /// </summary>
  160. /// <param name="user"></param>
  161. /// <param name="useDomain"></param>
  162. /// <returns></returns>
  163. public static string toUserName(this IPrincipal user, bool useDomain)
  164. {
  165. return user.Identity.Name.toUserName(useDomain);
  166. }
  167. public static string toUserName(this string userName)
  168. {
  169. return toUserName(userName, true);
  170. }
  171. public static string toUserName(this string userName, bool useDomain)
  172. {
  173. if (userName.noContent()) return "";
  174. if (useDomain) return userName;
  175. if (userName.Contains(lSlash) || userName.Contains(rSlash)) return userName.Split('\\', '/')[1];
  176. return userName;
  177. }
  178. public static string toDomain(this string userName)
  179. {
  180. if (userName.noContent()) return "";
  181. if (userName.Contains(lSlash) || userName.Contains(rSlash))
  182. return userName.Split('\\', '/')[0];
  183. return LocalDomainName;
  184. }
  185. public static string toHtml(this string str)
  186. {
  187. return str
  188. .replace("&", "&amp;")
  189. .replace("<", "&lt;")
  190. .replace(">", "&gt;")
  191. .replace(@"[\r\n]{1,2}", m => "<br/>\r\n")
  192. .replace(@"\t", "<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>")
  193. .replace(@"-{5,}","<hr/>\r\n")
  194. ;
  195. }
  196. }
  197. }