KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > compiler > When


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
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.xalan.xsltc.compiler.util.BooleanType;
23 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
24 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
25 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
26 import org.apache.xalan.xsltc.compiler.util.Type;
27 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
28 import org.apache.xalan.xsltc.compiler.util.Util;
29
30 /**
31  * @author Jacek Ambroziak
32  * @author Santiago Pericas-Geertsen
33  * @author Morten Jorgensen
34  */

35 final class When extends Instruction {
36
37     private Expression _test;
38     private boolean _ignore = false;
39
40     public void display(int indent) {
41     indent(indent);
42     Util.println("When");
43     indent(indent + IndentIncrement);
44     System.out.print("test ");
45     Util.println(_test.toString());
46     displayContents(indent + IndentIncrement);
47     }
48         
49     public Expression getTest() {
50     return _test;
51     }
52
53     public boolean ignore() {
54     return(_ignore);
55     }
56
57     public void parseContents(Parser parser) {
58     _test = parser.parseExpression(this, "test", null);
59
60     // Ignore xsl:if when test is false (function-available() and
61
// element-available())
62
Object JavaDoc result = _test.evaluateAtCompileTime();
63     if (result != null && result instanceof Boolean JavaDoc) {
64         _ignore = !((Boolean JavaDoc) result).booleanValue();
65     }
66
67     parseChildren(parser);
68
69     // Make sure required attribute(s) have been set
70
if (_test.isDummy()) {
71         reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "test");
72     }
73     }
74
75     /**
76      * Type-check this when element. The test should always be type checked,
77      * while we do not bother with the contents if we know the test fails.
78      * This is important in cases where the "test" expression tests for
79      * the support of a non-available element, and the <xsl:when> body contains
80      * this non-available element.
81      */

82     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
83     // Type-check the test expression
84
if (_test.typeCheck(stable) instanceof BooleanType == false) {
85         _test = new CastExpr(_test, Type.Boolean);
86     }
87     // Type-check the contents (if necessary)
88
if (!_ignore) {
89         typeCheckContents(stable);
90     }
91
92     return Type.Void;
93     }
94
95     /**
96      * This method should never be called. An Otherwise object will explicitly
97      * translate the "test" expression and and contents of this element.
98      */

99     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
100     final ErrorMsg msg = new ErrorMsg(ErrorMsg.STRAY_WHEN_ERR, this);
101     getParser().reportError(Constants.ERROR, msg);
102     }
103 }
104
Popular Tags