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: CopyOf.java,v 1.15 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.INVOKESTATIC;
  23. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  24. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  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.NodeSetType;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeType;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
  31. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;
  32. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  33. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  34. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  35. /**
  36. * @author Jacek Ambroziak
  37. * @author Santiago Pericas-Geertsen
  38. */
  39. final class CopyOf extends Instruction {
  40. private Expression _select;
  41. public void display(int indent) {
  42. indent(indent);
  43. Util.println("CopyOf");
  44. indent(indent + IndentIncrement);
  45. Util.println("select " + _select.toString());
  46. }
  47. public void parseContents(Parser parser) {
  48. _select = parser.parseExpression(this, "select", null);
  49. // make sure required attribute(s) have been set
  50. if (_select.isDummy()) {
  51. reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
  52. return;
  53. }
  54. }
  55. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  56. final Type tselect = _select.typeCheck(stable);
  57. if (tselect instanceof NodeType ||
  58. tselect instanceof NodeSetType ||
  59. tselect instanceof ReferenceType ||
  60. tselect instanceof ResultTreeType) {
  61. // falls through
  62. }
  63. else {
  64. _select = new CastExpr(_select, Type.String);
  65. }
  66. return Type.Void;
  67. }
  68. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  69. final ConstantPoolGen cpg = classGen.getConstantPool();
  70. final InstructionList il = methodGen.getInstructionList();
  71. final Type tselect = _select.getType();
  72. final String CPY1_SIG = "("+NODE_ITERATOR_SIG+TRANSLET_OUTPUT_SIG+")V";
  73. final int cpy1 = cpg.addInterfaceMethodref(DOM_INTF, "copy", CPY1_SIG);
  74. final String CPY2_SIG = "("+NODE_SIG+TRANSLET_OUTPUT_SIG+")V";
  75. final int cpy2 = cpg.addInterfaceMethodref(DOM_INTF, "copy", CPY2_SIG);
  76. final String getDoc_SIG = "()"+NODE_SIG;
  77. final int getDoc = cpg.addInterfaceMethodref(DOM_INTF, "getDocument", getDoc_SIG);
  78. if (tselect instanceof NodeSetType) {
  79. il.append(methodGen.loadDOM());
  80. // push NodeIterator
  81. _select.translate(classGen, methodGen);
  82. _select.startIterator(classGen, methodGen);
  83. // call copy from the DOM 'library'
  84. il.append(methodGen.loadHandler());
  85. il.append(new INVOKEINTERFACE(cpy1, 3));
  86. }
  87. else if (tselect instanceof NodeType) {
  88. il.append(methodGen.loadDOM());
  89. _select.translate(classGen, methodGen);
  90. il.append(methodGen.loadHandler());
  91. il.append(new INVOKEINTERFACE(cpy2, 3));
  92. }
  93. else if (tselect instanceof ResultTreeType) {
  94. _select.translate(classGen, methodGen);
  95. // We want the whole tree, so we start with the root node
  96. il.append(DUP); //need a pointer to the DOM ;
  97. il.append(new INVOKEINTERFACE(getDoc,1)); //ICONST_0);
  98. il.append(methodGen.loadHandler());
  99. il.append(new INVOKEINTERFACE(cpy2, 3));
  100. }
  101. else if (tselect instanceof ReferenceType) {
  102. _select.translate(classGen, methodGen);
  103. il.append(methodGen.loadHandler());
  104. il.append(methodGen.loadCurrentNode());
  105. il.append(methodGen.loadDOM());
  106. final int copy = cpg.addMethodref(BASIS_LIBRARY_CLASS, "copy",
  107. "("
  108. + OBJECT_SIG
  109. + TRANSLET_OUTPUT_SIG
  110. + NODE_SIG
  111. + DOM_INTF_SIG
  112. + ")V");
  113. il.append(new INVOKESTATIC(copy));
  114. }
  115. else {
  116. il.append(classGen.loadTranslet());
  117. _select.translate(classGen, methodGen);
  118. il.append(methodGen.loadHandler());
  119. il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS,
  120. CHARACTERSW,
  121. CHARACTERSW_SIG)));
  122. }
  123. }
  124. }