View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: When.java,v 1.2.4.1 2005/09/05 09:36:58 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.compiler;
25  
26  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
27  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
28  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
29  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
30  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
31  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
32  import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
33  
34  /**
35   * @author Jacek Ambroziak
36   * @author Santiago Pericas-Geertsen
37   * @author Morten Jorgensen
38   */
39  final class When extends Instruction {
40  
41      private Expression _test;
42      private boolean _ignore = false;
43  
44      public void display(int indent) {
45          indent(indent);
46          Util.println("When");
47          indent(indent + IndentIncrement);
48          System.out.print("test ");
49          Util.println(_test.toString());
50          displayContents(indent + IndentIncrement);
51      }
52  
53      public Expression getTest() {
54          return _test;
55      }
56  
57      public boolean ignore() {
58          return(_ignore);
59      }
60  
61      public void parseContents(Parser parser) {
62          _test = parser.parseExpression(this, "test", null);
63  
64          // Ignore xsl:if when test is false (function-available() and
65          // element-available())
66          Object result = _test.evaluateAtCompileTime();
67          if (result != null && result instanceof Boolean) {
68              _ignore = !((Boolean) result).booleanValue();
69          }
70  
71          parseChildren(parser);
72  
73          // Make sure required attribute(s) have been set
74          if (_test.isDummy()) {
75              reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "test");
76          }
77      }
78  
79      /**
80       * Type-check this when element. The test should always be type checked,
81       * while we do not bother with the contents if we know the test fails.
82       * This is important in cases where the "test" expression tests for
83       * the support of a non-available element, and the <xsl:when> body contains
84       * this non-available element.
85       */
86      public Type typeCheck(SymbolTable stable) throws TypeCheckError {
87          // Type-check the test expression
88          if (_test.typeCheck(stable) instanceof BooleanType == false) {
89              _test = new CastExpr(_test, Type.Boolean);
90          }
91          // Type-check the contents (if necessary)
92          if (!_ignore) {
93              typeCheckContents(stable);
94          }
95  
96          return Type.Void;
97      }
98  
99      /**
100      * This method should never be called. An Otherwise object will explicitly
101      * translate the "test" expression and and contents of this element.
102      */
103     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
104         final ErrorMsg msg = new ErrorMsg(ErrorMsg.STRAY_WHEN_ERR, this);
105         getParser().reportError(Constants.ERROR, msg);
106     }
107 }