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: FunctionAvailableCall.java,v 1.19 2004/02/23 10:29:35 aruny Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import java.lang.reflect.Method;
  21. import java.lang.reflect.Modifier;
  22. import java.util.Vector;
  23. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  24. import com.sun.org.apache.bcel.internal.generic.PUSH;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  28. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  31. /**
  32. * @author G. Todd Miller
  33. * @author Santiago Pericas-Geertsen
  34. */
  35. final class FunctionAvailableCall extends FunctionCall {
  36. private Expression _arg;
  37. private String _nameOfFunct = null;
  38. private String _namespaceOfFunct = null;
  39. private boolean _isFunctionAvailable = false;
  40. /**
  41. * Constructs a FunctionAvailableCall FunctionCall. Takes the
  42. * function name qname, for example, 'function-available', and
  43. * a list of arguments where the arguments must be instances of
  44. * LiteralExpression.
  45. */
  46. public FunctionAvailableCall(QName fname, Vector arguments) {
  47. super(fname, arguments);
  48. _arg = (Expression)arguments.elementAt(0);
  49. _type = null;
  50. if (_arg instanceof LiteralExpr) {
  51. LiteralExpr arg = (LiteralExpr) _arg;
  52. _namespaceOfFunct = arg.getNamespace();
  53. _nameOfFunct = arg.getValue();
  54. if (!isInternalNamespace()) {
  55. _isFunctionAvailable = hasMethods();
  56. }
  57. }
  58. }
  59. /**
  60. * Argument of function-available call must be literal, typecheck
  61. * returns the type of function-available to be boolean.
  62. */
  63. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  64. if (_type != null) {
  65. return _type;
  66. }
  67. if (_arg instanceof LiteralExpr) {
  68. return _type = Type.Boolean;
  69. }
  70. ErrorMsg err = new ErrorMsg(ErrorMsg.NEED_LITERAL_ERR,
  71. "function-available", this);
  72. throw new TypeCheckError(err);
  73. }
  74. /**
  75. * Returns an object representing the compile-time evaluation
  76. * of an expression. We are only using this for function-available
  77. * and element-available at this time.
  78. */
  79. public Object evaluateAtCompileTime() {
  80. return getResult() ? Boolean.TRUE : Boolean.FALSE;
  81. }
  82. /**
  83. * for external java functions only: reports on whether or not
  84. * the specified method is found in the specifed class.
  85. */
  86. private boolean hasMethods() {
  87. LiteralExpr arg = (LiteralExpr)_arg;
  88. // Get the class name from the namespace uri
  89. String className = getClassNameFromUri(_namespaceOfFunct);
  90. // Get the method name from the argument to function-available
  91. String methodName = null;
  92. int colonIndex = _nameOfFunct.indexOf(":");
  93. if (colonIndex > 0) {
  94. String functionName = _nameOfFunct.substring(colonIndex+1);
  95. int lastDotIndex = functionName.lastIndexOf('.');
  96. if (lastDotIndex > 0) {
  97. methodName = functionName.substring(lastDotIndex+1);
  98. if (className != null && !className.equals(""))
  99. className = className + "." + functionName.substring(0, lastDotIndex);
  100. else
  101. className = functionName.substring(0, lastDotIndex);
  102. }
  103. else
  104. methodName = functionName;
  105. }
  106. else
  107. methodName = _nameOfFunct;
  108. if (className == null || methodName == null) {
  109. return false;
  110. }
  111. // Replace the '-' characters in the method name
  112. if (methodName.indexOf('-') > 0)
  113. methodName = replaceDash(methodName);
  114. try {
  115. final Class clazz = ObjectFactory.findProviderClass(
  116. className, ObjectFactory.findClassLoader(), true);
  117. if (clazz == null) {
  118. return false;
  119. }
  120. final Method[] methods = clazz.getMethods();
  121. for (int i = 0; i < methods.length; i++) {
  122. final int mods = methods[i].getModifiers();
  123. if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
  124. && methods[i].getName().equals(methodName))
  125. {
  126. return true;
  127. }
  128. }
  129. }
  130. catch (ClassNotFoundException e) {
  131. return false;
  132. }
  133. return false;
  134. }
  135. /**
  136. * Reports on whether the function specified in the argument to
  137. * xslt function 'function-available' was found.
  138. */
  139. public boolean getResult() {
  140. if (_nameOfFunct == null) {
  141. return false;
  142. }
  143. if (isInternalNamespace()) {
  144. final Parser parser = getParser();
  145. _isFunctionAvailable =
  146. parser.functionSupported(Util.getLocalName(_nameOfFunct));
  147. }
  148. return _isFunctionAvailable;
  149. }
  150. /**
  151. * Return true if the namespace uri is null or it is the XSLTC translet uri.
  152. */
  153. private boolean isInternalNamespace() {
  154. return (_namespaceOfFunct == null ||
  155. _namespaceOfFunct.equals(EMPTYSTRING) ||
  156. _namespaceOfFunct.equals(TRANSLET_URI));
  157. }
  158. /**
  159. * Calls to 'function-available' are resolved at compile time since
  160. * the namespaces declared in the stylsheet are not available at run
  161. * time. Consequently, arguments to this function must be literals.
  162. */
  163. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  164. final ConstantPoolGen cpg = classGen.getConstantPool();
  165. methodGen.getInstructionList().append(new PUSH(cpg, getResult()));
  166. }
  167. }