KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > DecimalFormatting


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DecimalFormatting.java,v 1.14 2004/02/24 03:55:47 zongaro Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.bcel.generic.ConstantPoolGen;
23 import org.apache.bcel.generic.GETSTATIC;
24 import org.apache.bcel.generic.INVOKESPECIAL;
25 import org.apache.bcel.generic.INVOKEVIRTUAL;
26 import org.apache.bcel.generic.InstructionList;
27 import org.apache.bcel.generic.NEW;
28 import org.apache.bcel.generic.PUSH;
29 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
30 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
31 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
32 import org.apache.xalan.xsltc.compiler.util.Type;
33 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
34 import org.apache.xml.utils.XMLChar;
35
36 /**
37  * @author Jacek Ambroziak
38  * @author Santiago Pericas-Geertsen
39  * @author Morten Jorgensen
40  */

41 final class DecimalFormatting extends TopLevelElement {
42
43     private static final String JavaDoc DFS_CLASS = "java.text.DecimalFormatSymbols";
44     private static final String JavaDoc DFS_SIG = "Ljava/text/DecimalFormatSymbols;";
45
46     private QName _name = null;
47
48     /**
49      * No type check needed for the <xsl:decimal-formatting/> element
50      */

51     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
52     return Type.Void;
53     }
54
55     /**
56      * Parse the name of the <xsl:decimal-formatting/> element
57      */

58     public void parseContents(Parser parser) {
59     // Get the name of these decimal formatting symbols
60
final String JavaDoc name = getAttribute("name");
61         if (name.length() > 0) {
62             if (!XMLChar.isValidQName(name)){
63                 ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
64                 parser.reportError(Constants.ERROR, err);
65             }
66         }
67         _name = parser.getQNameIgnoreDefaultNs(name);
68         if (_name == null) {
69             _name = parser.getQNameIgnoreDefaultNs(EMPTYSTRING);
70         }
71
72     // Check if a set of symbols has already been registered under this name
73
SymbolTable stable = parser.getSymbolTable();
74     if (stable.getDecimalFormatting(_name) != null) {
75         reportWarning(this, parser, ErrorMsg.SYMBOLS_REDEF_ERR,
76         _name.toString());
77     }
78     else {
79         stable.addDecimalFormatting(_name, this);
80     }
81     }
82
83     /**
84      * This method is called when the constructor is compiled in
85      * Stylesheet.compileConstructor() and not as the syntax tree is traversed.
86      */

87     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
88
89     ConstantPoolGen cpg = classGen.getConstantPool();
90     InstructionList il = methodGen.getInstructionList();
91     
92     // DecimalFormatSymbols.<init>(Locale);
93
// xsl:decimal-format - except for the NaN and infinity attributes.
94
final int init = cpg.addMethodref(DFS_CLASS, "<init>",
95                                           "("+LOCALE_SIG+")V");
96
97     // Push the format name on the stack for call to addDecimalFormat()
98
il.append(classGen.loadTranslet());
99     il.append(new PUSH(cpg, _name.toString()));
100
101     // Manufacture a DecimalFormatSymbols on the stack
102
// for call to addDecimalFormat()
103
// Use the US Locale as the default, as most of its settings
104
// are equivalent to the default settings required of
105
il.append(new NEW(cpg.addClass(DFS_CLASS)));
106     il.append(DUP);
107         il.append(new GETSTATIC(cpg.addFieldref(LOCALE_CLASS, "US",
108                                                 LOCALE_SIG)));
109     il.append(new INVOKESPECIAL(init));
110
111     String JavaDoc tmp = getAttribute("NaN");
112     if ((tmp == null) || (tmp.equals(EMPTYSTRING))) {
113         int nan = cpg.addMethodref(DFS_CLASS,
114                        "setNaN", "(Ljava/lang/String;)V");
115         il.append(DUP);
116         il.append(new PUSH(cpg, "NaN"));
117         il.append(new INVOKEVIRTUAL(nan));
118     }
119
120     tmp = getAttribute("infinity");
121     if ((tmp == null) || (tmp.equals(EMPTYSTRING))) {
122         int inf = cpg.addMethodref(DFS_CLASS,
123                        "setInfinity",
124                        "(Ljava/lang/String;)V");
125         il.append(DUP);
126         il.append(new PUSH(cpg, "Infinity"));
127         il.append(new INVOKEVIRTUAL(inf));
128     }
129         
130     final int nAttributes = _attributes.getLength();
131     for (int i = 0; i < nAttributes; i++) {
132         final String JavaDoc name = _attributes.getQName(i);
133         final String JavaDoc value = _attributes.getValue(i);
134
135         boolean valid = true;
136         int method = 0;
137
138         if (name.equals("decimal-separator")) {
139         // DecimalFormatSymbols.setDecimalSeparator();
140
method = cpg.addMethodref(DFS_CLASS,
141                       "setDecimalSeparator", "(C)V");
142         }
143         else if (name.equals("grouping-separator")) {
144         method = cpg.addMethodref(DFS_CLASS,
145                        "setGroupingSeparator", "(C)V");
146         }
147         else if (name.equals("minus-sign")) {
148         method = cpg.addMethodref(DFS_CLASS,
149                       "setMinusSign", "(C)V");
150         }
151         else if (name.equals("percent")) {
152         method = cpg.addMethodref(DFS_CLASS,
153                       "setPercent", "(C)V");
154         }
155         else if (name.equals("per-mille")) {
156         method = cpg.addMethodref(DFS_CLASS,
157                       "setPerMill", "(C)V");
158         }
159         else if (name.equals("zero-digit")) {
160         method = cpg.addMethodref(DFS_CLASS,
161                       "setZeroDigit", "(C)V");
162         }
163         else if (name.equals("digit")) {
164         method = cpg.addMethodref(DFS_CLASS,
165                       "setDigit", "(C)V");
166         }
167         else if (name.equals("pattern-separator")) {
168         method = cpg.addMethodref(DFS_CLASS,
169                       "setPatternSeparator", "(C)V");
170         }
171         else if (name.equals("NaN")) {
172         method = cpg.addMethodref(DFS_CLASS,
173                       "setNaN", "(Ljava/lang/String;)V");
174             il.append(DUP);
175         il.append(new PUSH(cpg, value));
176         il.append(new INVOKEVIRTUAL(method));
177         valid = false;
178         }
179         else if (name.equals("infinity")) {
180         method = cpg.addMethodref(DFS_CLASS,
181                       "setInfinity",
182                       "(Ljava/lang/String;)V");
183             il.append(DUP);
184         il.append(new PUSH(cpg, value));
185         il.append(new INVOKEVIRTUAL(method));
186         valid = false;
187         }
188         else {
189         valid = false;
190         }
191
192         if (valid) {
193         il.append(DUP);
194         il.append(new PUSH(cpg, value.charAt(0)));
195         il.append(new INVOKEVIRTUAL(method));
196         }
197
198     }
199
200     final int put = cpg.addMethodref(TRANSLET_CLASS,
201                      "addDecimalFormat",
202                      "("+STRING_SIG+DFS_SIG+")V");
203     il.append(new INVOKEVIRTUAL(put));
204     }
205
206     /**
207      * Creates the default, nameless, DecimalFormat object in
208      * AbstractTranslet's format_symbols hashtable.
209      * This should be called for every stylesheet, and the entry
210      * may be overridden by later nameless xsl:decimal-format instructions.
211      */

212     public static void translateDefaultDFS(ClassGenerator classGen,
213                        MethodGenerator methodGen) {
214
215     ConstantPoolGen cpg = classGen.getConstantPool();
216     InstructionList il = methodGen.getInstructionList();
217     final int init = cpg.addMethodref(DFS_CLASS, "<init>",
218                                           "("+LOCALE_SIG+")V");
219
220     // Push the format name, which is empty, on the stack
221
// for call to addDecimalFormat()
222
il.append(classGen.loadTranslet());
223     il.append(new PUSH(cpg, EMPTYSTRING));
224
225     // Manufacture a DecimalFormatSymbols on the stack for
226
// call to addDecimalFormat(). Use the US Locale as the
227
// default, as most of its settings are equivalent to
228
// the default settings required of xsl:decimal-format -
229
// except for the NaN and infinity attributes.
230
il.append(new NEW(cpg.addClass(DFS_CLASS)));
231     il.append(DUP);
232         il.append(new GETSTATIC(cpg.addFieldref(LOCALE_CLASS, "US",
233                                                 LOCALE_SIG)));
234     il.append(new INVOKESPECIAL(init));
235
236     int nan = cpg.addMethodref(DFS_CLASS,
237                    "setNaN", "(Ljava/lang/String;)V");
238     il.append(DUP);
239     il.append(new PUSH(cpg, "NaN"));
240     il.append(new INVOKEVIRTUAL(nan));
241
242     int inf = cpg.addMethodref(DFS_CLASS,
243                    "setInfinity",
244                    "(Ljava/lang/String;)V");
245     il.append(DUP);
246     il.append(new PUSH(cpg, "Infinity"));
247     il.append(new INVOKEVIRTUAL(inf));
248
249     final int put = cpg.addMethodref(TRANSLET_CLASS,
250                      "addDecimalFormat",
251                      "("+STRING_SIG+DFS_SIG+")V");
252     il.append(new INVOKEVIRTUAL(put));
253     }
254 }
255
Popular Tags