KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > DecimalFormatManager


1 package com.icl.saxon;
2
3 import java.text.DecimalFormatSymbols JavaDoc;
4 import java.util.Hashtable JavaDoc;
5 import javax.xml.transform.TransformerConfigurationException JavaDoc;
6
7 /**
8   * DecimalFormatManager manages the collection of named and unnamed decimal formats
9   * @version 10 December 1999: extracted from Controller
10   * @author Michael H. Kay (mhkay@iclway.co.uk)
11   */

12   
13 public class DecimalFormatManager {
14
15     private DecimalFormatSymbols JavaDoc defaultDFS;
16     private Hashtable JavaDoc formatTable; // table for named decimal formats
17
private boolean usingOriginalDefault = true;
18
19     /**
20     * create a Controller and initialise variables
21     */

22
23     public DecimalFormatManager() {
24         formatTable = new Hashtable JavaDoc();
25         DecimalFormatSymbols JavaDoc d = new DecimalFormatSymbols JavaDoc();
26         setDefaults(d);
27         defaultDFS = d;
28     }
29
30     /**
31     * Set up the XSLT-defined default attributes in a DecimalFormatSymbols
32     */

33
34     public static void setDefaults(DecimalFormatSymbols JavaDoc d) {
35         d.setDecimalSeparator('.');
36         d.setGroupingSeparator(',');
37         d.setInfinity("Infinity");
38         d.setMinusSign('-');
39         d.setNaN("NaN");
40         d.setPercent('%');
41         d.setPerMill('\u2030');
42         d.setZeroDigit('0');
43         d.setDigit('#');
44         d.setPatternSeparator(';');
45     }
46
47     /**
48     * Register the default decimal-format.
49     * Note that it is an error to register the same decimal-format twice, even with different
50     * precedence
51     */

52
53     public void setDefaultDecimalFormat(DecimalFormatSymbols JavaDoc dfs)
54     throws TransformerConfigurationException JavaDoc {
55         if (!usingOriginalDefault) {
56             if (!dfs.equals(defaultDFS)) {
57                 throw new TransformerConfigurationException JavaDoc(
58                     "There are two conflicting definitions of the default decimal format");
59             }
60         }
61         defaultDFS = dfs;
62         usingOriginalDefault = false;
63     }
64
65     /**
66     * Get the default decimal-format.
67     */

68
69     public DecimalFormatSymbols JavaDoc getDefaultDecimalFormat() {
70         return defaultDFS;
71     }
72
73     /**
74     * Set a named decimal format.
75     * Note that it is an error to register the same decimal-format twice, even with different
76     * precedence.
77     */

78
79     public void setNamedDecimalFormat(int fingerprint, DecimalFormatSymbols JavaDoc dfs)
80     throws TransformerConfigurationException JavaDoc {
81         Integer JavaDoc dfskey = new Integer JavaDoc(fingerprint);
82         DecimalFormatSymbols JavaDoc old = (DecimalFormatSymbols JavaDoc)formatTable.get(dfskey);
83         if (old!=null) {
84             if (!dfs.equals(old)) {
85                 throw new TransformerConfigurationException JavaDoc("Duplicate declaration of decimal-format");
86             }
87         }
88         formatTable.put(dfskey, dfs);
89     }
90
91     /**
92     * Get a named decimal-format registered using setNamedDecimalFormat
93     * @param fingerprint The fingerprint of the name of the decimal format
94     * @return the DecimalFormatSymbols object corresponding to the named locale, if any
95     * or null if not set.
96     */

97
98     public DecimalFormatSymbols JavaDoc getNamedDecimalFormat(int fingerprint) {
99         return (DecimalFormatSymbols JavaDoc)formatTable.get(new Integer JavaDoc(fingerprint));
100     }
101
102
103 }
104
105 //
106
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
107
// you may not use this file except in compliance with the License. You may obtain a copy of the
108
// License at http://www.mozilla.org/MPL/
109
//
110
// Software distributed under the License is distributed on an "AS IS" basis,
111
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
112
// See the License for the specific language governing rights and limitations under the License.
113
//
114
// The Original Code is: all this file.
115
//
116
// The Initial Developer of the Original Code is
117
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
118
//
119
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
120
//
121
// Contributor(s): none.
122
//
123
Popular Tags