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: ElementAvailableCall.java,v 1.10 2004/02/16 22:24:28 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.util.Vector;
  21. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  22. import com.sun.org.apache.bcel.internal.generic.PUSH;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  28. /**
  29. * @author Jacek Ambroziak
  30. * @author Santiago Pericas-Geertsen
  31. */
  32. final class ElementAvailableCall extends FunctionCall {
  33. public ElementAvailableCall(QName fname, Vector arguments) {
  34. super(fname, arguments);
  35. }
  36. /**
  37. * Force the argument to this function to be a literal string.
  38. */
  39. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  40. if (argument() instanceof LiteralExpr) {
  41. return _type = Type.Boolean;
  42. }
  43. ErrorMsg err = new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
  44. "element-available", this);
  45. throw new TypeCheckError(err);
  46. }
  47. /**
  48. * Returns an object representing the compile-time evaluation
  49. * of an expression. We are only using this for function-available
  50. * and element-available at this time.
  51. */
  52. public Object evaluateAtCompileTime() {
  53. return getResult() ? Boolean.TRUE : Boolean.FALSE;
  54. }
  55. /**
  56. * Returns the result that this function will return
  57. */
  58. public boolean getResult() {
  59. try {
  60. final LiteralExpr arg = (LiteralExpr) argument();
  61. final String qname = arg.getValue();
  62. final int index = qname.indexOf(':');
  63. final String localName = (index > 0) ?
  64. qname.substring(index + 1) : qname;
  65. return getParser().elementSupported(arg.getNamespace(),
  66. localName);
  67. }
  68. catch (ClassCastException e) {
  69. return false;
  70. }
  71. }
  72. /**
  73. * Calls to 'element-available' are resolved at compile time since
  74. * the namespaces declared in the stylsheet are not available at run
  75. * time. Consequently, arguments to this function must be literals.
  76. */
  77. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  78. final ConstantPoolGen cpg = classGen.getConstantPool();
  79. final boolean result = getResult();
  80. methodGen.getInstructionList().append(new PUSH(cpg, result));
  81. }
  82. }