1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2001-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 /* 21 * $Id: ElementAvailableCall.java,v 1.2.4.1 2005/09/01 14:13:01 pvedula Exp $ 22 */ 23 24 package com.sun.org.apache.xalan.internal.xsltc.compiler; 25 26 import java.util.Vector; 27 28 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen; 29 import com.sun.org.apache.bcel.internal.generic.PUSH; 30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator; 33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type; 34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError; 35 36 /** 37 * @author Jacek Ambroziak 38 * @author Santiago Pericas-Geertsen 39 */ 40 final class ElementAvailableCall extends FunctionCall { 41 42 public ElementAvailableCall(QName fname, Vector arguments) { 43 super(fname, arguments); 44 } 45 46 /** 47 * Force the argument to this function to be a literal string. 48 */ 49 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 50 if (argument() instanceof LiteralExpr) { 51 return _type = Type.Boolean; 52 } 53 ErrorMsg err = new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR, 54 "element-available", this); 55 throw new TypeCheckError(err); 56 } 57 58 /** 59 * Returns an object representing the compile-time evaluation 60 * of an expression. We are only using this for function-available 61 * and element-available at this time. 62 */ 63 public Object evaluateAtCompileTime() { 64 return getResult() ? Boolean.TRUE : Boolean.FALSE; 65 } 66 67 /** 68 * Returns the result that this function will return 69 */ 70 public boolean getResult() { 71 try { 72 final LiteralExpr arg = (LiteralExpr) argument(); 73 final String qname = arg.getValue(); 74 final int index = qname.indexOf(':'); 75 final String localName = (index > 0) ? 76 qname.substring(index + 1) : qname; 77 return getParser().elementSupported(arg.getNamespace(), 78 localName); 79 } 80 catch (ClassCastException e) { 81 return false; 82 } 83 } 84 85 /** 86 * Calls to 'element-available' are resolved at compile time since 87 * the namespaces declared in the stylsheet are not available at run 88 * time. Consequently, arguments to this function must be literals. 89 */ 90 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 91 final ConstantPoolGen cpg = classGen.getConstantPool(); 92 final boolean result = getResult(); 93 methodGen.getInstructionList().append(new PUSH(cpg, result)); 94 } 95 }