KickJava   Java API By Example, From Geeks To Geeks.

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


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: If.java,v 1.13 2004/02/16 22:24:29 minchau Exp $
18  */

19
20 package org.apache.xalan.xsltc.compiler;
21
22 import org.apache.bcel.generic.InstructionHandle;
23 import org.apache.bcel.generic.InstructionList;
24 import org.apache.xalan.xsltc.compiler.util.BooleanType;
25 import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
26 import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
27 import org.apache.xalan.xsltc.compiler.util.MethodGenerator;
28 import org.apache.xalan.xsltc.compiler.util.Type;
29 import org.apache.xalan.xsltc.compiler.util.TypeCheckError;
30 import org.apache.xalan.xsltc.compiler.util.Util;
31
32 /**
33  * @author Jacek Ambroziak
34  * @author Santiago Pericas-Geertsen
35  * @author Morten Jorgensen
36  */

37 final class If extends Instruction {
38
39     private Expression _test;
40     private boolean _ignore = false;
41
42     /**
43      * Display the contents of this element
44      */

45     public void display(int indent) {
46     indent(indent);
47     Util.println("If");
48     indent(indent + IndentIncrement);
49     System.out.print("test ");
50     Util.println(_test.toString());
51     displayContents(indent + IndentIncrement);
52     }
53
54     /**
55      * Parse the "test" expression and contents of this element.
56      */

57     public void parseContents(Parser parser) {
58     // Parse the "test" expression
59
_test = parser.parseExpression(this, "test", null);
60
61         // Make sure required attribute(s) have been set
62
if (_test.isDummy()) {
63         reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "test");
64         return;
65         }
66
67     // Ignore xsl:if when test is false (function-available() and
68
// element-available())
69
Object JavaDoc result = _test.evaluateAtCompileTime();
70     if (result != null && result instanceof Boolean JavaDoc) {
71         _ignore = !((Boolean JavaDoc) result).booleanValue();
72     }
73
74     parseChildren(parser);
75     }
76
77     /**
78      * Type-check the "test" expression and contents of this element.
79      * The contents will be ignored if we know the test will always fail.
80      */

81     public Type typeCheck(SymbolTable stable) throws TypeCheckError {
82     // Type-check the "test" expression
83
if (_test.typeCheck(stable) instanceof BooleanType == false) {
84         _test = new CastExpr(_test, Type.Boolean);
85     }
86     // Type check the element contents
87
if (!_ignore) {
88         typeCheckContents(stable);
89     }
90     return Type.Void;
91     }
92
93     /**
94      * Translate the "test" expression and contents of this element.
95      * The contents will be ignored if we know the test will always fail.
96      */

97     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
98     final InstructionList il = methodGen.getInstructionList();
99     _test.translateDesynthesized(classGen, methodGen);
100     // remember end of condition
101
final InstructionHandle truec = il.getEnd();
102     if (!_ignore) {
103         translateContents(classGen, methodGen);
104     }
105     _test.backPatchFalseList(il.append(NOP));
106     _test.backPatchTrueList(truec.getNext());
107     }
108 }
109
Popular Tags