KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > trans > DecimalFormatManager


1 package net.sf.saxon.trans;
2
3 import net.sf.saxon.functions.FormatNumber2;
4
5 import java.io.Serializable JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11 /**
12   * DecimalFormatManager manages the collection of named and unnamed decimal formats
13   * @author Michael H. Kay
14   */

15
16 public class DecimalFormatManager implements Serializable JavaDoc {
17
18     private DecimalSymbols defaultDFS;
19     private HashMap JavaDoc formatTable; // table for named decimal formats
20
private boolean usingOriginalDefault = true;
21
22     /**
23     * create a DecimalFormatManager and initialise variables
24     */

25
26     public DecimalFormatManager() {
27         formatTable = new HashMap JavaDoc(10);
28         DecimalSymbols d = new DecimalSymbols();
29         setDefaults(d);
30         defaultDFS = d;
31     }
32
33     /**
34     * Set up the XSLT-defined default attributes in a DecimalFormatSymbols
35     */

36
37     public static void setDefaults(DecimalSymbols d) {
38         d.decimalSeparator = ('.');
39         d.groupingSeparator = (',');
40         d.infinity = ("Infinity");
41         d.minusSign = ('-');
42         d.NaN = ("NaN");
43         d.percent = ('%');
44         d.permill = ('\u2030');
45         d.zeroDigit = ('0');
46         d.digit = ('#');
47         d.patternSeparator = (';');
48     }
49
50     /**
51     * Register the default decimal-format.
52     * Note that it is an error to register the same decimal-format twice, even with different
53     * precedence
54     */

55
56     public void setDefaultDecimalFormat(DecimalSymbols dfs, int precedence)
57     throws StaticError {
58         if (!usingOriginalDefault) {
59             if (!dfs.equals(defaultDFS)) {
60                 StaticError err = new StaticError(
61                     "There are two conflicting definitions of the default decimal format");
62                 err.setErrorCode("XTSE1290");
63                 throw err;
64             }
65         }
66         defaultDFS = dfs;
67         usingOriginalDefault = false;
68         setNamedDecimalFormat("", "", dfs, precedence);
69             // this is to trigger fixup of calls
70
}
71
72     /**
73     * Method called at the end of stylesheet compilation to fix up any format-number() calls
74     * to the "default default" decimal format
75     */

76
77     public void fixupDefaultDefault() throws StaticError {
78         if (usingOriginalDefault) {
79             setNamedDecimalFormat("", "", defaultDFS, -1000);
80         }
81     }
82
83     /**
84     * Get the default decimal-format.
85     */

86
87     public DecimalSymbols getDefaultDecimalFormat() {
88         return defaultDFS;
89     }
90
91     /**
92     * Set a named decimal format.
93     * Note that it is an error to register the same decimal-format twice, unless hte values are
94      * equal, or unless there is another of higher precedence. This method assumes that decimal-formats
95      * are registered in order of decreasing precedence
96     * @param uri The URI of the name of the decimal format
97     * @param localName The local part of the name of the decimal format
98     */

99
100     public void setNamedDecimalFormat(String JavaDoc uri, String JavaDoc localName, DecimalSymbols dfs, int precedence)
101     throws StaticError {
102         String JavaDoc dfskey = localName + '#' + uri;
103         Object JavaDoc o = formatTable.get(dfskey);
104         if (o != null) {
105             if (o instanceof List JavaDoc) {
106                 // this indicates there are forwards references to this decimal format that need to be fixed up
107
for (Iterator JavaDoc iter = ((List JavaDoc)o).iterator(); iter.hasNext(); ) {
108                     FormatNumber2 call = (FormatNumber2)iter.next();
109                     call.fixup(dfs);
110                 }
111             } else {
112                 DecimalFormatInfo info = (DecimalFormatInfo)o;
113                 DecimalSymbols old = info.dfs;
114                 int oldPrecedence = info.precedence;
115                 if (precedence < oldPrecedence) {
116                     return;
117                 }
118                 if (precedence==oldPrecedence && !dfs.equals(old)) {
119                     StaticError err = new StaticError("There are two conflicting definitions of the named decimal-format");
120                     err.setErrorCode("XTSE1290");
121                     throw err;
122                 }
123             }
124         }
125         DecimalFormatInfo dfi = new DecimalFormatInfo();
126         dfi.dfs = dfs;
127         dfi.precedence = precedence;
128         formatTable.put(dfskey, dfi);
129     }
130
131     /**
132     * Register a format-number() function call that uses a particular decimal format. This
133     * allows early compile time resolution to a DecimalFormatSymbols object where possible,
134     * even in the case of a forwards reference
135     */

136
137     public void registerUsage(String JavaDoc uri, String JavaDoc localName, FormatNumber2 call) {
138         String JavaDoc dfskey = localName + '#' + uri;
139         Object JavaDoc o = formatTable.get(dfskey);
140         if (o == null) {
141             // it's a forwards reference
142
List JavaDoc list = new ArrayList JavaDoc(10);
143             list.add(call);
144             formatTable.put(dfskey, list);
145         } else if (o instanceof List JavaDoc) {
146             // it's another forwards reference
147
List JavaDoc list = (List JavaDoc)o;
148             list.add(call);
149         } else {
150             // it's a backwards reference
151
DecimalFormatInfo dfi = (DecimalFormatInfo)o;
152             call.fixup(dfi.dfs);
153         }
154     }
155
156     /**
157     * Get a named decimal-format registered using setNamedDecimalFormat
158     * @param uri The URI of the name of the decimal format
159     * @param localName The local part of the name of the decimal format
160     * @return the DecimalFormatSymbols object corresponding to the named locale, if any
161     * or null if not set.
162     */

163
164     public DecimalSymbols getNamedDecimalFormat(String JavaDoc uri, String JavaDoc localName) {
165         String JavaDoc dfskey = localName + '#' + uri;
166         DecimalFormatInfo dfi = ((DecimalFormatInfo)formatTable.get(dfskey));
167         if (dfi == null) {
168             return null;
169         }
170         return dfi.dfs;
171     }
172
173     private static class DecimalFormatInfo implements Serializable JavaDoc {
174         public DecimalSymbols dfs;
175         public int precedence;
176     }
177
178
179 }
180
181 //
182
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
183
// you may not use this file except in compliance with the License. You may obtain a copy of the
184
// License at http://www.mozilla.org/MPL/
185
//
186
// Software distributed under the License is distributed on an "AS IS" basis,
187
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
188
// See the License for the specific language governing rights and limitations under the License.
189
//
190
// The Original Code is: all this file.
191
//
192
// The Initial Developer of the Original Code is Michael H. Kay.
193
//
194
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
195
//
196
// Contributor(s): none.
197
//
198
Popular Tags