KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > query > model > functions > AbstractCaseFunction


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.functions;
17
18 import org.outerj.daisy.query.model.AbstractFunction;
19 import org.outerj.daisy.query.model.ValueExpr;
20 import org.outerj.daisy.query.model.QValueType;
21 import org.outerj.daisy.query.model.SqlGenerationContext;
22 import org.outerj.daisy.query.QueryContext;
23 import org.outerj.daisy.repository.query.QueryException;
24 import org.outerj.daisy.repository.query.EvaluationContext;
25 import org.outerj.daisy.repository.Document;
26 import org.outerj.daisy.repository.Version;
27
28 import java.util.Locale JavaDoc;
29
30 /**
31  * Base class for upper and lower case functions
32  */

33 public abstract class AbstractCaseFunction extends AbstractFunction {
34
35     public void prepare(QueryContext context) throws QueryException {
36         super.prepare(context);
37
38         if (params.size() != 1)
39             throw new QueryException("Function " + getFunctionName() + " expects exactly one parameter.");
40
41         ValueExpr param = getParam(0);
42         QValueType paramValueType = param.getValueType();
43         if ((paramValueType != QValueType.UNDEFINED && paramValueType != QValueType.STRING) || param.isSymbolicIdentifier() || param.isMultiValue())
44             throw new QueryException("Function " + getFunctionName() + " needs a string as parameter.");
45     }
46
47     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
48         sql.append(' ').append(getSqlFunctionName(context)).append("( ");
49         super.generateSqlValueExpr(sql, context);
50         sql.append(") ");
51     }
52
53     protected abstract String JavaDoc getSqlFunctionName(SqlGenerationContext context);
54
55     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
56         String JavaDoc value = (String JavaDoc)((ValueExpr)params.get(0)).evaluate(QValueType.STRING, evaluationContext);
57         return evaluate(value);
58     }
59
60     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
61         String JavaDoc value = (String JavaDoc)((ValueExpr)params.get(0)).evaluate(QValueType.STRING, document, version, evaluationContext);
62         return evaluate(value);
63     }
64
65     private Object JavaDoc evaluate(String JavaDoc value) {
66         if (value == null)
67             return null;
68         else
69             return applyCase(value);
70     }
71
72     protected abstract String JavaDoc applyCase(String JavaDoc value);
73
74     public QValueType getValueType() {
75         return QValueType.STRING;
76     }
77
78     public String JavaDoc getTitle(Locale JavaDoc locale) {
79         return getExpression();
80     }
81
82     public QValueType getOutputValueType() {
83         return QValueType.STRING;
84     }
85
86     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
87         return evaluate(null, document, version, evaluationContext);
88     }
89 }
90
91
92
Popular Tags