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: When.java,v 1.2.4.1 2005/09/05 09:36:58 pvedula Exp $
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xalan.internal.xsltc.compiler;
286N/A
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 When extends Instruction {
286N/A
286N/A private Expression _test;
286N/A private boolean _ignore = false;
286N/A
286N/A public void display(int indent) {
286N/A indent(indent);
286N/A Util.println("When");
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 public Expression getTest() {
286N/A return _test;
286N/A }
286N/A
286N/A public boolean ignore() {
286N/A return(_ignore);
286N/A }
286N/A
286N/A public void parseContents(Parser parser) {
286N/A _test = parser.parseExpression(this, "test", null);
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 // 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 }
286N/A }
286N/A
286N/A /**
286N/A * Type-check this when element. The test should always be type checked,
286N/A * while we do not bother with the contents if we know the test fails.
286N/A * This is important in cases where the "test" expression tests for
286N/A * the support of a non-available element, and the <xsl:when> body contains
286N/A * this non-available element.
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 contents (if necessary)
286N/A if (!_ignore) {
286N/A typeCheckContents(stable);
286N/A }
286N/A
286N/A return Type.Void;
286N/A }
286N/A
286N/A /**
286N/A * This method should never be called. An Otherwise object will explicitly
286N/A * translate the "test" expression and and contents of this element.
286N/A */
286N/A public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
286N/A final ErrorMsg msg = new ErrorMsg(ErrorMsg.STRAY_WHEN_ERR, this);
286N/A getParser().reportError(Constants.ERROR, msg);
286N/A }
286N/A}