01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.template.instruction;
18:
19: import java.util.Stack;
20:
21: import org.apache.cocoon.components.expression.ExpressionContext;
22: import org.apache.cocoon.template.environment.ErrorHolder;
23: import org.apache.cocoon.template.environment.ExecutionContext;
24: import org.apache.cocoon.template.environment.ParsingContext;
25: import org.apache.cocoon.template.expression.JXTExpression;
26: import org.apache.cocoon.template.script.event.Event;
27: import org.apache.cocoon.template.script.event.StartElement;
28: import org.apache.cocoon.xml.XMLConsumer;
29: import org.xml.sax.Attributes;
30: import org.xml.sax.Locator;
31: import org.xml.sax.SAXException;
32: import org.xml.sax.SAXParseException;
33:
34: /**
35: * @version SVN $Id: If.java 449189 2006-09-23 06:52:29Z crossley $
36: */
37: public class If extends Instruction {
38: private final JXTExpression test;
39:
40: public If(ParsingContext parsingContext, StartElement raw,
41: Attributes attrs, Stack stack) throws SAXException {
42:
43: super (raw);
44:
45: Locator locator = getLocation();
46: String test = attrs.getValue("test");
47: if (test != null) {
48: this .test = parsingContext.getStringTemplateParser()
49: .compileExpr(test, "if: \"test\": ", locator);
50: // Why is test lenient?
51: this .test.setLenient(Boolean.TRUE);
52: } else {
53: throw new SAXParseException("if: \"test\" is required",
54: locator, null);
55: }
56: }
57:
58: public Event execute(final XMLConsumer consumer,
59: ExpressionContext expressionContext,
60: ExecutionContext executionContext,
61: MacroContext macroContext, Event startEvent, Event endEvent)
62: throws SAXException {
63:
64: Object val;
65: try {
66: val = this .test.getValue(expressionContext);
67: } catch (Exception e) {
68: throw new SAXParseException(e.getMessage(), getLocation(),
69: e);
70: } catch (Error err) {
71: throw new SAXParseException(err.getMessage(),
72: getLocation(), new ErrorHolder(err));
73: }
74: boolean result = false;
75: if (val instanceof Boolean) {
76: result = ((Boolean) val).booleanValue();
77: } else {
78: result = (val != null);
79: }
80: if (!result) {
81: return getEndInstruction().getNext();
82: }
83: return getNext();
84: }
85: }
|