1+ package org .geoscript .js .filter ;
2+
3+ import org .geoscript .js .GeoObject ;
4+ import org .geotools .factory .CommonFactoryFinder ;
5+ import org .geotools .filter .text .cql2 .CQL ;
6+ import org .geotools .filter .text .cql2 .CQLException ;
7+ import org .geotools .filter .text .ecql .ECQL ;
8+ import org .mozilla .javascript .*;
9+ import org .mozilla .javascript .annotations .JSConstructor ;
10+ import org .mozilla .javascript .annotations .JSGetter ;
11+ import org .mozilla .javascript .annotations .JSSetter ;
12+ import org .mozilla .javascript .annotations .JSStaticFunction ;
13+ import org .opengis .filter .FilterFactory ;
14+ import org .opengis .filter .expression .Literal ;
15+
16+ public class Expression extends GeoObject implements Wrapper {
17+
18+ /**
19+ * serialVersionUID
20+ */
21+ private static final long serialVersionUID = 4426338466323185386L ;
22+
23+ /**
24+ * The GeoTools Expression
25+ */
26+ private org .opengis .filter .expression .Expression expression ;
27+
28+ private static FilterFactory filterFactory = CommonFactoryFinder .getFilterFactory (null );
29+
30+ /**
31+ * The prototype constructor
32+ */
33+ public Expression () {
34+ }
35+
36+ public Expression (org .opengis .filter .expression .Expression expression ) {
37+ this .expression = expression ;
38+ }
39+
40+ public Expression (Object value ) {
41+ this (filterFactory .literal (value ));
42+ }
43+
44+ public Expression (Scriptable scope , org .opengis .filter .expression .Expression expression ) {
45+ this (expression );
46+ setParentScope (scope );
47+ this .setPrototype (Module .getClassPrototype (Expression .class ));
48+ }
49+
50+ public Expression (Scriptable scope , Object value ) {
51+ this (scope , filterFactory .literal (value ));
52+ }
53+
54+ @ JSConstructor
55+ public static Object constructor (Context cx , Object [] args , Function ctorObj , boolean inNewExpr ) {
56+ if (args .length == 0 ) {
57+ return new Expression ();
58+ }
59+ Expression expression = null ;
60+ Object arg = args [0 ];
61+ Object value = null ;
62+ if (arg instanceof String || arg instanceof Number ) {
63+ value = arg ;
64+ } else if (arg instanceof NativeObject ) {
65+ NativeObject config = (NativeObject ) arg ;
66+ value = config .get ("text" , config );
67+ } else {
68+ throw ScriptRuntime .constructError ("Error" , "Cannot create Expression from provided value: " + Context .toString (ctorObj ));
69+ }
70+ if (inNewExpr ) {
71+ expression = new Expression (toExpression (value .toString ()));
72+ } else {
73+ expression = new Expression (ctorObj .getParentScope (), toExpression (value .toString ()));
74+ }
75+ return expression ;
76+ }
77+
78+ @ JSGetter
79+ public String getText () {
80+ String text ;
81+ if (this .getLiteral ()) {
82+ Object value = ((Literal ) this .expression ).getValue ();
83+ if (value instanceof String ) {
84+ text = "'" + value + "'" ;
85+ } else {
86+ text = value .toString ();
87+ }
88+ } else {
89+ text = this .expression .toString ();
90+ }
91+ return text ;
92+ }
93+
94+ @ JSSetter
95+ public void setText (String text ) {
96+ this .expression = toExpression (text );
97+ }
98+
99+ private static org .opengis .filter .expression .Expression toExpression (String text ) {
100+ try {
101+ return ECQL .toExpression (text );
102+ } catch (CQLException ex1 ) {
103+ try {
104+ return CQL .toExpression (text );
105+ } catch (CQLException ex2 ) {
106+ throw ScriptRuntime .constructError ("Error" ,
107+ "Cannot parse the following text with " +
108+ "CQL (" + ex2 .getMessage () + ") or ECQL (" + ex1 .getMessage () + ")!" );
109+ }
110+ }
111+ }
112+
113+ // TODO: Remove once Style API has been converted
114+ @ JSGetter ("_expression" )
115+ public Object getExpression () {
116+ return this .expression ;
117+ }
118+
119+ @ JSGetter
120+ public boolean getLiteral () {
121+ return this .expression instanceof Literal ;
122+ }
123+
124+ @ JSGetter
125+ public Scriptable getConfig () {
126+ Scriptable config = super .getConfig ();
127+ config .put ("type" , config , "Expression" );
128+ config .put ("text" , config , getText ());
129+ return config ;
130+ }
131+
132+ @ Override
133+ public String toFullString () {
134+ return getText ();
135+ }
136+
137+ @ Override
138+ public String getClassName () {
139+ return getClass ().getName ();
140+ }
141+
142+ @ Override
143+ public org .opengis .filter .expression .Expression unwrap () {
144+ return this .expression ;
145+ }
146+
147+ @ JSStaticFunction ("from_" )
148+ public static Expression from (Scriptable scriptable ) {
149+ org .opengis .filter .expression .Expression expr = null ;
150+ if (scriptable instanceof Wrapper ) {
151+ Object obj = ((Wrapper ) scriptable ).unwrap ();
152+ if (obj instanceof org .opengis .filter .expression .Expression ) {
153+ expr = (org .opengis .filter .expression .Expression ) obj ;
154+ }
155+ }
156+ if (expr == null ) {
157+ throw ScriptRuntime .constructError ("Error" , "Cannot create expression from " + Context .toString (scriptable ));
158+ }
159+ return new Expression (getTopLevelScope (scriptable ), expr );
160+ }
161+
162+ @ JSStaticFunction
163+ public static Expression literal (Scriptable valueObj ) {
164+ Object value = valueObj .getDefaultValue (null );
165+ return new Expression (getTopLevelScope (valueObj ), value );
166+ }
167+ }
0 commit comments