2004-04-24 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslDecimalFormat.cs
1 //
2 // XslDecimalFormat.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      
7 // (C) 2003 Ben Maurer
8 //
9
10 using System;
11 using System.Collections;
12 using System.Globalization;
13 using System.Text;
14 using System.Xml;
15 using System.Xml.XPath;
16 using System.Xml.Xsl;
17
18 using QName = System.Xml.XmlQualifiedName;
19
20 namespace Mono.Xml.Xsl {
21         internal class XslDecimalFormat {
22                 
23                 NumberFormatInfo info = new NumberFormatInfo ();
24                 char digit = '#', zeroDigit = '0', patternSeparator = ';';
25                 string baseUri;
26                 int lineNumber;
27                 int linePosition;
28
29                 public static readonly XslDecimalFormat Default = new XslDecimalFormat ();
30                 
31                 XslDecimalFormat () {} // Default ctor for default info.
32                 public XslDecimalFormat (Compiler c)
33                 {
34                         XPathNavigator n = c.Input;
35
36                         IXmlLineInfo li = n as IXmlLineInfo;
37                         if (li != null) {
38                                 lineNumber = li.LineNumber;
39                                 linePosition = li.LinePosition;
40                         }
41                         baseUri = n.BaseURI;
42
43                         if (n.MoveToFirstAttribute ()) {
44                                 do {
45                                         if (n.NamespaceURI != String.Empty)
46                                                 continue;
47                                         
48                                         switch (n.LocalName) {
49                                         case "name": break; // already handled
50                                         case "decimal-separator":
51                                                 if (n.Value.Length != 1)
52                                                         throw new XsltCompileException ("XSLT decimal-separator value must be exact one character.", null, n);
53                                                 info.NumberDecimalSeparator = n.Value;
54                                                 break;
55                                                 
56                                         case "grouping-separator":
57                                                 if (n.Value.Length != 1)
58                                                         throw new XsltCompileException ("XSLT grouping-separator value must be exact one character.", null, n);
59                                                 info.NumberGroupSeparator = n.Value;
60                                                 break;
61                                                 
62                                         case "infinity":
63                                                 info.PositiveInfinitySymbol = n.Value;
64                                                 break;
65                                         case "minus-sign":
66                                                 if (n.Value.Length != 1)
67                                                         throw new XsltCompileException ("XSLT minus-sign value must be exact one character.", null, n);
68                                                 info.NegativeSign = n.Value;
69                                                 break;
70                                         case "NaN":
71                                                 info.NaNSymbol = n.Value;
72                                                 break;
73                                         case "percent":
74                                                 if (n.Value.Length != 1)
75                                                         throw new XsltCompileException ("XSLT percent value must be exact one character.", null, n);
76                                                 info.PercentSymbol = n.Value;
77                                                 break;
78                                         case "per-mille":
79                                                 if (n.Value.Length != 1)
80                                                         throw new XsltCompileException ("XSLT per-mille value must be exact one character.", null, n);
81                                                 info.PerMilleSymbol = n.Value;
82                                                 break;
83                                         case "digit":
84                                                 if (n.Value.Length != 1)
85                                                         throw new XsltCompileException ("XSLT digit value must be exact one character.", null, n);
86                                                 digit = n.Value [0];
87                                                 break;
88                                         case "zero-digit":
89                                                 if (n.Value.Length != 1)
90                                                         throw new XsltCompileException ("XSLT zero-digit value must be exact one character.", null, n);
91                                                 zeroDigit = n.Value [0];
92                                                 break;
93                                         case "pattern-separator":
94                                                 if (n.Value.Length != 1)
95                                                         throw new XsltCompileException ("XSLT pattern-separator value must be exact one character.", null, n);
96                                                 patternSeparator = n.Value [0];
97                                                 break;
98                                         }
99                                 } while (n.MoveToNextAttribute ());
100                                 n.MoveToParent ();
101                                 
102                                 info.NegativeInfinitySymbol = info.NegativeSign + info.PositiveInfinitySymbol;
103                         }
104                 }
105
106                 public char Digit { get { return digit; } }
107                 public char ZeroDigit { get { return zeroDigit; } }
108                 public NumberFormatInfo Info { get { return info; } }
109                 public char PatternSeparator { get { return patternSeparator; } }
110
111                 public void CheckSameAs (XslDecimalFormat other)
112                 {
113                         if (this.digit != other.digit ||
114                                 this.patternSeparator != other.patternSeparator ||
115                                 this.zeroDigit != other.zeroDigit ||
116                                 this.info.NumberDecimalSeparator != other.info.NumberDecimalSeparator ||
117                                 this.info.NumberGroupSeparator != other.info.NumberGroupSeparator ||
118                                 this.info.PositiveInfinitySymbol != other.info.PositiveInfinitySymbol ||
119                                 this.info.NegativeSign != other.info.NegativeSign ||
120                                 this.info.NaNSymbol != other.info.NaNSymbol ||
121                                 this.info.PercentSymbol != other.info.PercentSymbol ||
122                                 this.info.PerMilleSymbol != other.info.PerMilleSymbol)
123                                 throw new XsltCompileException (null, other.baseUri, other.lineNumber, other.linePosition);
124                 }
125
126                 public string FormatNumber (double number, string pattern)
127                 {
128                         return ParsePatternSet (pattern).FormatNumber (number);
129                 }
130
131                 private DecimalFormatPatternSet ParsePatternSet (string pattern)
132                 {
133                         return new DecimalFormatPatternSet (pattern, this);
134                 }
135         }
136
137         // set of positive pattern and negative pattern
138         internal class DecimalFormatPatternSet
139         {
140                 DecimalFormatPattern positivePattern;
141                 DecimalFormatPattern negativePattern;
142 //              XslDecimalFormat decimalFormat;
143
144                 public DecimalFormatPatternSet (string pattern, XslDecimalFormat decimalFormat)
145                 {
146                         Parse (pattern, decimalFormat);
147                 }
148
149                 private void Parse (string pattern, XslDecimalFormat format)
150                 {
151                         positivePattern = new DecimalFormatPattern ();
152
153                         int pos = positivePattern.ParsePattern (0, pattern, format);
154                         if (pos < pattern.Length) {
155                                 if (pattern [pos] != format.PatternSeparator)
156                                         // Expecting caught and wrapped by caller,
157                                         // since it cannot provide XPathNavigator.
158                                         throw new ArgumentException ("Invalid number format pattern string.");
159                                 pos++;
160                                 negativePattern = new DecimalFormatPattern ();
161                                 pos = negativePattern.ParsePattern (pos, pattern, format);
162                                 if (pos < pattern.Length)
163                                         throw new ArgumentException ("Number format pattern string ends with extraneous part.");
164                         }
165                         else
166                                 negativePattern = positivePattern;
167                 }
168
169                 public string FormatNumber (double number)
170                 {
171                         if (number >= 0)
172                                 return positivePattern.FormatNumber (number);
173                         else
174                                 return negativePattern.FormatNumber (number);
175                 }
176         }
177
178         internal class DecimalFormatPattern
179         {
180                 public string Prefix;
181                 public string Suffix;
182                 public string NumberPart;
183                 NumberFormatInfo info;
184
185                 StringBuilder builder;
186
187                 internal int ParsePattern (int start, string pattern, XslDecimalFormat format)
188                 {
189                         if (start == 0) // positive pattern
190                                 this.info = format.Info;
191                         else {
192                                 this.info = format.Info.Clone () as NumberFormatInfo;
193                                 info.NegativeSign = String.Empty; // should be specified in Prefix
194                         }
195
196                         // prefix
197                         int pos = start;
198                         while (pos < pattern.Length) {
199                                 if (pattern [pos] == format.ZeroDigit || pattern [pos] == format.Digit || pattern [pos] == format.Info.CurrencySymbol [0])
200                                         break;
201                                 else
202                                         pos++;
203                         }
204
205                         Prefix = pattern.Substring (start, pos - start);
206                         if (pos == pattern.Length)
207                                 throw new ArgumentException ("Invalid number format pattern."); 
208
209                         // number
210                         pos = ParseNumber (pos, pattern, format);
211                         int suffixStart = pos;
212
213                         // suffix
214                         while (pos < pattern.Length) {
215                                 if (pattern [pos] == format.ZeroDigit || pattern [pos] == format.Digit || pattern [pos] == format.PatternSeparator || pattern [pos] == format.Info.CurrencySymbol [0])
216                                         break;
217                                 else
218                                         pos++;
219                         }
220
221                         Suffix = pattern.Substring (suffixStart, pos - suffixStart);
222
223                         return pos;
224                 }
225
226                 [MonoTODO ("Collect grouping digits")]
227                 private int ParseNumber (int start, string pattern, XslDecimalFormat format)
228                 {
229                         builder = new StringBuilder ();
230
231                         int pos = start;
232                         // process non-minint part.
233                         for (; pos < pattern.Length; pos++) {
234                                 if (pattern [pos] == format.Digit)
235                                         builder.Append ('#');
236                                 else if (pattern [pos] == format.Info.NumberGroupSeparator [0])
237                                         builder.Append (',');
238                                 else
239                                         break;
240                         }
241                         
242                         // minint part.
243                         for (; pos < pattern.Length; pos++) {
244                                 if (pattern [pos] == format.ZeroDigit)
245                                         builder.Append ('0');
246                                 else if (pattern [pos] == format.Info.NumberGroupSeparator [0])
247                                         builder.Append (',');
248                                 else
249                                         break;
250                         }
251
252                         // optional fraction part
253                         if (pos < pattern.Length) {
254                                 if (pattern [pos] == format.Info.NumberDecimalSeparator [0]) {
255                                         builder.Append ('.');
256                                         pos++;
257                                 }
258                                 while (pos < pattern.Length) {
259                                         if (pattern [pos] == format.ZeroDigit) {
260                                                 pos++;
261                                                 builder.Append ('0');
262                                         }
263                                         else
264                                                 break;
265                                 }
266                                 while (pos < pattern.Length) {
267                                         if (pattern [pos] == format.Digit) {
268                                                 pos++;
269                                                 builder.Append ('#');
270                                         }
271                                         else
272                                                 break;
273                                 }
274                         }
275                         // optional exponent part
276                         if (pos + 1 < pattern.Length && pattern [pos] == 'E' && pattern [pos + 1] == format.ZeroDigit) {
277                                 pos += 2;
278                                 builder.Append ("E0");
279                                 while (pos < pattern.Length) {
280                                         if (pattern [pos] == format.ZeroDigit) {
281                                                 pos++;
282                                                 builder.Append ('0');
283                                         }
284                                         else
285                                                 break;
286                                 }
287                         }
288                         // misc special characters
289                         if (pos < pattern.Length) {
290                                 if (pattern [pos] == this.info.PercentSymbol [0])
291                                         builder.Append ('%');
292                                 else if (pattern [pos] == this.info.PerMilleSymbol [0])
293                                         builder.Append ('\u2030');
294                                 else if (pattern [pos] == this.info.CurrencySymbol [0])
295                                         throw new ArgumentException ("Currency symbol is not supported for number format pattern string.");
296                                 else
297                                         pos--;
298                                 pos++;
299                         }
300
301                         NumberPart = builder.ToString ();
302                         return pos;
303                 }
304
305                 public string FormatNumber (double number)
306                 {
307                         builder.Length = 0;
308                         builder.Append (Prefix);
309                         builder.Append (number.ToString (NumberPart, info));
310                         builder.Append (Suffix);
311                         return builder.ToString ();
312                 }
313         }
314 }