Esempio n. 1
0
        public static string EncodeHTML(string source)
        {
            if (source == null)
            {
                return(null);
            }
            string result = source;

            result = StringProcess.RemoveTag(result, @"&", @"&");
            result = StringProcess.RemoveTag(result, "\"", @""");
            result = StringProcess.RemoveTag(result, @"<", @"&lt;");
            result = StringProcess.RemoveTag(result, @">", @"&gt;");
            return(result);
        }
Esempio n. 2
0
        public static string FormatStringExportXML(string textContent)
        {
            if (textContent == null)
            {
                return(null);
            }
            string result = textContent;

            result = StringProcess.RemoveTag(result, @"[html]", "");
            result = EncodeHTML(result);
            result = StringProcess.RemoveTag(result, @"&lt;cbr&gt;", @"<br/>");
            //result = "<p>" + result;
            //result = StringProcess.RemoveTag(result, @"\n", "<br/>");
            //result = StringProcess.RemoveTag(result, @"&lt;cbr&gt;", @"<p/><p>");
            //result = result + "<p/>";
            return(result);
        }
Esempio n. 3
0
        public List <QuestionTmpModel> StripTagsCharArray(StreamReader reader, bool checkCate, bool checkHTML)
        {
            List <QuestionTmpModel> list = new List <QuestionTmpModel>();

            try
            {
                string line = null;
                List <OptionViewModel> options = new List <OptionViewModel>();
                bool isStartQuestion           = false;
                //string destination = "[html]";
                int           countCode     = 0;
                int           inLine        = 0;
                bool          isPlain       = false;
                StringProcess stringProcess = new StringProcess();
                while ((line = reader.ReadLine()) != null)
                {
                    string id       = null;
                    int    colon    = 0;
                    string question = null;
                    string right    = null;
                    string wrong    = null;
                    string result   = null;
                    string mark     = null;
                    //string flagCategory = "";
                    int countRight = 0;
                    int countWrong = 0;
                    //int countCate = 0;
                    //int countStartCate = 0;
                    int  countElement = 1;
                    bool isBlock      = false;
                    //bool isStartCate = false;
                    bool isInLine = false;
                    bool isStart  = false;
                    bool isEnd    = false;
                    //bool keeping = false;

                    //bool isComma = false;
                    bool isMultipleChoice = false;
                    if (line.Contains("$CATEGORY"))
                    {
                        string[] temp = line.Split('/');
                        for (int i = 0; i < temp.Count(); i++)
                        {
                            if (i == 1)
                            {
                                category = temp[i];
                            }
                            if (i == 2)
                            {
                                learningOutcome = temp[i];
                            }
                            if (i == 3)
                            {
                                level = temp[i];
                            }
                        }
                    }
                    if (!line.StartsWith("//"))
                    {
                        line = stringProcess.RemoveHtmlBrTag(line);

                        string resultTmp = "";

                        if (line.Contains("[plain]"))
                        {
                            isPlain = true;
                        }
                        if (checkHTML == false && isPlain == false)
                        {
                            HtmlDocument htmlDoc = new HtmlDocument();
                            htmlDoc.LoadHtml(line);
                            resultTmp = htmlDoc.DocumentNode.InnerText;
                        }
                        if (!resultTmp.Equals(""))
                        {
                            result = WebUtility.HtmlDecode(resultTmp);
                        }
                        else
                        {
                            result = line;
                        }

                        result = stringProcess.RemoveHtmlTagGIFT(result);
                        for (int i = 0; i < result.Length; i++)
                        {
                            char let = result[i];
                            #region start count to track the position
                            //if (let == '$' && !isStartQuestion)
                            //{
                            //    category = "";
                            //    level = "";
                            //    learningOutcome = "";
                            //    countStartCate++;
                            //    continue;
                            //}
                            //if (let == '$')
                            //{
                            //    category = "";
                            //    level = "";
                            //    learningOutcome = "";
                            //    flagCategory += let;


                            //}
                            //if (flagCategory.Equals("$CATEGORY"))
                            //{
                            //    countStartCate++;
                            //    continue;
                            //}

                            //if (let == '/' && !isStartQuestion)
                            //{

                            //    isStartCate = true;
                            //    countCate++;
                            //    continue;
                            //}
                            if (let == ':' && !isStartQuestion)
                            {
                                countCode++;
                                colon    = colon + 1;
                                isStart  = true;
                                isInLine = true;
                                continue;
                            }
                            if (inLine == 1)
                            {
                                char tab = (char)9;

                                if (let == '=' || let == '~' || let == tab)
                                {
                                    inLine          = 2; //{ is end question
                                    countCode       = 0;
                                    isStartQuestion = true;
                                    // continue;
                                }
                                else
                                {
                                    inLine = 3; // { in question
                                }
                            }
                            if (let == '=' && !isBlock && isStartQuestion && isInLine == false && inLine == 2)
                            {
                                countRight++;
                                countWrong = 0;
                                isBlock    = true;
                                isInLine   = true;
                                continue;
                            }
                            if (let == '~' && !isBlock && isStartQuestion && isInLine == false && inLine == 2)
                            {
                                countWrong++;
                                countRight = 0;
                                isBlock    = true;
                                isInLine   = true;
                                continue;
                            }
                            if (let == '}' && isStartQuestion && isInLine == false)
                            {
                                isEnd           = true;
                                isStartQuestion = false;
                                continue;
                            }

                            //if (let == '?')
                            //{
                            //    isComma = true;

                            //}
                            if (let == '{' && !isBlock)
                            {
                                //countCode = 0;
                                //isStartQuestion = true;
                                inLine = 1; // not sure to end question
                            }


                            #endregion

                            #region add character to variables
                            //if (countStartCate == 3 && isStartCate)
                            //{

                            //    if (countCate == 1)
                            //    {
                            //        category += let;
                            //        continue;
                            //    }
                            //    if (countCate == 2)
                            //    {
                            //        learningOutcome += let;
                            //        continue;
                            //    }
                            //    if (countCate == 3)
                            //    {
                            //        level += let;
                            //        continue;
                            //    }


                            //}
                            // if (countCode <= 3 && countStartCate == 0 && isStart)
                            if (countCode <= 3 && isStart)
                            {
                                id += let;
                                continue;
                            }
                            if (countCode >= 4)
                            {
                                // { char is in question
                                if (inLine == 3)
                                {
                                    question += '{';
                                    if (colon >= 5)
                                    {
                                        question += ':';
                                        colon     = 4;
                                    }
                                    question += let;
                                    inLine    = 0;
                                    isStart   = false;
                                    continue;
                                }
                                if (inLine == 0)
                                {
                                    if (colon >= 5)
                                    {
                                        question += ':';
                                        colon     = 4;
                                    }
                                    question += let;
                                    inLine    = 0;
                                    isStart   = false;
                                    continue;
                                }
                            }
                            if (countRight >= 1 && !isEnd)
                            {
                                right += let;
                                continue;
                            }
                            if (isMultipleChoice == true)
                            {
                                if (let != '%')
                                {
                                    mark += let;
                                    continue;
                                }
                            }
                            if (mark != null)
                            {
                                if (!mark.Equals(""))
                                {
                                    if (int.Parse(mark) < 0)
                                    {
                                        countWrong++;
                                        countRight       = 0;
                                        isBlock          = true;
                                        isMultipleChoice = false;
                                        mark             = null;
                                        continue;
                                    }
                                    if (int.Parse(mark) > 0)
                                    {
                                        countRight++;
                                        countWrong       = 0;
                                        isBlock          = true;
                                        isMultipleChoice = false;
                                        mark             = null;
                                        continue;
                                    }
                                }
                            }

                            if (countWrong >= 1 && !isEnd)
                            {
                                if (let == '%' && countElement == 1)
                                {
                                    isMultipleChoice = true;
                                    continue;
                                }
                                else
                                {
                                    wrong += let;
                                    countElement++;
                                    continue;
                                }
                            }
                            #endregion
                        }
                    }
                    if (category != null /*&& learningOutcome != null && level != null*/ && checkCate == true)
                    {
                        if (checkCate == false)
                        {
                            category        = "";
                            learningOutcome = "";
                            level           = "";
                            //countStartCate = 0;
                            //isStartCate = false;
                        }
                        else
                        {
                            quesModel.Category        = category;
                            quesModel.LearningOutcome = learningOutcome;
                            quesModel.Level           = level;
                            //countStartCate = 0;
                            //isStartCate = false;
                        }
                    }
                    if (id != null)
                    {
                        quesModel.Code = id;
                        id             = null;
                    }
                    if (question != null)
                    {
                        if (question.Equals(""))
                        {
                            quesModel.QuestionContentError = "Question content is empty";
                        }

                        if (quesModel.QuestionContent != null)
                        {
                            quesModel.QuestionContent += "<br>" + question;
                        }
                        else
                        {
                            question = stringProcess.UpperCaseKeyWord(question);
                            if (question.Contains("[html]"))
                            {
                                quesModel.QuestionContent = question;
                            }
                            else
                            {
                                quesModel.QuestionContent = "[html]" + question;
                            }
                        }
                        question = null;
                    }

                    if (right != null)
                    {
                        optionModel = new OptionViewModel();
                        optionModel.OptionContent = "[html]" + right;
                        optionModel.IsCorrect     = true;
                        options.Add(optionModel);
                    }
                    if (wrong != null)
                    {
                        optionModel = new OptionViewModel();
                        optionModel.OptionContent = "[html]" + wrong;
                        optionModel.IsCorrect     = false;
                        options.Add(optionModel);
                    }

                    if (quesModel.QuestionContent != null && isEnd && quesModel.Code != null)
                    {
                        int  countMatching  = 0;
                        bool isMatchingQues = false;
                        if (options.Count < 4)
                        {
                            quesModel.Error = "Number of option " + options.Count.ToString();
                        }

                        quesModel.Options = options;
                        foreach (var item in quesModel.Options)
                        {
                            if (item.IsCorrect == true)
                            {
                                countMatching  = countMatching + 1;
                                isMatchingQues = true;
                            }
                            if (item.IsCorrect == false)
                            {
                                countMatching = 0;
                            }
                            if (countMatching >= 4 && isMatchingQues)
                            {
                                quesModel.Status = (int)Enum.StatusEnum.Invalid;
                            }
                        }
                        list.Add(quesModel);
                        quesModel        = new QuestionTmpModel();
                        options          = new List <OptionViewModel>();
                        countCode        = 0;
                        inLine           = 0;
                        isMultipleChoice = false;
                        isPlain          = false;
                    }
                }
            }
            catch (Exception ex)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                quesModel.OtherError = "Other Error " + ex.Message.ToString();
            }

            return(list);
        }