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: ValueOf.java,v 1.11 2004/02/16 22:25:10 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.INVOKEVIRTUAL;
  23. import com.sun.org.apache.bcel.internal.generic.InstructionList;
  24. import com.sun.org.apache.bcel.internal.generic.PUSH;
  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.Type;
  29. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  30. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  31. /**
  32. * @author Jacek Ambroziak
  33. * @author Santiago Pericas-Geertsen
  34. * @author Morten Jorgensen
  35. */
  36. final class ValueOf extends Instruction {
  37. private Expression _select;
  38. private boolean _escaping = true;
  39. private boolean _isString = false;
  40. public void display(int indent) {
  41. indent(indent);
  42. Util.println("ValueOf");
  43. indent(indent + IndentIncrement);
  44. Util.println("select " + _select.toString());
  45. }
  46. public void parseContents(Parser parser) {
  47. _select = parser.parseExpression(this, "select", null);
  48. // make sure required attribute(s) have been set
  49. if (_select.isDummy()) {
  50. reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "select");
  51. return;
  52. }
  53. final String str = getAttribute("disable-output-escaping");
  54. if ((str != null) && (str.equals("yes"))) _escaping = false;
  55. }
  56. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  57. Type type = _select.typeCheck(stable);
  58. // Prefer to handle the value as a node; fall back to String, otherwise
  59. if (type != null && !type.identicalTo(Type.Node)) {
  60. /***
  61. *** %HZ% Would like to treat result-tree fragments in the same
  62. *** %HZ% way as node sets for value-of, but that's running into
  63. *** %HZ% some snags. Instead, they'll be converted to String
  64. if (type.identicalTo(Type.ResultTree)) {
  65. _select = new CastExpr(new CastExpr(_select, Type.NodeSet),
  66. Type.Node);
  67. } else
  68. ***/
  69. if (type.identicalTo(Type.NodeSet)) {
  70. _select = new CastExpr(_select, Type.Node);
  71. } else {
  72. _isString = true;
  73. if (!type.identicalTo(Type.String)) {
  74. _select = new CastExpr(_select, Type.String);
  75. }
  76. _isString = true;
  77. }
  78. }
  79. return Type.Void;
  80. }
  81. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  82. final ConstantPoolGen cpg = classGen.getConstantPool();
  83. final InstructionList il = methodGen.getInstructionList();
  84. final int setEscaping = cpg.addInterfaceMethodref(OUTPUT_HANDLER,
  85. "setEscaping","(Z)Z");
  86. // Turn off character escaping if so is wanted.
  87. if (!_escaping) {
  88. il.append(methodGen.loadHandler());
  89. il.append(new PUSH(cpg,false));
  90. il.append(new INVOKEINTERFACE(setEscaping,2));
  91. }
  92. // Translate the contents. If the value is a string, use the
  93. // translet.characters(String, TranslatOutputHandler) method.
  94. // Otherwise, the value is a node, and the
  95. // dom.characters(int node, TransletOutputHandler) method can dispatch
  96. // the string value of the node to the output handler more efficiently.
  97. if (_isString) {
  98. final int characters = cpg.addMethodref(TRANSLET_CLASS,
  99. CHARACTERSW,
  100. CHARACTERSW_SIG);
  101. il.append(classGen.loadTranslet());
  102. _select.translate(classGen, methodGen);
  103. il.append(methodGen.loadHandler());
  104. il.append(new INVOKEVIRTUAL(characters));
  105. } else {
  106. final int characters = cpg.addInterfaceMethodref(DOM_INTF,
  107. CHARACTERS,
  108. CHARACTERS_SIG);
  109. il.append(methodGen.loadDOM());
  110. _select.translate(classGen, methodGen);
  111. il.append(methodGen.loadHandler());
  112. il.append(new INVOKEINTERFACE(characters, 3));
  113. }
  114. // Restore character escaping setting to whatever it was.
  115. if (!_escaping) {
  116. il.append(methodGen.loadHandler());
  117. il.append(SWAP);
  118. il.append(new INVOKEINTERFACE(setEscaping,2));
  119. il.append(POP);
  120. }
  121. }
  122. }