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: ElementAvailableCall.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.PUSH;
028 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
029 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
030 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
031 import org.apache.xalan.xsltc.compiler.util.Type;
032 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
033
034 /**
035 * @author Jacek Ambroziak
036 * @author Santiago Pericas-Geertsen
037 */
038 final class ElementAvailableCall extends FunctionCall {
039
040 public ElementAvailableCall(QName fname, Vector arguments) {
041 super(fname, arguments);
042 }
043
044 /**
045 * Force the argument to this function to be a literal string.
046 */
047 public Type typeCheck(SymbolTable stable) throws TypeCheckError {
048 if (argument() instanceof LiteralExpr) {
049 return _type = Type.Boolean;
050 }
051 ErrorMsg err = new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
052 "element-available", this);
053 throw new TypeCheckError(err);
054 }
055
056 /**
057 * Returns an object representing the compile-time evaluation
058 * of an expression. We are only using this for function-available
059 * and element-available at this time.
060 */
061 public Object evaluateAtCompileTime() {
062 return getResult() ? Boolean.TRUE : Boolean.FALSE;
063 }
064
065 /**
066 * Returns the result that this function will return
067 */
068 public boolean getResult() {
069 try {
070 final LiteralExpr arg = (LiteralExpr) argument();
071 final String qname = arg.getValue();
072 final int index = qname.indexOf(':');
073 final String localName = (index > 0) ?
074 qname.substring(index + 1) : qname;
075 return getParser().elementSupported(arg.getNamespace(),
076 localName);
077 }
078 catch (ClassCastException e) {
079 return false;
080 }
081 }
082
083 /**
084 * Calls to 'element-available' are resolved at compile time since
085 * the namespaces declared in the stylsheet are not available at run
086 * time. Consequently, arguments to this function must be literals.
087 */
088 public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
089 final ConstantPoolGen cpg = classGen.getConstantPool();
090 final boolean result = getResult();
091 methodGen.getInstructionList().append(new PUSH(cpg, result));
092 }
093 }