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: Comment.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.GETFIELD;
  22. import com.sun.org.apache.bcel.internal.generic.INVOKEINTERFACE;
  23. import com.sun.org.apache.bcel.internal.generic.INVOKEVIRTUAL;
  24. import com.sun.org.apache.bcel.internal.generic.PUSH;
  25. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  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. /**
  31. * @author Jacek Ambroziak
  32. * @author Santiago Pericas-Geertsen
  33. * @author Morten Jorgensen
  34. */
  35. final class Comment extends Instruction {
  36. public void parseContents(Parser parser) {
  37. parseChildren(parser);
  38. }
  39. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  40. typeCheckContents(stable);
  41. return Type.String;
  42. }
  43. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  44. final ConstantPoolGen cpg = classGen.getConstantPool();
  45. final InstructionList il = methodGen.getInstructionList();
  46. // Shortcut for literal strings
  47. Text rawText = null;
  48. if (elementCount() == 1) {
  49. Object content = elementAt(0);
  50. if (content instanceof Text) {
  51. rawText = (Text) content;
  52. }
  53. }
  54. // If the content is literal text, call comment(char[],int,int) or
  55. // comment(String), as appropriate. Otherwise, use a
  56. // StringValueHandler to gather the textual content of the xsl:comment
  57. // and call comment(String) with the result.
  58. if (rawText != null) {
  59. il.append(methodGen.loadHandler());
  60. if (rawText.canLoadAsArrayOffsetLength()) {
  61. rawText.loadAsArrayOffsetLength(classGen, methodGen);
  62. final int comment =
  63. cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
  64. "comment",
  65. "([CII)V");
  66. il.append(new INVOKEINTERFACE(comment, 4));
  67. } else {
  68. il.append(new PUSH(cpg, rawText.getText()));
  69. final int comment =
  70. cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
  71. "comment",
  72. "(" + STRING_SIG + ")V");
  73. il.append(new INVOKEINTERFACE(comment, 2));
  74. }
  75. } else {
  76. // Save the current handler base on the stack
  77. il.append(methodGen.loadHandler());
  78. il.append(DUP); // first arg to "comment" call
  79. // Get the translet's StringValueHandler
  80. il.append(classGen.loadTranslet());
  81. il.append(new GETFIELD(cpg.addFieldref(TRANSLET_CLASS,
  82. "stringValueHandler",
  83. STRING_VALUE_HANDLER_SIG)));
  84. il.append(DUP);
  85. il.append(methodGen.storeHandler());
  86. // translate contents with substituted handler
  87. translateContents(classGen, methodGen);
  88. // get String out of the handler
  89. il.append(new INVOKEVIRTUAL(cpg.addMethodref(STRING_VALUE_HANDLER,
  90. "getValue",
  91. "()" + STRING_SIG)));
  92. // call "comment"
  93. final int comment =
  94. cpg.addInterfaceMethodref(TRANSLET_OUTPUT_INTERFACE,
  95. "comment",
  96. "(" + STRING_SIG + ")V");
  97. il.append(new INVOKEINTERFACE(comment, 2));
  98. // Restore old handler base from stack
  99. il.append(methodGen.storeHandler());
  100. }
  101. }
  102. }