/// <summary>Initializes a new instance of the <see cref="CharacterFormat"/> class.</summary> /// <param name="characterCasing">The character casing.</param> /// <param name="removeSpace">The remove multiple spaces.</param> /// <param name="phoneExtension">The phone extension.</param> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value characterCasing is not defined.</exception> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value removeSpace is not defined.</exception> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value phoneExtension is not defined.</exception> public CharacterFormat(CharacterCasing characterCasing, RemoveSpace removeSpace, PhoneExtension phoneExtension) { if (!Enum.IsDefined(typeof(CharacterCasing), characterCasing)) { throw new InvalidEnumArgumentException(nameof(characterCasing), (Int32)characterCasing, typeof(CharacterCasing)); } if (!Enum.IsDefined(typeof(RemoveSpace), removeSpace)) { throw new InvalidEnumArgumentException(nameof(removeSpace), (Int32)removeSpace, typeof(RemoveSpace)); } if (!Enum.IsDefined(typeof(PhoneExtension), phoneExtension)) { throw new InvalidEnumArgumentException(nameof(phoneExtension), (Int32)phoneExtension, typeof(PhoneExtension)); } this.CharacterCasing = characterCasing; this.RemoveSpace = removeSpace; this.PhoneExtension = phoneExtension; }
/// <summary>Corrects the text character casing and optionally format phone fields similar to Microsoft Outlook.</summary> /// <param name="inputString">String to be case corrected and optionally formatted.</param> /// <param name="characterCase">Character case and format.</param> /// <param name="phoneExtension">The phone extension.</param> /// <returns>String case corrected and optionally formatted.</returns> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value characterCase is not defined.</exception> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value phoneExtension is not defined.</exception> public static String ApplyCharacterCasing(String inputString, CharacterCasing characterCase, PhoneExtension phoneExtension = PhoneExtension.Keep) { if (!Enum.IsDefined(typeof(CharacterCasing), characterCase)) { throw new InvalidEnumArgumentException(nameof(characterCase), (Int32)characterCase, typeof(CharacterCasing)); } if (!Enum.IsDefined(typeof(PhoneExtension), phoneExtension)) { throw new InvalidEnumArgumentException(nameof(phoneExtension), (Int32)phoneExtension, typeof(PhoneExtension)); } inputString = inputString.Trim(); if (inputString.Length == 0) { return(inputString); } Int32 intX; switch (characterCase) { case CharacterCasing.None: return(inputString); case CharacterCasing.LowerCase: return(inputString.ToLower()); case CharacterCasing.UpperCase: return(inputString.ToUpper()); case CharacterCasing.PhoneWithDashesNoProperName: case CharacterCasing.PhoneWithDotsNoProperName: case CharacterCasing.PhoneNoProperName: return(FormatOutLookPhone(inputString, characterCase, phoneExtension)); case CharacterCasing.PhoneWithDashesUpper: case CharacterCasing.PhoneWithDotsUpper: case CharacterCasing.PhoneUpper: return(FormatOutLookPhone(inputString, characterCase, phoneExtension).ToUpper()); } inputString = inputString.ToLower(); String previous = " "; String previousTwo = " "; String previousThree = " "; String charString; for (intX = 0; intX < inputString.Length; intX++) { charString = inputString.Substring(intX, 1); if (Char.IsLetter(Convert.ToChar(charString)) && charString != charString.ToUpper()) { if (previous == " " || previous == "." || previous == "-" || previous == "/" || previousThree == " O'" || previousTwo == "Mc") { inputString = inputString.Remove(intX, 1); inputString = inputString.Insert(intX, charString.ToUpper()); previous = charString.ToUpper(); } else { previous = charString; } } else { previous = charString; } previousTwo = previousTwo.Substring(1, 1) + previous; previousThree = previousThree.Substring(1, 1) + previousThree.Substring(2, 1) + previous; } intX = inputString.IndexOf("'", StringComparison.Ordinal); if (intX == 1) { String insertString = inputString.Substring(2, 1).ToUpper(); inputString = inputString.Remove(2, 1); inputString = inputString.Insert(2, insertString); } try { intX = inputString.IndexOf("'", 3, StringComparison.Ordinal); if (intX > 3 && inputString.Substring(intX - 2, 1) == " ") { String insertString = inputString.Substring(intX + 1, 1).ToUpper(); inputString = inputString.Remove(intX + 1, 1); inputString = inputString.Insert(intX + 1, insertString); } } catch { } //never remove this code inputString += " "; foreach (CharacterCasingCheck check in CharacterCasingChecks.GetChecks()) { if (inputString.Contains(check.LookFor)) { Int32 foundIndex = inputString.IndexOf(check.LookFor, StringComparison.Ordinal); if (foundIndex > -1) { inputString = inputString.Remove(foundIndex, check.LookFor.Length); inputString = inputString.Insert(foundIndex, check.ReplaceWith); } } } if (characterCase == CharacterCasing.PhoneProperName || characterCase == CharacterCasing.PhoneWithDashesProperName || characterCase == CharacterCasing.PhoneWithDotsProperName) { inputString = FormatOutLookPhone(inputString, characterCase, phoneExtension); } return(inputString.Trim()); }
static String FormatOutLookPhone(String inputString, CharacterCasing characterCase, PhoneExtension phoneExtension) { if (inputString.Trim().Length == 0) { return(inputString); } String tempCasted = inputString + " "; try { String temp = tempCasted; Int32 intX = temp.IndexOf(" ", 8, StringComparison.Ordinal); if (intX > 0) { temp = inputString.Substring(0, intX); temp = temp.Replace("(", ""); temp = temp.Replace(")", ""); temp = temp.Replace(" ", ""); temp = temp.Replace("-", ""); temp = temp.Replace(".", ""); var characterCaseName = Enum.GetName(typeof(CharacterCasing), characterCase); String extension = null; if (Int64.TryParse(temp, out var lngTemp) && temp.Length == 10) { if (phoneExtension == PhoneExtension.Keep) { extension = tempCasted.Substring(intX).Trim(); } if (characterCaseName.Contains("Dots")) { tempCasted = lngTemp.ToString("###.###.####"); } else if (characterCaseName.Contains("Dashes")) { tempCasted = lngTemp.ToString("###-###-####"); } else { tempCasted = lngTemp.ToString("(###) ###-####"); } if (phoneExtension == PhoneExtension.Keep && !String.IsNullOrWhiteSpace(extension)) { tempCasted = tempCasted + " " + extension; } } } } catch { } return(tempCasted); }
/// <summary>Adds a CharacterCasing Formatting rule to the list of rules to be executed when the property is changed.</summary> /// <param name="propertyName">Name of the property.</param> /// <param name="characterCasing">The desired character casing formatting.</param> /// <param name="removeSpace">The remove multiple space.</param> /// <param name="phoneExtension">The phone extension.</param> /// <exception cref="ArgumentNullEmptyWhiteSpaceException">Thrown when propertyName is null, empty, or white space.</exception> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value characterCasing is not defined.</exception> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value removeSpace is not defined.</exception> /// <exception cref="InvalidEnumArgumentException">Thrown when enum value phoneExtension is not defined.</exception> public void AddRule(String propertyName, CharacterCasing characterCasing, RemoveSpace removeSpace, PhoneExtension phoneExtension) { if (String.IsNullOrWhiteSpace(propertyName)) { throw new ArgumentNullEmptyWhiteSpaceException(nameof(propertyName)); } if (!Enum.IsDefined(typeof(CharacterCasing), characterCasing)) { throw new InvalidEnumArgumentException(nameof(characterCasing), (Int32)characterCasing, typeof(CharacterCasing)); } if (!Enum.IsDefined(typeof(RemoveSpace), removeSpace)) { throw new InvalidEnumArgumentException(nameof(removeSpace), (Int32)removeSpace, typeof(RemoveSpace)); } if (!Enum.IsDefined(typeof(PhoneExtension), phoneExtension)) { throw new InvalidEnumArgumentException(nameof(phoneExtension), (Int32)phoneExtension, typeof(PhoneExtension)); } this.RulesDictionary.Add(propertyName, new CharacterFormat(characterCasing, removeSpace, phoneExtension)); }
/// <summary> /// Method to delete an extension /// </summary> /// <param name="pe"></param> private void removeExtension(PhoneExtension pe) { if (pe != null) { pe.ToServer(serverIP, Utilities.UpdateType.DELETE, Utilities.ErrorHandling.IGNORE); } }
/// <summary> /// Method to create an extension. Try to find a unique extension within 600 tries, if an extension number is not supplied /// </summary> /// <param name="pe"></param> /// <returns></returns> private bool createExtension(string extensionNumber, out PhoneExtension pe) { pe = null; bool created = false; int extNum; try { // If extension number was not supplied, find a free extension and use it if (extensionNumber == null) { Random rn = new Random(Environment.TickCount); for (int numTries = 0; numTries < 600 && created == false; numTries++) { extNum = rn.Next(100, 799); pe = new PhoneExtension("GatewayTestExtension_" + extNum, extNum.ToString()); if (!pe.ExistsOn(serverIP)) { pe.ToServer(serverIP, Utilities.UpdateType.ADD, Utilities.ErrorHandling.HANDLE); created = true; } } } else { // If extension number was supplied, use that. If that extension already exists, fail and return pe = new PhoneExtension("GatewayTextExtension_" + extensionNumber, extensionNumber); if (pe.ExistsOn(serverIP)) { Console.WriteLine("Error: Specified extension \"{0}\" already exists on the server.", extensionNumber); } else { pe.ToServer(serverIP, Utilities.UpdateType.ADD, Utilities.ErrorHandling.HANDLE); created = true; } } } catch (Exception e) { Console.WriteLine("Exception in CDSWrapper.createExtension: " + e.Message + "\nStack Trace: " + e.StackTrace); created = false; pe = null; } if (created == false) { pe = null; } return created; }