ParseNode.cs :  » GUI » NPOI » NPOI » SS » Formula » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » NPOI 
NPOI » NPOI » SS » Formula » ParseNode.cs
/* ====================================================================
   Licensed To the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file To You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed To in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

namespace NPOI.SS.Formula{

    using System;
    using System.Text;
    using NPOI.HSSF.Record.Formula;
    using NPOI.HSSF.Record.Formula.Function;
    /**
     * Represents a syntactic element from a formula by encapsulating the corresponding <tt>Ptg</tt>
     * Token.  Each <tt>ParseNode</tt> may have child <tt>ParseNode</tt>s in the case when the wrapped
     * <tt>Ptg</tt> is non-atomic.
     * 
     * @author Josh Micich
     */
    class ParseNode
    {

        public static ParseNode[] EMPTY_ARRAY = { };
        private Ptg _token;
        private ParseNode[] _children;
        private bool _isIf;
        private int _tokenCount;

        public ParseNode(Ptg token, ParseNode[] children)
        {
            _token = token;
            _children = children;
            _isIf = IsIf(token);
            int tokenCount = 1;
            for (int i = 0; i < children.Length; i++)
            {
                tokenCount += children[i].TokenCount;
            }
            if (_isIf)
            {
                // there will be 2 or 3 extra tAttr Tokens according To whether the false param is present
                tokenCount += children.Length;
            }
            _tokenCount = tokenCount;
        }
        public ParseNode(Ptg token)
            : this(token, EMPTY_ARRAY)
        {

        }
        public ParseNode(Ptg token, ParseNode child0)
            : this(token, new ParseNode[] { child0, })
        {

        }
        public ParseNode(Ptg token, ParseNode child0, ParseNode child1)
            : this(token, new ParseNode[] { child0, child1, })
        {

        }
        private int TokenCount
        {
            get
            {
                return _tokenCount;
            }
        }
        public int EncodedSize
        {
            get
            {
                int result = _token is ArrayPtg ? ArrayPtg.PLAIN_TOKEN_SIZE : _token.Size;
                for (int i = 0; i < _children.Length; i++)
                {
                    result += _children[i].EncodedSize;
                }
                return result;
            }
        }

        /**
         * Collects the array of <tt>Ptg</tt> Tokens for the specified tree.
         */
        public static Ptg[] ToTokenArray(ParseNode rootNode)
        {
            TokenCollector temp = new TokenCollector(rootNode.TokenCount);
            rootNode.CollectPtgs(temp);
            return temp.GetResult();
        }
        private void CollectPtgs(TokenCollector temp)
        {
            if (IsIf(GetToken()))
            {
                CollectIfPtgs(temp);
                return;
            }
            for (int i = 0; i < GetChildren().Length; i++)
            {
                GetChildren()[i].CollectPtgs(temp);
            }
            temp.Add(GetToken());
        }
        /**
         * The IF() function Gets marked up with two or three tAttr Tokens.
         * Similar logic will be required for CHOOSE() when it is supported
         * 
         * See excelfileformat.pdf sec 3.10.5 "tAttr (19H)
         */
        private void CollectIfPtgs(TokenCollector temp)
        {

            // condition goes first
            GetChildren()[0].CollectPtgs(temp);

            // placeholder for tAttrIf
            int ifAttrIndex = temp.CreatePlaceholder();

            // true parameter
            GetChildren()[1].CollectPtgs(temp);

            // placeholder for first skip attr
            int skipAfterTrueParamIndex = temp.CreatePlaceholder();
            int trueParamSize = temp.sumTokenSizes(ifAttrIndex + 1, skipAfterTrueParamIndex);

            AttrPtg attrIf = new AttrPtg();
            attrIf.IsOptimizedIf=(true);
            AttrPtg attrSkipAfterTrue = new AttrPtg();
            attrSkipAfterTrue.IsGoto=(true);

            if (GetChildren().Length > 2)
            {
                // false param present

                // false parameter
                GetChildren()[2].CollectPtgs(temp);

                int skipAfterFalseParamIndex = temp.CreatePlaceholder();

                AttrPtg attrSkipAfterFalse = new AttrPtg();
                attrSkipAfterFalse.IsGoto=(true);

                int falseParamSize = temp.sumTokenSizes(skipAfterTrueParamIndex + 1, skipAfterFalseParamIndex);

                attrIf.Data=((short)(trueParamSize + 4)); // distance To start of false parameter. +4 for skip after true
                attrSkipAfterTrue.Data=((short)(falseParamSize + 4 + 4 - 1)); // 1 less than distance To end of if FuncVar(size=4). +4 for attr skip before 
                attrSkipAfterFalse.Data=((short)(4 - 1)); // 1 less than distance To end of if FuncVar(size=4). 

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
                temp.SetPlaceholder(skipAfterFalseParamIndex, attrSkipAfterFalse);
            }
            else
            {
                // false parameter not present
                attrIf.Data=((short)(trueParamSize + 4)); // distance To start of FuncVar. +4 for skip after true
                attrSkipAfterTrue.Data=((short)(4 - 1)); // 1 less than distance To end of if FuncVar(size=4). 

                temp.SetPlaceholder(ifAttrIndex, attrIf);
                temp.SetPlaceholder(skipAfterTrueParamIndex, attrSkipAfterTrue);
            }

            temp.Add(GetToken());
        }

        private static bool IsIf(Ptg token)
        {
            if (token is FuncVarPtg)
            {
                FuncVarPtg func = (FuncVarPtg)token;
                if (FunctionMetadataRegistry.FUNCTION_NAME_IF.Equals(func.Name))
                {
                    return true;
                }
            }
            return false;
        }

        public Ptg GetToken()
        {
            return _token;
        }

        public ParseNode[] GetChildren()
        {
            return _children;
        }

        private class TokenCollector
        {

            private Ptg[] _ptgs;
            private int _offset;

            public TokenCollector(int tokenCount)
            {
                _ptgs = new Ptg[tokenCount];
                _offset = 0;
            }

            public int sumTokenSizes(int fromIx, int ToIx)
            {
                int result = 0;
                for (int i = fromIx; i < ToIx; i++)
                {
                    result += _ptgs[i].Size;
                }
                return result;
            }

            public int CreatePlaceholder()
            {
                return _offset++;
            }

            public void Add(Ptg token)
            {
                if (token == null)
                {
                    throw new ArgumentException("token must not be null");
                }
                _ptgs[_offset] = token;
                _offset++;
            }

            public void SetPlaceholder(int index, Ptg token)
            {
                if (_ptgs[index] != null)
                {
                    throw new InvalidOperationException("Invalid placeholder index (" + index + ")");
                }
                _ptgs[index] = token;
            }

            public Ptg[] GetResult()
            {
                return _ptgs;
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.