KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > query > model > FunctionFactory


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.query.model;
17
18 // Note: the ParseException class is generated by JavaCC
19
import org.outerj.daisy.query.ParseException;
20 import org.outerj.daisy.query.model.functions.*;
21
22 import java.util.HashMap JavaDoc;
23
24 public class FunctionFactory {
25     private static final HashMap JavaDoc FUNCTIONS;
26     static {
27         FUNCTIONS = new HashMap JavaDoc();
28         // string functions
29
FUNCTIONS.put(LengthFunction.NAME, LengthFunction.class);
30         FUNCTIONS.put(ConcatFunction.NAME, ConcatFunction.class);
31         FUNCTIONS.put(LeftFunction.NAME, LeftFunction.class);
32         FUNCTIONS.put(RightFunction.NAME, RightFunction.class);
33         FUNCTIONS.put(SubstringFunction.NAME, SubstringFunction.class);
34         FUNCTIONS.put(UpperCaseFunction.NAME, UpperCaseFunction.class);
35         FUNCTIONS.put(LowerCaseFunction.NAME, LowerCaseFunction.class);
36
37         // date functions
38
FUNCTIONS.put(CurrentDateFunction.NAME, CurrentDateFunction.class);
39         FUNCTIONS.put(YearFunction.NAME, YearFunction.class);
40         FUNCTIONS.put(MonthFunction.NAME, MonthFunction.class);
41         FUNCTIONS.put(DayOfWeekFunction.NAME, DayOfWeekFunction.class);
42         FUNCTIONS.put(DayOfMonthFunction.NAME, DayOfMonthFunction.class);
43         FUNCTIONS.put(DayOfYearFunction.NAME, DayOfYearFunction.class);
44         FUNCTIONS.put(WeekFunction.NAME, WeekFunction.class);
45         FUNCTIONS.put(RelativeDateFunction.NAME, RelativeDateFunction.class);
46
47         // datetime functions
48
FUNCTIONS.put(CurrentDateTimeFunction.NAME, CurrentDateTimeFunction.class);
49         FUNCTIONS.put(RelativeDateTimeFunction.NAME, RelativeDateTimeFunction.class);
50
51         // numeric functions
52
FUNCTIONS.put(RandomFunction.NAME, RandomFunction.class);
53         FUNCTIONS.put(ModFunction.NAME, ModFunction.class);
54         FUNCTIONS.put(AbsFunction.NAME, AbsFunction.class);
55         FUNCTIONS.put(FloorFunction.NAME, FloorFunction.class);
56         FUNCTIONS.put(CeilingFunction.NAME, CeilingFunction.class);
57         FUNCTIONS.put(RoundFunction.NAME, RoundFunction.class);
58
59         // other
60
FUNCTIONS.put(ContextDocFunction.NAME, ContextDocFunction.class);
61         FUNCTIONS.put(UserIdFunction.NAME, UserIdFunction.class);
62     }
63
64     public static Function createFunction(String JavaDoc name) throws ParseException {
65         Class JavaDoc functionClass = (Class JavaDoc)FUNCTIONS.get(name);
66         if (functionClass == null)
67             throw new ParseException("No such function: " + name);
68         try {
69             return (Function)functionClass.newInstance();
70         } catch (Exception JavaDoc e) {
71             throw new ParseException("Error creating function " + name + ": " + e.toString());
72         }
73     }
74 }
75
Popular Tags