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:
18: package org.apache.cocoon.forms.expression;
19:
20: import java.util.List;
21:
22: import org.apache.avalon.framework.component.Component;
23: import org.apache.avalon.framework.configuration.Configurable;
24: import org.apache.avalon.framework.configuration.Configuration;
25: import org.apache.avalon.framework.configuration.ConfigurationException;
26: import org.apache.avalon.framework.thread.ThreadSafe;
27: import org.outerj.expression.DefaultFunctionFactory;
28: import org.outerj.expression.Expression;
29: import org.outerj.expression.ExpressionException;
30: import org.outerj.expression.FormulaParser;
31: import org.outerj.expression.ParseException;
32:
33: /**
34: * Implementation of the {@link ExpressionManager} role.
35: *
36: * Custom functions can be added using configuration elements:
37: * <pre>
38: * <function name="MyFunction" class="net.foo.MyFunction"/>
39: * </pre>
40: *
41: * @version $Id: DefaultExpressionManager.java 449149 2006-09-23 03:58:05Z crossley $
42: */
43: public class DefaultExpressionManager implements ExpressionManager,
44: Component, Configurable, ThreadSafe {
45: // FIXME: Component is there to allow this block to also run in the 2.1 branch
46:
47: private DefaultFunctionFactory factory;
48:
49: public void configure(Configuration config)
50: throws ConfigurationException {
51: factory = new DefaultFunctionFactory();
52:
53: Configuration[] functions = config.getChildren("function");
54: for (int i = 0; i < functions.length; i++) {
55: String name = functions[i].getAttribute("name");
56: String clazz = functions[i].getAttribute("class");
57: try {
58: factory.registerFunction(name, Class.forName(clazz));
59: } catch (ClassNotFoundException e) {
60: throw new ConfigurationException("Can not find class "
61: + clazz + " for function " + name + ": " + e);
62: }
63: }
64: }
65:
66: public Expression parse(String expressionString)
67: throws ParseException, ExpressionException {
68: FormulaParser parser = new FormulaParser(
69: new java.io.StringReader(expressionString), factory);
70: parser.parse();
71:
72: Expression expression = parser.getExpression();
73: expression.check();
74:
75: return expression;
76: }
77:
78: public List parseVariables(String expressionString)
79: throws ParseException, ExpressionException {
80: FormulaParser parser = new FormulaParser(
81: new java.io.StringReader(expressionString), factory);
82: parser.parse();
83: return parser.getVariables();
84: }
85:
86: }
|