KickJava   Java API By Example, From Geeks To Geeks.

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


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: FormatNumberCall.java,v 1.11 2004/02/16 22:24:28 minchau Exp $
18  */

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

41 final class FormatNumberCall extends FunctionCall {
42     private Expression _value;
43     private Expression _format;
44     private Expression _name;
45     private QName _resolvedQName = null;
46
47     public FormatNumberCall(QName fname, Vector JavaDoc arguments) {
48     super(fname, arguments);
49     _value = argument(0);
50     _format = argument(1);
51     _name = argumentCount() == 3 ? argument(2) : null;
52     }
53
54     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
55
56     // Inform stylesheet to instantiate a DecimalFormat object
57
getStylesheet().numberFormattingUsed();
58
59     final Type tvalue = _value.typeCheck(stable);
60     if (tvalue instanceof RealType == false) {
61         _value = new CastExpr(_value, Type.Real);
62     }
63     final Type tformat = _format.typeCheck(stable);
64     if (tformat instanceof StringType == false) {
65         _format = new CastExpr(_format, Type.String);
66     }
67     if (argumentCount() == 3) {
68         final Type tname = _name.typeCheck(stable);
69
70         if (_name instanceof LiteralExpr) {
71         final LiteralExpr literal = (LiteralExpr) _name;
72         _resolvedQName =
73             getParser().getQNameIgnoreDefaultNs(literal.getValue());
74         }
75         else if (tname instanceof StringType == false) {
76         _name = new CastExpr(_name, Type.String);
77         }
78     }
79     return _type = Type.String;
80     }
81     
82     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
83     final ConstantPoolGen cpg = classGen.getConstantPool();
84     final InstructionList il = methodGen.getInstructionList();
85
86     _value.translate(classGen, methodGen);
87     _format.translate(classGen, methodGen);
88
89     final int fn3arg = cpg.addMethodref(BASIS_LIBRARY_CLASS,
90                         "formatNumber",
91                         "(DLjava/lang/String;"+
92                         "Ljava/text/DecimalFormat;)"+
93                         "Ljava/lang/String;");
94     final int get = cpg.addMethodref(TRANSLET_CLASS,
95                      "getDecimalFormat",
96                      "(Ljava/lang/String;)"+
97                      "Ljava/text/DecimalFormat;");
98     
99     il.append(classGen.loadTranslet());
100     if (_name == null) {
101         il.append(new PUSH(cpg, EMPTYSTRING));
102     }
103     else if (_resolvedQName != null) {
104         il.append(new PUSH(cpg, _resolvedQName.toString()));
105     }
106     else {
107         _name.translate(classGen, methodGen);
108     }
109     il.append(new INVOKEVIRTUAL(get));
110     il.append(new INVOKESTATIC(fn3arg));
111     }
112 }
113
Popular Tags