示例#1
0
        private void ParseText(char ch, StiRtfToken token)
        {
            StringBuilder text = new StringBuilder(ch.ToString());

            int c = rtf.Peek();

            while (c == '\r' || c == '\n' || c == '\t' || c == '\0')
            {
                rtf.Read();
                c = rtf.Peek();
            }

            while (c != '\\' && c != '}' && c != '{' && c != Eof)
            {
                rtf.Read();
                text.Append((char)c);
                c = rtf.Peek();
                while (c == '\r' || c == '\n' || c == '\t' || c == '\0')
                {
                    rtf.Read();
                    c = rtf.Peek();
                }
            }

            token.Key = text.ToString();
        }
示例#2
0
 internal StiRtfTreeNode(StiRtfToken token)
 {
     this.children = new StiRtfNodeCollection();
     this.type     = (StiRtfNodeType)token.Type;
     this.key      = token.Key;
     this.hasParam = token.HasParameter;
     this.param    = token.Parameter;
 }
示例#3
0
        public StiRtfToken NextToken()
        {
            int         c;
            StiRtfToken token = new StiRtfToken();

            c = rtf.Read();
            while (c == '\r' || c == '\n' || c == '\t' || c == '\0')
            {
                c = rtf.Read();
            }

            if (c != Eof)
            {
                switch (c)
                {
                case '{':
                    token.Type = StiRtfTokenType.GroupStart;
                    break;

                case '}':
                    token.Type = StiRtfTokenType.GroupEnd;
                    break;

                case '\\':
                    ParseKeyword(token);
                    break;

                default:
                    token.Type = StiRtfTokenType.Text;
                    ParseText((char)c, token);
                    break;
                }
            }
            else
            {
                token.Type = StiRtfTokenType.Eof;
            }

            return(token);
        }
示例#4
0
        private void ParseKeyword(StiRtfToken token)
        {
            int c = rtf.Peek();

            #region Special character or control symbol
            if (!Char.IsLetter((char)c))
            {
                rtf.Read();
                if (c == '\\' || c == '{' || c == '}')
                {
                    //Special character
                    token.Type = StiRtfTokenType.Text;
                    token.Key  = ((char)c).ToString();
                }
                else
                {
                    //Control symbol
                    token.Type = StiRtfTokenType.Control;
                    token.Key  = ((char)c).ToString();
                    if (token.Key == "\'")
                    {
                        string code = string.Format("{0}{1}", (char)rtf.Read(), (char)rtf.Read());
                        token.HasParameter = true;
                        token.Parameter    = Convert.ToInt32(code, 16);
                    }
                }
                return;
            }
            #endregion

            #region Keyword
            StringBuilder keyWord = new StringBuilder();
            c = rtf.Peek();
            while (Char.IsLetter((char)c))
            {
                rtf.Read();
                keyWord.Append((char)c);
                c = rtf.Peek();
            }
            token.Type = StiRtfTokenType.Keyword;
            token.Key  = keyWord.ToString();
            #endregion

            #region Parameter
            if (Char.IsDigit((char)c) || c == '-')
            {
                token.HasParameter = true;

                bool negative = false;
                if (c == '-')
                {
                    negative = true;
                    rtf.Read();
                }
                StringBuilder paramStr = new StringBuilder();
                c = rtf.Peek();
                while (Char.IsDigit((char)c))
                {
                    rtf.Read();
                    paramStr.Append((char)c);
                    c = rtf.Peek();
                }
                int paramInt = Convert.ToInt32(paramStr.ToString());
                if (negative)
                {
                    paramInt = -paramInt;
                }
                token.Parameter = paramInt;
            }
            #endregion

            if (c == ' ')
            {
                rtf.Read();
            }
        }
示例#5
0
        private int ParseRtfTree()
        {
            int res         = 0;
            int level       = 0;
            int tokensCount = 0;
            int delayCount  = 0;

            Encoding encoding = Encoding.Default;

            StiRtfTreeNode curNode = rootNode;
            StiRtfTreeNode newNode = null;

            StiRtfToken tok = lex.NextToken();

            while (tok.Type != StiRtfTokenType.Eof)
            {
                tokensCount++;
                switch (tok.Type)
                {
                case StiRtfTokenType.GroupStart:
                    newNode = new StiRtfTreeNode(StiRtfNodeType.Group, "GROUP", false, 0);
                    curNode.AppendChild(newNode);
                    curNode = newNode;
                    level++;
                    break;

                case StiRtfTokenType.GroupEnd:
                    newNode = new StiRtfTreeNode(StiRtfNodeType.GroupEnd, "GROUPEND", false, 0);
                    curNode.AppendChild(newNode);
                    curNode = curNode.ParentNode;
                    //newNode = new StiRtfTreeNode(StiRtfNodeType.None, "NoneAfterGroup", false, 0);
                    //curNode.AppendChild(newNode);
                    level--;
                    //show progress
                    if (delayCount == 0)
                    {
                        delayCount = 8;
                    }
                    else
                    {
                        delayCount--;
                    }
                    break;

                case StiRtfTokenType.Keyword:
                case StiRtfTokenType.Control:
                case StiRtfTokenType.Text:
                    if (mergeSpecialCharacters)
                    {
                        bool isText = tok.Type == StiRtfTokenType.Text || (tok.Type == StiRtfTokenType.Control && tok.Key == "'");
                        if (curNode.LastChild != null && (curNode.LastChild.NodeType == StiRtfNodeType.Text && isText))
                        {
                            if (tok.Type == StiRtfTokenType.Text)
                            {
                                curNode.LastChild.NodeKey += tok.Key;
                                break;
                            }
                            if (tok.Type == StiRtfTokenType.Control && tok.Key == "'")
                            {
                                curNode.LastChild.NodeKey += DecodeControlChar(tok.Parameter, encoding);
                                break;
                            }
                        }
                        else
                        {
                            if (tok.Type == StiRtfTokenType.Control && tok.Key == "'")
                            {
                                newNode = new StiRtfTreeNode(StiRtfNodeType.Text, DecodeControlChar(tok.Parameter, encoding), false, 0);
                                curNode.AppendChild(newNode);
                                break;
                            }
                        }
                    }

                    newNode = new StiRtfTreeNode(tok);
                    curNode.AppendChild(newNode);

                    if (mergeSpecialCharacters)
                    {
                        if (level == 1 && newNode.NodeType == StiRtfNodeType.Keyword && newNode.NodeKey == "ansicpg")
                        {
                            encoding = Encoding.GetEncoding(newNode.Parameter);
                        }
                    }

                    break;

                default:
                    res = -1;
                    break;
                }
                tok = lex.NextToken();
            }

            rootNode.UpdateNodeIndex(true);

            if (level != 0)
            {
                res = -1;
            }
            return(res);
        }