Example #1
0
        /**
         * Creates a elapsed time formatter.
         *
         * @param pattern The pattern to Parse.
         */
        public CellElapsedFormatter(String pattern)
            : base(pattern)
        {
            specs = new List <TimeSpec>();

            StringBuilder desc = CellFormatPart.ParseFormat(pattern,
                                                            CellFormatType.ELAPSED, new ElapsedPartHandler(this));

            //ListIterator<TimeSpec> it = specs.ListIterator(specs.Count);
            //while (it.HasPrevious())
            for (int i = specs.Count - 1; i >= 0; i--)
            {
                //TimeSpec spec = it.Previous();
                TimeSpec spec = specs[i];
                //desc.Replace(spec.pos, spec.pos + spec.len, "%0" + spec.len + "d");
                desc.Remove(spec.pos, spec.len);
                desc.Insert(spec.pos, "D" + spec.len);
                if (spec.type != topmost.type)
                {
                    spec.modBy = modFor(spec.type, spec.len);
                }
            }

            printfFmt = desc.ToString();
        }
Example #2
0
        /**
         * Creates a new date formatter with the given specification.
         *
         * @param format The format.
         */
        public CellDateFormatter(String format)
            : base(format)
        {
            DatePartHandler partHandler = new DatePartHandler(this);
            StringBuilder   descBuf     = CellFormatPart.ParseFormat(format,
                                                                     CellFormatType.DATE, partHandler);

            partHandler.Finish(descBuf);
            dateFmt = new SimpleDateFormat(descBuf.ToString());
        }
Example #3
0
        /**
         * Creates a new date formatter with the given specification.
         *
         * @param format The format.
         */
        public CellDateFormatter(String format)
            : base(format)
        {
            DatePartHandler partHandler = new DatePartHandler(this);
            StringBuilder   descBuf     = CellFormatPart.ParseFormat(format,
                                                                     CellFormatType.DATE, partHandler);

            partHandler.Finish(descBuf);
            dateFmt = new SimpleDateFormat(descBuf.ToString());

            //NPOI do not need this
            // tweak the format pattern to pass tests on JDK 1.7,
            // See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369
            //String ptrn = descBuf.toString().replaceAll("((y)(?!y))(?<!yy)", "yy");
            //dateFmt = new SimpleDateFormat(ptrn, LOCALE);
        }
Example #4
0
        public CellTextFormatter(String format)
            : base(format)
        {
            ;

            int[]       numPlaces = new int[1];
            PartHandler handler   = new PartHandler(numPlaces[0]);

            desc = CellFormatPart.ParseFormat(format, CellFormatType.TEXT, handler).ToString();

            // Remember the "@" positions in last-to-first order (to make insertion easier)
            textPos = new int[handler.NumPlace];
            int pos = desc.Length - 1;

            for (int i = 0; i < textPos.Length; i++)
            {
                textPos[i] = desc.LastIndexOf("\u0000", pos);
                pos        = textPos[i] - 1;
            }
        }
Example #5
0
        /**
         * Creates a new cell number formatter.
         *
         * @param format The format to Parse.
         */
        public CellNumberFormatter(String format)
            : base(format)
        {
            CellNumberPartHandler ph      = new CellNumberPartHandler();
            StringBuilder         descBuf = CellFormatPart.ParseFormat(format, CellFormatType.NUMBER, ph);

            exponent = ph.Exponent;
            specials.AddRange(ph.Specials);
            improperFraction = ph.IsImproperFraction;

            // These are inconsistent settings, so ditch 'em
            if ((ph.DecimalPoint != null || ph.Exponent != null) && ph.Slash != null)
            {
                slash     = null;
                numerator = null;
            }
            else
            {
                slash     = ph.Slash;
                numerator = ph.Numerator;
            }

            int precision         = interpretPrecision(ph.DecimalPoint, specials);
            int fractionPartWidth = 0;

            if (ph.DecimalPoint != null)
            {
                fractionPartWidth = 1 + precision;
                if (precision == 0)
                {
                    // This means the format has a ".", but that output should have no decimals after it.
                    // We just stop treating it specially
                    specials.Remove(ph.DecimalPoint);
                    decimalPoint = null;
                }
                else
                {
                    decimalPoint = ph.DecimalPoint;
                }
            }
            else
            {
                decimalPoint = null;
            }

            if (decimalPoint != null)
            {
                afterInteger = decimalPoint;
            }
            else if (exponent != null)
            {
                afterInteger = exponent;
            }
            else if (numerator != null)
            {
                afterInteger = numerator;
            }
            else
            {
                afterInteger = null;
            }

            if (exponent != null)
            {
                afterFractional = exponent;
            }
            else if (numerator != null)
            {
                afterFractional = numerator;
            }
            else
            {
                afterFractional = null;
            }

            double[] scaleByRef = { ph.Scale };
            integerCommas = interpretIntegerCommas(descBuf, specials, decimalPoint, integerEnd(), fractionalEnd(), scaleByRef);
            if (exponent == null)
            {
                scale = scaleByRef[0];
            }
            else
            {
                // in "e" formats,% and trailing commas have no scaling effect
                scale = 1;
            }

            if (precision != 0)
            {
                // TODO: if decimalPoint is null (-> index == -1), return the whole list?
                int startIndex = specials.IndexOf(decimalPoint) + 1;
                fractionalSpecials.AddRange(specials.GetRange(startIndex, fractionalEnd() - startIndex));
            }

            if (exponent != null)
            {
                int exponentPos = specials.IndexOf(exponent);
                exponentSpecials.AddRange(specialsFor(exponentPos, 2));
                exponentDigitSpecials.AddRange(specialsFor(exponentPos + 2));
            }

            if (slash != null)
            {
                if (numerator != null)
                {
                    numeratorSpecials.AddRange(specialsFor(specials.IndexOf(numerator)));
                }

                denominatorSpecials.AddRange(specialsFor(specials.IndexOf(slash) + 1));
                if (denominatorSpecials.Count == 0)
                {
                    // no denominator follows the slash, drop the fraction idea
                    numeratorSpecials.Clear();
                    maxDenominator = 1;
                    numeratorFmt   = null;
                    denominatorFmt = null;
                }
                else
                {
                    maxDenominator = maxValue(denominatorSpecials);
                    numeratorFmt   = SingleNumberFormat(numeratorSpecials);
                    denominatorFmt = SingleNumberFormat(denominatorSpecials);
                }
            }
            else
            {
                maxDenominator = 1;
                numeratorFmt   = null;
                denominatorFmt = null;
            }

            integerSpecials.AddRange(specials.GetRange(0, integerEnd()));

            if (exponent == null)
            {
                StringBuilder fmtBuf = new StringBuilder();

                int integerPartWidth = calculateintPartWidth();
                //int totalWidth = integerPartWidth + fractionPartWidth;

                fmtBuf.Append('0', integerPartWidth).Append('.').Append('0', precision);

                //fmtBuf.Append("f");
                printfFmt = fmtBuf.ToString();

                //this number format is legal in C#;
                //printfFmt = fmt;
                decimalFmt = null;
            }
            else
            {
                StringBuilder  fmtBuf      = new StringBuilder();
                bool           first       = true;
                List <Special> specialList = integerSpecials;
                if (integerSpecials.Count == 1)
                {
                    // If we don't do this, we Get ".6e5" instead of "6e4"
                    fmtBuf.Append("0");
                    first = false;
                }
                else
                {
                    foreach (Special s in specialList)
                    {
                        if (IsDigitFmt(s))
                        {
                            fmtBuf.Append(first ? '#' : '0');
                            first = false;
                        }
                    }
                }
                if (fractionalSpecials.Count > 0)
                {
                    fmtBuf.Append('.');
                    foreach (Special s in fractionalSpecials)
                    {
                        if (IsDigitFmt(s))
                        {
                            if (!first)
                            {
                                fmtBuf.Append('0');
                            }
                            first = false;
                        }
                    }
                }
                fmtBuf.Append('E');
                placeZeros(fmtBuf, exponentSpecials.GetRange(2, exponentSpecials.Count - 2));
                decimalFmt = new DecimalFormat(fmtBuf.ToString());

                printfFmt = null;
            }

            desc = descBuf.ToString();
        }
Example #6
0
        /**
         * Creates a new cell number formatter.
         *
         * @param format The format to Parse.
         */
        public CellNumberFormatter(String format)
            : base(format)
        {
            ;

            scale = 1;

            specials = new List <Special>();

            NumPartHandler partHandler = new NumPartHandler(this);
            StringBuilder  descBuf     = CellFormatPart.ParseFormat(format,
                                                                    CellFormatType.NUMBER, partHandler);
            string fmt = descBuf.ToString();

            // These are inconsistent Settings, so ditch 'em
            if ((decimalPoint != null || exponent != null) && slash != null)
            {
                slash     = null;
                numerator = null;
            }

            interpretCommas(descBuf);

            int precision;
            int fractionPartWidth = 0;

            if (decimalPoint == null)
            {
                precision = 0;
            }
            else
            {
                precision         = interpretPrecision();
                fractionPartWidth = 1 + precision;
                if (precision == 0)
                {
                    // This means the format has a ".", but that output should have no decimals After it.
                    // We just stop treating it specially
                    specials.Remove(decimalPoint);
                    decimalPoint = null;
                }
            }

            if (precision == 0)
            {
                fractionalSpecials = EmptySpecialList;
            }
            else
            {
                int pos = specials.IndexOf(decimalPoint) + 1;
                fractionalSpecials = specials.GetRange(pos, fractionalEnd() - pos);
            }
            if (exponent == null)
            {
                exponentSpecials = EmptySpecialList;
            }
            else
            {
                int exponentPos = specials.IndexOf(exponent);
                exponentSpecials      = specialsFor(exponentPos, 2);
                exponentDigitSpecials = specialsFor(exponentPos + 2);
            }

            if (slash == null)
            {
                numeratorSpecials   = EmptySpecialList;
                denominatorSpecials = EmptySpecialList;
            }
            else
            {
                if (numerator == null)
                {
                    numeratorSpecials = EmptySpecialList;
                }
                else
                {
                    numeratorSpecials = specialsFor(specials.IndexOf(numerator));
                }

                denominatorSpecials = specialsFor(specials.IndexOf(slash) + 1);
                if (denominatorSpecials.Count == 0)
                {
                    // no denominator follows the slash, drop the fraction idea
                    numeratorSpecials = EmptySpecialList;
                }
                else
                {
                    maxDenominator = maxValue(denominatorSpecials);
                    numeratorFmt   = SingleNumberFormat(numeratorSpecials);
                    denominatorFmt = SingleNumberFormat(denominatorSpecials);
                }
            }

            integerSpecials = specials.GetRange(0, integerEnd());

            if (exponent == null)
            {
                StringBuilder fmtBuf = new StringBuilder();

                int integerPartWidth = calculateintPartWidth();
                //int totalWidth = integerPartWidth + fractionPartWidth;

                fmtBuf.Append('0', integerPartWidth).Append('.').Append('0', precision);

                //fmtBuf.Append("f");
                printfFmt = fmtBuf.ToString();

                //this number format is legal in C#;
                //printfFmt = fmt;
            }
            else
            {
                StringBuilder  fmtBuf      = new StringBuilder();
                bool           first       = true;
                List <Special> specialList = integerSpecials;
                if (integerSpecials.Count == 1)
                {
                    // If we don't do this, we Get ".6e5" instead of "6e4"
                    fmtBuf.Append("0");
                    first = false;
                }
                else
                {
                    foreach (Special s in specialList)
                    {
                        if (IsDigitFmt(s))
                        {
                            fmtBuf.Append(first ? '#' : '0');
                            first = false;
                        }
                    }
                }
                if (fractionalSpecials.Count > 0)
                {
                    fmtBuf.Append('.');
                    foreach (Special s in fractionalSpecials)
                    {
                        if (IsDigitFmt(s))
                        {
                            if (!first)
                            {
                                fmtBuf.Append('0');
                            }
                            first = false;
                        }
                    }
                }
                fmtBuf.Append('E');
                placeZeros(fmtBuf, exponentSpecials.GetRange(2,
                                                             exponentSpecials.Count - 2));
                decimalFmt = new DecimalFormat(fmtBuf.ToString());
            }

            if (exponent != null)
            {
                scale =
                    1;      // in "e" formats,% and trailing commas have no scaling effect
            }
            desc = descBuf.ToString();
        }