001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: LangCall.java 468650 2006-10-28 07:03:30Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.compiler;
023    
024    import java.util.Vector;
025    
026    import org.apache.bcel.generic.ConstantPoolGen;
027    import org.apache.bcel.generic.ILOAD;
028    import org.apache.bcel.generic.INVOKESTATIC;
029    import org.apache.bcel.generic.InstructionList;
030    import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
031    import org.apache.xalan.xsltc.compiler.util.FilterGenerator;
032    import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
033    import org.apache.xalan.xsltc.compiler.util.StringType;
034    import org.apache.xalan.xsltc.compiler.util.Type;
035    import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
036    
037    /**
038     * @author Morten Jorgensen
039     */
040    final class LangCall extends FunctionCall {
041        private Expression _lang;
042        private Type _langType;
043    
044        /**
045         * Get the parameters passed to function:
046         *   lang(string)
047         */
048        public LangCall(QName fname, Vector arguments) {
049            super(fname, arguments);
050            _lang = argument(0);
051        }
052    
053        /**
054         *
055         */
056        public Type typeCheck(SymbolTable stable) throws TypeCheckError {
057            _langType = _lang.typeCheck(stable);
058            if (!(_langType instanceof StringType)) {
059                _lang = new CastExpr(_lang, Type.String);
060            }
061            return Type.Boolean;
062        }
063    
064        /**
065         *
066         */
067        public Type getType() {
068            return(Type.Boolean);
069        }
070    
071        /**
072         * This method is called when the constructor is compiled in
073         * Stylesheet.compileConstructor() and not as the syntax tree is traversed.
074         */
075        public void translate(ClassGenerator classGen,
076                              MethodGenerator methodGen) {
077            final ConstantPoolGen cpg = classGen.getConstantPool();
078            final InstructionList il = methodGen.getInstructionList();
079    
080            final int tst = cpg.addMethodref(BASIS_LIBRARY_CLASS,
081                                             "testLanguage",
082                                             "("+STRING_SIG+DOM_INTF_SIG+"I)Z");
083            _lang.translate(classGen,methodGen);
084            il.append(methodGen.loadDOM());
085            if (classGen instanceof FilterGenerator)
086                il.append(new ILOAD(1));
087            else
088                il.append(methodGen.loadContextNode());
089            il.append(new INVOKESTATIC(tst));
090        }
091    }