RTF parser node, this source code evolution from other software.
Exemplo n.º 1
0
 /// <summary>
 /// search child node special keyword
 /// </summary>
 /// <param name="Key">special keyword</param>
 /// <param name="Deeply">whether search deeplyl</param>
 /// <returns>node find</returns>
 public RTFNode SearchKey(string Key, bool Deeply)
 {
     foreach (RTFNode node in myNodes)
     {
         if (node.Type == RTFNodeType.Keyword ||
             node.Type == RTFNodeType.ExtKeyword ||
             node.Type == RTFNodeType.Control)
         {
             if (node.Keyword == Key)
             {
                 return(node);
             }
         }
         if (Deeply)
         {
             if (node is RTFNodeGroup)
             {
                 RTFNodeGroup g = ( RTFNodeGroup )node;
                 RTFNode      n = g.SearchKey(Key, true);
                 if (n != null)
                 {
                     return(n);
                 }
             }
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        private DateTime ReadDateTime(RTFNode g)
        {
            int yr  = g.Nodes.GetParameter("yr", 1900);
            int mo  = g.Nodes.GetParameter("mo", 1);
            int dy  = g.Nodes.GetParameter("dy", 1);
            int hr  = g.Nodes.GetParameter("hr", 0);
            int min = g.Nodes.GetParameter("min", 0);
            int sec = g.Nodes.GetParameter("sec", 0);

            return(new DateTime(yr, mo, dy, hr, min, sec));
        }
Exemplo n.º 3
0
        private DateTime ReadDateTime(RTFNode g)
        {
            var yr  = g.Nodes.GetParameter("yr", 1900);
            var mo  = g.Nodes.GetParameter("mo", 1);
            var dy  = g.Nodes.GetParameter("dy", 1);
            var hr  = g.Nodes.GetParameter("hr", 0);
            var min = g.Nodes.GetParameter("min", 0);
            var sec = g.Nodes.GetParameter("sec", 0);

            return(new DateTime(yr, mo, dy, hr, min, sec));
        }
Exemplo n.º 4
0
 /// <summary>
 /// delete node
 /// </summary>
 /// <param name="node">node</param>
 public void RemoveChild(RTFNode node)
 {
     CheckNodes();
     if (node == null)
     {
         throw new System.ArgumentNullException("node");
     }
     if (node == this)
     {
         throw new System.ArgumentException("node != this");
     }
     this.Nodes.Remove(node);
 }
Exemplo n.º 5
0
 /// <summary>
 /// insert node
 /// </summary>
 /// <param name="index">index</param>
 /// <param name="node">node</param>
 public void InsertNode(int index, RTFNode node)
 {
     CheckNodes();
     if (node == null)
     {
         throw new System.ArgumentNullException("node");
     }
     if (node == this)
     {
         throw new System.ArgumentException("node != this");
     }
     node.Parent        = this;
     node.OwnerDocument = myOwnerDocument;
     this.Nodes.Insert(index, node);
 }
Exemplo n.º 6
0
 /// <summary>
 /// append child node
 /// </summary>
 /// <param name="node">node</param>
 public void AppendChild(RTFNode node)
 {
     CheckNodes();
     if (node == null)
     {
         throw new System.ArgumentNullException("node");
     }
     if (node == this)
     {
         throw new System.ArgumentException("node != this");
     }
     node.Parent        = this;
     node.OwnerDocument = myOwnerDocument;
     this.Nodes.Add(node);
 }
Exemplo n.º 7
0
 /// <summary>
 /// add node
 /// </summary>
 /// <param name="node">node</param>
 internal void Add(RTFNode node)
 {
     //node.OwnerList = this ;
     List.Add(node);
 }
Exemplo n.º 8
0
 /// <summary>
 /// get index of node in this list
 /// </summary>
 /// <param name="node">node</param>
 /// <returns>index , if node does no in this list , return -1</returns>
 public int IndexOf(RTFNode node)
 {
     return(List.IndexOf(node));
 }
Exemplo n.º 9
0
 /// <summary>
 /// delete node
 /// </summary>
 /// <param name="node">node</param>
 public void RemoveChild(RTFNode node)
 {
     CheckNodes();
     if (node == null)
         throw new ArgumentNullException("node");
     if (node == this)
         throw new ArgumentException("node != this");
     Nodes.Remove(node);
 }
Exemplo n.º 10
0
 /// <summary>
 /// insert node
 /// </summary>
 /// <param name="index">index</param>
 /// <param name="node">node</param>
 public void InsertNode(int index, RTFNode node)
 {
     CheckNodes();
     if (node == null)
         throw new ArgumentNullException("node");
     if (node == this)
         throw new ArgumentException("node != this");
     node.Parent = this;
     node.OwnerDocument = MyOwnerDocument;
     Nodes.Insert(index, node);
 }
Exemplo n.º 11
0
 /// <summary>
 /// append child node
 /// </summary>
 /// <param name="node">node</param>
 public void AppendChild(RTFNode node)
 {
     CheckNodes();
     if (node == null)
         throw new ArgumentNullException("node");
     if (node == this)
         throw new ArgumentException("node != this");
     node.Parent = this;
     node.OwnerDocument = MyOwnerDocument;
     Nodes.Add(node);
 }
Exemplo n.º 12
0
 private DateTime ReadDateTime( RTFNode g )
 {
     int yr = g.Nodes.GetParameter( "yr" , 1900 );
     int mo = g.Nodes.GetParameter( "mo" , 1 );
     int dy = g.Nodes.GetParameter( "dy" , 1 );
     int hr = g.Nodes.GetParameter( "hr" , 0 );
     int min = g.Nodes.GetParameter( "min" , 0 );
     int sec = g.Nodes.GetParameter( "sec" , 0 );
     return new DateTime( yr , mo , dy , hr , min ,sec );
 }
Exemplo n.º 13
0
        /// <summary>
        /// load rtf
        /// </summary>
        /// <param name="reader">RTF text reader</param>
        public void Load( RTFReader reader )
        {
            myNodes.Clear();
            System.Collections.Stack groups = new System.Collections.Stack();
            RTFNodeGroup NewGroup = null ;
            RTFNode NewNode = null;
            while( reader.ReadToken() != null )
            {
                if( reader.TokenType == RTFTokenType.GroupStart )
                {
                    // begin group
                    if( NewGroup == null)
                    {
                        NewGroup = this ;
                    }
                    else
                    {
                        NewGroup = new RTFNodeGroup();
                        NewGroup.OwnerDocument = this ;
                    }
                    if( NewGroup != this )
                    {
                        RTFNodeGroup g = ( RTFNodeGroup ) groups.Peek();
                        g.AppendChild( NewGroup );
                    }
                    groups.Push( NewGroup );
                }
                else if( reader.TokenType == RTFTokenType.GroupEnd )
                {
                    // end group
                    NewGroup = ( RTFNodeGroup ) groups.Pop();
                    NewGroup.MergeText();
                    if (NewGroup.FirstNode is RTFNode)
                    {
                        switch (NewGroup.Keyword)
                        {
                            case RTFConsts._fonttbl:
                                // read font table
                                ReadFontTable(NewGroup);
                                break;
                            case RTFConsts._colortbl:
                                // read color table
                                ReadColorTable(NewGroup);
                                break;
                            case RTFConsts._info :
                                // read document information
                                ReadDocumentInfo(NewGroup);
                                break;
                        }
                    }
                    if (groups.Count > 0)
                    {
                        NewGroup = (RTFNodeGroup)groups.Peek();
                    }
                    else
                    {
                        break;
                    }
                    //NewGroup.MergeText();
                }
                else
                {
                    // read content

                    NewNode = new RTFNode( reader.CurrentToken );
                    NewNode.OwnerDocument = this ;
                    NewGroup.AppendChild( NewNode );
                    if (NewNode.Keyword == RTFConsts._f )
                    {
                        RTFFont font = this.FontTable[NewNode.Parameter];
                        if (font != null)
                        {
                            myFontChartset = font.Encoding;
                        }
                        else
                        {
                            myFontChartset = null;
                        }
                        //myFontChartset = RTFFont.GetRTFEncoding( NewNode.Parameter );
                    }
                    else if (NewNode.Keyword == RTFConsts._af)
                    {
                        RTFFont font = this.FontTable[NewNode.Parameter];
                        if (font != null)
                        {
                            myAssociateFontChartset = font.Encoding;
                        }
                        else
                        {
                            myAssociateFontChartset = null;
                        }
                    }
                }
            }// while( reader.ReadToken() != null )
            while( groups.Count > 0 )
            {
                NewGroup = ( RTFNodeGroup ) groups.Pop();
                NewGroup.MergeText();
            }
            //this.UpdateInformation();
        }
Exemplo n.º 14
0
 private DateTime ReadDateTime(RTFNode g)
 {
     var yr = g.Nodes.GetParameter("yr", 1900);
     var mo = g.Nodes.GetParameter("mo", 1);
     var dy = g.Nodes.GetParameter("dy", 1);
     var hr = g.Nodes.GetParameter("hr", 0);
     var min = g.Nodes.GetParameter("min", 0);
     var sec = g.Nodes.GetParameter("sec", 0);
     return new DateTime(yr, mo, dy, hr, min, sec);
 }
Exemplo n.º 15
0
        /// <summary>
        /// load rtf
        /// </summary>
        /// <param name="reader">RTF text reader</param>
        public void Load(RTFReader reader)
        {
            MyNodes.Clear();
            var groups = new Stack<RTFNodeGroup>();
            RTFNodeGroup newGroup = null;
            RTFNode newNode = null;
            while (reader.ReadToken() != null)
            {
                if (reader.TokenType == RTFTokenType.GroupStart)
                {
                    // begin group
                    if (newGroup == null)
                    {
                        newGroup = this;
                    }
                    else
                    {
                        newGroup = new RTFNodeGroup();
                        newGroup.OwnerDocument = this;
                    }
                    if (newGroup != this)
                    {
                        var g = groups.Peek();
                        g.AppendChild(newGroup);
                    }
                    groups.Push(newGroup);
                }
                else if (reader.TokenType == RTFTokenType.GroupEnd)
                {
                    // end group
                    newGroup = groups.Pop();
                    newGroup.MergeText();
                    if (newGroup.FirstNode is RTFNode)
                    {
                        switch (newGroup.Keyword)
                        {
                            case RTFConsts.Fonttbl:
                                // read font table
                                ReadFontTable(newGroup);
                                break;
                            case RTFConsts.Colortbl:
                                // read color table
                                ReadColorTable(newGroup);
                                break;
                            case RTFConsts.Info:
                                // read document information
                                ReadDocumentInfo(newGroup);
                                break;
                        }
                    }
                    if (groups.Count > 0)
                    {
                        newGroup = groups.Peek();
                    }
                    else
                    {
                        break;
                    }
                    //NewGroup.MergeText();
                }
                else
                {
                    // read content

                    newNode = new RTFNode(reader.CurrentToken);
                    newNode.OwnerDocument = this;
                    newGroup.AppendChild(newNode);
                    if (newNode.Keyword == RTFConsts.F)
                    {
                        var font = FontTable[newNode.Parameter];
                        if (font != null)
                        {
                            _myFontChartset = font.Encoding;
                        }
                        else
                        {
                            _myFontChartset = null;
                        }
                        //myFontChartset = RTFFont.GetRTFEncoding( NewNode.Parameter );
                    }
                    else if (newNode.Keyword == RTFConsts.Af)
                    {
                        var font = FontTable[newNode.Parameter];
                        if (font != null)
                        {
                            _myAssociateFontChartset = font.Encoding;
                        }
                        else
                        {
                            _myAssociateFontChartset = null;
                        }
                    }
                }
            } // while( reader.ReadToken() != null )
            while (groups.Count > 0)
            {
                newGroup = groups.Pop();
                newGroup.MergeText();
            }
            //this.UpdateInformation();
        }
Exemplo n.º 16
0
        /// <summary>
        /// load rtf
        /// </summary>
        /// <param name="reader">RTF text reader</param>
        public void Load(RTFReader reader)
        {
            myNodes.Clear();
            var          groups   = new System.Collections.Stack();
            RTFNodeGroup NewGroup = null;
            RTFNode      NewNode  = null;

            while (reader.ReadToken() != null)
            {
                if (reader.TokenType == RTFTokenType.GroupStart)
                {
                    // begin group
                    if (NewGroup == null)
                    {
                        NewGroup = this;
                    }
                    else
                    {
                        NewGroup = new RTFNodeGroup();
                        NewGroup.OwnerDocument = this;
                    }
                    if (NewGroup != this)
                    {
                        var g = ( RTFNodeGroup )groups.Peek();
                        g.AppendChild(NewGroup);
                    }
                    groups.Push(NewGroup);
                }
                else if (reader.TokenType == RTFTokenType.GroupEnd)
                {
                    // end group
                    NewGroup = ( RTFNodeGroup )groups.Pop();
                    NewGroup.MergeText();
                    if (NewGroup.FirstNode is RTFNode)
                    {
                        switch (NewGroup.Keyword)
                        {
                        case RTFConsts._fonttbl:
                            // read font table
                            ReadFontTable(NewGroup);
                            break;

                        case RTFConsts._colortbl:
                            // read color table
                            ReadColorTable(NewGroup);
                            break;

                        case RTFConsts._info:
                            // read document information
                            ReadDocumentInfo(NewGroup);
                            break;
                        }
                    }
                    if (groups.Count > 0)
                    {
                        NewGroup = (RTFNodeGroup)groups.Peek();
                    }
                    else
                    {
                        break;
                    }
                    //NewGroup.MergeText();
                }
                else
                {
                    // read content

                    NewNode = new RTFNode(reader.CurrentToken);
                    NewNode.OwnerDocument = this;
                    NewGroup.AppendChild(NewNode);
                    if (NewNode.Keyword == RTFConsts._f)
                    {
                        var font = FontTable[NewNode.Parameter];
                        if (font != null)
                        {
                            myFontChartset = font.Encoding;
                        }
                        else
                        {
                            myFontChartset = null;
                        }
                        //myFontChartset = RTFFont.GetRTFEncoding( NewNode.Parameter );
                    }
                    else if (NewNode.Keyword == RTFConsts._af)
                    {
                        var font = FontTable[NewNode.Parameter];
                        if (font != null)
                        {
                            myAssociateFontChartset = font.Encoding;
                        }
                        else
                        {
                            myAssociateFontChartset = null;
                        }
                    }
                }
            }            // while( reader.ReadToken() != null )
            while (groups.Count > 0)
            {
                NewGroup = ( RTFNodeGroup )groups.Pop();
                NewGroup.MergeText();
            }
            //this.UpdateInformation();
        }
Exemplo n.º 17
0
 /// <summary>
 /// remvoe node
 /// </summary>
 /// <param name="node">node</param>
 internal void Remove(RTFNode node)
 {
     Remove(node);
 }
Exemplo n.º 18
0
 /// <summary>
 /// get index of node in this list
 /// </summary>
 /// <param name="node">node</param>
 /// <returns>index , if node does no in this list , return -1</returns>
 public int IndexOf( RTFNode node )
 {
     return this.List.IndexOf( node );
 }
Exemplo n.º 19
0
 /// <summary>
 /// remvoe node
 /// </summary>
 /// <param name="node">node</param>
 internal void Remove( RTFNode node )
 {
     this.Remove( node );
 }
Exemplo n.º 20
0
 /// <summary>
 /// insert node
 /// </summary>
 /// <param name="index">index</param>
 /// <param name="node">node</param>
 internal void Insert( int index , RTFNode node )
 {
     this.List.Insert( index , node );
 }
Exemplo n.º 21
0
 /// <summary>
 /// add node
 /// </summary>
 /// <param name="node">node</param>
 internal void Add( RTFNode node )
 {
     //node.OwnerList = this ;
     this.List.Add( node );
 }
Exemplo n.º 22
0
 /// <summary>
 /// insert node
 /// </summary>
 /// <param name="index">index</param>
 /// <param name="node">node</param>
 internal void Insert(int index, RTFNode node)
 {
     List.Insert(index, node);
 }
Exemplo n.º 23
0
 /// <summary>
 /// remvoe node
 /// </summary>
 /// <param name="node">node</param>
 internal void Remove(RTFNode node)
 {
     this.Remove(node);
 }