2005-11-24 Chris Toshok <toshok@ximian.com>
[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                 }
52
53                 public XslDecimalFormat (Compiler c)
54                         :this ()
55                 {
56                         Initialize(c); 
57                 }
58
59                 private void Initialize(Compiler c)
60                 {
61                         XPathNavigator n = c.Input;
62
63                         IXmlLineInfo li = n as IXmlLineInfo;
64                         if (li != null) {
65                                 lineNumber = li.LineNumber;
66                                 linePosition = li.LinePosition;
67                         }
68                         baseUri = n.BaseURI;
69
70                         if (n.MoveToFirstAttribute ()) {
71                                 do {
72                                         if (n.NamespaceURI != String.Empty)
73                                                 continue;
74                                         
75                                         switch (n.LocalName) {
76                                         case "name": break; // already handled
77                                         case "decimal-separator":
78                                                 if (n.Value.Length != 1)
79                                                         throw new XsltCompileException ("XSLT decimal-separator value must be exact one character.", null, n);
80                                                 javaFormat.setDecimalSeparator (n.Value[0]);
81                                                 break;
82                                                 
83                                         case "grouping-separator":
84                                                 if (n.Value.Length != 1)
85                                                         throw new XsltCompileException ("XSLT grouping-separator value must be exact one character.", null, n);
86                                                 javaFormat.setGroupingSeparator (n.Value[0]);
87                                                 break;
88                                                 
89                                         case "infinity":
90                                                 javaFormat.setInfinity (n.Value);
91                                                 break;
92                                         case "minus-sign":
93                                                 if (n.Value.Length != 1)
94                                                         throw new XsltCompileException ("XSLT minus-sign value must be exact one character.", null, n);
95                                                 javaFormat.setMinusSign (n.Value[0]);
96                                                 break;
97                                         case "NaN":
98                                                 javaFormat.setNaN (n.Value);
99                                                 break;
100                                         case "percent":
101                                                 if (n.Value.Length != 1)
102                                                         throw new XsltCompileException ("XSLT percent value must be exact one character.", null, n);
103                                                 javaFormat.setPercent (n.Value[0]);
104                                                 break;
105                                         case "per-mille":
106                                                 if (n.Value.Length != 1)
107                                                         throw new XsltCompileException ("XSLT per-mille value must be exact one character.", null, n);
108                                                 javaFormat.setPerMill (n.Value[0]);
109                                                 break;
110                                         case "digit":
111                                                 if (n.Value.Length != 1)
112                                                         throw new XsltCompileException ("XSLT digit value must be exact one character.", null, n);
113                                                 javaFormat.setDigit (n.Value[0]);
114                                                 break;
115                                         case "zero-digit":
116                                                 if (n.Value.Length != 1)
117                                                         throw new XsltCompileException ("XSLT zero-digit value must be exact one character.", null, n);
118                                                 javaFormat.setZeroDigit (n.Value [0]);
119                                                 break;
120                                         case "pattern-separator":
121                                                 if (n.Value.Length != 1)
122                                                         throw new XsltCompileException ("XSLT pattern-separator value must be exact one character.", null, n);
123                                                 javaFormat.setPatternSeparator (n.Value [0]);
124                                                 break;
125                                         }
126                                 } while (n.MoveToNextAttribute ());
127                                 n.MoveToParent ();
128                         }
129                 }
130
131                 public void CheckSameAs (XslDecimalFormat other)
132                 {
133                         if (! this.javaFormat.equals (other.javaFormat))
134                                 throw new XsltCompileException (null, other.baseUri, other.lineNumber, other.linePosition);
135                 }
136
137                 public string FormatNumber (double number, string pattern)
138                 {
139                         java.text.DecimalFormat frm = new java.text.DecimalFormat("", javaFormat);
140
141                         frm.applyLocalizedPattern (pattern);
142                         java.lang.StringBuffer buffer= new java.lang.StringBuffer ();
143                         java.text.FieldPosition fld = new java.text.FieldPosition (0);
144
145                         frm.format (number, buffer, fld);
146                         return buffer.ToString();
147                 }
148         }
149 }