private bool EvaluateIndividualCondition(string conditionText) { ExportCore.WriteLog(" Evaluating condition " + conditionText); bool response = false; List <string> operands = new List <string>(Regex.Split(conditionText, Constants.ALLOWED_OPERATORS)); // check if there are 2 operands if (3 != operands.Count) { throw new SmartExportException("Unsupported syntax. Check documentation: " + conditionText); } //check if allowed operator are used if (!Regex.Match(operands[1].Trim(), Constants.ALLOWED_OPERATORS).Success) { throw new SmartExportException("Unsupported syntax. Check documentation: " + conditionText); } if (operands[1].Trim() == Constants.Operators.CONTAINS && !operands[0].Trim().Equals(Constants.ConditionString.FILE_PAGE_TYPES)) { throw new SmartExportException("CONTAINS operator can be used only with " + Constants.ConditionString.FILE_PAGE_TYPES + ". Invalid usage: " + conditionText); } Regex rx = new Regex(pattern); for (int i = 0; i < operands.Count; i++) { //replace DCO referencing expressions if (rx.IsMatch(operands[i].Trim())) { string expr = operands[i]; operands[i] = dCODataRetriever.getDCOValue(operands[i].Trim()); if ("" == operands[i]) { ExportCore.WriteLog("Could not find value for " + expr + " in " + conditionText); return(false); } } else if (Constants.ConditionString.DOCUMENT_TYPE == operands[i].Trim()) { operands[i] = dCODataRetriever.getDocumentType(); } else if (Constants.ConditionString.PAGE_TYPE == operands[i].Trim()) { operands[i] = dCODataRetriever.getPageType(); } else if (Constants.ConditionString.TABLE_TYPE == operands[i].Trim()) { operands[i] = dCODataRetriever.getTableType(); } else if (Constants.ConditionString.FILE_PAGE_TYPES == operands[i].Trim()) { operands[i] = dCODataRetriever.getPageTypesInFile(); } else { operands[i] = operands[i].Trim(); } } //check if comparisons are done for same data types string operandOneType = dataTypeChecker.getType(operands[0].Trim()); string operandTwoType = dataTypeChecker.getType(operands[2].Trim()); // allow operations on numeric opernads even if they are not of the same data type if (operandOneType != operandTwoType && dataTypeChecker.numericTypes.Contains(operandOneType) && dataTypeChecker.numericTypes.Contains(operandTwoType)) { operandTwoType = operandOneType = castNumericDataTypes(operandOneType, operandTwoType); } if (operandOneType != operandTwoType) { throw new SmartExportException("Invalid comparisons in : " + conditionText + " " + operandOneType + " " + operands[1] + " " + operandTwoType); } try { response = ExpressionEvaluator.evaluateExpression(operands[0].Trim(), operands[2].Trim(), operandOneType, operands[1].Trim()); } catch (Exception exp) { // Evaluating conditions using this technique is risky (injections) // Moreover, this API is deprecated. This is for the timebeing (to prototype and define the POV) // If conditions do not evaluate properly, write log and skip the condition // TODO: Lexical parser to get the conditions and transform them to c# code constructs string message = "Condition evalution failed for condition:" + conditionText; ExportCore.WriteErrorLog(message); ExportCore.WriteErrorLog(exp.StackTrace); throw new SmartExportException(message); } ExportCore.WriteLog(" Condition " + conditionText + " is evaluated as " + response); return(response); }
public void EvaluateData(XmlNode DataNode) { Stopwatch sw = Stopwatch.StartNew(); string NodeName = ((XmlElement)DataNode).Name; if (DataNode.HasChildNodes) { StringBuilder text = new StringBuilder(Constants.EMPTYSTRING); foreach (XmlNode node in DataNode.ChildNodes) { switch (node.Name) { case Constants.TEXT_NODE_NAME: text.Append(node.Value.Trim()); break; case Constants.SE_TAB_NODE_NAME: text.Append(Constants.TAB_SPACE); break; case Constants.SE_NEW_LINE_NODE_NAME: text.Append(Constants.NEW_LINE); break; case Constants.SE_COMMA_NODE_NAME: text.Append(Constants.COMMA); break; case Constants.SE_VALUE_NODE_NAME: string value = ""; if (isTableColumn) { value = dCODataRetriever.getColumnValueForRow(node.Attributes["select"].Value); text.Append(ExportCore.getExportUtil.escapeString(value, columnSeparator)); } else { value = dCODataRetriever.getDCOValue(node.Attributes["select"].Value).Trim(); text.Append(ExportCore.getExportUtil.escapeString(value)); } break; case Constants.SE_SMART_PARAM_NODE_NAME: text.Append(SmartNav.MetaWord(Constants.SMARTP_AT + node.InnerText.Trim()).Trim()); ExportCore.WriteDebugLog("smart param value for '" + node.InnerText.Trim() + "' is " + text); break; default: ExportCore.WriteInfoLog("Node type [" + node.Name + "] is not supported inside data node. Will be ignored "); break; } } if (text.Length > 0) { if (isHeader) { Globals.Instance.SetData(Constants.CSV_HEADERS, text.ToString()); } else { ExportCore.getExportUtil.addToOutPutList(text.ToString()); } } } ExportCore.WriteDebugLog(" EvaluateData(" + DataNode + ") completed in " + sw.ElapsedMilliseconds + " ms."); sw.Stop(); }