286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: If.java,v 1.2.4.1 2005/09/01 15:39:47 pvedula Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler;
286N/A
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionHandle;
286N/Aimport com.sun.org.apache.bcel.internal.generic.InstructionList;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
286N/Aimport com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
286N/A
286N/A/**
286N/A * @author Jacek Ambroziak
286N/A * @author Santiago Pericas-Geertsen
286N/A * @author Morten Jorgensen
286N/A */
286N/Afinal class If extends Instruction {
286N/A
286N/A private Expression _test;
286N/A private boolean _ignore = false;
286N/A
286N/A /**
286N/A * Display the contents of this element
286N/A */
286N/A public void display(int indent) {
286N/A indent(indent);
286N/A Util.println("If");
286N/A indent(indent + IndentIncrement);
286N/A System.out.print("test ");
286N/A Util.println(_test.toString());
286N/A displayContents(indent + IndentIncrement);
286N/A }
286N/A
286N/A /**
286N/A * Parse the "test" expression and contents of this element.
286N/A */
286N/A public void parseContents(Parser parser) {
286N/A // Parse the "test" expression
286N/A _test = parser.parseExpression(this, "test", null);
286N/A
286N/A // Make sure required attribute(s) have been set
286N/A if (_test.isDummy()) {
286N/A reportError(this, parser, ErrorMsg.REQUIRED_ATTR_ERR, "test");
286N/A return;
286N/A }
286N/A
286N/A // Ignore xsl:if when test is false (function-available() and
286N/A // element-available())
286N/A Object result = _test.evaluateAtCompileTime();
286N/A if (result != null && result instanceof Boolean) {
286N/A _ignore = !((Boolean) result).booleanValue();
286N/A }
286N/A
286N/A parseChildren(parser);
286N/A }
286N/A
286N/A /**
286N/A * Type-check the "test" expression and contents of this element.
286N/A * The contents will be ignored if we know the test will always fail.
286N/A */
286N/A public Type typeCheck(SymbolTable stable) throws TypeCheckError {
286N/A // Type-check the "test" expression
286N/A if (_test.typeCheck(stable) instanceof BooleanType == false) {
286N/A _test = new CastExpr(_test, Type.Boolean);
286N/A }
286N/A // Type check the element contents
286N/A if (!_ignore) {
286N/A typeCheckContents(stable);
286N/A }
286N/A return Type.Void;
286N/A }
286N/A
286N/A /**
286N/A * Translate the "test" expression and contents of this element.
286N/A * The contents will be ignored if we know the test will always fail.
286N/A */
286N/A public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
286N/A final InstructionList il = methodGen.getInstructionList();
286N/A _test.translateDesynthesized(classGen, methodGen);
286N/A // remember end of condition
286N/A final InstructionHandle truec = il.getEnd();
286N/A if (!_ignore) {
286N/A translateContents(classGen, methodGen);
286N/A }
286N/A _test.backPatchFalseList(il.append(NOP));
286N/A _test.backPatchTrueList(truec.getNext());
286N/A }
286N/A}