Esempio n. 1
0
        public void TEST_OXMLMathParseOperator()
        {
            OXMLMathParseOperator parse;

            parse = new OXMLMathParseOperator(@"\OPER", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test  1 fail, parser return: " + parse.sOperator);

            parse = new OXMLMathParseOperator(@"dы\OPER(123)", 2);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test  2 fail, parser return:" + parse.sOperator);
            if (parse.sVar1 != "123")				Assert.Fail("Test  3 fail, parser return:" + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test  4 fail, parser return:" + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(123)(456)", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test  5 fail, parser return:" + parse.sOperator);
            if (parse.sVar1 != "123")				Assert.Fail("Test  6 fail, parser return:" + parse.sVar1);
            if (parse.sVar2 != "456")				Assert.Fail("Test  7 fail, parser return:" + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(123) (456)", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test  8 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "123")				Assert.Fail("Test  9 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test 10 fail, parser return: " + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(123)4(456)", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test 11 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "123")				Assert.Fail("Test 12 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test 13 fail, parser return: " + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(123)+(456)", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test 14 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "123")				Assert.Fail("Test 15 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test 16 fail, parser return: " + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER (123)(456)", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test 17 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "")					Assert.Fail("Test 18 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test 19 fail, parser return: " + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(123 ", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test 20 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "")					Assert.Fail("Test 21 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test 22 fail, parser return: " + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(1(2)3)(45(6))", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test 23 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "1(2)3")				Assert.Fail("Test 24 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "45(6)")				Assert.Fail("Test 25 fail, parser return: " + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(1(23)(456)", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test 26 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "")					Assert.Fail("Test 27 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test 28 fail, parser return: " + parse.sVar2);

            parse = new OXMLMathParseOperator(@"\OPER(", 0);
            if (parse.sOperator != @"\OPER")		Assert.Fail("Test 29 fail, parser return: " + parse.sOperator);
            if (parse.sVar1 != "")					Assert.Fail("Test 30 fail, parser return: " + parse.sVar1);
            if (parse.sVar2 != "")					Assert.Fail("Test 31 fail, parser return: " + parse.sVar2);

            Assert.Pass();
        }
Esempio n. 2
0
        public static void StrParse(string str, OpenXmlElement parent)
        {
            if (parent == null) return;

            string simpletext = "";

            for (int i = 0; i < str.Length; i++)
            {
                switch (str[i])
                {
                    case '\\':
                        var MOperator = new OXMLMathParseOperator(str, i);

                        //проверка на символ \\
                        if (MOperator.sOperator == "\\")
                        {
                            simpletext += '\\';
                            break;
                        }

                        //добавляем ранее пройденный текст
                        if (simpletext != "")
                        {
                            var rText = new OXMLMathRunWrap(simpletext);
                            parent.Append(rText.MainXMLElement);
                            simpletext = "";
                        }

                        //произошла какая либо ошибка
                        if (MOperator.sOperator == "") break;

                        //анализ операторов
                        if (MOperator.sOperator == @"\SUB")
                        {
                            if ((MOperator.sVar1 == "") && (MOperator.sVar2 == "")) break;

                            var sub = new OXMLMathSubscriptWrap();
                            StrParse(MOperator.sVar1, sub.XMLBase);
                            StrParse(MOperator.sVar2, sub.XMLSubArgument);
                            parent.Append(sub.MainXMLElement);

                            i = MOperator.iLastIndex;
                        }
                        if (MOperator.sOperator == @"\SUP")
                        {
                            if ((MOperator.sVar1 == "") && (MOperator.sVar2 == "")) break;

                            var sup = new OXMLMathSuperscriptWrap();
                            StrParse(MOperator.sVar1, sup.XMLBase);
                            StrParse(MOperator.sVar2, sup.XMLSuperArgument);
                            parent.Append(sup.MainXMLElement);

                            i = MOperator.iLastIndex;
                        }

                        if (MOperator.sOperator == @"\FRAC")
                        {
                            if ((MOperator.sVar1 == "") && (MOperator.sVar2 == "")) break;

                            var frac = new OXMLMathFractionWrap();
                            StrParse(MOperator.sVar1, frac.XMLNumerator);
                            StrParse(MOperator.sVar2, frac.XMLDenominator);
                            parent.Append(frac.MainXMLElement);

                            i = MOperator.iLastIndex;
                        }

                        break;
                    case '(':
                        int iCloseBracket = OXMLMathParseOperator.findCloseBracket(str, i);
                        if (iCloseBracket == -1)
                        {
                            simpletext += '(';
                            break;
                        }

                        string inner = str.Substring(i + 1, iCloseBracket - i - 1);

                        var delim = new OXMLMathDelimiterWrap();
                        StrParse(inner, delim.XMLBase);
                        parent.Append(delim.MainXMLElement);

                        i = iCloseBracket;
                        break;
                    default:
                        simpletext += str[i];
                        break;
                }
            }

            //Смотрим, остался ли простой текст
            if (simpletext != "")
            {
                var rText = new OXMLMathRunWrap(simpletext);
                parent.Append(rText.MainXMLElement);
            }
        }