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: When.java,v 1.13 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.xalan.internal.xsltc.compiler.util.BooleanType;
  21. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
  22. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  23. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
  24. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
  27. /**
  28. * @author Jacek Ambroziak
  29. * @author Santiago Pericas-Geertsen
  30. * @author Morten Jorgensen
  31. */
  32. final class When extends Instruction {
  33. private Expression _test;
  34. private boolean _ignore = false;
  35. public void display(int indent) {
  36. indent(indent);
  37. Util.println("When");
  38. indent(indent + IndentIncrement);
  39. System.out.print("test ");
  40. Util.println(_test.toString());
  41. displayContents(indent + IndentIncrement);
  42. }
  43. public Expression getTest() {
  44. return _test;
  45. }
  46. public boolean ignore() {
  47. return(_ignore);
  48. }
  49. public void parseContents(Parser parser) {
  50. _test = parser.parseExpression(this, "test", null);
  51. // Ignore xsl:if when test is false (function-available() and
  52. // element-available())
  53. Object result = _test.evaluateAtCompileTime();
  54. if (result != null && result instanceof Boolean) {
  55. _ignore = !((Boolean) result).booleanValue();
  56. }
  57. parseChildren(parser);
  58. // Make sure required attribute(s) have been set
  59. if (_test.isDummy()) {
  60. reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "test");
  61. }
  62. }
  63. /**
  64. * Type-check this when element. The test should always be type checked,
  65. * while we do not bother with the contents if we know the test fails.
  66. * This is important in cases where the "test" expression tests for
  67. * the support of a non-available element, and the <xsl:when> body contains
  68. * this non-available element.
  69. */
  70. public Type typeCheck(SymbolTable stable) throws TypeCheckError {
  71. // Type-check the test expression
  72. if (_test.typeCheck(stable) instanceof BooleanType == false) {
  73. _test = new CastExpr(_test, Type.Boolean);
  74. }
  75. // Type-check the contents (if necessary)
  76. if (!_ignore) {
  77. typeCheckContents(stable);
  78. }
  79. return Type.Void;
  80. }
  81. /**
  82. * This method should never be called. An Otherwise object will explicitly
  83. * translate the "test" expression and and contents of this element.
  84. */
  85. public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
  86. final ErrorMsg msg = new ErrorMsg(ErrorMsg.STRAY_WHEN_ERR, this);
  87. getParser().reportError(Constants.ERROR, msg);
  88. }
  89. }