public QuestionPaper GetQuestionPaperFromText() { QuestionPaper questionPaper = null; if (!string.IsNullOrEmpty(this.TextData)) { questionPaper = new QuestionPaper(); List <string> questionList = GetQuestionList(); foreach (string questionString in questionList) { if (!(questionString.Equals("\n") || questionString.Equals(string.Empty))) { Question question = new Question(); string description = GetQuestionDescription(questionString); question.SetDescription(GetQuestionDescriptionWithoutImage(description)); question.SetImage(GetQuestionImage(description)); question.SetOptionList(GetOptions(questionString)); question.SetOptionType(GetOptionType(questionString)); question.SetDifficultyLevel(GetDifficultyLevel(questionString)); question.SetCategoryId(GetCategoryId(questionString)); questionPaper.AddQuestion(question); } } } return(questionPaper); }
private void ConvertFile(string inputFile, string outputFile) { string fileName = Path.GetFileName(inputFile); WordReader wordReader = new WordReader(inputFile); string fileData = wordReader.ReadData(); if (string.IsNullOrWhiteSpace(fileData)) { logger.AppendLog("Blank file."); logger.AppendError("Aborting file conversion. Please ensure file contains appropriate data."); return; } logger.AppendLog("Getting formatting rules from config file..."); QuestionPaperFormattingRules formattingRules; try { using (StreamReader r = new StreamReader(NamedValue.TestPaperConversion.RulesConfigFile)) { string json = r.ReadToEnd(); formattingRules = JsonConvert.DeserializeObject <QuestionPaperFormattingRules>(json); } logger.AppendLog("Formatting rules successully retrieved."); } catch { string errorMessage = "Error reading TestPaperFormattingRules.json file." + " Ensure file is present and correct options are chosen."; logger.AppendError(errorMessage); throw new Exception(errorMessage); } logger.AppendLog(string.Empty); logger.AppendLog("Validating file format..."); QuestionPaperFormatValidator formatValidator = new QuestionPaperFormatValidator( fileData, formattingRules); List <FileFormattingError> errorList = formatValidator.Validate(); logger.AppendLog(string.Empty); logger.AppendLog("Validated file format."); if (errorList.Count() == 0) { logger.AppendLog("No errors found."); logger.AppendLog(string.Empty); logger.AppendLog("Beginning file conversion..."); logger.AppendLog("Processing file..."); QuestionPaperParser questionPaperParser = new QuestionPaperParser(fileData, formattingRules); QuestionPaper questionPaper = questionPaperParser.GetQuestionPaperFromText(); questionPaper.Name = fileName; QuestionPaperTable testDt = new QuestionPaperTable(questionPaper); DataTable dataTable = testDt.GetDataTable(); logger.AppendLog("Writing data to excel file..."); ExcelWriter dataWriter = new ExcelWriter(outputFile); dataWriter.WriteData(dataTable); logger.AppendLog("Output file generated."); logger.AppendLog("outputFile: " + outputFile); logger.AppendLog("File conversion successful."); } else { logger.AppendError("Format error."); logger.AppendLog(string.Empty); logger.AppendLog("Error summary..."); logger.AppendLog("------------------------------"); foreach (FileFormattingError error in errorList) { logger.AppendLog("Question " + (error.QuestionNumber + 1).ToString()); foreach (string errorMessage in error.ErrorMessage) { logger.AppendLog(errorMessage.ToString()); } logger.AppendLog(string.Empty); } logger.AppendError("File conversion failed. Correct the format and try again."); } }
public QuestionPaperTable(QuestionPaper questionPaper) { this.questionPaper = questionPaper; }