1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
//
// XslDecimalFormat.cs
//
// Authors:
// Ben Maurer (bmaurer@users.sourceforge.net)
//
// (C) 2003 Ben Maurer
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using QName = System.Xml.XmlQualifiedName;
namespace Mono.Xml.Xsl {
internal class XslDecimalFormat {
NumberFormatInfo info = new NumberFormatInfo ();
char digit = '#', zeroDigit = '0', patternSeparator = ';';
string baseUri;
int lineNumber;
int linePosition;
public static readonly XslDecimalFormat Default = new XslDecimalFormat ();
XslDecimalFormat () {} // Default ctor for default info.
public XslDecimalFormat (Compiler c)
{
XPathNavigator n = c.Input;
IXmlLineInfo li = n as IXmlLineInfo;
if (li != null) {
lineNumber = li.LineNumber;
linePosition = li.LinePosition;
}
baseUri = n.BaseURI;
if (n.MoveToFirstAttribute ()) {
do {
if (n.NamespaceURI != String.Empty)
continue;
switch (n.LocalName) {
case "name": break; // already handled
case "decimal-separator":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT decimal-separator value must be exact one character", null, n);
info.NumberDecimalSeparator = n.Value;
break;
case "grouping-separator":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT grouping-separator value must be exact one character", null, n);
info.NumberGroupSeparator = n.Value;
break;
case "infinity":
info.PositiveInfinitySymbol = n.Value;
break;
case "minus-sign":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT minus-sign value must be exact one character", null, n);
info.NegativeSign = n.Value;
break;
case "NaN":
info.NaNSymbol = n.Value;
break;
case "percent":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT percent value must be exact one character", null, n);
info.PercentSymbol = n.Value;
break;
case "per-mille":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT per-mille value must be exact one character", null, n);
info.PerMilleSymbol = n.Value;
break;
case "digit":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT digit value must be exact one character", null, n);
digit = n.Value [0];
break;
case "zero-digit":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT zero-digit value must be exact one character", null, n);
zeroDigit = n.Value [0];
break;
case "pattern-separator":
if (n.Value.Length != 1)
throw new XsltCompileException ("XSLT pattern-separator value must be exact one character", null, n);
patternSeparator = n.Value [0];
break;
}
} while (n.MoveToNextAttribute ());
n.MoveToParent ();
info.NegativeInfinitySymbol = info.NegativeSign + info.PositiveInfinitySymbol;
}
}
public char Digit { get { return digit; } }
public char ZeroDigit { get { return zeroDigit; } }
public NumberFormatInfo Info { get { return info; } }
public char PatternSeparator { get { return patternSeparator; } }
public void CheckSameAs (XslDecimalFormat other)
{
if (this.digit != other.digit ||
this.patternSeparator != other.patternSeparator ||
this.zeroDigit != other.zeroDigit ||
this.info.NumberDecimalSeparator != other.info.NumberDecimalSeparator ||
this.info.NumberGroupSeparator != other.info.NumberGroupSeparator ||
this.info.PositiveInfinitySymbol != other.info.PositiveInfinitySymbol ||
this.info.NegativeSign != other.info.NegativeSign ||
this.info.NaNSymbol != other.info.NaNSymbol ||
this.info.PercentSymbol != other.info.PercentSymbol ||
this.info.PerMilleSymbol != other.info.PerMilleSymbol)
throw new XsltCompileException (null, other.baseUri, other.lineNumber, other.linePosition);
}
public string FormatNumber (double number, string pattern)
{
return ParsePatternSet (pattern).FormatNumber (number);
}
private DecimalFormatPatternSet ParsePatternSet (string pattern)
{
return new DecimalFormatPatternSet (pattern, this);
}
}
// set of positive pattern and negative pattern
internal class DecimalFormatPatternSet
{
DecimalFormatPattern positivePattern;
DecimalFormatPattern negativePattern;
// XslDecimalFormat decimalFormat;
public DecimalFormatPatternSet (string pattern, XslDecimalFormat decimalFormat)
{
Parse (pattern, decimalFormat);
}
private void Parse (string pattern, XslDecimalFormat format)
{
if (pattern.Length == 0)
throw new ArgumentException ("Invalid number format pattern string.");
positivePattern = new DecimalFormatPattern ();
negativePattern = positivePattern;
int pos = positivePattern.ParsePattern (0, pattern, format);
if (pos < pattern.Length) {
if (pattern [pos] != format.PatternSeparator)
// Expecting caught and wrapped by caller,
// since it cannot provide XPathNavigator.
// throw new ArgumentException ("Invalid number format pattern string.");
return;
pos++;
negativePattern = new DecimalFormatPattern ();
pos = negativePattern.ParsePattern (pos, pattern, format);
if (pos < pattern.Length)
throw new ArgumentException ("Number format pattern string ends with extraneous part.");
}
}
public string FormatNumber (double number)
{
if (number >= 0)
return positivePattern.FormatNumber (number);
else
return negativePattern.FormatNumber (number);
}
}
internal class DecimalFormatPattern
{
public string Prefix = String.Empty;
public string Suffix = String.Empty;
public string NumberPart;
NumberFormatInfo info;
StringBuilder builder = new StringBuilder ();
internal int ParsePattern (int start, string pattern, XslDecimalFormat format)
{
if (start == 0) // positive pattern
this.info = format.Info;
else {
this.info = format.Info.Clone () as NumberFormatInfo;
info.NegativeSign = String.Empty; // should be specified in Prefix
}
// prefix
int pos = start;
while (pos < pattern.Length) {
if (pattern [pos] == format.ZeroDigit || pattern [pos] == format.Digit || pattern [pos] == format.Info.CurrencySymbol [0])
break;
else
pos++;
}
Prefix = pattern.Substring (start, pos - start);
if (pos == pattern.Length) {
// Invalid number pattern.
// throw new ArgumentException ("Invalid number format pattern.");
return pos;
}
// number
pos = ParseNumber (pos, pattern, format);
int suffixStart = pos;
// suffix
while (pos < pattern.Length) {
if (pattern [pos] == format.ZeroDigit || pattern [pos] == format.Digit || pattern [pos] == format.PatternSeparator || pattern [pos] == format.Info.CurrencySymbol [0])
break;
else
pos++;
}
Suffix = pattern.Substring (suffixStart, pos - suffixStart);
return pos;
}
// FIXME: Collect grouping digits
private int ParseNumber (int start, string pattern, XslDecimalFormat format)
{
int pos = start;
// process non-minint part.
for (; pos < pattern.Length; pos++) {
if (pattern [pos] == format.Digit)
builder.Append ('#');
else if (pattern [pos] == format.Info.NumberGroupSeparator [0])
builder.Append (',');
else
break;
}
// minint part.
for (; pos < pattern.Length; pos++) {
if (pattern [pos] == format.ZeroDigit)
builder.Append ('0');
else if (pattern [pos] == format.Info.NumberGroupSeparator [0])
builder.Append (',');
else
break;
}
// optional fraction part
if (pos < pattern.Length) {
if (pattern [pos] == format.Info.NumberDecimalSeparator [0]) {
builder.Append ('.');
pos++;
}
while (pos < pattern.Length) {
if (pattern [pos] == format.ZeroDigit) {
pos++;
builder.Append ('0');
}
else
break;
}
while (pos < pattern.Length) {
if (pattern [pos] == format.Digit) {
pos++;
builder.Append ('#');
}
else
break;
}
}
// optional exponent part
if (pos + 1 < pattern.Length && pattern [pos] == 'E' && pattern [pos + 1] == format.ZeroDigit) {
pos += 2;
builder.Append ("E0");
while (pos < pattern.Length) {
if (pattern [pos] == format.ZeroDigit) {
pos++;
builder.Append ('0');
}
else
break;
}
}
// misc special characters
if (pos < pattern.Length) {
if (pattern [pos] == this.info.PercentSymbol [0])
builder.Append ('%');
else if (pattern [pos] == this.info.PerMilleSymbol [0])
builder.Append ('\u2030');
else if (pattern [pos] == this.info.CurrencySymbol [0])
throw new ArgumentException ("Currency symbol is not supported for number format pattern string.");
else
pos--;
pos++;
}
NumberPart = builder.ToString ();
return pos;
}
public string FormatNumber (double number)
{
builder.Length = 0;
builder.Append (Prefix);
builder.Append (number.ToString (NumberPart, info));
builder.Append (Suffix);
return builder.ToString ();
}
}
}
|