01: /*
02: * Copyright 2007 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.repository.serverimpl.query;
17:
18: import java.util.Date;
19: import java.util.Locale;
20:
21: import org.outerj.daisy.query.model.QValueType;
22: import org.outerj.daisy.query.model.ValueExpr;
23: import org.outerj.daisy.repository.HierarchyPath;
24: import org.outerj.daisy.repository.VariantKey;
25: import org.outerj.daisy.repository.query.QueryHelper;
26:
27: public class QueryFormatter implements Formatter {
28:
29: final Locale locale;
30:
31: public QueryFormatter(Locale locale) {
32: this .locale = locale;
33: }
34:
35: public String format(ValueExpr valueExpr, Object value) {
36: String returnValue;
37: QValueType outputValueType = valueExpr.getOutputValueType();
38:
39: if (value instanceof HierarchyPath) {
40: HierarchyPath path = (HierarchyPath) value;
41: StringBuilder result = new StringBuilder("matchesPath('");
42: for (int i = 0; i < path.getElements().length; i++) {
43: result.append("/");
44: String elementValue = format(valueExpr, path
45: .getElements()[i]);
46: result.append(elementValue.replaceAll("'", ""));
47: }
48: result.append("')");
49: returnValue = result.toString();
50: } else {
51:
52: if (outputValueType == QValueType.STRING) {
53: returnValue = QueryHelper
54: .formatString(value.toString());
55: } else if (outputValueType == QValueType.LONG
56: || outputValueType == QValueType.DOUBLE
57: || outputValueType == QValueType.DECIMAL) {
58: returnValue = value.toString();
59: } else if (outputValueType == QValueType.DATE) {
60: returnValue = QueryHelper.formatDate((Date) value);
61: } else if (outputValueType == QValueType.DATETIME) {
62: returnValue = QueryHelper.formatDate((Date) value);
63: } else if (outputValueType == QValueType.LINK) {
64: VariantKey key = (VariantKey) value;
65: returnValue = "'daisy:" + key.getDocumentId() + '@'
66: + key.getBranchId() + ':' + key.getLanguageId()
67: + '\'';
68: } else {
69: returnValue = "'" + value.toString() + "'";
70: }
71: }
72:
73: return returnValue;
74: }
75:
76: public Locale getLocale() {
77: return locale;
78: }
79:
80: }
|