/// <summary> /// Gets a particular Attribute Variation based on the ID. /// This was implemented to allow for cases when more than one Attribute Variation /// needs to be applied on the Default Attribute Variation. /// </summary> /// <param name="caseid"></param> /// <returns></returns> internal AttributeVariation GetAttributeVariation(string caseid) { if (String.IsNullOrEmpty(caseid)) { return(null); } if (attributeVariationList.Count == 0) { return(null); } // If the key is not duplicated then a simple check if the key is // contained in the Hashtable should be sufficient. for (int i = 0; i < attributeVariationList.Count; i++) { AttributeVariation currentvariation = (AttributeVariation)attributeVariationList[i]; if (currentvariation == null) { continue; } if (currentvariation.ID == caseid) { return(currentvariation); } } return(null); }
/// <summary> /// Reads all variations specified in current scenario. /// Setup to read only when required when the default tree is being parsed for example. /// </summary> internal virtual void ReadVariations() { if (currentscenarionode == null) { UtilsLogger.LogDiagnostic = "Current scenario is null, there is no variations to read."; return; } if (currentscenarionode.HasChildNodes == false) { UtilsLogger.LogDiagnostic = "Current scenario does not contain any Variations"; return; } // In Scenario elements nested Variation elements are not allowed. // Thus the loop here only looks for child elements of the Scenario node. // If there are nested Variation elements an Exception will be thrown when the // variation is initialized. // If a non-variation element is found that element is stored in a list for processing // by later processing. for (int i = 0; i < currentscenarionode.ChildNodes.Count; i++) { if (currentscenarionode.ChildNodes[i] == null) { continue; } switch (currentscenarionode.ChildNodes[i].Name) { case Constants.NodeVariationElement: NodeVariation newnodevariation = new NodeVariation(); newnodevariation.Initialize(currentscenarionode.ChildNodes[i]); nodevariationList.Add(newnodevariation.ID, newnodevariation); // if (nodevariationList.ContainsKey(newnodevariation.ID)) // { // nodevariationList.Add(Convert.ToInt32(GenerateRandomNumber()) + "_" + newnodevariation.ID, newnodevariation); // } // else // { // nodevariationList.Add(newnodevariation.ID, newnodevariation); // } break; case Constants.AttributeVariationElement: AttributeVariation newattribvariation = new AttributeVariation(); newattribvariation.Initialize(currentscenarionode.ChildNodes[i]); attributeVariationList.Add(newattribvariation); break; case Constants.TextVariationElement: TextVariation newtextvariation = new TextVariation(); newtextvariation.Initialize(currentscenarionode.ChildNodes[i]); textVariationList.Add(newtextvariation.ID, newtextvariation); break; case Constants.RootNodeVariationElement: if (rootnodevariation != null) { throw new ApplicationException("No more than one RootNodeVariation can be specified in the document."); } rootnodevariation = new RootNodeVariation(); rootnodevariation.Initialize(currentscenarionode.ChildNodes[i]); break; default: if (unrecognizednodeslist == null) { unrecognizednodeslist = new List <XmlNode>(); } unrecognizednodeslist.Add(currentscenarionode.ChildNodes[i]); break; } } if (nodevariationList.Count == 0 && attributeVariationList.Count == 0 && textVariationList.Count == 0) { // Todo: Removed this exception to allow changing only filename for a variation. //throw new NotSupportedException("Current scenario does not contain any supported variations."); } }
/// <summary> /// Similar to GetAttributeVariation, RemoveAttributeVariation is used to remove /// an attribute variation when it's duplicated. /// </summary> /// <param name="caseid"></param> internal void RemoveAttributeVariation(string caseid) { if (String.IsNullOrEmpty(caseid)) { return; } if (attributeVariationList.Count == 0) { return; } for (int i = 0; i < attributeVariationList.Count; i++) { AttributeVariation currentvariation = (AttributeVariation)attributeVariationList[i]; if (currentvariation == null) { continue; } if (currentvariation.ID == caseid) { attributeVariationList.RemoveAt(i); return; } } // if (attributeVariationList.ContainsKey(caseid)) // { // attributeVariationList.Remove(caseid); // return; // } // // AttributeVariation currentvariation = null; // // IDictionaryEnumerator enumerator = attributeVariationList.GetEnumerator(); // while (enumerator.MoveNext()) // { // currentvariation = (AttributeVariation)enumerator.Value; // // if (currentvariation.ID.Contains("_") == false) // { // currentvariation = null; // continue; // } // // int index = currentvariation.ID.IndexOf('_'); // // // As the contains("_") already took care of check if _ exists or not // // index should always be >= 0 // string currentid = currentvariation.ID.Substring(index + 1); // if (currentid.CompareTo(caseid) < 0) // { // currentvariation = null; // continue; // } // else // { // attributeVariationList.Remove(currentvariation.ID); // break; // } // } }
/// <summary> /// Attribute variation is a little special as there can be multiple of them specified in the /// Scenario to change multiple Attributes on a element. /// For this we keep looping until all the variations are processed. /// </summary> /// <param name="node"></param> /// <returns></returns> private bool ApplyAttributeVariation(XmlNode node) { if (currentScenario == null) { // There nothing to do. return(false); } if (node.HasChildNodes == false) { // Todo: Attribute variation has to have one child node. Throw exception return(false); } if (node.ChildNodes.Count > 1) { // Todo: Only one allowed. Throw exception return(false); } AttributeVariation temp = new AttributeVariation(); temp.Initialize(node); string nodeid = temp.ID; if (variationids.Count != 0) { if (variationids.Contains(nodeid) == false) { RestoreDefaults(node, temp.attributestable); return(true); } } else if (variationids.Contains(nodeid) == false) { RestoreDefaults(node, temp.attributestable); return(false); } AttributeVariation attributevariation = currentScenario.GetAttributeVariation(nodeid); if (attributevariation == null) { RestoreDefaults(node, temp.attributestable); return(true); } // Loop until there are no more attributevariations with the same id to process. XmlNode nodetomodify = null; do { if (nodetomodify == null) { nodetomodify = node.ChildNodes[0]; } if (nodetomodify.Attributes.Count == 0) { // Nothing to change. attributevariation = null; RestoreDefaults(node, temp.attributestable); return(true); } // Change the attribute if it is found on the node. XmlAttribute attrib = nodetomodify.Attributes[attributevariation.AttributeName]; if (attrib != null) { if (attributevariation.RemoveAttribute == false) { attrib.Value = attributevariation.AttributeValue; } else { XmlNode parentnode = (XmlNode)attrib.OwnerElement; parentnode.Attributes.Remove(attrib); } } else { attrib = nodetomodify.OwnerDocument.CreateAttribute(attributevariation.AttributeName); attrib.Value = attributevariation.AttributeValue; nodetomodify.Attributes.Append(attrib); } NodeVariationApplied(node.ParentNode, nodetomodify, attributevariation.attributestable); currentScenario.RemoveAttributeVariation(nodeid); // Delete the node and the variation id from the list only when there are no more // variations with the same ID to apply. attributevariation = currentScenario.GetAttributeVariation(nodeid); if (attributevariation == null) { XmlNode parentnode = node.ParentNode; parentnode.InsertAfter(nodetomodify, node); parentnode.RemoveChild(node); variationids.Remove(nodeid); } } while (attributevariation != null); CleanupPlaceHolder(); return(true); }