Ejemplo n.º 1
0
        ///       <summary>
        ///       The method returns the value corresponding to the DCO field specified, from the current DCO for the field.
        ///       <param name="DCOTree">DCO Expression in the format [DCO].[page_type].[field_name]</param>
        ///       <returns>The value corresponding to the DCO field specified, from the current DCO.</returns>
        ///       </summary>
        public override string getDCOValueForField(string DCOTree)
        {
            // DCO reference in the template file should adhere to a 3 part string [DCO].[page_type].[field_name]
            // Parse the DCO reference and extract the page_type and field_name which can then be used to look up in the
            // current document that is being processed
            DCOTree = DCOTree.Replace("[", "").Replace("]", "");
            char[]      sep      = { '.' };
            string[]    dcoArray = DCOTree.Split(sep, 4, StringSplitOptions.None);
            TDCOLib.DCO page     = CurrentDCO.Parent();
            string      pageID   = page.ID;

            string output = "";

            try
            {
                // this is to pick up the field from the right page type
                if (dcoArray[2] == CurrentDCO.ID && dcoArray[1] == page.Type)
                {
                    output = CurrentDCO.Text;
                }
                else
                {
                    ExportCore.WriteLog(" Current field " + CurrentDCO.ID +
                                        " is  different from required field which is" + DCOTree);
                }
            }
            catch (Exception exp)
            {
                // There could be reference in the template for the documents that are not processed in the current batch
                // Template in TravelDocs can have reference to a field under Flight but the current batch doesn't have
                // any flight related input. Alternatively, Flight and Car Rental gets processed but for the Car Rental
                // data output, there cannot be any Flight reference
                ExportCore.WriteErrorLog(" Unable to find DCO reference for the field : " + DCOTree);
                ExportCore.WriteErrorLog(" Error while reading DCO reference: " + exp.ToString());
            }

            return(output);
        }
Ejemplo n.º 2
0
 public bool Parse()
 {
     try
     {
         XmlDocument templateXML = new XmlDocument();
         templateXML.Load(TemplateFilePath);
         ExportCore.WriteInfoLog("template file :" + TemplateFilePath);
         this.NameSpcManager = new XmlNamespaceManager(templateXML.NameTable);
         this.NameSpcManager.AddNamespace(Constants.SE_NAMESPACE_NAME, Constants.SE_NAMESPACE_URL);
         this.TemplateRoot = templateXML.DocumentElement;
         if (this.TemplateRoot.HasChildNodes)
         {
             this.HasMoreNodes = true;
             this.CurrentNode  = TemplateRoot.FirstChild;
         }
         templateContents = System.IO.File.ReadAllLines(TemplateFilePath);
     }
     catch (Exception exp)
     {
         ExportCore.WriteErrorLog("Error while parsing the template file, terminating. Details: " + exp.Message);
         throw new SmartExportException("Error while parsing template file. Please verify the syntax and semantics of the template file.");
     }
     return(true);
 }
Ejemplo n.º 3
0
        public void writeToFile(Dictionary <String, String> singleOutputFileNameMap)
        {
            try
            {
                // this ckeck is done to prevent empty file getting generated
                if (outputStringList.Count == 0 && !tempFileNameMap.ContainsKey(templateParser.GetOutputFileName()))
                {
                    exportCore.WriteLog("Empty content. Skipping writing to file: " + templateParser.GetOutputFileName());
                    return;
                }
                bool   clearBuffer    = false;
                string outputFileName = null;
                string outputFilePath = null;
                //this scenario happens only when the project doesn't have document and the action is attached at batch level
                //and the collate batch output flag is false
                if (null == singleOutputFileNameMap)
                {
                    string prefix = "";
                    //names output file with the name of the input file if true
                    if (templateParser.NameBatchOutputAfterInput())
                    {
                        prefix = Path.GetFileNameWithoutExtension((string)Globals.Instance.GetData(Constants.forLoopString.CURRENTFILE));
                    }
                    else
                    {
                        //names output file with thevalue sepcified in filename tag or use the default name
                        prefix = templateParser.GetOutputFileName();
                    }
                    outputFileName = prefix
                                     + "_"
                                     + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fffffff") + '.'
                                     + templateParser.GetOutputFileExt();
                    outputFilePath = Path.Combine(templateParser.GetOutputDirectory(),
                                                  outputFileName);

                    clearBuffer = true;
                }
                else
                {
                    outputFileName = templateParser.GetOutputFileName() + "_"
                                     + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fffffff") + '.'
                                     + templateParser.GetOutputFileExt();
                    outputFilePath = Path.Combine(templateParser.GetOutputDirectory(),
                                                  templateParser.AppendToFile() ?
                                                  singleOutputFileNameMap[templateParser.GetOutputFileName()] + "." + templateParser.GetOutputFileExt()
                                               : outputFileName);
                }
                //If append to file is false and one iteration is complete, then code will rename temp file to actual file name
                //and delete the entry in the map, so in next iteration new temp file is created for this template
                //as append to file is false
                if (!templateParser.AppendToFile() && tempFileNameMap.ContainsKey(templateParser.GetOutputFileName()))
                {
                    string tempPath = tempFileNameMap[templateParser.GetOutputFileName()];
                    createOrAppendToFile(tempPath, clearBuffer);
                    File.Move(tempPath, outputFilePath);
                    tempFileNameMap.Remove(templateParser.GetOutputFileName());
                    return;
                }

                //if appendtofile is true and temp map has an entry for temp file, for first iteration data is written in temp file
                //and then renamed to actual file, for all next iteration the data from the list is added to the actual file
                //not in temp file.
                //this condition will pass only for first iteration.
                if (templateParser.AppendToFile() && tempFileNameMap.ContainsKey(templateParser.GetOutputFileName()) && !File.Exists(outputFilePath))
                {
                    string tempPath = tempFileNameMap[templateParser.GetOutputFileName()];
                    createOrAppendToFile(tempPath, clearBuffer);
                    File.Move(tempPath, outputFilePath);
                    tempFileNameMap[templateParser.GetOutputFileName()] = outputFilePath;
                    return;
                }

                //if there are no temp file created then data is flushed to output file here.
                createOrAppendToFile(outputFilePath, clearBuffer);
            }
            catch (System.Exception exp)
            {
                exportCore.WriteErrorLog(exp.StackTrace);
                throw new SmartExportException("Error while writing output to file: " + exp.Message);
            }
        }
        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);
        }