KickJava   Java API By Example, From Geeks To Geeks.

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


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.repository.Document;
19 import org.outerj.daisy.repository.Version;
20 import org.outerj.daisy.repository.query.QueryException;
21 import org.outerj.daisy.repository.query.EvaluationContext;
22 import org.outerj.daisy.query.QueryContext;
23 import org.outerj.daisy.query.model.*;
24
25 import java.util.Locale JavaDoc;
26
27 public class LengthFunction extends AbstractFunction {
28     public static final String JavaDoc NAME = "Length";
29
30     public String JavaDoc getFunctionName() {
31         return NAME;
32     }
33
34     public void prepare(QueryContext context) throws QueryException {
35         super.prepare(context);
36
37         if (params.size() != 1)
38             throw new QueryException("Function " + NAME + " expects exactly one parameter.");
39         ValueExpr param = getParam(0);
40         QValueType paramValueType = param.getValueType();
41         if ((paramValueType != QValueType.UNDEFINED && paramValueType != QValueType.STRING) || param.isSymbolicIdentifier() || param.isMultiValue())
42             throw new QueryException("Function " + NAME + " needs a string as parameter.");
43     }
44
45     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
46         String JavaDoc[] sqlFunction = context.getJdbcHelper().getStringLengthFunction();
47         sql.append(' ').append(sqlFunction[0]);
48         super.generateSqlValueExpr(sql, context);
49         sql.append(sqlFunction[1]).append(' ');
50     }
51
52     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
53         String JavaDoc value = (String JavaDoc)((ValueExpr)params.get(0)).evaluate(QValueType.STRING, evaluationContext);
54         return evaluate(value);
55     }
56
57     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
58         String JavaDoc value = (String JavaDoc)((ValueExpr)params.get(0)).evaluate(QValueType.STRING, document, version, evaluationContext);
59         return evaluate(value);
60     }
61
62     private Object JavaDoc evaluate(String JavaDoc value) {
63         if (value == null)
64             return null;
65         else
66             return new Long JavaDoc(value.length());
67     }
68
69     public QValueType getValueType() {
70         return QValueType.LONG;
71     }
72
73     public String JavaDoc getTitle(Locale JavaDoc locale) {
74         return getExpression();
75     }
76
77     public QValueType getOutputValueType() {
78         return QValueType.LONG;
79     }
80
81     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
82         return evaluate(null, document, version, evaluationContext);
83     }
84 }
85
Popular Tags