private DecimalFormatter CreateDecimalFormatter(string formatPicture, string infinitySymbol, string nanSymbol, string characters)
        {
            NumberFormatInfo info = new NumberFormatInfo();

            info.NumberDecimalSeparator = char.ToString(characters[0]);
            info.NumberGroupSeparator   = char.ToString(characters[1]);
            info.PositiveInfinitySymbol = infinitySymbol;
            info.NegativeSign           = char.ToString(characters[7]);
            info.NaNSymbol              = nanSymbol;
            info.PercentSymbol          = char.ToString(characters[2]);
            info.PerMilleSymbol         = char.ToString(characters[3]);
            info.NegativeInfinitySymbol = info.NegativeSign + info.PositiveInfinitySymbol;

            DecimalFormat formatInfo = new DecimalFormat(info, characters[5], characters[4], characters[6]);

            return(new DecimalFormatter(formatPicture, formatInfo));
        }
Esempio n. 2
0
 internal void AddDecimalFormat(XmlQualifiedName name, DecimalFormat formatinfo)
 {
     _rootAction.AddDecimalFormat(name, formatinfo);
 }
Esempio n. 3
0
        public DecimalFormatter(string formatPicture, DecimalFormat decimalFormat)
        {
            Debug.Assert(formatPicture != null && decimalFormat != null);
            if (formatPicture.Length == 0)
            {
                throw XsltException.Create(SR.Xslt_InvalidFormat);
            }

            _zeroDigit     = decimalFormat.zeroDigit;
            _posFormatInfo = (NumberFormatInfo)decimalFormat.info.Clone();
            StringBuilder temp = new StringBuilder();

            bool integer = true;
            bool sawPattern = false, sawZeroDigit = false, sawDigit = false, sawDecimalSeparator = false;
            bool digitOrZeroDigit = false;
            char decimalSeparator = _posFormatInfo.NumberDecimalSeparator[0];
            char groupSeparator   = _posFormatInfo.NumberGroupSeparator[0];
            char percentSymbol    = _posFormatInfo.PercentSymbol[0];
            char perMilleSymbol   = _posFormatInfo.PerMilleSymbol[0];

            int commaIndex     = 0;
            int groupingSize   = 0;
            int decimalIndex   = -1;
            int lastDigitIndex = -1;

            for (int i = 0; i < formatPicture.Length; i++)
            {
                char ch = formatPicture[i];

                if (ch == decimalFormat.digit)
                {
                    if (sawZeroDigit && integer)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat1, formatPicture);
                    }
                    lastDigitIndex = temp.Length;
                    sawDigit       = digitOrZeroDigit = true;
                    temp.Append('#');
                    continue;
                }
                if (ch == decimalFormat.zeroDigit)
                {
                    if (sawDigit && !integer)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat2, formatPicture);
                    }
                    lastDigitIndex = temp.Length;
                    sawZeroDigit   = digitOrZeroDigit = true;
                    temp.Append('0');
                    continue;
                }
                if (ch == decimalFormat.patternSeparator)
                {
                    if (!digitOrZeroDigit)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat8);
                    }
                    if (sawPattern)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat3, formatPicture);
                    }
                    sawPattern = true;

                    if (decimalIndex < 0)
                    {
                        decimalIndex = lastDigitIndex + 1;
                    }
                    groupingSize = RemoveTrailingComma(temp, commaIndex, decimalIndex);

                    if (groupingSize > 9)
                    {
                        groupingSize = 0;
                    }
                    _posFormatInfo.NumberGroupSizes = new int[] { groupingSize };
                    if (!sawDecimalSeparator)
                    {
                        _posFormatInfo.NumberDecimalDigits = 0;
                    }

                    _posFormat = temp.ToString();

                    temp.Length                 = 0;
                    decimalIndex                = -1;
                    lastDigitIndex              = -1;
                    commaIndex                  = 0;
                    sawDigit                    = sawZeroDigit = digitOrZeroDigit = false;
                    sawDecimalSeparator         = false;
                    integer                     = true;
                    _negFormatInfo              = (NumberFormatInfo)decimalFormat.info.Clone();
                    _negFormatInfo.NegativeSign = string.Empty;
                    continue;
                }
                if (ch == decimalSeparator)
                {
                    if (sawDecimalSeparator)
                    {
                        throw XsltException.Create(SR.Xslt_InvalidFormat5, formatPicture);
                    }
                    decimalIndex        = temp.Length;
                    sawDecimalSeparator = true;
                    sawDigit            = sawZeroDigit = integer = false;
                    temp.Append('.');
                    continue;
                }
                if (ch == groupSeparator)
                {
                    commaIndex     = temp.Length;
                    lastDigitIndex = commaIndex;
                    temp.Append(',');
                    continue;
                }
                if (ch == percentSymbol)
                {
                    temp.Append('%');
                    continue;
                }
                if (ch == perMilleSymbol)
                {
                    temp.Append('\u2030');
                    continue;
                }
                if (ch == '\'')
                {
                    int pos = formatPicture.IndexOf('\'', i + 1);
                    if (pos < 0)
                    {
                        pos = formatPicture.Length - 1;
                    }
                    temp.Append(formatPicture, i, pos - i + 1);
                    i = pos;
                    continue;
                }
                // Escape literal digits with EscChar, double literal EscChar
                if ('0' <= ch && ch <= '9' || ch == EscChar)
                {
                    if (decimalFormat.zeroDigit != '0')
                    {
                        temp.Append(EscChar);
                    }
                }
                // Escape characters having special meaning for CLR
                if (ClrSpecialChars.IndexOf(ch) >= 0)
                {
                    temp.Append('\\');
                }
                temp.Append(ch);
            }

            if (!digitOrZeroDigit)
            {
                throw XsltException.Create(SR.Xslt_InvalidFormat8);
            }
            NumberFormatInfo formatInfo = sawPattern ? _negFormatInfo : _posFormatInfo;

            if (decimalIndex < 0)
            {
                decimalIndex = lastDigitIndex + 1;
            }
            groupingSize = RemoveTrailingComma(temp, commaIndex, decimalIndex);
            if (groupingSize > 9)
            {
                groupingSize = 0;
            }
            formatInfo.NumberGroupSizes = new int[] { groupingSize };
            if (!sawDecimalSeparator)
            {
                formatInfo.NumberDecimalDigits = 0;
            }

            if (sawPattern)
            {
                _negFormat = temp.ToString();
            }
            else
            {
                _posFormat = temp.ToString();
            }
        }
Esempio n. 4
0
 public static string Format(double value, string formatPicture, DecimalFormat decimalFormat)
 {
     return(new DecimalFormatter(formatPicture, decimalFormat).Format(value));
 }
        public DecimalFormatter(string formatPicture, DecimalFormat decimalFormat) {
            Debug.Assert(formatPicture != null && decimalFormat != null);
            if (formatPicture.Length == 0) {
                throw XsltException.Create(Res.Xslt_InvalidFormat);
            }

            zeroDigit = decimalFormat.zeroDigit;
            posFormatInfo = (NumberFormatInfo)decimalFormat.info.Clone();
            StringBuilder temp = new StringBuilder();

            bool integer = true;
            bool sawPattern = false, sawZeroDigit = false, sawDigit = false, sawDecimalSeparator = false;
            bool digitOrZeroDigit = false;
            char decimalSeparator = posFormatInfo.NumberDecimalSeparator[0];
            char groupSeparator   = posFormatInfo.NumberGroupSeparator[0];
            char percentSymbol    = posFormatInfo.PercentSymbol[0];
            char perMilleSymbol   = posFormatInfo.PerMilleSymbol[0];

            int commaIndex = 0;
            int groupingSize = 0;
            int decimalIndex = -1;
            int lastDigitIndex = -1;

            for (int i = 0; i < formatPicture.Length; i++) {
                char ch = formatPicture[i];

                if (ch == decimalFormat.digit) {
                    if (sawZeroDigit && integer) {
                        throw XsltException.Create(Res.Xslt_InvalidFormat1, formatPicture);
                    }
                    lastDigitIndex = temp.Length;
                    sawDigit = digitOrZeroDigit = true;
                    temp.Append('#');
                    continue;
                }
                if (ch == decimalFormat.zeroDigit) {
                    if (sawDigit && !integer) {
                        throw XsltException.Create(Res.Xslt_InvalidFormat2, formatPicture);
                    }
                    lastDigitIndex = temp.Length;
                    sawZeroDigit = digitOrZeroDigit = true;
                    temp.Append('0');
                    continue;
                }
                if (ch == decimalFormat.patternSeparator) {
                    if (!digitOrZeroDigit) {
                        throw XsltException.Create(Res.Xslt_InvalidFormat8);
                    }
                    if (sawPattern) {
                        throw XsltException.Create(Res.Xslt_InvalidFormat3, formatPicture);
                    }
                    sawPattern = true;

                    if (decimalIndex < 0) {
                        decimalIndex = lastDigitIndex + 1;
                    }
                    groupingSize = RemoveTrailingComma(temp, commaIndex, decimalIndex);

                    if (groupingSize > 9) {
                        groupingSize = 0;
                    }
                    posFormatInfo.NumberGroupSizes = new int[] { groupingSize };
                    if (!sawDecimalSeparator) {
                        posFormatInfo.NumberDecimalDigits = 0;
                    }

                    posFormat = temp.ToString();

                    temp.Length = 0;
                    decimalIndex = -1;
                    lastDigitIndex = -1;
                    commaIndex = 0;
                    sawDigit = sawZeroDigit = digitOrZeroDigit = false;
                    sawDecimalSeparator = false;
                    integer = true;
                    negFormatInfo = (NumberFormatInfo)decimalFormat.info.Clone();
                    negFormatInfo.NegativeSign = string.Empty;
                    continue;
                }
                if (ch == decimalSeparator) {
                    if (sawDecimalSeparator) {
                        throw XsltException.Create(Res.Xslt_InvalidFormat5, formatPicture);
                    }
                    decimalIndex = temp.Length;
                    sawDecimalSeparator = true;
                    sawDigit = sawZeroDigit = integer = false;
                    temp.Append('.');
                    continue;
                }
                if (ch == groupSeparator) {
                    commaIndex = temp.Length;
                    lastDigitIndex = commaIndex;
                    temp.Append(',');
                    continue;
                }
                if (ch == percentSymbol) {
                    temp.Append('%');
                    continue;
                }
                if (ch == perMilleSymbol) {
                    temp.Append('\u2030');
                    continue;
                }
                if (ch == '\'') {
                    int pos = formatPicture.IndexOf('\'', i + 1);
                    if (pos < 0) {
                        pos = formatPicture.Length - 1;
                    }
                    temp.Append(formatPicture, i, pos - i + 1);
                    i = pos;
                    continue;
                }
                // Escape literal digits with EscChar, double literal EscChar
                if ('0' <= ch && ch <= '9' || ch == EscChar) {
                    if (decimalFormat.zeroDigit != '0') {
                        temp.Append(EscChar);
                    }
                }
                // Escape characters having special meaning for CLR
                if (ClrSpecialChars.IndexOf(ch) >= 0) {
                    temp.Append('\\');
                }
                temp.Append(ch);
            }

            if (!digitOrZeroDigit) {
                throw XsltException.Create(Res.Xslt_InvalidFormat8);
            }
            NumberFormatInfo formatInfo = sawPattern ? negFormatInfo : posFormatInfo;

            if (decimalIndex < 0) {
                decimalIndex = lastDigitIndex + 1;
            }
            groupingSize = RemoveTrailingComma(temp, commaIndex, decimalIndex);
            if (groupingSize > 9) {
                groupingSize = 0;
            }
            formatInfo.NumberGroupSizes = new int[] { groupingSize };
            if (!sawDecimalSeparator) {
                formatInfo.NumberDecimalDigits = 0;
            }

            if (sawPattern) {
                negFormat = temp.ToString();
            } else {
                posFormat = temp.ToString();
            }
        }
 public static string Format(double value, string formatPicture, DecimalFormat decimalFormat) {
     return new DecimalFormatter(formatPicture, decimalFormat).Format(value);
 }
 private DecimalFormat ResolveFormatName(string formatName) {
     string ns = string.Empty, local = string.Empty;
     if (formatName != null) {
         string prefix;
         PrefixQName.ParseQualifiedName(formatName, out prefix, out local);
         ns = LookupNamespace(prefix);
     }
     DecimalFormat formatInfo = this.processor.RootAction.GetDecimalFormat(new XmlQualifiedName(local, ns));
     if (formatInfo == null) {
         if (formatName != null) {
             throw XsltException.Create(Res.Xslt_NoDecimalFormat, formatName);
         }
         formatInfo = new DecimalFormat(new NumberFormatInfo(), '#', '0', ';');
     }
     return formatInfo;
 }
        protected void CompileDecimalFormat(Compiler compiler){
            NumberFormatInfo info   = new NumberFormatInfo();
            DecimalFormat    format = new DecimalFormat(info, '#', '0', ';');
            XmlQualifiedName  Name  = null;
            NavigatorInput   input  = compiler.Input;
            if (input.MoveToFirstAttribute()) {
                do {
                    if (input.Prefix.Length != 0) continue;

                    string name   = input.LocalName;
                    string value  = input.Value;

                    if (Ref.Equal(name, input.Atoms.Name)) {
                        Name = compiler.CreateXPathQName(value);
                    }
                    else if (Ref.Equal(name, input.Atoms.DecimalSeparator)) {
                        info.NumberDecimalSeparator = value;
                    }
                    else if (Ref.Equal(name, input.Atoms.GroupingSeparator)) {
                        info.NumberGroupSeparator = value;
                    }
                    else if (Ref.Equal(name, input.Atoms.Infinity)) {
                        info.PositiveInfinitySymbol = value;
                    }
                    else if (Ref.Equal(name, input.Atoms.MinusSign)) {
                        info.NegativeSign = value;
                    }
                    else if (Ref.Equal(name, input.Atoms.NaN)) {
                        info.NaNSymbol = value;
                    }
                    else if (Ref.Equal(name, input.Atoms.Percent)) {
                        info.PercentSymbol = value;
                    }
                    else if (Ref.Equal(name, input.Atoms.PerMille)) {
                        info.PerMilleSymbol = value;
                    }
                    else if (Ref.Equal(name, input.Atoms.Digit)) {
                        if (CheckAttribute(value.Length == 1, compiler)) {
                            format.digit = value[0];
                        }
                    }
                    else if (Ref.Equal(name, input.Atoms.ZeroDigit)) {
                        if (CheckAttribute(value.Length == 1, compiler)) {
                            format.zeroDigit = value[0];
                        }
                    }
                    else if (Ref.Equal(name, input.Atoms.PatternSeparator)) {
                        if (CheckAttribute(value.Length == 1, compiler)) {
                            format.patternSeparator = value[0];
                        }
                    }
                }
                while(input.MoveToNextAttribute());
                input.ToParent();
            }
            info.NegativeInfinitySymbol = String.Concat(info.NegativeSign, info.PositiveInfinitySymbol);
            if (Name == null) {
                Name = new XmlQualifiedName();
            }
            compiler.AddDecimalFormat(Name, format);
            CheckEmpty(compiler);
        }
 internal void AddDecimalFormat(XmlQualifiedName name, DecimalFormat formatinfo) { 
     DecimalFormat exist = (DecimalFormat) this.decimalFormatTable[name];
     if (exist != null) {
         NumberFormatInfo info    = exist.info;
         NumberFormatInfo newinfo = formatinfo.info;
         if (info.NumberDecimalSeparator   != newinfo.NumberDecimalSeparator   ||
             info.NumberGroupSeparator     != newinfo.NumberGroupSeparator     ||
             info.PositiveInfinitySymbol   != newinfo.PositiveInfinitySymbol   ||
             info.NegativeSign             != newinfo.NegativeSign             ||
             info.NaNSymbol                != newinfo.NaNSymbol                ||
             info.PercentSymbol            != newinfo.PercentSymbol            ||
             info.PerMilleSymbol           != newinfo.PerMilleSymbol           ||
             exist.zeroDigit               != formatinfo.zeroDigit             ||
             exist.digit                   != formatinfo.digit                 ||
             exist.patternSeparator        != formatinfo.patternSeparator 
         ) {
             throw XsltException.Create(Res.Xslt_DupDecimalFormat, name.ToString());
         }
     }
     this.decimalFormatTable[name] = formatinfo;
 }
        private DecimalFormatter CreateDecimalFormatter(string formatPicture, string infinitySymbol, string nanSymbol, string characters) {
            NumberFormatInfo info       = new NumberFormatInfo();
            info.NumberDecimalSeparator = char.ToString(characters[0]);
            info.NumberGroupSeparator   = char.ToString(characters[1]);
            info.PositiveInfinitySymbol = infinitySymbol;
            info.NegativeSign           = char.ToString(characters[7]);
            info.NaNSymbol              = nanSymbol;
            info.PercentSymbol          = char.ToString(characters[2]);
            info.PerMilleSymbol         = char.ToString(characters[3]);
            info.NegativeInfinitySymbol = info.NegativeSign + info.PositiveInfinitySymbol;

            DecimalFormat formatInfo = new DecimalFormat(info, characters[5], characters[4], characters[6]);
            return new DecimalFormatter(formatPicture, formatInfo);
        }