Class EnvFunction
- All Implemented Interfaces:
Expression
,Function
,FunctionExpression
,Factory
Example: in the application, prior to rendering...
EnvFunction.setGlobalValue("foo", 42);
Then, in the SLD document we can refer to this variable using the "env" function
...
<FeatureTypeStyle>
<Rule>
<Filter>
<PropertyIsEqualTo>
<PropertyName>FooValue</PropertyName>
<Function name="env">
<literal>foo</literal>
</Function>
</PropertyIsEqualTo>
</Filter>
...
The function provides a lookup table that is local to the active thread so that a given variable can hold different
values in different threads. There is also a global lookup table, accessible from all threads. When the function is
given a variable to look up it first searches the thread's local table and then, if the variable was not found, the
global table. All lookups are case-insensitive.
Setting a fallback value is not supported in accordance with SLD 1.1 specification. However, you can provide a default value when calling the function as in these examples:
<!-- Here, if variable foo is not set the function returns null -->
<Function name="env">
<Literal>foo</Literal>
</Function>
<!-- Here, a second argument is provided. If foo is not set the -->
<!-- function will return 0. -->
<Function name="env">
<Literal>foo</Literal>
<Literal>0</Literal>
</Function>
The same approach can be used programmatically...
// set argument to set a default return value of 0
FilterFactory ff = ...
ff.function("env", ff.literal("foo"), ff.literal(0));
if the value for a key is null its possible to check it with isNull:
EnvFunction.setGlobalValue("foo", null);
boolean isNull = ff.isNull(ff.function("env", ff.literal("foo"))).evaluate(null);
...
and within SLD:
<Filter> <PropertyIsNull> <Function name="env"> <literal>foo</literal> </Function> </PropertyIsNull> </Filter>To verify if a key is available use isNil:
// foo-not-set has never been set ..
boolean isNil = ff.isNil(ff.function("env", ff.literal("foo-not-set")), null).evaluate(null);
...
- Since:
- 2.6
- Author:
- Andrea Aime, Michael Bedward, Frank Gasdorf
-
Field Summary
FieldsFields inherited from class FunctionExpressionImpl
fallback, functionName, name, params
Fields inherited from interface Expression
NIL
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Clear all values from the global (accessible from any thread) lookup table.static void
Clear all values from the local (to this thread) lookup table.Subclass should override, default implementation returns null.Returns the local values as a read only mapstatic void
removeGlobalValue
(String name) Remove a named value from the global (accessible from any thread) lookup table.static void
removeLocalValue
(String name) Remove a named value from the local (to this thread) lookup table.void
setFallbackValue
(Literal fallback) Fallback value to use in the event the function is unavailable in the requested environment.static void
setGlobalValue
(String name, Object value) Add a named value to the global (accessible from any thread) lookup table.static void
setGlobalValues
(Map<String, Object> values) Set the table of global lookup values that is accessible from any thread, replacing the previously set table.static void
setLocalValue
(String name, Object value) Add a named value to the local (to this thread) lookup table.static void
setLocalValues
(Map<String, Object> values) Set the local (to this thread) table of lookup values, deleting any previously set table.void
setParameters
(List<Expression> params) Sets the function parameters.Methods inherited from class FunctionExpressionImpl
accept, equals, functionName, getExpression, getFallbackValue, getFunctionName, getImplementationHints, getName, getParameters, hashCode, toString
Methods inherited from class DefaultExpression
isAttributeExpression, isExpression, isFunctionExpression, isGeometryExpression, isLiteralExpression, isMathExpression
Methods inherited from class ExpressionAbstract
evaluate
Methods inherited from interface Expression
evaluate
-
Field Details
-
NAME
-
-
Constructor Details
-
EnvFunction
public EnvFunction()Create a new instance of this function.
-
-
Method Details
-
setLocalValues
Set the local (to this thread) table of lookup values, deleting any previously set table. The inputMap
is copied.- Parameters:
values
- the lookup table; ifnull
the existing lookup table will be cleared.
-
getLocalValues
Returns the local values as a read only map- Returns:
- A read only view of the local values
-
clearLocalValues
public static void clearLocalValues()Clear all values from the local (to this thread) lookup table. -
setGlobalValues
Set the table of global lookup values that is accessible from any thread, replacing the previously set table. The inputMap
is copied.- Parameters:
values
- the lookup table; ifnull
the existing lookup table will be cleared.
-
clearGlobalValues
public static void clearGlobalValues()Clear all values from the global (accessible from any thread) lookup table. -
setLocalValue
Add a named value to the local (to this thread) lookup table. If the name is already present in the table it will be assigned the new value.- Parameters:
name
- the namevalue
- the value
-
removeLocalValue
Remove a named value from the local (to this thread) lookup table.- Parameters:
name
- the name to remove from local lookup table
-
setGlobalValue
Add a named value to the global (accessible from any thread) lookup table. If the name is already present in the table it will be assigned the new value. to remove values from global lookup table please useremoveGlobalValue(String)
- Parameters:
name
- the namevalue
- the value, null is an allowed value
-
removeGlobalValue
Remove a named value from the global (accessible from any thread) lookup table.- Parameters:
name
- the name to remove from global
-
evaluate
Subclass should override, default implementation returns null. The variable name to search for is provided as the single argument to this function. The active thread's local lookup table is searched first. If the name is not found there the global table is searched.- Specified by:
evaluate
in interfaceExpression
- Overrides:
evaluate
in classFunctionExpressionImpl
- Returns:
- the variable value or
null
if the variable was not found
-
setParameters
Sets the function parameters. This method is overriden to allow for either a single parameter (variable name) or two parameters (variable name plus default value).- Specified by:
setParameters
in interfaceFunctionExpression
- Overrides:
setParameters
in classFunctionExpressionImpl
-
setFallbackValue
Fallback value to use in the event the function is unavailable in the requested environment.The fallback value is not provided as one of the arguments, as it is an advanced option used in style layer descriptor documents to facilitate interoperability. It allows a user to specify an SQL function, and provide a value to use when the documented is used with a WFS that does not support the provided function. This method is overriden to ignore the fallback value and log a warning message. If you want to set a default value it can be provided as a second argument when calling the function. See the class description for details.
- Specified by:
setFallbackValue
in interfaceFunctionExpression
- Overrides:
setFallbackValue
in classFunctionExpressionImpl
-