コード例 #1
0
ファイル: RTFNode.cs プロジェクト: yodus/RtfDomParser
 public RTFNode(RTFToken token)
 {
     this.strKeyword      = token.Key;
     this.bolHasParameter = token.HasParam;
     this.intParameter    = token.Param;
     if (token.Type == RTFTokenType.Control)
     {
         intType = RTFNodeType.Control;
     }
     else if (token.Type == RTFTokenType.Control)
     {
         intType = RTFNodeType.Control;
     }
     else if (token.Type == RTFTokenType.Keyword)
     {
         intType = RTFNodeType.Keyword;
     }
     else if (token.Type == RTFTokenType.ExtKeyword)
     {
         intType = RTFNodeType.ExtKeyword;
     }
     else if (token.Type == RTFTokenType.Text)
     {
         intType = RTFNodeType.Text;
     }
     else
     {
         intType = RTFNodeType.Text;
     }
 }
コード例 #2
0
        private void ParseText(int c, RTFToken token)
        {
            System.Text.StringBuilder myStr = new System.Text.StringBuilder((( char )c).ToString());

            c = ClearWhiteSpace();

            while (c != '\\' && c != '}' && c != '{' && c != EOF)
            {
                myReader.Read();
                myStr.Append(( char )c);
                c = ClearWhiteSpace();
            }

            token.Key = myStr.ToString();
        }
コード例 #3
0
 /// <summary>
 /// read token
 /// </summary>
 /// <returns>token readed</returns>
 public RTFToken ReadToken()
 {
     bolFirstTokenInGroup = false;
     myLastToken          = myCurrentToken;
     if (myLastToken != null && myLastToken.Type == RTFTokenType.GroupStart)
     {
         bolFirstTokenInGroup = true;
     }
     myCurrentToken = myLex.NextToken( );
     if (myCurrentToken == null || myCurrentToken.Type == RTFTokenType.Eof)
     {
         myCurrentToken = null;
         return(null);
     }
     intTokenCount++;
     if (myCurrentToken.Type == RTFTokenType.GroupStart)
     {
         if (_LayerStack.Count == 0)
         {
             _LayerStack.Push(new RTFRawLayerInfo());
         }
         else
         {
             RTFRawLayerInfo info = _LayerStack.Peek();
             _LayerStack.Push(info.Clone());
         }
         intLevel++;
     }
     else if (myCurrentToken.Type == RTFTokenType.GroupEnd)
     {
         if (_LayerStack.Count > 0)
         {
             _LayerStack.Pop();
         }
         intLevel--;
     }
     if (this.EnableDefaultProcess)
     {
         this.DefaultProcess();
     }
     //if (myTokenStack.Count > 0)
     //{
     //    myCurrentToken.Parent = (RTFToken)myTokenStack[myTokenStack.Count - 1];
     //}
     //myTokenStack.Add( myCurrentToken );
     return(myCurrentToken);
 }
コード例 #4
0
 public RTFNode( RTFToken token )
 {
     this.strKeyword = token.Key ;
     this.bolHasParameter = token.HasParam ;
     this.intParameter = token.Param ;
     if( token.Type == RTFTokenType.Control )
         intType = RTFNodeType.Control ;
     else if( token.Type == RTFTokenType.Control )
         intType = RTFNodeType.Control ;
     else if( token.Type == RTFTokenType.Keyword  )
         intType = RTFNodeType.Keyword ;
     else if( token.Type == RTFTokenType.ExtKeyword )
         intType = RTFNodeType.ExtKeyword ;
     else if( token.Type == RTFTokenType.Text )
         intType = RTFNodeType.Text ;
     else
         intType = RTFNodeType.Text ;
 }
コード例 #5
0
        private void ParseKeyword(RTFToken token)
        {
            int  c   = 0;
            bool ext = false;

            c = myReader.Peek();
            if (char.IsLetter(( char )c) == false)
            {
                myReader.Read();
                if (c == '*')
                {
                    // expend keyword
                    token.Type = RTFTokenType.Keyword;
                    myReader.Read();
                    //myReader.Read();
                    ext = true;
                    goto ReadKeywrod;
                }
                else if (c == '\\' || c == '{' || c == '}')
                {
                    // special character
                    token.Type = RTFTokenType.Text;
                    token.Key  = (( char )c).ToString();
                }
                else
                {
                    token.Type = RTFTokenType.Control;
                    token.Key  = (( char )c).ToString();
                    if (token.Key == "\'")
                    {
                        // read 2 hex characters
                        System.Text.StringBuilder text = new System.Text.StringBuilder();
                        text.Append(( char )myReader.Read());
                        text.Append(( char )myReader.Read());
                        token.HasParam = true;
                        token.Param    = Convert.ToInt32(text.ToString().ToLower(), 16);
                        if (token.Param == 0)
                        {
                            token.Param = 0;
                        }
                    }
                }
                return;
            }            //if( char.IsLetter( ( char ) c ) == false )

ReadKeywrod:

            // read keyword
            System.Text.StringBuilder Keyword = new System.Text.StringBuilder();
            c = myReader.Peek();
            while (char.IsLetter(( char )c))
            {
                myReader.Read();
                Keyword.Append(( char )c);
                c = myReader.Peek();
            }

            if (ext)
            {
                token.Type = RTFTokenType.ExtKeyword;
            }
            else
            {
                token.Type = RTFTokenType.Keyword;
            }
            token.Key = Keyword.ToString();

            // read a interger
            if (char.IsDigit(( char )c) || c == '-')
            {
                token.HasParam = true;
                bool Negative = false;
                if (c == '-')
                {
                    Negative = true;
                    myReader.Read();
                }
                c = myReader.Peek();
                System.Text.StringBuilder text = new System.Text.StringBuilder();
                while (char.IsDigit(( char )c))
                {
                    myReader.Read();
                    text.Append(( char )c);
                    c = myReader.Peek();
                }
                int p = Convert.ToInt32(text.ToString());
                if (Negative)
                {
                    p = -p;
                }
                token.Param = p;
            }            //if( char.IsDigit( ( char ) c ) || c == '-' )

            if (c == ' ')
            {
                myReader.Read();
            }
        }
コード例 #6
0
        /// <summary>
        /// read next token
        /// </summary>
        /// <returns>token</returns>
        public RTFToken NextToken( )
        {
            int      c     = 0;
            RTFToken token = new RTFToken();

            //myReader.Read();

            c = myReader.Read();

            /*
             * // Unknown what this particular code is for. It appears that if a block starts with a quote
             * // then we read until and end quote is found and add that as a text block. However, if no
             * // end quote is found and we hit and open or close token, then any text is thrown away.
             * // What is this code for?
             *
             * if (c == '\"')
             * {
             *  // 以双引号开头,读取连续的字符
             *  System.Text.StringBuilder str = new System.Text.StringBuilder();
             *  while (true)
             *  {
             *      c = myReader.Read();
             *      if (c < 0)
             *      {
             *          // 读取结束
             *          break;
             *      }
             *      if (c == '\"')
             *      {
             *          // 读取结束
             *          break;
             *      }
             *      else
             *      {
             *          str.Append((char)c);
             *      }
             *  }//while
             *  token.Type = RTFTokenType.Text;
             *  token.Key = str.ToString();
             *  return token;
             * }
             */

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

            //
//			c = myReader.Read();
//			while( c == '\r'
//				|| c == '\n'
//				|| c == '\t'
//				|| c == '\0')
//			{
//				c = myReader.Read();
//			}
            if (c != EOF)
            {
                switch (c)
                {
                case '{':
                    token.Type = RTFTokenType.GroupStart;
                    break;

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

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

                default:
                    token.Type = RTFTokenType.Text;
                    ParseText(c, token);
                    break;
                }
            }
            else
            {
                token.Type = RTFTokenType.Eof;
            }
            return(token);
        }
コード例 #7
0
 /// <summary>
 /// Accept rtf token
 /// </summary>
 /// <param name="token">RTF token</param>
 /// <returns>Is accept it?</returns>
 public bool Accept(RTFToken token, RTFReader reader)
 {
     if (token == null)
     {
         return(false);
     }
     if (token.Type == RTFTokenType.Text)
     {
         if (reader != null)
         {
             if (token.Key[0] == '?')
             {
                 if (reader.LastToken != null)
                 {
                     if (reader.LastToken.Type == RTFTokenType.Keyword &&
                         reader.LastToken.Key == "u" &&
                         reader.LastToken.HasParam)
                     {
                         // 紧跟在在“\uN”后面的问号忽略掉
                         if (token.Key.Length > 0)
                         {
                             CheckBuffer();
                             //myStr.Append(token.Key.Substring(1));
                         }
                         return(true);
                     }
                 }
             }
             //else if (token.Key == "\"")
             //{
             //    // 双引号开头,一直读取内容到双引号结束
             //    CheckBuffer();
             //    while (true)
             //    {
             //        int v = reader.InnerReader.Read();
             //        if (v > 0)
             //        {
             //            if (v == (int)'"')
             //            {
             //                break;
             //            }
             //            else
             //            {
             //                myStr.Append((char)v);
             //            }
             //        }
             //        else
             //        {
             //            break;
             //        }
             //    }//while
             //    return true;
             //}
         }
         CheckBuffer();
         myStr.Append(token.Key);
         return(true);
     }
     else if (token.Type == RTFTokenType.Control &&
              token.Key == "'" && token.HasParam)
     {
         if (reader.CurrentLayerInfo.CheckUCValueCount())
         {
             myBuffer.Add((byte)token.Param);
         }
         return(true);
     }
     if (token.Key == RTFConsts._u && token.HasParam)
     {
         // Unicode char
         CheckBuffer();
         // 不忽略 \u 指令
         myStr.Append((char)token.Param);
         reader.CurrentLayerInfo.UCValueCount = reader.CurrentLayerInfo.UCValue;
         return(true);
     }
     if (token.Key == "tab")
     {
         CheckBuffer();
         myStr.Append("\t");
         return(true);
     }
     if (token.Key == "emdash")
     {
         CheckBuffer();
         myStr.Append('—');
         return(true);
     }
     if (token.Key == "")
     {
         // 提示未识别的字符
         CheckBuffer();
         myStr.Append('-');
         return(true);
     }
     return(false);
 }
コード例 #8
0
ファイル: Parser.cs プロジェクト: MichaelLHerman/RtfDomParser
 /// <summary>
 /// Accept rtf token
 /// </summary>
 /// <param name="token">RTF token</param>
 /// <param name="reader"></param>
 /// <returns>Is accept it?</returns>
 public bool Accept(RTFToken token, RTFReader reader)
 {
     if (token == null)
     {
         return false;
     }
     if (token.Type == RTFTokenType.Text)
     {
         if (reader != null)
         {
             if (token.Key[0] == '?')
             {
                 if (reader.LastToken != null)
                 {
                     if (reader.LastToken.Type == RTFTokenType.Keyword
                         && reader.LastToken.Key == "u"
                         && reader.LastToken.HasParam)
                     {
                         if (token.Key.Length > 0)
                         {
                             CheckBuffer();
                             //myStr.Append(token.Key.Substring(1));
                         }
                         return true;
                     }
                 }
             }
             //else if (token.Key == "\"")
             //{
             //    CheckBuffer();
             //    while (true)
             //    {
             //        int v = reader.InnerReader.Read();
             //        if (v > 0)
             //        {
             //            if (v == (int)'"')
             //            {
             //                break;
             //            }
             //            else
             //            {
             //                myStr.Append((char)v);
             //            }
             //        }
             //        else
             //        {
             //            break;
             //        }
             //    }//while
             //    return true;
             //}
         }
         CheckBuffer();
         _myStr.Append(token.Key);
         return true;
     }
     if (token.Type == RTFTokenType.Control
         && token.Key == "'" && token.HasParam)
     {
         if (reader.CurrentLayerInfo.CheckUcValueCount())
         {
             _myBuffer.Add((byte) token.Param);
         }
         return true;
     }
     if (token.Key == RTFConsts.U && token.HasParam)
     {
         // Unicode char
         CheckBuffer();
         _myStr.Append((char) token.Param);
         reader.CurrentLayerInfo.UcValueCount = reader.CurrentLayerInfo.UcValue;
         return true;
     }
     if (token.Key == "tab")
     {
         CheckBuffer();
         _myStr.Append("\t");
         return true;
     }
     if (token.Key == "emdash")
     {
         CheckBuffer();
         _myStr.Append('—');
         return true;
     }
     if (token.Key == "")
     {
         CheckBuffer();
         _myStr.Append('-');
         return true;
     }
     return false;
 }
コード例 #9
0
ファイル: Parser.cs プロジェクト: MichaelLHerman/RtfDomParser
        /// <summary>
        /// read next token
        /// </summary>
        /// <returns>token</returns>
        public RTFToken NextToken()
        {
            int c;
            var token = new RTFToken();

            //myReader.Read();

            c = _myReader.Read();
            if (c == '\"')
            {
                var str = new StringBuilder();
                while (true)
                {
                    c = _myReader.Read();
                    if (c < 0)
                    {
                        break;
                    }
                    if (c == '\"')
                    {
                        break;
                    }
                    str.Append((char) c);
                } //while
                token.Type = RTFTokenType.Text;
                token.Key = str.ToString();
                return token;
            }

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

            //
            //			c = myReader.Read();
            //			while( c == '\r'
            //				|| c == '\n'
            //				|| c == '\t'
            //				|| c == '\0')
            //			{
            //				c = myReader.Read();
            //			}
            if (c != Eof)
            {
                switch (c)
                {
                    case '{':
                        token.Type = RTFTokenType.GroupStart;
                        break;
                    case '}':
                        token.Type = RTFTokenType.GroupEnd;
                        break;
                    case '\\':
                        ParseKeyword(token);
                        break;
                    default:
                        token.Type = RTFTokenType.Text;
                        ParseText(c, token);
                        break;
                }
            }
            else
            {
                token.Type = RTFTokenType.Eof;
            }
            return token;
        }
コード例 #10
0
ファイル: Parser.cs プロジェクト: MichaelLHerman/RtfDomParser
        private void ParseText(int c, RTFToken token)
        {
            var myStr = new StringBuilder(((char) c).ToString());

            c = ClearWhiteSpace();

            while (c != '\\' && c != '}' && c != '{' && c != Eof)
            {
                _myReader.Read();
                myStr.Append((char) c);
                c = ClearWhiteSpace();
            }

            token.Key = myStr.ToString();
        }
コード例 #11
0
ファイル: Parser.cs プロジェクト: MichaelLHerman/RtfDomParser
        private void ParseKeyword(RTFToken token)
        {
            int c;
            var ext = false;
            c = _myReader.Peek();
            if (char.IsLetter((char) c) == false)
            {
                _myReader.Read();
                if (c == '*')
                {
                    // expend keyword
                    token.Type = RTFTokenType.Keyword;
                    _myReader.Read();
                    //myReader.Read();
                    ext = true;
                    goto ReadKeywrod;
                }
                if (c == '\\' || c == '{' || c == '}')
                {
                    // special character
                    token.Type = RTFTokenType.Text;
                    token.Key = ((char) c).ToString();
                }
                else
                {
                    token.Type = RTFTokenType.Control;
                    token.Key = ((char) c).ToString();
                    if (token.Key == "\'")
                    {
                        // read 2 hex characters
                        var text = new StringBuilder();
                        text.Append((char) _myReader.Read());
                        text.Append((char) _myReader.Read());
                        token.HasParam = true;
                        token.Param = Convert.ToInt32(text.ToString().ToLower(), 16);
                        if (token.Param == 0)
                        {
                            token.Param = 0;
                        }
                    }
                }
                return;
            } //if( char.IsLetter( ( char ) c ) == false )

            ReadKeywrod :

            // read keyword
            var keyword = new StringBuilder();
            c = _myReader.Peek();
            while (char.IsLetter((char) c))
            {
                _myReader.Read();
                keyword.Append((char) c);
                c = _myReader.Peek();
            }

            if (ext)
                token.Type = RTFTokenType.ExtKeyword;
            else
                token.Type = RTFTokenType.Keyword;
            token.Key = keyword.ToString();

            // read a interger
            if (char.IsDigit((char) c) || c == '-')
            {
                token.HasParam = true;
                var negative = false;
                if (c == '-')
                {
                    negative = true;
                    _myReader.Read();
                }
                c = _myReader.Peek();
                var text = new StringBuilder();
                while (char.IsDigit((char) c))
                {
                    _myReader.Read();
                    text.Append((char) c);
                    c = _myReader.Peek();
                }
                var p = Convert.ToInt32(text.ToString());
                if (negative)
                    p = -p;
                token.Param = p;
            } //if( char.IsDigit( ( char ) c ) || c == '-' )

            if (c == ' ')
            {
                _myReader.Read();
            }
        }
コード例 #12
0
        /// <summary>
        /// read next token
        /// </summary>
        /// <returns>token</returns>
        public RTFToken NextToken( )
        {
            int      c     = 0;
            RTFToken token = new RTFToken();

            //myReader.Read();

            c = myReader.Read();
            if (c == '\"')
            {
                // 以双引号开头,读取连续的字符
                System.Text.StringBuilder str = new System.Text.StringBuilder();
                while (true)
                {
                    c = myReader.Read();
                    if (c < 0)
                    {
                        // 读取结束
                        break;
                    }
                    if (c == '\"')
                    {
                        // 读取结束
                        break;
                    }
                    else
                    {
                        str.Append((char)c);
                    }
                }//while
                token.Type = RTFTokenType.Text;
                token.Key  = str.ToString();
                return(token);
            }

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

            //
//			c = myReader.Read();
//			while( c == '\r'
//				|| c == '\n'
//				|| c == '\t'
//				|| c == '\0')
//			{
//				c = myReader.Read();
//			}
            if (c != EOF)
            {
                switch (c)
                {
                case '{':
                    token.Type = RTFTokenType.GroupStart;
                    break;

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

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

                default:
                    token.Type = RTFTokenType.Text;
                    ParseText(c, token);
                    break;
                }
            }
            else
            {
                token.Type = RTFTokenType.Eof;
            }
            return(token);
        }
コード例 #13
0
 /// <summary>
 /// read token
 /// </summary>
 /// <returns>token readed</returns>
 public RTFToken ReadToken()
 {
     bolFirstTokenInGroup = false;
     myLastToken = myCurrentToken;
     if (myLastToken != null && myLastToken.Type == RTFTokenType.GroupStart)
     {
         bolFirstTokenInGroup = true;
     }
     myCurrentToken = myLex.NextToken( );
     if( myCurrentToken == null || myCurrentToken.Type == RTFTokenType.Eof )
     {
         myCurrentToken = null;
         return null;
     }
     intTokenCount++;
     if (myCurrentToken.Type == RTFTokenType.GroupStart)
     {
         if (_LayerStack.Count == 0)
         {
             _LayerStack.Push(new RTFRawLayerInfo());
         }
         else
         {
             RTFRawLayerInfo info = _LayerStack.Peek();
             _LayerStack.Push(info.Clone());
         }
         intLevel++;
     }
     else if (myCurrentToken.Type == RTFTokenType.GroupEnd)
     {
         if (_LayerStack.Count > 0)
         {
             _LayerStack.Pop();
         }
         intLevel--;
     }
     if (this.EnableDefaultProcess)
     {
         this.DefaultProcess();
     }
     //if (myTokenStack.Count > 0)
     //{
     //    myCurrentToken.Parent = (RTFToken)myTokenStack[myTokenStack.Count - 1];
     //}
     //myTokenStack.Add( myCurrentToken );
     return myCurrentToken ;
 }
コード例 #14
0
 /// <summary>
 /// load rtf text
 /// </summary>
 /// <param name="strText">RTF text</param>
 /// <returns>is operation successful</returns>
 public bool LoadRTFText( string strText )
 {
     //myTokenStack.Clear();
     myCurrentToken = null;
     if( strText != null && strText.Length > 3 )
     {
         myReader = new System.IO.StringReader( strText );
         myLex = new RTFLex( myReader );
         return true ;
     }
     return false ;
 }
コード例 #15
0
 /// <summary>
 /// load rtf document
 /// </summary>
 /// <param name="strFileName">spcial file name</param>
 /// <returns>is operation successful</returns>
 public bool LoadRTFFile( string strFileName )
 {
     //myTokenStack.Clear();
     myCurrentToken = null ;
     if( System.IO.File.Exists( strFileName ))
     {
         System.IO.FileStream stream = new System.IO.FileStream( strFileName , System.IO.FileMode.Open , System.IO.FileAccess.Read );
         myReader = new System.IO.StreamReader( stream , System.Text.Encoding.ASCII );
         myBaseStream = stream;
         myLex = new RTFLex( myReader );
         return true ;
     }
     return false;
 }
コード例 #16
0
 /// <summary>
 /// load rtf document
 /// </summary>
 /// <param name="reader">text reader</param>
 /// <returns>is operation successful</returns>
 public bool LoadReader( System.IO.TextReader reader )
 {
     //myTokenStack.Clear();
     myCurrentToken = null;
     if( reader != null )
     {
         myReader = reader;
         myLex = new RTFLex( myReader );
         return true ;
     }
     return false;
 }