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: NameBase.java,v 1.12 2004/02/16 22:24:29 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.INVOKESTATIC;
  23. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  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 Morten Jorgensen
  30. * @author Erwin Bolwidt <ejb@klomp.org>
  31. */
  32. class NameBase extends FunctionCall {
  33. private Expression _param = null;
  34. private Type _paramType = Type.Node;
  35. /**
  36. * Handles calls with no parameter (current node is implicit parameter).
  37. */
  38. public NameBase(QName fname) {
  39. super(fname);
  40. }
  41. /**
  42. * Handles calls with one parameter (either node or node-set).
  43. */
  44. public NameBase(QName fname, Vector arguments) {
  45. super(fname, arguments);
  46. _param = argument(0);
  47. }
  48. /**
  49. * Check that we either have no parameters or one parameter that is
  50. * either a node or a node-set.
  51. */
  52. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  53. // Check the argument type (if any)
  54. switch(argumentCount()) {
  55. case 0:
  56. _paramType = Type.Node;
  57. break;
  58. case 1:
  59. _paramType = _param.typeCheck(stable);
  60. break;
  61. default:
  62. throw new TypeCheckError(this);
  63. }
  64. // The argument has to be a node, a node-set or a node reference
  65. if ((_paramType != Type.NodeSet) &&
  66. (_paramType != Type.Node) &&
  67. (_paramType != Type.Reference)) {
  68. throw new TypeCheckError(this);
  69. }
  70. return (_type = Type.String);
  71. }
  72. public Type getType() {
  73. return _type;
  74. }
  75. /**
  76. * Translate the code required for getting the node for which the
  77. * QName, local-name or namespace URI should be extracted.
  78. */
  79. public void translate(ClassGenerator classGen,
  80. MethodGenerator methodGen) {
  81. final ConstantPoolGen cpg = classGen.getConstantPool();
  82. final InstructionList il = methodGen.getInstructionList();
  83. il.append(methodGen.loadDOM());
  84. // Function was called with no parameters
  85. if (argumentCount() == 0) {
  86. il.append(methodGen.loadContextNode());
  87. }
  88. // Function was called with node parameter
  89. else if (_paramType == Type.Node) {
  90. _param.translate(classGen, methodGen);
  91. }
  92. else if (_paramType == Type.Reference) {
  93. _param.translate(classGen, methodGen);
  94. il.append(new INVOKESTATIC(cpg.addMethodref
  95. (BASIS_LIBRARY_CLASS,
  96. "referenceToNodeSet",
  97. "("
  98. + OBJECT_SIG
  99. + ")"
  100. + NODE_ITERATOR_SIG)));
  101. il.append(methodGen.nextNode());
  102. }
  103. // Function was called with node-set parameter
  104. else {
  105. _param.translate(classGen, methodGen);
  106. _param.startIterator(classGen, methodGen);
  107. il.append(methodGen.nextNode());
  108. }
  109. }
  110. }