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.Invoker;
27: import org.apache.cocoon.template.script.event.Event;
28: import org.apache.cocoon.template.script.event.StartElement;
29: import org.apache.cocoon.xml.XMLConsumer;
30: import org.xml.sax.Attributes;
31: import org.xml.sax.SAXException;
32: import org.xml.sax.SAXParseException;
33:
34: /**
35: * @version SVN $Id: Eval.java 449189 2006-09-23 06:52:29Z crossley $
36: */
37: public class Eval extends Instruction {
38: private final JXTExpression value;
39:
40: public Eval(ParsingContext parsingContext, StartElement raw,
41: Attributes attrs, Stack stack) throws SAXException {
42:
43: super (raw);
44:
45: String select = attrs.getValue("select");
46: this .value = parsingContext
47: .getStringTemplateParser()
48: .compileExpr(select, "eval: \"select\":", getLocation());
49: }
50:
51: public Event execute(final XMLConsumer consumer,
52: ExpressionContext expressionContext,
53: ExecutionContext executionContext,
54: MacroContext macroContext, Event startEvent, Event endEvent)
55: throws SAXException {
56: try {
57: Object val = this .value.getNode(expressionContext);
58: if (!(val instanceof StartElement)) {
59: throw new Exception(
60: "macro invocation required instead of: " + val);
61: }
62: StartElement call = (StartElement) val;
63:
64: //FIXME: eval does not allow to call macro providing macro body
65: MacroContext newMacroContext = new MacroContext(call
66: .getQname(), null, null);
67: Invoker.execute(consumer, expressionContext,
68: executionContext, newMacroContext, call.getNext(),
69: call.getEndElement());
70: } catch (Exception exc) {
71: throw new SAXParseException(exc.getMessage(),
72: getLocation(), exc);
73: } catch (Error err) {
74: throw new SAXParseException(err.getMessage(),
75: getLocation(), new ErrorHolder(err));
76: }
77: return getEndInstruction().getNext();
78: }
79: }
|