/// <summary> /// Evaluate full expression after alias mapping. /// </summary> /// <param name="value"></param> /// <param name="classNameAlias"></param> /// <returns></returns> private string GetFullExpressionFromAliasEvaluation(string value, Dictionary <string, AliasReference> classNameAlias, out bool isIterator, bool isDetailsView = false) { try { isIterator = false; do { foreach (string key in classNameAlias.Keys) { AliasReference alias = classNameAlias[key]; if (alias.referenceObject == null) { value = value.Replace("[" + key + "]", "[" + alias.iteration.ToString() + "]"); } } string[] expressionSplit = value.Split('.'); if (!expressionSplit[0].StartsWith("kresult")) { if (classNameAlias.ContainsKey(expressionSplit[0]?.ToLower())) { AliasReference aliasReference = classNameAlias[expressionSplit[0]?.ToLower()]; if (aliasReference.referenceObject == null) { expressionSplit[0] = aliasReference.iteration.ToString(); isIterator = true; } else { expressionSplit[0] = aliasReference.referenceObject + (aliasReference.iteration == -1 ? "" : "[" + aliasReference.iteration + "]"); } } //Handle K-Object aliasing to arrays. else if (classNameAlias.ContainsKey(expressionSplit[0]?.Split('[')[0]?.ToLower())) { string[] expSplit = expressionSplit[0]?.Split('['); AliasReference aliasReference = classNameAlias[expSplit[0]?.ToLower()]; if (aliasReference.referenceObject != null && aliasReference.iteration == -1) { expSplit[0] = aliasReference.referenceObject; expressionSplit[0] = string.Join("[", expSplit); } } } value = string.Join(".", expressionSplit); } while (classNameAlias.ContainsKey(value.Split('.')[0].ToLower()) && value.Split('.')[0] != "kresult"); return(value); } catch (Exception ex) { throw; } }
/// <summary> /// Verify if the current depth is under iteration. /// </summary> /// <param name="depth"></param> /// <param name="classNameAlias"></param> /// <param name="classNameAliasDepth"></param> /// <returns></returns> private bool HasIteration(int depth, Dictionary <string, AliasReference> classNameAlias, Dictionary <int, string> classNameAliasDepth) { try { bool hasIteration = false; if (classNameAliasDepth[depth] != null) { string className = classNameAliasDepth[depth]; AliasReference aliasReference = classNameAlias[className]; if (aliasReference.iteration != -1) { hasIteration = true; } } return(hasIteration); } catch (Exception ex) { throw; } }
public string Execute(string customerId, KEntity entity, dynamic fpDetails, Pagination viewDetails, string queryString, HtmlDocument document, Dictionary <string, long> functionLog, bool isDetailsView = false, bool isNFSite = false) { try { Dictionary <string, AliasReference> classNameAlias = new Dictionary <string, AliasReference>(); Dictionary <int, string> classNameAliasDepth = new Dictionary <int, string>(); int depth = 0; bool isIteration = false; bool isRepeat = false; ExpressionEvaluator evaluator = new ExpressionEvaluator(); HtmlNode readNode = document.DocumentNode.FirstChild; HtmlDocument finalDocument = new HtmlDocument(); if (readNode.OuterHtml.ToLower() == "<!doctype html>") { finalDocument.LoadHtml(readNode.OuterHtml); } else { finalDocument.LoadHtml(GetProcessedNode(readNode, classNameAlias, classNameAliasDepth, depth, isRepeat, evaluator, customerId, entity, fpDetails, viewDetails, queryString, functionLog, isDetailsView, isNFSite).OuterHtml); } HtmlNode writeNode = finalDocument.DocumentNode.FirstChild; while (readNode.NodeType != HtmlNodeType.Document) { if (isIteration) { readNode = readNode.FirstChild; writeNode.InsertAfter(GetProcessedNode(readNode, classNameAlias, classNameAliasDepth, depth, isRepeat, evaluator, customerId, entity, fpDetails, viewDetails, queryString, functionLog, isDetailsView, isNFSite), writeNode.LastChild); writeNode = writeNode.LastChild; depth++; isIteration = false; isRepeat = true; } else if (readNode.HasChildNodes && writeNode.NodeType != HtmlNodeType.Comment) { readNode = readNode.FirstChild; writeNode.PrependChild(GetProcessedNode(readNode, classNameAlias, classNameAliasDepth, depth, isRepeat, evaluator, customerId, entity, fpDetails, viewDetails, queryString, functionLog, isDetailsView, isNFSite)); writeNode = writeNode.FirstChild; depth++; } else if (readNode.NextSibling != null) { if (classNameAliasDepth.ContainsKey(depth)) { string classAlias = classNameAliasDepth[depth]; if (classNameAlias.ContainsKey(classAlias)) { classNameAlias.Remove(classAlias); } classNameAliasDepth.Remove(depth); } readNode = readNode.NextSibling; writeNode.ParentNode.InsertAfter(GetProcessedNode(readNode, classNameAlias, classNameAliasDepth, depth, isRepeat, evaluator, customerId, entity, fpDetails, viewDetails, queryString, functionLog, isDetailsView, isNFSite), writeNode); writeNode = writeNode.NextSibling; } else { while (!isIteration && readNode.NextSibling == null && readNode.NodeType != HtmlNodeType.Document) { depth--; readNode = readNode.ParentNode; writeNode = writeNode.ParentNode; if (classNameAliasDepth.ContainsKey(depth)) { string classAlias = classNameAliasDepth[depth]; AliasReference aliasReference = classNameAlias[classAlias]; if (classAlias != "kresult" && ++aliasReference.iteration < aliasReference.maxIteration) { isIteration = true; } else { if (classAlias == "kresult") { fpDetails["_system"]["kresult"] = null; } if (classNameAlias.ContainsKey(classAlias)) { classNameAlias.Remove(classAlias); } //classNameAliasDepth.Remove(depth-1); classNameAliasDepth.Remove(depth); } } } if (!isIteration && readNode.NextSibling != null) { readNode = readNode.NextSibling; writeNode.ParentNode.InsertAfter(GetProcessedNode(readNode, classNameAlias, classNameAliasDepth, depth, isRepeat, evaluator, customerId, entity, fpDetails, viewDetails, queryString, functionLog, isDetailsView, isNFSite), writeNode); writeNode = writeNode.NextSibling; } } if (isRepeat && classNameAliasDepth.Count == 0) { isRepeat = false; } } HtmlNodeCollection kScriptNodes = finalDocument.DocumentNode.SelectNodes("//k-script"); if (kScriptNodes != null && kScriptNodes.Count() > 0) { foreach (HtmlNode kscriptNode in kScriptNodes) { if (kscriptNode.PreviousSibling != null) { HtmlNode sibling = kscriptNode.PreviousSibling; foreach (HtmlNode childNode in kscriptNode.ChildNodes) { kscriptNode.ParentNode.InsertAfter(childNode, sibling); sibling = childNode; } } else { kscriptNode.ParentNode.PrependChildren(kscriptNode.ChildNodes); } kscriptNode.Remove(); } } string htmlString = finalDocument.DocumentNode.OuterHtml; if (!String.IsNullOrEmpty(htmlString)) { return(System.Net.WebUtility.HtmlDecode(htmlString)); } else { return(null); } } catch (Exception ex) { throw; } }