private static ChoiceModel ParseChoiceElement(XmlElement choiceElement) { var choiceModel = new ChoiceModel(); WithAttributeDo(choiceElement, "label", label => choiceModel.Label = label); WithAttributeDo(choiceElement, "name", name => choiceModel.Name = name); ForEachElementDo(choiceElement, "var", v => ParseVarElement(v, choiceModel.Variables)); return(choiceModel); }
private SelectorModel ParseSelector(XmlElement selectorElement) { var selectorModel = new SelectorModel(this.config); WithAttributeDo(selectorElement, "label", n => selectorModel.Label = n); WithAttributeDo(selectorElement, "name", n => selectorModel.Name = n); WithAttributeDo(selectorElement, "type", n => selectorModel.SelectorType = ParseAsEnum <SelectorType>(n)); WithAttributeDo(selectorElement, "width", n => selectorModel.ControlWidth = StringToLength(n)); WithAttributeDo(selectorElement, "height", n => selectorModel.ControlHeight = StringToLength(n)); WithAttributeDo(selectorElement, "active", n => selectorModel.IsActiveExpr = n); WithAttributeDo(selectorElement, "hidden", n => selectorModel.IsHidden = bool.Parse(n)); WithAttributeDo(selectorElement, "color", n => selectorModel.Color = ParseColor(n)); WithAttributeDo(selectorElement, "bordercolor", n => selectorModel.BorderColor = ParseColor(n)); // textbox is different from other controls as it can affect only one variable // and has no choices. if (selectorModel.SelectorType == SelectorType.TextBox || selectorModel.SelectorType == SelectorType.Directory || selectorModel.SelectorType == SelectorType.File) { var vars = new Dictionary <string, VarModel>(); ParseVarElement(selectorElement.SelectSingleNode("var") as XmlElement, vars); if (vars.Count > 0) { var onlyVar = vars.First().Value; var onlyChoice = new ChoiceModel(); onlyChoice.Variables.Add(onlyVar.Name, onlyVar); selectorModel.SelectedChoice = onlyChoice; } return(selectorModel); } ForEachElementDo( selectorElement, "choice", choiceElement => selectorModel.Choices.Add(ParseChoiceElement(choiceElement))); // Check whether checkbox selector choices are booleans, // otherwise switch to listbox. if (selectorModel.SelectorType == SelectorType.CheckBox) { if (selectorModel.Choices.Count > 2) { selectorModel.SelectorType = SelectorType.ListBox; } else { bool hasTrue = false; bool hasFalse = false; // Should be exactly true and false, or one or these. foreach (var v in selectorModel.Choices) { bool result; if (bool.TryParse(v.Name, out result)) { if (result) { if (hasTrue) { throw new Exception( string.Format( "More than one true choice defined for selector {0}", selectorModel.Name)); } hasTrue = true; } else { if (hasFalse) { throw new Exception( string.Format( "More than one true choice defined for selector {0}", selectorModel.Name)); } hasFalse = true; } } } if (!hasTrue) { selectorModel.Choices.Add(new ChoiceModel { Name = "true" }); } if (!hasFalse) { selectorModel.Choices.Add(new ChoiceModel { Name = "false" }); } } } // Define undefined variables as empty strings var allKeys = new List <string>(); // Collect all variable keys foreach (var val in selectorModel.Choices) { allKeys = allKeys.Union(val.Variables.Keys).ToList(); } // Add empty string variable to each choice where it is missing. foreach (var choiceModel in selectorModel.Choices) { foreach (var key in allKeys) { if (!choiceModel.Variables.ContainsKey(key)) { choiceModel.Variables[key] = new VarModel() { Name = key, Value = string.Empty }; } } } // Set default WithAttributeDo( selectorElement, "default", n => { if (!string.IsNullOrWhiteSpace(n)) { selectorModel.SelectedChoice = selectorModel.Choices.FirstOrDefault(o => o.Name == n); } }); return(selectorModel); }