KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class LeftFunction extends AbstractFunction {
31     public static final String JavaDoc NAME = "Left";
32
33     public String JavaDoc getFunctionName() {
34         return NAME;
35     }
36
37     public void prepare(QueryContext context) throws QueryException {
38         super.prepare(context);
39
40         if (params.size() != 2)
41             throw new QueryException("Function " + getFunctionName() + " expects exactly two parameters.");
42         ValueExpr stringParam = getParam(0);
43         QValueType paramValueType = stringParam.getValueType();
44         if ((paramValueType != QValueType.UNDEFINED && paramValueType != QValueType.STRING) || stringParam.isSymbolicIdentifier() || stringParam.isMultiValue())
45             throw new QueryException("Function " + getFunctionName() + " needs a string as first parameter.");
46
47         ValueExpr lengthParam = getParam(1);
48         paramValueType = lengthParam.getValueType();
49         if ((paramValueType != QValueType.UNDEFINED && paramValueType != QValueType.LONG) || lengthParam.isSymbolicIdentifier() || lengthParam.isMultiValue())
50             throw new QueryException("Function " + getFunctionName() + " needs an integer number as second parameter.");
51     }
52
53     public void generateSqlValueExpr(StringBuffer JavaDoc sql, SqlGenerationContext context) throws QueryException {
54         String JavaDoc sqlFunction = getFunctionName(context);
55         sql.append(' ').append(sqlFunction).append('(');
56         super.generateSqlValueExpr(sql, context);
57         sql.append(") ");
58     }
59
60     protected String JavaDoc getFunctionName(SqlGenerationContext context) {
61         return context.getJdbcHelper().getStringLeftFunction();
62     }
63
64     public Object JavaDoc evaluate(QValueType valueType, EvaluationContext evaluationContext) throws QueryException {
65         String JavaDoc value = (String JavaDoc)((ValueExpr)params.get(0)).evaluate(QValueType.STRING, evaluationContext);
66         int length = ((Long JavaDoc)((ValueExpr)params.get(1)).evaluate(QValueType.LONG, evaluationContext)).intValue();
67         return evaluate(value, length);
68     }
69
70     public Object JavaDoc evaluate(QValueType valueType, Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
71         String JavaDoc value = (String JavaDoc)((ValueExpr)params.get(0)).evaluate(QValueType.STRING, document, version, evaluationContext);
72         int length = ((Long JavaDoc)((ValueExpr)params.get(1)).evaluate(QValueType.LONG, document, version, evaluationContext)).intValue();
73         return evaluate(value, length);
74     }
75
76     protected Object JavaDoc evaluate(String JavaDoc value, int length) throws QueryException {
77         if (value == null)
78             return null;
79         else if (length < 0)
80             throw new QueryException("Length parameter of " + getFunctionName() + " function cannot be negative.");
81         else if (length > value.length())
82             return value;
83         else
84             return value.substring(0, length);
85     }
86
87     public QValueType getValueType() {
88         return QValueType.STRING;
89     }
90
91     public String JavaDoc getTitle(Locale JavaDoc locale) {
92         return getExpression();
93     }
94
95     public QValueType getOutputValueType() {
96         return QValueType.STRING;
97     }
98
99     public Object JavaDoc getOutputValue(Document document, Version version, EvaluationContext evaluationContext) throws QueryException {
100         return evaluate(null, document, version, evaluationContext);
101     }
102 }
103
Popular Tags