//*-----------------------------------------------------------------------* //*-----------------------------------------------------------------------* //* SetValue * //*-----------------------------------------------------------------------* /// <summary> /// Set the corresponding value of the named configuration entry. /// </summary> /// <param name="name"> /// Name to match. /// </param> /// <param name="value"> /// Value to set on the matching name. /// </param> public void SetValue(string name, string value) { bool bFound = false; int cCount = 0; int cIndex = 0; ConfigurationItem cItem = null; ConfigurationEntryItem eItem = null; string eValue = ""; string nLower = ""; if (this.Count == 0) { // If a collection didn't exist, create one first. cItem = new ConfigurationItem(); this.Add(cItem); } if (name?.Length > 0) { nLower = name.ToLower(); eValue = (value?.Length > 0 ? value : ""); cCount = this.Count; for (cIndex = 0; cIndex < cCount; cIndex++) { cItem = this[cIndex]; if (cItem.Entries.Exists(x => x.Name.ToLower() == nLower)) { bFound = true; eItem = cItem.Entries.First(x => x.Name.ToLower() == nLower); eItem.Value = eValue; break; } } if (!bFound) { // If the item didn't exist, place it in the closest collection. cItem = this[0]; eItem = new ConfigurationEntryItem(); eItem.Name = name; eItem.Value = value; cItem.Entries.Add(eItem); } } }
//*-----------------------------------------------------------------------* //*-----------------------------------------------------------------------* //* Parse * //*-----------------------------------------------------------------------* /// <summary> /// Parse the source content and return a populated object containing /// the deserialized information. /// </summary> /// <param name="name"> /// Name of the new configuration page. /// </param> /// <param name="lines"> /// Collection of string source lines representing the raw information to /// be parsed. /// </param> /// <param name="projectPath"> /// Current project path. /// </param> /// <returns> /// Newly created configuration item. /// </returns> public static ConfigurationItem Parse(string name, StringCollection lines, string projectPath) { int eCount = 0; int eIndex = 0; string element = ""; ConfigurationEntryCollection elements = null; ConfigurationEntryItem entry = null; string filename = ""; int iCount = 0; FileInfo ifile = null; int iIndex = 0; string insert = ""; MatchCollection matches = null; ConfigurationItem result = new ConfigurationItem(); string[] sArray = new string[0]; result.mName = name; eCount = lines.Count; for (eIndex = 0; eIndex < eCount; eIndex++) { // Get the current line. element = lines[eIndex]; matches = Regex.Matches(element, @"(?i:\{Include\((?<f>[^\)]+)\)\})"); if (matches.Count > 0) { // Inserts were found. In this version, that means no other entries will // be present on this line. foreach (Match match in matches) { filename = Tools.GetValue(match, "f"); if (Tools.IsRelative(filename)) { filename = Path.Combine(projectPath, filename); } ifile = new FileInfo(filename); if (ifile.Exists) { insert = File.ReadAllText(ifile.FullName); elements = JsonConvert.DeserializeObject <ConfigurationEntryCollection>(insert); iCount = elements.Count; for (iIndex = 0; iIndex < iCount; iIndex++) { result.Entries.Add(elements[iIndex]); } } else { Console.WriteLine( string.Format( "Error could not read file: [{0}]...", ifile.FullName)); } } } else { // No inserts present on the line. element = element.Trim(); if (element.StartsWith("[")) { // Collection of entries. elements = JsonConvert. DeserializeObject <ConfigurationEntryCollection>(element); iCount = elements.Count; for (iIndex = 0; iIndex < iCount; iIndex++) { result.Entries.Add(elements[iIndex]); } } else if (element.StartsWith("{")) { // One entry object. entry = JsonConvert.DeserializeObject <ConfigurationEntryItem>(element); result.Entries.Add(entry); } else if (element.IndexOf(":") > -1) { // Name / Value. entry = new ConfigurationEntryItem(); sArray = element.Split(new char[] { ':' }); if (sArray.Length > 1) { entry.Name = sArray[0].Replace("\"", "").Trim(); entry.Value = sArray[1].Replace("\"", "").Trim(); } result.Entries.Add(entry); } } } return(result); }