Advertisement
Guest User

cr

a guest
Feb 17th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. namespace ConfigCheckerEngine.Models
  2. {
  3.     using ConfigCheckerEngine.Models.FeatureMappings;
  4.     using ConfigCheckerEngine.Models.JSONMappings;
  5.     using ConfigCheckerEngine.Utils.Constants;
  6.     using System.Collections.Generic;
  7.     using System.Linq;
  8.     using System.Text;
  9.     using System.Xml;
  10.  
  11.     public class ConfigReader
  12.     {
  13.         public List<Feature> ReadAllFeaturesForOperator(XmlDocument doc)
  14.         {
  15.             Dictionary<string, Feature> allKeys = new Dictionary<string, Feature>();
  16.  
  17.             var applicationSettingsNodes = doc.GetElementsByTagName(Constants.XmlNodeForExtraction).Item(0).ChildNodes;
  18.  
  19.             for (int i = 0; i < applicationSettingsNodes.Count; i++)
  20.             {
  21.                 if (applicationSettingsNodes[i].NodeType == XmlNodeType.Comment)
  22.                 {
  23.                     continue;
  24.                 }
  25.  
  26.                 var currentNodeKey = applicationSettingsNodes[i].Attributes[Constants.KeyNodeAttribute].Value;
  27.  
  28.                 if (!allKeys.ContainsKey(currentNodeKey))
  29.                 {
  30.                     string curNodeValue = "";
  31.                     if (applicationSettingsNodes[i].Attributes[Constants.ValueNodeAttribute] != null)
  32.                     {
  33.                         curNodeValue = applicationSettingsNodes[i].Attributes[Constants.ValueNodeAttribute].Value;
  34.                     }
  35.                     var curNodeTransformType = "";
  36.                     if (applicationSettingsNodes[i].Attributes[Constants.TransformTypeNodeAttribute] != null)
  37.                     {
  38.                         curNodeTransformType = applicationSettingsNodes[i].Attributes[Constants.TransformTypeNodeAttribute].Value;
  39.                     }
  40.                     var curNodeLocator = "";
  41.                     if (curNodeTransformType == "SetAttributes")
  42.                     {
  43.                         curNodeLocator = applicationSettingsNodes[i].Attributes[Constants.LocatorNodeAttribute].Value;
  44.                     }
  45.                     allKeys.Add(currentNodeKey, new Feature() {
  46.                         Key = currentNodeKey,
  47.                         Value = curNodeValue,
  48.                         TransformType = curNodeTransformType,
  49.                         Locator = curNodeLocator
  50.                     });
  51.                 }
  52.             }
  53.  
  54.             return allKeys.Select(kvp => kvp.Value).ToList();
  55.         }
  56.         public List<string> FindWebConfigDuplicates(XmlDocument doc)
  57.         {
  58.             List<string> allKeys = new List<string>();
  59.             List<string> repeatedKeys = new List<string>();
  60.  
  61.             var applicationSettingsNodes = doc.GetElementsByTagName(Constants.XmlNodeForExtraction).Item(0).ChildNodes;
  62.  
  63.             for (int i = 0; i < applicationSettingsNodes.Count; i++)
  64.             {
  65.                 if (applicationSettingsNodes[i].NodeType == XmlNodeType.Comment)
  66.                 {
  67.                     continue;
  68.                 }
  69.  
  70.                 var currentNodeKey = applicationSettingsNodes[i].Attributes[Constants.KeyNodeAttribute].Value;
  71.  
  72.                 if (!allKeys.Contains(currentNodeKey))
  73.                 {
  74.                     allKeys.Add(currentNodeKey);
  75.                 }
  76.                 else
  77.                 {
  78.                     repeatedKeys.Add(currentNodeKey);
  79.                 }
  80.             }
  81.  
  82.             return repeatedKeys;
  83.         }
  84.  
  85.         public XmlDocument ConvertToXMLFromPath(string input)
  86.         {
  87.             XmlDocument doc = new XmlDocument();
  88.  
  89.             doc.Load(input);
  90.  
  91.             return doc;
  92.         }
  93.  
  94.         public XmlDocument ConvertToXMLFromString(string input)
  95.         {
  96.             XmlDocument doc = new XmlDocument();
  97.            
  98.             doc.LoadXml(input);
  99.  
  100.             return doc;
  101.         }
  102.  
  103.         private string RemoveBOMSymbol(string xmlAsString)
  104.         {
  105.             string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
  106.  
  107.             if (xmlAsString.StartsWith(_byteOrderMarkUtf8))
  108.             {
  109.                 xmlAsString = xmlAsString.Replace(_byteOrderMarkUtf8, string.Empty);
  110.             }
  111.  
  112.             return xmlAsString;
  113.         }
  114.  
  115.         public XmlDocument ConvertJSONObjectToXML(RootObject deserializedObject)
  116.         {
  117.             var xmlAsString = "";
  118.  
  119.             foreach (var item in deserializedObject.lines)
  120.             {
  121.                 xmlAsString += (item.text);
  122.             }
  123.  
  124.             return ConvertToXMLFromString(RemoveBOMSymbol(xmlAsString));
  125.         }
  126.  
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement