/// <summary> /// Constructor of PtfConfig /// </summary> /// <param name="activeConfigFile">A list of active configfile</param> /// <param name="defaultConfigFile">A list of default configfile</param> public PtfConfig(List<string> activeConfigFile, List<string> defaultConfigFile) { ConfigFileNames = activeConfigFile; List<string> DefaultConfigFileNames = defaultConfigFile; XmlDocList = new List<XmlDocument>(); //Deal with config file in Env file PtfProperty DefaultPtfPropertyRoot = new PtfProperty(); Merge(DefaultConfigFileNames, DefaultPtfPropertyRoot); AdjustDefaultGroup(DefaultPtfPropertyRoot); XmlDocList = new List<XmlDocument>(); //Deal with config file in Bin file PtfPropertyRoot = new PtfProperty(); FileProperties = Merge(ConfigFileNames, PtfPropertyRoot); AdjustDefaultGroup(PtfPropertyRoot); //Set Env value as default value SetDefaultValues(DefaultPtfPropertyRoot); }
public void GetPropertyByName() { PtfConfig ptfConfig = new PtfConfig( new List <string>() { @"Resources\PtfConfig_Config1.ptfconfig", @"Resources\PtfConfig_Config2.ptfconfig" }, new List <string>() { @"Resources\PtfConfig_Config1_default.ptfconfig", @"Resources\PtfConfig_Config2_default.ptfconfig" } ); PtfProperty p0 = ptfConfig.GetPropertyNodeByName("Group02.Property03"); Assert.AreEqual("value03", p0.Value, "Verify property value of Group02.Property03."); PtfProperty p1 = ptfConfig.GetPropertyNodeByName("Property01"); Assert.AreEqual("value01-2", p1.Value, "Verify property value of Property01."); }
public void IsGroupProperty() { PtfConfig ptfConfig = new PtfConfig( new List <string>() { @"Resources\PtfConfig_Config1.ptfconfig", @"Resources\PtfConfig_Config2.ptfconfig" }, new List <string>() { @"Resources\PtfConfig_Config1_default.ptfconfig", @"Resources\PtfConfig_Config2_default.ptfconfig" } ); PtfProperty defaultGroup = ptfConfig.PtfPropertyRoot.FindChildByName("Default Group"); Assert.IsTrue(defaultGroup.ValueType == PtfPropertyType.Group, "defaultGroup.IsGroup is true."); PtfProperty property01 = defaultGroup.FindChildByName("Property01"); Assert.IsFalse(property01.ValueType == PtfPropertyType.Group, "property01.IsGroup is false."); PtfProperty group01 = ptfConfig.PtfPropertyRoot.FindChildByName("Group01"); Assert.IsTrue(group01.ValueType == PtfPropertyType.Group, "group01.IsGroup is true."); }
/// <summary> /// Set Propert under Properties node into DefaultGroup /// </summary> public void AdjustDefaultGroup(PtfProperty RootNode) { PtfProperty defaultGroup = new PtfProperty() { Name = StringResource.DefaultGroupName, Description = "", ValueType = PtfPropertyType.Group }; for (int i = 0; i < RootNode.Count; i++) { if (RootNode[i].ValueType != PtfPropertyType.Group) { defaultGroup.Add(RootNode[i]); RootNode.RemoveAt(i); i--; } } //Set DefaultGroup node to be the first one RootNode.Insert(0, defaultGroup); }
public TreeStackItem(PtfProperty node, PtfPropertyView view) { PropertyNode = node; PropertyView = view; Path = new List<string>(); }
/// <summary> /// Set Environment config as default value /// </summary> private void SetDefaultValues(PtfProperty DefaultPropertyRoot) { Stack<PtfProperty> ConfigStack = new Stack<PtfProperty>(); ConfigStack.Push(PtfPropertyRoot); while (ConfigStack.Count > 0) { PtfProperty topNode = ConfigStack.Pop(); string defaultValue = GetDefaultValueByName(DefaultPropertyRoot, topNode.Name); topNode.DefaultValue = defaultValue; foreach (PtfProperty cn in topNode) { ConfigStack.Push(cn); } } }
/// <summary> /// recurrsively create Config Node tree from ptfconfig file /// </summary> /// <param name="baseConfigNode"></param> /// <param name="root"></param> private void MergePropertyAndGroup(PtfProperty baseConfigNode, XmlNode root) { Dictionary<string, XmlNode> propertyDict = new Dictionary<string, XmlNode>(); Dictionary<string, XmlNode> groupDict = new Dictionary<string, XmlNode>(); string value = ""; //to sort Group node before Property node //record the pos of first Property node. If no Propert, default value is -1 int propertyPos = -1; foreach (XmlNode child in root.ChildNodes) { if (child.NodeType == XmlNodeType.Element) { value = child.Attributes["name"].Value; if (child.Name == "Property") { if (propertyDict.ContainsKey(value)) throw new InvalidOperationException( string.Format(StringResource.DuplicatePTFConfigNode, child.Name, value)); propertyDict.Add(value, child); } else { if (groupDict.ContainsKey(value)) throw new InvalidOperationException( string.Format(StringResource.DuplicatePTFConfigNode, child.Name, value)); groupDict.Add(value, child); } } } //Merge Group First foreach (XmlNode child in groupDict.Values) { bool duplicate = false; PtfProperty config = null; for (int i = 0; i < baseConfigNode.Count; i++) { if (baseConfigNode[i].ValueType != PtfPropertyType.Group && propertyPos == -1) { propertyPos = i; } if (baseConfigNode[i].ValueType == PtfPropertyType.Group && baseConfigNode[i].Name == child.Attributes["name"].Value) { duplicate = true; config = baseConfigNode[i]; break; } } if (duplicate) { //duplicate, first merge Group Node's Attribute //Recurrsively Merge Property and Group to create data structure if (child.Attributes["description"] != null) { config.Description = child.Attributes["description"].Value; config.RefXmlNode = child; } MergePropertyAndGroup(config, child); } else { //create new Group Node //Insert Group before Property //Recurrsively Merge Property and Group to create data structure PtfProperty newGroup = new PtfProperty(child, true); if (propertyPos >= 0) { baseConfigNode.Insert(propertyPos, newGroup); propertyPos++; } else { baseConfigNode.Add(newGroup); } MergePropertyAndGroup(newGroup, child); } } //Merge Property foreach (XmlNode child in propertyDict.Values) { bool duplicate = false; PtfProperty config = null; foreach (PtfProperty childConfig in baseConfigNode) { if (childConfig.ValueType != PtfPropertyType.Group && childConfig.Name == child.Attributes["name"].Value) { duplicate = true; config = childConfig; break; } } //First remove old node if (duplicate) { baseConfigNode.Remove(config); } //Insert new node PtfProperty newProperty = new PtfProperty(child, false); baseConfigNode.Add(newProperty); } groupDict.Clear(); propertyDict.Clear(); }
private string GetDefaultValueByName(PtfProperty root, string NodeName) { Stack<PtfProperty> ConfigStack = new Stack<PtfProperty>(); ConfigStack.Push(root); while (ConfigStack.Count > 0) { PtfProperty topNode = ConfigStack.Pop(); if (topNode.Name == NodeName) { return topNode.Value; } foreach (PtfProperty cn in topNode) { ConfigStack.Push(cn); } } return ""; }
/// <summary> /// Merges PTFConfig files /// </summary> /// <param name="configFileNames">PTFConfig file names</param> /// <param name="RootNode">PTF property tree</param> /// <returns>Filename-property list dictionary</returns> public Dictionary<string, List<string>> Merge(List<string> configFileNames, PtfProperty RootNode) { Dictionary<string, List<string>> propertyMap = new Dictionary<string, List<string>>(); try { if (configFileNames == null) { throw new ArgumentException(StringResource.ConfigFileNameNotSpecified); } //Create an XmlNamespaceManager for resolving namespaces. XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); nsmgr.AddNamespace("tc", StringResource.DefaultNamespace); foreach (string configFileName in configFileNames) { if (configFileName == null) continue; XmlDocument doc = new XmlDocument(); doc.XmlResolver = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.XmlResolver = null; settings.DtdProcessing = DtdProcessing.Prohibit; using (var xmlReader = XmlReader.Create(configFileName, settings)) { doc.Load(xmlReader); //record each xmldoc for Config node to refer to XmlDocList.Add(doc); //Properties XmlNode node = doc.DocumentElement.SelectSingleNode("tc:Properties", nsmgr); string filename = System.IO.Path.GetFileName(configFileName); propertyMap[filename] = new List<string>(); Stack<string> parent = new Stack<string>(); Stack<XmlNode> nodes = new Stack<XmlNode>(); foreach (XmlNode child in node.ChildNodes) { string propertyName = ""; if (child.NodeType == XmlNodeType.Element) { propertyName = child.Attributes["name"].Value; if (child.Name == "Property") { propertyMap[filename].Add(propertyName); } else if (child.Name == "Group") { foreach (XmlNode subChild in child.ChildNodes) { if (subChild.NodeType == XmlNodeType.Element) { parent.Push(child.Attributes["name"].Value); nodes.Push(subChild); } } } } } while (nodes.Count > 0) { var n = nodes.Pop(); var p = parent.Pop(); if (n.Name == "Property") { propertyMap[filename].Add(string.Format("{0}.{1}", p, n.Attributes["name"].Value)); } else if (n.Name == "Group") { foreach (XmlNode child in n.ChildNodes) { if (child.NodeType == XmlNodeType.Element) { parent.Push(string.Format("{0}.{1}", p, n.Attributes["name"].Value)); nodes.Push(child); } } } } MergePropertyAndGroup(RootNode, node); } } LoadAdapters(); } catch (XmlException e) { throw new XmlException("Merge Exception" + e); } return propertyMap; }