KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > XSLDecimalFormat


1 package net.sf.saxon.style;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.instruct.Executable;
4 import net.sf.saxon.om.AttributeCollection;
5 import net.sf.saxon.om.Name;
6 import net.sf.saxon.om.NamespaceException;
7 import net.sf.saxon.om.QNameException;
8 import net.sf.saxon.trans.DecimalFormatManager;
9 import net.sf.saxon.trans.DecimalSymbols;
10 import net.sf.saxon.trans.StaticError;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.value.StringValue;
13
14 import java.util.HashMap JavaDoc;
15
16 /**
17 * Handler for xsl:decimal-format elements in stylesheet. <br>
18 */

19
20 public class XSLDecimalFormat extends StyleElement {
21
22     boolean prepared = false;
23
24     String JavaDoc name;
25     String JavaDoc decimalSeparator;
26     String JavaDoc groupingSeparator;
27     String JavaDoc infinity;
28     String JavaDoc minusSign;
29     String JavaDoc NaN;
30     String JavaDoc percent;
31     String JavaDoc perMille;
32     String JavaDoc zeroDigit;
33     String JavaDoc digit;
34     String JavaDoc patternSeparator;
35
36     public void prepareAttributes() throws XPathException {
37
38         if (prepared) {
39             return;
40         }
41         prepared = true;
42
43         AttributeCollection atts = getAttributeList();
44
45         for (int a=0; a<atts.getLength(); a++) {
46             int nc = atts.getNameCode(a);
47             String JavaDoc f = getNamePool().getClarkName(nc);
48             if (f==StandardNames.NAME) {
49                 name = atts.getValue(a).trim();
50             } else if (f==StandardNames.DECIMAL_SEPARATOR) {
51                 decimalSeparator = atts.getValue(a);
52             } else if (f==StandardNames.GROUPING_SEPARATOR) {
53                 groupingSeparator = atts.getValue(a);
54             } else if (f==StandardNames.INFINITY) {
55                 infinity = atts.getValue(a);
56             } else if (f==StandardNames.MINUS_SIGN) {
57                 minusSign = atts.getValue(a);
58             } else if (f==StandardNames.NAN) {
59                 NaN = atts.getValue(a);
60             } else if (f==StandardNames.PERCENT) {
61                 percent = atts.getValue(a);
62             } else if (f==StandardNames.PER_MILLE) {
63                 perMille = atts.getValue(a);
64             } else if (f==StandardNames.ZERO_DIGIT) {
65                 zeroDigit = atts.getValue(a);
66             } else if (f==StandardNames.DIGIT) {
67                 digit = atts.getValue(a);
68             } else if (f==StandardNames.PATTERN_SEPARATOR) {
69                 patternSeparator = atts.getValue(a);
70             } else {
71                 checkUnknownAttribute(nc);
72             }
73         }
74     }
75
76     public void validate() throws XPathException {
77         checkTopLevel(null);
78         checkEmpty();
79     }
80
81     public DecimalSymbols makeDecimalFormatSymbols() throws XPathException {
82         DecimalSymbols d = new DecimalSymbols();
83         DecimalFormatManager.setDefaults(d);
84         if (decimalSeparator!=null) {
85             d.decimalSeparator = (toChar(decimalSeparator));
86         }
87         if (groupingSeparator!=null) {
88             d.groupingSeparator = (toChar(groupingSeparator));
89         }
90         if (infinity!=null) {
91             d.infinity = (infinity);
92         }
93         if (minusSign!=null) {
94             d.minusSign = (toChar(minusSign));
95         }
96         if (NaN!=null) {
97             d.NaN = (NaN);
98         }
99         if (percent!=null) {
100             d.percent = (toChar(percent));
101         }
102         if (perMille!=null) {
103             d.permill = (toChar(perMille));
104         }
105         if (zeroDigit!=null) {
106             d.zeroDigit = (toChar(zeroDigit));
107         }
108         if (digit!=null) {
109             d.digit = (toChar(digit));
110         }
111         if (patternSeparator!=null) {
112             d.patternSeparator = (toChar(patternSeparator));
113         }
114         checkDistinctRoles(d);
115         return d;
116     }
117
118     /**
119      * Check that no character is used in more than one role
120      * @throws XPathException
121      */

122
123     private void checkDistinctRoles(DecimalSymbols dfs) throws XPathException {
124         HashMap JavaDoc map = new HashMap JavaDoc(20);
125         Integer JavaDoc c = new Integer JavaDoc(dfs.decimalSeparator);
126         map.put(c, StandardNames.DECIMAL_SEPARATOR);
127
128         c = new Integer JavaDoc(dfs.groupingSeparator);
129         if (map.get(c) != null) {
130             duplicate(StandardNames.GROUPING_SEPARATOR, (String JavaDoc)map.get(c));
131         }
132         map.put(c, StandardNames.GROUPING_SEPARATOR);
133
134         c = new Integer JavaDoc(dfs.percent);
135         if (map.get(c) != null) {
136             duplicate(StandardNames.PERCENT, (String JavaDoc)map.get(c));
137         }
138         map.put(c, StandardNames.PERCENT);
139
140         c = new Integer JavaDoc(dfs.permill);
141         if (map.get(c) != null) {
142             duplicate(StandardNames.PER_MILLE, (String JavaDoc)map.get(c));
143         }
144         map.put(c, StandardNames.PER_MILLE);
145
146         c = new Integer JavaDoc(dfs.zeroDigit);
147         if (map.get(c) != null) {
148             duplicate(StandardNames.ZERO_DIGIT, (String JavaDoc)map.get(c));
149         }
150         map.put(c, StandardNames.ZERO_DIGIT);
151
152         c = new Integer JavaDoc(dfs.digit);
153         if (map.get(c) != null) {
154             duplicate(StandardNames.DIGIT, (String JavaDoc)map.get(c));
155         }
156         map.put(c, StandardNames.DIGIT);
157
158         c = new Integer JavaDoc(dfs.patternSeparator);
159         if (map.get(c) != null) {
160             duplicate(StandardNames.PATTERN_SEPARATOR, (String JavaDoc)map.get(c));
161         }
162         map.put(c, StandardNames.PATTERN_SEPARATOR);
163     }
164
165     private void duplicate(String JavaDoc role1, String JavaDoc role2) throws XPathException {
166         compileError("The same character is used as the " + role1 +
167                 " and as the " + role2, "XTSE1300");
168     }
169
170     public void register() throws XPathException
171     {
172         prepareAttributes();
173         DecimalSymbols d = makeDecimalFormatSymbols();
174         DecimalFormatManager dfm = getPrincipalStylesheet().getDecimalFormatManager();
175         if (name==null) {
176             try {
177                 dfm.setDefaultDecimalFormat(d, getPrecedence());
178             } catch (StaticError err) {
179                 compileError(err.getMessage(), err.getErrorCodeLocalPart());
180             }
181         } else {
182             try {
183                 makeNameCode(name); // checks for reserved namespaces
184
String JavaDoc[] parts = Name.getQNameParts(name);
185                 String JavaDoc uri = getURIForPrefix(parts[0], false);
186                 try {
187                     dfm.setNamedDecimalFormat(uri, parts[1], d, getPrecedence());
188                 } catch (StaticError err) {
189                     compileError(err.getMessage(), err.getErrorCodeLocalPart());
190                 }
191             } catch (XPathException err) {
192                 compileError("Invalid decimal format name. " + err.getMessage(), "XTSE0020");
193             } catch (QNameException err) {
194                 compileError("Invalid decimal format name. " + err.getMessage(), "XTSE0020");
195             } catch (NamespaceException err) {
196                 compileError("Invalid decimal format name. " + err.getMessage(), "XTSE0280");
197             }
198         }
199     }
200
201     public Expression compile(Executable exec) throws XPathException {
202         return null;
203     }
204
205     /**
206      * Get the Unicode codepoint corresponding to a String, which must represent a single Unicode character
207      * @param s the input string, representing a single Unicode character, perhaps as a surrogate pair
208      * @return
209      * @throws XPathException
210      */

211     private int toChar(String JavaDoc s) throws XPathException {
212         int[] e = StringValue.expand(s);
213         if (e.length!=1)
214             compileError("Attribute \"" + s + "\" should be a single character", "XTSE0020");
215         return e[0];
216     }
217
218 }
219 //
220
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
221
// you may not use this file except in compliance with the License. You may obtain a copy of the
222
// License at http://www.mozilla.org/MPL/
223
//
224
// Software distributed under the License is distributed on an "AS IS" basis,
225
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
226
// See the License for the specific language governing rights and limitations under the License.
227
//
228
// The Original Code is: all this file.
229
//
230
// The Initial Developer of the Original Code is Michael H. Kay.
231
//
232
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
233
//
234
// Contributor(s): none.
235
//
236
Popular Tags