KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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 CVS $Id: DefaultExpressionManager.java 30932 2004-07-29 17:35:38Z vgritsenko $
40  */

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