示例#1
0
        public bool Read(XmlNodeSW datanode)
        {
            try
            {
                XmlDocumentSW defaultDoc = new XmlDocumentSW();
                defaultDoc.LoadXml(datanode.OuterXml);

                if (defaultDoc.DocumentElement.Name != Constants.XMLVariationTemplateElement)
                {
                    throw new ApplicationException(defaultDoc.DocumentElement.Name + " is not a supported root element");
                }

                // Check for Include Elements first
                // In this case if there are default data and Scenario elements they get ignored.
                if (defaultDoc.HasChildNodes == false && defaultDoc.DocumentElement.HasChildNodes)
                {
                    UtilsLogger.LogError = "Empty document specified";
                    return(false);
                }

                bool defaultdatafilespecified = false;
                bool scenariosfilespecified   = false;

                // Loop through the elements under the Document element and get information.
                // If there are duplicates found or there are overrides found do the right thing.
                for (int i = 0; i < defaultDoc.DocumentElement.ChildNodes.Count; i++)
                {
                    XmlNode currentnode = defaultDoc.DocumentElement.ChildNodes[i];
                    switch (currentnode.Name)
                    {
                    case Constants.IncludeElement:
                        // There are 2 types of Includes.
                        // Include - TemplateData & Scenarios.
                        // If both these are specified parse the files they point to.
                        // If duplicates are specified or more than one error out.
                        if (currentnode.Attributes.Count > 0)
                        {
                            if (currentnode.Attributes[Constants.TypeAttribute] == null)
                            {
                                UtilsLogger.LogError = "Include element does not have Attribute Type";
                                return(false);
                            }

                            string file = null;

                            // Read the Item attribute on the element which means the file to include.
                            if (currentnode.Attributes[Constants.TypeAttribute].Value == Constants.TemplateDataElement ||
                                currentnode.Attributes[Constants.TypeAttribute].Value == Constants.ScenariosElement)
                            {
                                // If current node contains attribute "TemplateData" or "Scenarios" get
                                // value of "Item" attribute which is a path to an xml file and load the xml.
                                string includefile = currentnode.Attributes["Item"].Value;
                                if (includefile == null)
                                {
                                    UtilsLogger.LogError = "Item attribute not specified on Include element";
                                    return(false);
                                }

                                file = CommonHelper.VerifyFileExists(includefile);
                                if (file == null)
                                {
                                    // Filenot found
                                    throw new FileNotFoundException("Could not find file - " + includefile);
                                }
                            }
                            else
                            {
                                UtilsLogger.LogError = "Use Either TemplateData or Scenarios.";
                                throw new NotSupportedException("Unsupported type - " + currentnode.Attributes[Constants.TypeAttribute].Value + " value.");
                            }

                            // Depending on Type attribute value get the defaultdatalist or scenariosdoc.
                            switch (currentnode.Attributes[Constants.TypeAttribute].Value)
                            {
                            case Constants.TemplateDataElement:
                                if (defaultdatafilespecified == false)
                                {
                                    ReadTemplateData(file);
                                    defaultdatafilespecified = true;
                                }
                                else
                                {
                                    throw new NotSupportedException("Cannot have multipe TemplateData include elements");
                                }
                                break;

                            case Constants.ScenariosElement:
                                if (scenariosfilespecified == false)
                                {
                                    ReadScenarios(file);
                                    scenariosfilespecified = true;
                                }
                                else
                                {
                                    throw new NotSupportedException("Cannot have multiple Scenario include elements");
                                }
                                break;

                            default:
                                Console.WriteLine("Include attribute equals {0}", currentnode.Attributes[Constants.TypeAttribute].Value);
                                throw new NotSupportedException("Include attribute Type does not equal \"TemplateData\" or \"Scenarios\".");
                            }
                        }
                        else
                        {
                            // Time for another error.
                            Console.WriteLine("No attributes found on the Include element");
                            return(false);
                        }
                        break;

                    case Constants.TemplateDataElement:
                        // Check if there was an include that already read the default data.

                        if (defaultdatafilespecified == false)
                        {
                            ReadTemplateData(currentnode);
                        }
                        else
                        {
                            Console.WriteLine("Ignoring TemplateData section in Xml document");
                        }
                        break;

                    case Constants.ScenariosElement:
                        // Check if there was an include that already read the Scenarios.
                        if (scenariosfilespecified == false)
                        {
                            ReadScenarios(currentnode);
                        }
                        else
                        {
                            Console.WriteLine("Ignoring Scenarios section in Xml document");
                        }
                        break;
                    }
                }

                if (_dd == null)
                {
                    throw new ApplicationException("No TemplateData node was found.");
                }

                if (scenarios == null)
                {
                    throw new ApplicationException("No Scenarios node was found.");
                }

                if (_dd != null && scenarios != null)
                {
                    DoDeepCopy();
                }
            }
            catch (XmlException ex)
            {
                // Get Exception Message and print.
                UtilsLogger.DisplayExceptionInformation(ex);
                return(false);
            }

            return(true);
        }
示例#2
0
        /// <summary>
        /// Apply a specific scenario to the current default data document.
        /// </summary>
        /// <param name="scenarioid"></param>
        /// <param name="desiredvariationids"></param>
        /// <returns></returns>
        internal bool ApplyScenario(string scenarioid, string[] desiredvariationids)
        {
            if (scenarioid == null)
            {
                UtilsLogger.LogDiagnostic = "Scenario ID is null";
                return(false);
            }

            if (_dd == null)
            {
                UtilsLogger.LogDiagnostic = "TemplateData object is null";
                return(false);
            }

            if (_dd.templatedataelement == null)
            {
                // Todo: ---
                UtilsLogger.LogDiagnostic = "TemplateData has not been tokenized";
                return(false);
            }

            if (scenarios == null)
            {
                UtilsLogger.LogDiagnostic = "Scenarios object is null";
                return(false);
            }

            canvasdoc = new XmlDocumentSW();
            string rootxml = null;

            if (_dd.rootelement != null)
            {
                tokendocumentnamespace = _dd.rootelement.NamespaceURI;

                rootxml  = "<" + Constants.TemplateDataElement;
                rootxml += " xmlns=\"" + tokendocumentnamespace + "\">";
                rootxml += "</" + Constants.TemplateDataElement + ">";
            }
            else
            {
                tokendocumentnamespace = "";
                rootxml  = "<" + Constants.TemplateDataElement + ">";
                rootxml += "</" + Constants.TemplateDataElement + ">";
            }

            if (_dd.bcontainsrootnodeelement)
            {
                if (_dd.templatedataelement.ChildNodes.Count != 1)
                {
                    throw new ApplicationException("No more than one RootNodeVariation can be specified");
                }

                tokendocumentnamespace = _dd.templatedataelement.ChildNodes[0].NamespaceURI;
            }

            canvasdoc.LoadXml(rootxml);
            canvasdoc.DocumentElement.InnerXml = _dd.templatedataelement.InnerXml;

            // Find the scenario ID from scenariolist.
            for (int i = 0; _scenariolist != null && i < _scenariolist.Count; i++)
            {
                if (_scenariolist[i].Case == scenarioid)
                {
                    currentScenario = _scenariolist[i];
                    break;
                }
            }

            if (currentScenario == null)
            {
                throw new ApplicationException("Could not find scenario with ID = " + scenarioid);
            }

            // Read variations relevant to that scenario.
            currentScenario.ReadVariations();

            variationids = new Hashtable();

            if (desiredvariationids != null && desiredvariationids.Length != 0)
            {
                for (int i = 0; i < desiredvariationids.Length; i++)
                {
                    variationids.Add(desiredvariationids[i], i);
                }
            }
            else
            {
                int count = 0;
                IDictionaryEnumerator enumerator = currentScenario.nodevariationList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    variationids.Add(((BaseVariation)enumerator.Value).ID, count++);
                }

                enumerator = currentScenario.textVariationList.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    variationids.Add(((BaseVariation)enumerator.Value).ID, count++);
                }

                for (int i = 0; i < currentScenario.attributeVariationList.Count; i++)
                {
                    variationids.Add(((BaseVariation)currentScenario.attributeVariationList[i]).ID, count++);
                }
            }

            // Start applying those variations.
            if (canvasdoc.DocumentElement.ChildNodes.Count > 1)
            {
                List <XmlNode> nodelist = new List <XmlNode>(canvasdoc.DocumentElement.ChildNodes.Count);
                for (int i = 0; i < canvasdoc.DocumentElement.ChildNodes.Count; i++)
                {
                    nodelist.Add(canvasdoc.DocumentElement.ChildNodes[i]);
                }

                for (int i = 0; i < nodelist.Count; i++)
                {
                    RecurseTree(nodelist[i]);
                }

                nodelist = null;
            }
            else
            {
                RecurseTree(canvasdoc.DocumentElement);
            }

            if (currentScenario.unrecognizednodeslist != null && currentScenario.unrecognizednodeslist.Count > 0)
            {
                for (int i = 0; i < currentScenario.unrecognizednodeslist.Count; i++)
                {
                    if (ProcessUnRecognizedElements(currentScenario.unrecognizednodeslist[i]))
                    {
                        currentScenario.unrecognizednodeslist.Remove(currentScenario.unrecognizednodeslist[i]);
                        i--; // Reset back to one as one node was removed.
                    }
                }

                // Todo : Add code to specify that unrecognized code was found.
            }

            Cleanup();
            return(true);
        }
示例#3
0
        /// <summary>
        /// Encapsulated method - taking 3 input params
        /// Does the file generation.
        /// First based on inputs decides the final file name for the file to be generated.
        /// </summary>
        /// <param name="scenarioid"></param>
        /// <param name="variationstring"></param>
        private void GenerateFile(string scenarioid, string variationstring)
        {
            // If no scenarioid is defined set to all.
            if (scenarioid == null)
            {
                scenarioid = "_all";
            }

            // If newfiledata is null make a bunch of checks to come up with final name and fileextension.
            FileData tempfiledata = definedfiledata;

            if (String.IsNullOrEmpty(tempfiledata.FileName))
            {
                // Use default filename (defined in Attribute or element in TemplateData). Refer GetTemplateData method.
                tempfiledata.FileName = defaultfiledata.FileName;
            }

            if (String.IsNullOrEmpty(tempfiledata.FileExtension))
            {
                // Use default fileextension (defined in Attribute or element in TemplateData). Refer GetTemplateData method.
                tempfiledata.FileExtension = defaultfiledata.FileExtension;
            }

            // Generate XmlDocument with defaultdata document innerxml.
            if (base.canvasdoc == null)
            {
                UtilsLogger.LogError = "Variation document is null";
                return;
            }

            if (base.canvasdoc.GetElementsByTagName(Constants.TemplateDataElement) == null)
            {
                UtilsLogger.LogError = "Variation document did not contain TemplateData element";
                return;
            }

            if (base.canvasdoc.GetElementsByTagName(Constants.TemplateDataElement).Count > 1)
            {
                UtilsLogger.LogError = "Variation document contained more than one TemplateData element";
                return;
            }

            // Generated file name.
            if (_retainfilename == false)
            {
                if (variationstring != null)
                {
                    generatedFile = tempfiledata.FileName + "_Sc" + scenarioid + "_Var" + variationstring + tempfiledata.FileExtension;
                }
                else
                {
                    generatedFile = tempfiledata.FileName + "_Sc" + scenarioid + "_Var" + "all" + tempfiledata.FileExtension;
                }
            }
            else
            {
                generatedFile = tempfiledata.FileName + tempfiledata.FileExtension;
            }

            if (String.IsNullOrEmpty(_fileoutputdirectory) == false)
            {
                if (DirectorySW.Exists(_fileoutputdirectory))
                {
                    generatedFile = _fileoutputdirectory + PathSW.DirectorySeparatorChar + generatedFile;
                }
            }

            if (generatedFile.Contains(PathSW.DirectorySeparatorChar.ToString()))
            {
                string directoryname = PathSW.GetDirectoryName(generatedFile);
                if (string.IsNullOrEmpty(directoryname) == false)
                {
                    if (DirectorySW.Exists(directoryname) == false)
                    {
                        if (DirectorySW.Exists(directoryname) == false)
                        {
                            DirectorySW.CreateDirectory(directoryname);
                        }
                    }
                }
            }

            if (_isxmldocument)
            {
                XmlDocumentSW tempdoc = new XmlDocumentSW();
                tempdoc.LoadXml(base.canvasdoc.DocumentElement.InnerXml);

                GenerateFile(ref tempdoc);

                tempdoc = null;
            }
            else
            {
                GenerateFile(base.canvasdoc.DocumentElement.InnerText);
            }
        }