KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > expression > DefaultExpressionManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.forms.expression;
18
19 import org.apache.avalon.framework.component.Component;
20 import org.apache.avalon.framework.configuration.Configurable;
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24
25 import org.outerj.expression.DefaultFunctionFactory;
26 import org.outerj.expression.Expression;
27 import org.outerj.expression.ExpressionException;
28 import org.outerj.expression.FormulaParser;
29 import org.outerj.expression.ParseException;
30
31 /**
32  * Implementation of the {@link ExpressionManager} role.
33  *
34  * Custom functions can be added using configuration elements:
35  * <pre>
36  * &lt;function name="MyFunction" class="net.foo.MyFunction"/&gt;
37  * </pre>
38  *
39  * @version $Id: DefaultExpressionManager.java 289538 2005-09-16 13:46:22Z sylvain $
40  */

41 public class DefaultExpressionManager
42         implements ExpressionManager, Component, Configurable, ThreadSafe {
43 // FIXME: Component is there to allow this block to also run in the 2.1 branch
44

45     private DefaultFunctionFactory factory;
46     
47     public void configure(Configuration config) throws ConfigurationException {
48         factory = new DefaultFunctionFactory();
49         
50         Configuration[] functions = config.getChildren("function");
51         for (int i = 0; i < functions.length; i++) {
52             String JavaDoc name = functions[i].getAttribute("name");
53             String JavaDoc clazz = functions[i].getAttribute("class");
54             try {
55                 factory.registerFunction(name, Class.forName(clazz));
56             } catch (ClassNotFoundException JavaDoc e) {
57                 throw new ConfigurationException("Can not find class " + clazz + " for function " + name + ": " + e);
58             }
59         }
60     }
61     
62     public Expression parse(String JavaDoc expressionString) throws ParseException, ExpressionException {
63         
64         FormulaParser parser = new FormulaParser(new java.io.StringReader JavaDoc(expressionString), factory);
65         parser.sum();
66
67         Expression expression = parser.getExpression();
68         expression.check();
69
70         return expression;
71     }
72 }
73
Popular Tags