2 // XslDecimalFormat.jvm.cs
5 // Andrew Skiba <andrews@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
33 using System.Xml.XPath;
36 using QName = System.Xml.XmlQualifiedName;
38 namespace Mono.Xml.Xsl {
39 internal class XslDecimalFormat {
41 java.text.DecimalFormatSymbols javaFormat;
46 public static readonly XslDecimalFormat Default = new XslDecimalFormat ();
50 javaFormat = new java.text.DecimalFormatSymbols ();
53 public XslDecimalFormat (Compiler c)
59 private void Initialize(Compiler c)
61 XPathNavigator n = c.Input;
63 IXmlLineInfo li = n as IXmlLineInfo;
65 lineNumber = li.LineNumber;
66 linePosition = li.LinePosition;
70 if (n.MoveToFirstAttribute ()) {
72 if (n.NamespaceURI != String.Empty)
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]);
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]);
90 javaFormat.setInfinity (n.Value);
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]);
98 javaFormat.setNaN (n.Value);
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]);
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]);
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]);
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]);
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]);
126 } while (n.MoveToNextAttribute ());
131 public void CheckSameAs (XslDecimalFormat other)
133 if (! this.javaFormat.equals (other.javaFormat))
134 throw new XsltCompileException (null, other.baseUri, other.lineNumber, other.linePosition);
137 public string FormatNumber (double number, string pattern)
139 java.text.DecimalFormat frm = new java.text.DecimalFormat("", javaFormat);
141 frm.applyLocalizedPattern (pattern);
142 java.lang.StringBuffer buffer= new java.lang.StringBuffer ();
143 java.text.FieldPosition fld = new java.text.FieldPosition (0);
145 frm.format (number, buffer, fld);
146 return buffer.ToString();