//************************************************************************* // Method: ParseFault // Description: parses the xml document and extracts a fault from it // // Parameters: // childNaviator - the path navigator that represents the fault node // to extract // // Return Value: the extracted fault //************************************************************************* protected Fault ParseFault(XPathNavigator childNavigator) { XPathNavigator faultNavigator = childNavigator.Clone(); Fault fault = new Fault(); // retrive Fault tag attributes bool hasMoreAttributes = faultNavigator.MoveToFirstAttribute(); while (hasMoreAttributes) { switch(faultNavigator.Name) { case "Name": { fault.Name = faultNavigator.Value; break; } case "ReturnValue": { fault.ReturnValue = faultNavigator.Value; break; } case "ErrorCode": { fault.ErrorCode = faultNavigator.Value; break; } } hasMoreAttributes = faultNavigator.MoveToNextAttribute(); } // get back to the fault tag faultNavigator.MoveToParent(); bool hasMoreFaultElements = faultNavigator.MoveToFirstChild(); while (hasMoreFaultElements) { if (faultNavigator.Name.CompareTo("Function") == 0) { FaultFunction function = ParseFaultFunction(faultNavigator); if (function != null) fault.Function.Add(function); } hasMoreFaultElements = faultNavigator.MoveToNext(); } return fault; }
//************************************************************************* // Method: AddFaultToTableByName // Description: Adding Fault to hashtable by FaultName as Key // // Parameters: // newFaultToAdd - the object of type Fault Class // // Return Value: true if successful, false otherwise //************************************************************************* protected bool AddFaultToTableByName(Fault newFaultToAdd) { FaultTableByName.Add(newFaultToAdd.Name,newFaultToAdd); return true; }
//************************************************************************* // Method: saveFaultXmlDocument // Description: recreating fault.xml document // // Parameters: // faultXMLNavigator // // Return Value: None // // Output: "Faults.xml" file will be created in this application directory //************************************************************************* public void saveFaultXmlDocument(FaultXMLNavigator faultXMLNavigator, string fileNameToSaveAs, string fileEncoding, bool isValidationRequired) { XmlTextWriter saveFaultXml = null; Console.WriteLine(fileNameToSaveAs); switch (fileEncoding.ToUpper()) { case "UTF-8": case "": { Console.WriteLine(fileNameToSaveAs); saveFaultXml = new XmlTextWriter(fileNameToSaveAs, System.Text.UTF8Encoding.UTF8); saveFaultXml.Formatting = Formatting.Indented; saveFaultXml.WriteRaw("<?xml version= \"1.0\"?>"); break; } case "UTF-7": { Console.WriteLine(fileNameToSaveAs); saveFaultXml = new XmlTextWriter(fileNameToSaveAs, System.Text.UTF7Encoding.UTF7); saveFaultXml.Formatting = Formatting.Indented; saveFaultXml.WriteRaw("<?xml version= \"1.0\" encoding=\"UTF-7\"?>"); break; } case "ASCII": { Console.WriteLine(fileNameToSaveAs); saveFaultXml = new XmlTextWriter(fileNameToSaveAs, System.Text.ASCIIEncoding.ASCII); saveFaultXml.Formatting = Formatting.Indented; saveFaultXml.WriteRaw("<?xml version= \"1.0\" encoding=\"ASCII\"?>"); break; } case "Unicode": { saveFaultXml = new XmlTextWriter(fileNameToSaveAs, System.Text.UnicodeEncoding.Unicode); saveFaultXml.Formatting = Formatting.Indented; saveFaultXml.WriteRaw("<?xml version= \"1.0\" encoding=\"Unicode\"?>"); break; } default: { saveFaultXml = new XmlTextWriter(fileNameToSaveAs, null); saveFaultXml.Formatting = Formatting.Indented; saveFaultXml.WriteRaw("<?xml version= \"1.0\"?>"); break; } } if (isValidationRequired) { saveFaultXml.WriteDocType("Faults", null, "faultsNew.dtd", ""); } saveFaultXml.WriteStartElement("Faults"); foreach (string FaultNameAsKey in FaultTableByName.Keys) { Fault FaultToSave = faultXMLNavigator.GetFaultByName(FaultNameAsKey); ///Element = Fault saveFaultXml.WriteStartElement("Fault"); ///Attribute = Name if (FaultToSave.Name != null) { saveFaultXml.WriteAttributeString("Name", FaultToSave.Name); } ///Attribute = ReturnValue if (FaultToSave.ReturnValue != null) { saveFaultXml.WriteAttributeString("ReturnValue", FaultToSave.ReturnValue); } ///Attribute = ErrorCode if (FaultToSave.ErrorCode != null) { saveFaultXml.WriteAttributeString("ErrorCode", FaultToSave.ErrorCode); } if (FaultToSave.Function != null) { foreach (FaultFunction function in FaultToSave.Function) { ///Element = Function saveFaultXml.WriteStartElement("Function"); /// Attribute = Name if (function.Name != null) { saveFaultXml.WriteAttributeString("Name", function.Name.ToString()); } /// Attribute = OverrideErrorCode if (function.OverrideErrorCode != null) { saveFaultXml.WriteAttributeString("OverrideErrorCode", function.OverrideErrorCode.ToString()); } /// Attribute = OverrideReturnValue if (function.OverrideReturnValue != null) { saveFaultXml.WriteAttributeString("OverrideReturnValue", function.OverrideReturnValue.ToString()); } /// Attribute = PassThrough if (function.PassThrough != null) { saveFaultXml.WriteAttributeString("PassThrough", function.PassThrough.ToString()); } /// Attribute = Exception if (function.Exception != null) { saveFaultXml.WriteAttributeString("Exception", function.Exception.ToString()); } /// Attribute = Allocation if (function.Allocation != null) { saveFaultXml.WriteAttributeString("Allocation", function.Allocation.ToString()); } // checkresource Tag if (function.CheckResource != null) { foreach (FaultFunctionCheckResource checkresource in function.CheckResource) { /// Element = CheckResource saveFaultXml.WriteStartElement("CheckResource"); /// Attribute = ParamIndex if (checkresource.ParamIndex != null) { saveFaultXml.WriteAttributeString("ParamIndex", checkresource.ParamIndex.ToString()); } /// Attribute = Exists if (checkresource.Exists != null) { saveFaultXml.WriteAttributeString("Exists", checkresource.Exists.ToString()); } /// end of checkresource Element saveFaultXml.WriteEndElement(); } } //matchparams tag if (function.MatchParams != null) { foreach (FaultFunctionMatchParams matchParams in function.MatchParams) { /// Element = MatchParams saveFaultXml.WriteStartElement("MatchParams"); if (matchParams.MatchParam != null) { foreach (FaultFunctionMatchParams matchParam in matchParams.MatchParam) { /// Element = MatchParam saveFaultXml.WriteStartElement("MatchParam"); /// Attribute = Name if (matchParam.Name != null) { saveFaultXml.WriteAttributeString("Name", matchParam.Name.ToString()); } /// Attribute = TestOperator if (matchParam.TestOperator != null) { saveFaultXml.WriteAttributeString("TestOperator", matchParam.TestOperator.ToString()); } /// Attribute = TestValue if (matchParam.TestValue != null) { saveFaultXml.WriteAttributeString("TestValue", matchParam.TestValue.ToString()); } /// Attribute = CompareAsType if (matchParam.CompareAsType != null) { saveFaultXml.WriteAttributeString("CompareAsType", matchParam.CompareAsType.ToString()); } /// Attribute = ID if (matchParam.ID != null) { saveFaultXml.WriteAttributeString("ID", matchParam.ID.ToString()); } /// end of MatchParam Element saveFaultXml.WriteEndElement(); } } /// end of MatchParams Element saveFaultXml.WriteEndElement(); } } /// end of Fault Functions Element saveFaultXml.WriteEndElement(); } /// end of Fault Element saveFaultXml.WriteEndElement(); } } //closeing Faluts Tag saveFaultXml.WriteFullEndElement(); //closing xmlwriter. saveFaultXml.Close( ); }
//************************************************************************* // Method: UpdateFault // Description: Updating entry of the edited Fault in FaultTableByName // // Parameters: // FaultName = name of the edited Fault here acting as Key in hashtable // EditedFault = Object of edited Fault // // Return Value: None //************************************************************************* public void UpdateFault(string FaultName, Fault EditedFault) { if(FaultTableByName.Contains(FaultName)) { FaultTableByName.Remove(FaultName); FaultTableByName.Add(EditedFault.Name,EditedFault); } else { FaultTableByName.Add(EditedFault.Name,EditedFault); } }
//************************************************************************* // Method: AddFaultToTableByName // Description: Adding Fault to hashtable by FaultName as Key // // Parameters: // newFaultToAdd - the object of type Fault Class // // Return Value: true if successful, false otherwise //************************************************************************* protected bool AddFaultToTableByName(Fault newFaultToAdd) { FaultTableByName.Add(newFaultToAdd.Name, newFaultToAdd); return(true); }