private TaskSwitch LoadTaskSwitch(XElement eSwitch) { char separator = eSwitch.GetAtt("separator", ",").FirstOrDefault(); TaskSwitch sw = new TaskSwitch(); sw.Level = ReadLevel(eSwitch); sw.IsInMemoryOnly = eSwitch.GetAtt("mem", false); sw.IsGlobal = eSwitch.GetAtt("g", false); string id = eSwitch.GetAtt("id", String.Empty); sw.IsLocalVariableSource = !sw.IsGlobal && !String.IsNullOrEmpty(id); if (!String.IsNullOrEmpty(id)) { sw.Id = id; // else has GUID as Id } sw.Value = eSwitch.GetAtt("value", ""); if (eSwitch.HasElements) { sw.IsTexts = true; var vls = new List <TaskText>(); var cases = new List <string>(); foreach (var cs in eSwitch.Elements("case")) { cases.Add(cs.GetAtt("value", "")); vls.Add(LoadTaskText(cs)); } sw.Cases = cases.ToArray(); sw.ValuesTexts = vls.ToArray(); if (eSwitch.Element("default") != null) { sw.DefaultText = LoadTaskText(eSwitch.Element("default")); } } else { sw.Cases = eSwitch.GetAtt("cases", "").Split(separator); sw.Default = eSwitch.GetAtt("default", ""); sw.Values = eSwitch.GetAtt("values", "").Split(separator); } return(sw); }
string GetSwitchValue(TaskSwitch sw, Dictionary <string, object> localVariables, TaskRandom random) { string val = TranslateStringValue(sw.Value, localVariables); var cases = TranslateStringValues(sw.Cases, localVariables); int index = cases.ToList().IndexOf(val); string result; if (sw.IsTexts) { if (index >= 0 && index < sw.ValuesTexts.Length) { result = RenderText(sw.ValuesTexts[index], random.GetSubRandom(), Setting.Level); } else { result = RenderText(sw.DefaultText, random.GetSubRandom(), Setting.Level); } } else if (index >= 0 && index < sw.Values.Length) { result = TranslateStringValue(sw.Values[index], localVariables); } else { result = TranslateStringValue(sw.Default, localVariables); } // Save local variable if (sw.IsLocalVariableSource) { localVariables[sw.Id] = result; } // Save global variable if (sw.IsGlobal) { globalVariables[sw.Id] = result; } return(result); }