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: AbsolutePathPattern.java,v 1.10 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.BranchHandle;
  21. import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
  22. import com.sun.org.apache.bcel.internal.generic.GOTO_W;
  23. import com.sun.org.apache.bcel.internal.generic.IF_ICMPEQ;
  24. import com.sun.org.apache.bcel.internal.generic.ILOAD;
  25. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  26. import com.sun.org.apache.bcel.internal.generic.ISTORE;
  27. import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
  28. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  29. import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
  30. import com.sun.org.apache.bcel.internal.generic.PUSH;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  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. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  36. import com.sun.org.apache.xml.internal.dtm.DTM;
  37. /**
  38. * @author Jacek Ambroziak
  39. * @author Santiago Pericas-Geertsen
  40. */
  41. final class AbsolutePathPattern extends LocationPathPattern {
  42. private final RelativePathPattern _left; // may be null
  43. public AbsolutePathPattern(RelativePathPattern left) {
  44. _left = left;
  45. if (left != null) {
  46. left.setParent(this);
  47. }
  48. }
  49. public void setParser(Parser parser) {
  50. super.setParser(parser);
  51. if (_left != null)
  52. _left.setParser(parser);
  53. }
  54. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  55. return _left == null ? Type.Root : _left.typeCheck(stable);
  56. }
  57. public boolean isWildcard() {
  58. return false;
  59. }
  60. public StepPattern getKernelPattern() {
  61. return _left != null ? _left.getKernelPattern() : null;
  62. }
  63. public void reduceKernelPattern() {
  64. _left.reduceKernelPattern();
  65. }
  66. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  67. final ConstantPoolGen cpg = classGen.getConstantPool();
  68. final InstructionList il = methodGen.getInstructionList();
  69. if (_left != null) {
  70. if (_left instanceof StepPattern) {
  71. final LocalVariableGen local =
  72. // absolute path pattern temporary
  73. methodGen.addLocalVariable2("apptmp",
  74. Util.getJCRefType(NODE_SIG),
  75. il.getEnd());
  76. il.append(DUP);
  77. il.append(new ISTORE(local.getIndex()));
  78. _left.translate(classGen, methodGen);
  79. il.append(methodGen.loadDOM());
  80. local.setEnd(il.append(new ILOAD(local.getIndex())));
  81. methodGen.removeLocalVariable(local);
  82. }
  83. else {
  84. _left.translate(classGen, methodGen);
  85. }
  86. }
  87. final int getParent = cpg.addInterfaceMethodref(DOM_INTF,
  88. GET_PARENT,
  89. GET_PARENT_SIG);
  90. final int getType = cpg.addInterfaceMethodref(DOM_INTF,
  91. "getExpandedTypeID",
  92. "(I)I");
  93. InstructionHandle begin = il.append(methodGen.loadDOM());
  94. il.append(SWAP);
  95. il.append(new INVOKEINTERFACE(getParent, 2));
  96. if (_left instanceof AncestorPattern) {
  97. il.append(methodGen.loadDOM());
  98. il.append(SWAP);
  99. }
  100. il.append(new INVOKEINTERFACE(getType, 2));
  101. il.append(new PUSH(cpg, DTM.DOCUMENT_NODE));
  102. final BranchHandle skip = il.append(new IF_ICMPEQ(null));
  103. _falseList.add(il.append(new GOTO_W(null)));
  104. skip.setTarget(il.append(NOP));
  105. if (_left != null) {
  106. _left.backPatchTrueList(begin);
  107. /*
  108. * If _left is an ancestor pattern, backpatch this pattern's false
  109. * list to the loop that searches for more ancestors.
  110. */
  111. if (_left instanceof AncestorPattern) {
  112. final AncestorPattern ancestor = (AncestorPattern) _left;
  113. _falseList.backPatch(ancestor.getLoopHandle()); // clears list
  114. }
  115. _falseList.append(_left._falseList);
  116. }
  117. }
  118. public String toString() {
  119. return "absolutePathPattern(" + (_left != null ? _left.toString() : ")");
  120. }
  121. }