Ejemplo n.º 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();
        }
Ejemplo n.º 2
0
            public String HandlePart(Match m, String part, CellFormatType type,
                                     StringBuilder desc)
            {
                int  pos     = desc.Length;
                char firstCh = part[0];

                switch (firstCh)
                {
                case '[':
                    if (part.Length < 3)
                    {
                        break;
                    }
                    if (_formatter.topmost != null)
                    {
                        throw new ArgumentException(
                                  "Duplicate '[' times in format");
                    }
                    part = part.ToLower();
                    int specLen = part.Length - 2;
                    _formatter.topmost = _formatter.AssignSpec(part[1], pos, specLen);
                    return(part.Substring(1, specLen));

                case 'h':
                case 'm':
                case 's':
                case '0':
                    part = part.ToLower();
                    _formatter.AssignSpec(part[0], pos, part.Length);
                    return(part);

                case '\n':
                    return("%n");

                case '\"':
                    part = part.Substring(1, part.Length - 2);
                    break;

                case '\\':
                    part = part.Substring(1);
                    break;

                case '*':
                    if (part.Length > 1)
                    {
                        part = CellFormatPart.ExpandChar(part);
                    }
                    break;

                // An escape we can let it handle because it can't have a '%'
                case '_':
                    return(null);
                }
                // Replace ever "%" with a "%%" so we can use printf
                //return PERCENTS.Matcher(part).ReplaceAll("%%");
                //return PERCENTS.Replace(part, "%%");
                return(part);
            }
Ejemplo n.º 3
0
        /**
         * Creates a new object.
         *
         * @param format The format.
         */
        private CellFormat(String format)
        {
            this.format = format;
            MatchCollection       mc    = ONE_PART.Matches(format);
            List <CellFormatPart> parts = new List <CellFormatPart>();

            //while (m.Success)
            foreach (Match m in mc)
            {
                try
                {
                    String valueDesc = m.Groups[0].Value;

                    // Strip out the semicolon if it's there
                    if (valueDesc.EndsWith(";"))
                    {
                        valueDesc = valueDesc.Substring(0, valueDesc.Length - 1);
                    }

                    parts.Add(new CellFormatPart(valueDesc));
                }
                catch (Exception)
                {
                    //CellFormatter.logger.Log(Level.WARNING,
                    //        "Invalid format: " + CellFormatter.Quote(m.Group()), e);
                    parts.Add(null);
                }
            }

            switch (parts.Count)
            {
            case 1:
                posNumFmt = zeroNumFmt = negNumFmt = parts[(0)];
                textFmt   = DEFAULT_TEXT_FORMAT;
                break;

            case 2:
                posNumFmt = zeroNumFmt = parts[0];
                negNumFmt = parts[1];
                textFmt   = DEFAULT_TEXT_FORMAT;
                break;

            case 3:
                posNumFmt  = parts[0];
                zeroNumFmt = parts[1];
                negNumFmt  = parts[2];
                textFmt    = DEFAULT_TEXT_FORMAT;
                break;

            case 4:
            default:
                posNumFmt  = parts[0];
                zeroNumFmt = parts[1];
                negNumFmt  = parts[2];
                textFmt    = parts[3];
                break;
            }
        }
Ejemplo n.º 4
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());
        }
Ejemplo n.º 5
0
            public override void Equivalent(String expected, String actual,
                                            CellFormatPart format)
            {
                double expectedVal = ExtractNumber(expected);
                double actualVal   = ExtractNumber(actual);
                // equal within 1%
                double delta = expectedVal / 100;

                Assert.AreEqual(expectedVal, actualVal, delta, "format \"" + format + "\"," + expected + " ~= " +
                                actual);
            }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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;
            }
        }
Ejemplo n.º 8
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();
        }
Ejemplo n.º 9
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();
        }