svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / expressionbuilder / formatters / $$Constant.java @ 46010
History | View | Annotate | Download (3.67 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters; |
25 |
|
26 |
import java.util.Objects; |
27 |
import org.apache.commons.lang3.StringUtils; |
28 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
29 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_$$CONSTANT; |
30 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
31 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
32 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
33 |
import org.gvsig.expressionevaluator.Formatter; |
34 |
import org.gvsig.expressionevaluator.SymbolTable; |
35 |
import org.gvsig.fmap.dal.SQLBuilder; |
36 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
37 |
|
38 |
/**
|
39 |
*
|
40 |
* @author jjdelcerro
|
41 |
*/
|
42 |
public class $$Constant implements Formatter<Value> { |
43 |
|
44 |
private final SQLBuilder sqlbuilder; |
45 |
private final Formatter<Value> formatter; |
46 |
|
47 |
public $$Constant(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
48 |
this.sqlbuilder = sqlbuilder;
|
49 |
this.formatter = formatter;
|
50 |
} |
51 |
|
52 |
@Override
|
53 |
public boolean canApply(ExpressionBuilder.Value value) { |
54 |
if (value instanceof ExpressionBuilder.Function) { |
55 |
return StringUtils.equalsIgnoreCase(FUNCTION_$$CONSTANT, ((Function) value).name()); |
56 |
} |
57 |
return false; |
58 |
} |
59 |
|
60 |
@Override
|
61 |
public String format(Value value) { |
62 |
return resolve(this.sqlbuilder.expression(), this.formatter, value); |
63 |
} |
64 |
|
65 |
public static boolean isConstant(Value value) { |
66 |
if( value instanceof ExpressionBuilder.Constant) { |
67 |
return true; |
68 |
} |
69 |
return value instanceof ExpressionBuilder.Function && |
70 |
StringUtils.equalsIgnoreCase(((ExpressionBuilder.Function)value).name(),FUNCTION_$$CONSTANT); |
71 |
} |
72 |
|
73 |
public static String resolve(Value value) { |
74 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
75 |
return resolve(builder, null, value); |
76 |
} |
77 |
|
78 |
public static String resolve(ExpressionBuilder builder, Formatter<Value> formatter, Value value) { |
79 |
if( value instanceof ExpressionBuilder.Constant) { |
80 |
Object x = ((ExpressionBuilder.Constant)value).value();
|
81 |
return builder.constant(x).toString(formatter);
|
82 |
} else if( value instanceof ExpressionBuilder.Function && |
83 |
StringUtils.equalsIgnoreCase(((ExpressionBuilder.Function)value).name(),FUNCTION_$$CONSTANT) ) { |
84 |
SymbolTable symbolTable = (SymbolTable) value.getProperty(PROP_SYMBOLTABLE); |
85 |
Object x = ExpressionUtils.evaluate(symbolTable, value.toString());
|
86 |
return builder.constant(x).toString(formatter);
|
87 |
} |
88 |
throw new RuntimeException("Expected constant value or $$CONSTANT function ("+Objects.toString(value)+")."); |
89 |
} |
90 |
} |