New test.
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslDecimalFormat.jvm.cs
1 //
2 // XslDecimalFormat.jvm.cs
3 //
4 // Authors:
5 //      Andrew Skiba <andrews@mainsoft.com>
6 //      
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Xml;
33 using System.Xml.XPath;
34 using System.Xml.Xsl;
35
36 using QName = System.Xml.XmlQualifiedName;
37
38 namespace Mono.Xml.Xsl {
39         internal class XslDecimalFormat {
40                 
41                 java.text.DecimalFormatSymbols javaFormat;
42                 string baseUri;
43                 int lineNumber;
44                 int linePosition;
45
46                 public static readonly XslDecimalFormat Default = new XslDecimalFormat ();
47                 
48                 XslDecimalFormat ()
49                 {
50                         javaFormat = new java.text.DecimalFormatSymbols ();
51                         javaFormat.setNaN ("NaN");
52                         javaFormat.setInfinity ("Infinity");
53                 }
54
55                 public XslDecimalFormat (Compiler c)
56                         :this ()
57                 {
58                         Initialize(c); 
59                 }
60
61                 private void Initialize(Compiler c)
62                 {
63                         XPathNavigator n = c.Input;
64
65                         IXmlLineInfo li = n as IXmlLineInfo;
66                         if (li != null) {
67                                 lineNumber = li.LineNumber;
68                                 linePosition = li.LinePosition;
69                         }
70                         baseUri = n.BaseURI;
71
72                         if (n.MoveToFirstAttribute ()) {
73                                 do {
74                                         if (n.NamespaceURI != String.Empty)
75                                                 continue;
76                                         
77                                         switch (n.LocalName) {
78                                         case "name": break; // already handled
79                                         case "decimal-separator":
80                                                 if (n.Value.Length != 1)
81                                                         throw new XsltCompileException ("XSLT decimal-separator value must be exact one character.", null, n);
82                                                 javaFormat.setDecimalSeparator (n.Value[0]);
83                                                 break;
84                                                 
85                                         case "grouping-separator":
86                                                 if (n.Value.Length != 1)
87                                                         throw new XsltCompileException ("XSLT grouping-separator value must be exact one character.", null, n);
88                                                 javaFormat.setGroupingSeparator (n.Value[0]);
89                                                 break;
90                                                 
91                                         case "infinity":
92                                                 javaFormat.setInfinity (n.Value);
93                                                 break;
94                                         case "minus-sign":
95                                                 if (n.Value.Length != 1)
96                                                         throw new XsltCompileException ("XSLT minus-sign value must be exact one character.", null, n);
97                                                 javaFormat.setMinusSign (n.Value[0]);
98                                                 break;
99                                         case "NaN":
100                                                 javaFormat.setNaN (n.Value);
101                                                 break;
102                                         case "percent":
103                                                 if (n.Value.Length != 1)
104                                                         throw new XsltCompileException ("XSLT percent value must be exact one character.", null, n);
105                                                 javaFormat.setPercent (n.Value[0]);
106                                                 break;
107                                         case "per-mille":
108                                                 if (n.Value.Length != 1)
109                                                         throw new XsltCompileException ("XSLT per-mille value must be exact one character.", null, n);
110                                                 javaFormat.setPerMill (n.Value[0]);
111                                                 break;
112                                         case "digit":
113                                                 if (n.Value.Length != 1)
114                                                         throw new XsltCompileException ("XSLT digit value must be exact one character.", null, n);
115                                                 javaFormat.setDigit (n.Value[0]);
116                                                 break;
117                                         case "zero-digit":
118                                                 if (n.Value.Length != 1)
119                                                         throw new XsltCompileException ("XSLT zero-digit value must be exact one character.", null, n);
120                                                 javaFormat.setZeroDigit (n.Value [0]);
121                                                 break;
122                                         case "pattern-separator":
123                                                 if (n.Value.Length != 1)
124                                                         throw new XsltCompileException ("XSLT pattern-separator value must be exact one character.", null, n);
125                                                 javaFormat.setPatternSeparator (n.Value [0]);
126                                                 break;
127                                         }
128                                 } while (n.MoveToNextAttribute ());
129                                 n.MoveToParent ();
130                         }
131                 }
132
133                 public void CheckSameAs (XslDecimalFormat other)
134                 {
135                         if (! this.javaFormat.Equals (other.javaFormat))
136                                 throw new XsltCompileException (null, other.baseUri, other.lineNumber, other.linePosition);
137                 }
138
139                 public string FormatNumber (double number, string pattern)
140                 {
141                         java.text.DecimalFormat frm = new java.text.DecimalFormat("", javaFormat);
142
143                         frm.applyLocalizedPattern (pattern);
144
145                         //TODO: the next 4 string could be replaced by just 
146                         //return frm.format (number);
147                         //I don't want to do that before release
148                         java.lang.StringBuffer buffer= new java.lang.StringBuffer ();
149                         java.text.FieldPosition fld = new java.text.FieldPosition (0);
150
151                         frm.format (number, buffer, fld);
152                         return buffer.ToString();
153                 }
154         }
155 }