1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xalan.internal.xsltc.compiler;
23
24import java.util.Vector;
25
26import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
27import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC;
28import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
29import com.sun.org.apache.bcel.internal.generic.InstructionList;
30import com.sun.org.apache.bcel.internal.generic.PUSH;
31import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
32import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
33import com.sun.org.apache.xalan.internal.xsltc.compiler.util.RealType;
34import com.sun.org.apache.xalan.internal.xsltc.compiler.util.StringType;
35import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
36import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
37
38/**
39 * @author Jacek Ambroziak
40 * @author Santiago Pericas-Geertsen
41 * @author Morten Jorgensen
42 */
43final class FormatNumberCall extends FunctionCall {
44    private Expression _value;
45    private Expression _format;
46    private Expression _name;
47    private QName      _resolvedQName = null;
48
49    public FormatNumberCall(QName fname, Vector arguments) {
50        super(fname, arguments);
51        _value = argument(0);
52        _format = argument(1);
53        _name = argumentCount() == 3 ? argument(2) : null;
54    }
55
56    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
57
58        // Inform stylesheet to instantiate a DecimalFormat object
59        getStylesheet().numberFormattingUsed();
60
61        final Type tvalue = _value.typeCheck(stable);
62        if (tvalue instanceof RealType == false) {
63            _value = new CastExpr(_value, Type.Real);
64        }
65        final Type tformat = _format.typeCheck(stable);
66        if (tformat instanceof StringType == false) {
67            _format = new CastExpr(_format, Type.String);
68        }
69        if (argumentCount() == 3) {
70            final Type tname = _name.typeCheck(stable);
71
72            if (_name instanceof LiteralExpr) {
73                final LiteralExpr literal = (LiteralExpr) _name;
74                _resolvedQName =
75                    getParser().getQNameIgnoreDefaultNs(literal.getValue());
76            }
77            else if (tname instanceof StringType == false) {
78                _name = new CastExpr(_name, Type.String);
79            }
80        }
81        return _type = Type.String;
82    }
83
84    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
85        final ConstantPoolGen cpg = classGen.getConstantPool();
86        final InstructionList il = methodGen.getInstructionList();
87
88        _value.translate(classGen, methodGen);
89        _format.translate(classGen, methodGen);
90
91        final int fn3arg = cpg.addMethodref(BASIS_LIBRARY_CLASS,
92                                            "formatNumber",
93                                            "(DLjava/lang/String;"+
94                                            "Ljava/text/DecimalFormat;)"+
95                                            "Ljava/lang/String;");
96        final int get = cpg.addMethodref(TRANSLET_CLASS,
97                                         "getDecimalFormat",
98                                         "(Ljava/lang/String;)"+
99                                         "Ljava/text/DecimalFormat;");
100
101        il.append(classGen.loadTranslet());
102        if (_name == null) {
103            il.append(new PUSH(cpg, EMPTYSTRING));
104        }
105        else if (_resolvedQName != null) {
106            il.append(new PUSH(cpg, _resolvedQName.toString()));
107        }
108        else {
109            _name.translate(classGen, methodGen);
110        }
111        il.append(new INVOKEVIRTUAL(get));
112        il.append(new INVOKESTATIC(fn3arg));
113    }
114}
115