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, paramsFields inherited from interface Expression
NIL -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidClear all values from the global (accessible from any thread) lookup table.static voidClear 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 voidremoveGlobalValue(String name) Remove a named value from the global (accessible from any thread) lookup table.static voidremoveLocalValue(String name) Remove a named value from the local (to this thread) lookup table.voidsetFallbackValue(Literal fallback) Fallback value to use in the event the function is unavailable in the requested environment.static voidsetGlobalValue(String name, Object value) Add a named value to the global (accessible from any thread) lookup table.static voidsetGlobalValues(Map<String, Object> values) Set the table of global lookup values that is accessible from any thread, replacing the previously set table.static voidsetLocalValue(String name, Object value) Add a named value to the local (to this thread) lookup table.static voidsetLocalValues(Map<String, Object> values) Set the local (to this thread) table of lookup values, deleting any previously set table.voidsetParameters(List<Expression> params) Sets the function parameters.Methods inherited from class FunctionExpressionImpl
accept, equals, functionName, getExpression, getFallbackValue, getFunctionName, getImplementationHints, getName, getParameters, hashCode, toStringMethods inherited from class DefaultExpression
isAttributeExpression, isExpression, isFunctionExpression, isGeometryExpression, isLiteralExpression, isMathExpressionMethods inherited from class ExpressionAbstract
evaluateMethods 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 inputMapis copied.- Parameters:
values- the lookup table; ifnullthe 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 inputMapis copied.- Parameters:
values- the lookup table; ifnullthe 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:
evaluatein interfaceExpression- Overrides:
evaluatein classFunctionExpressionImpl- Returns:
- the variable value or
nullif 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:
setParametersin interfaceFunctionExpression- Overrides:
setParametersin 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:
setFallbackValuein interfaceFunctionExpression- Overrides:
setFallbackValuein classFunctionExpressionImpl
-