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 ();
51 javaFormat.setNaN ("NaN");
52 javaFormat.setInfinity ("Infinity");
55 public XslDecimalFormat (Compiler c)
61 private void Initialize(Compiler c)
63 XPathNavigator n = c.Input;
65 IXmlLineInfo li = n as IXmlLineInfo;
67 lineNumber = li.LineNumber;
68 linePosition = li.LinePosition;
72 if (n.MoveToFirstAttribute ()) {
74 if (n.NamespaceURI != String.Empty)
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]);
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]);
92 javaFormat.setInfinity (n.Value);
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]);
100 javaFormat.setNaN (n.Value);
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]);
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]);
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]);
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]);
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]);
128 } while (n.MoveToNextAttribute ());
133 public void CheckSameAs (XslDecimalFormat other)
135 if (! this.javaFormat.Equals (other.javaFormat))
136 throw new XsltCompileException (null, other.baseUri, other.lineNumber, other.linePosition);
139 public string FormatNumber (double number, string pattern)
141 java.text.DecimalFormat frm = new java.text.DecimalFormat("", javaFormat);
143 frm.applyLocalizedPattern (pattern);
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);
151 frm.format (number, buffer, fld);
152 return buffer.ToString();