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: AbsoluteLocationPath.java,v 1.8 2004/02/16 22:24:29 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.compiler;
  20. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  21. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  22. import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
  23. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  24. import com.sun.org.apache.bcel.internal.generic.NEW;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  27. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
  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. /**
  31. * @author Jacek Ambroziak
  32. * @author Santiago Pericas-Geertsen
  33. */
  34. final class AbsoluteLocationPath extends Expression {
  35. private Expression _path; // may be null
  36. public AbsoluteLocationPath() {
  37. _path = null;
  38. }
  39. public AbsoluteLocationPath(Expression path) {
  40. _path = path;
  41. if (path != null) {
  42. _path.setParent(this);
  43. }
  44. }
  45. public void setParser(Parser parser) {
  46. super.setParser(parser);
  47. if (_path != null) {
  48. _path.setParser(parser);
  49. }
  50. }
  51. public Expression getPath() {
  52. return(_path);
  53. }
  54. public String toString() {
  55. return "AbsoluteLocationPath(" +
  56. (_path != null ? _path.toString() : "null") + ')';
  57. }
  58. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  59. if (_path != null) {
  60. final Type ptype = _path.typeCheck(stable);
  61. if (ptype instanceof NodeType) { // promote to node-set
  62. _path = new CastExpr(_path, Type.NodeSet);
  63. }
  64. }
  65. return _type = Type.NodeSet;
  66. }
  67. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  68. final ConstantPoolGen cpg = classGen.getConstantPool();
  69. final InstructionList il = methodGen.getInstructionList();
  70. if (_path != null) {
  71. final int initAI = cpg.addMethodref(ABSOLUTE_ITERATOR,
  72. "<init>",
  73. "("
  74. + NODE_ITERATOR_SIG
  75. + ")V");
  76. // Create new AbsoluteIterator
  77. il.append(new NEW(cpg.addClass(ABSOLUTE_ITERATOR)));
  78. il.append(DUP);
  79. // Compile relative path iterator(s)
  80. _path.translate(classGen, methodGen);
  81. // Initialize AbsoluteIterator with iterator from the stack
  82. il.append(new INVOKESPECIAL(initAI));
  83. }
  84. else {
  85. final int gitr = cpg.addInterfaceMethodref(DOM_INTF,
  86. "getIterator",
  87. "()"+NODE_ITERATOR_SIG);
  88. il.append(methodGen.loadDOM());
  89. il.append(new INVOKEINTERFACE(gitr, 1));
  90. }
  91. }
  92. }