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