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.template.environment.ParsingContext;
22: import org.apache.cocoon.template.expression.JXTExpression;
23: import org.apache.cocoon.template.script.event.StartElement;
24: import org.xml.sax.Attributes;
25: import org.xml.sax.Locator;
26: import org.xml.sax.SAXException;
27: import org.xml.sax.SAXParseException;
28:
29: /**
30: * @version SVN $Id: When.java 449189 2006-09-23 06:52:29Z crossley $
31: */
32: public class When extends Instruction {
33: private final JXTExpression test;
34: private When nextChoice;
35:
36: public When(ParsingContext parsingContext, StartElement raw,
37: Attributes attrs, Stack stack) throws SAXException {
38:
39: super (raw);
40:
41: Locator locator = getLocation();
42:
43: if (stack.size() == 0 || !(stack.peek() instanceof Choose)) {
44: throw new SAXParseException(
45: "<when> must be within <choose>", locator, null);
46: }
47: String test = attrs.getValue("test");
48: if (test != null) {
49: this .test = parsingContext.getStringTemplateParser()
50: .compileExpr(test, "when: \"test\": ", locator);
51: // Why is test lenient?
52: this .test.setLenient(Boolean.TRUE);
53:
54: Choose startChoose = (Choose) stack.peek();
55: if (startChoose.getFirstChoice() != null) {
56: When w = startChoose.getFirstChoice();
57: while (w.getNextChoice() != null) {
58: w = w.getNextChoice();
59: }
60: w.setNextChoice(this );
61: } else {
62: startChoose.setFirstChoice(this );
63: }
64: } else {
65: throw new SAXParseException("when: \"test\" is required",
66: locator, null);
67: }
68: }
69:
70: public JXTExpression getTest() {
71: return test;
72: }
73:
74: public void setNextChoice(When nextChoice) {
75: this .nextChoice = nextChoice;
76: }
77:
78: public When getNextChoice() {
79: return nextChoice;
80: }
81: }
|