public class EnvFunction extends FunctionExpressionImpl
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);
...
Modifier and Type | Field and Description |
---|---|
static FunctionName |
NAME |
fallback, functionName, name, params
NIL
Constructor and Description |
---|
EnvFunction()
Create a new instance of this function.
|
Modifier and Type | Method and Description |
---|---|
static void |
clearGlobalValues()
Clear all values from the global (accessible from any thread) lookup table.
|
static void |
clearLocalValues()
Clear all values from the local (to this thread) lookup table.
|
Object |
evaluate(Object feature)
Subclass should override, default implementation returns null.
|
static Map<String,Object> |
getLocalValues()
Returns the local values as a read only map
|
static 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.
|
accept, equals, functionName, getExpression, getFallbackValue, getFunctionName, getImplementationHints, getName, getParameters, hashCode, toString
isAttributeExpression, isExpression, isFunctionExpression, isGeometryExpression, isLiteralExpression, isMathExpression
evaluate
evaluate
public static FunctionName NAME
public static void setLocalValues(Map<String,Object> values)
Map
is copied.values
- the lookup table; if null
the existing lookup table will be cleared.public static Map<String,Object> getLocalValues()
public static void clearLocalValues()
public static void setGlobalValues(Map<String,Object> values)
Map
is copied.values
- the lookup table; if null
the existing lookup table will be cleared.public static void clearGlobalValues()
public static void setLocalValue(String name, Object value)
name
- the namevalue
- the valuepublic static void removeLocalValue(String name)
name
- the name to remove from local lookup tablepublic static void setGlobalValue(String name, Object value)
removeGlobalValue(String)
name
- the namevalue
- the value, null is an allowed valuepublic static void removeGlobalValue(String name)
name
- the name to remove from globalpublic Object evaluate(Object feature)
evaluate
in interface Expression
evaluate
in class FunctionExpressionImpl
null
if the variable was not foundpublic void setParameters(List<Expression> params)
setParameters
in interface FunctionExpression
setParameters
in class FunctionExpressionImpl
public void setFallbackValue(Literal fallback)
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.
setFallbackValue
in interface FunctionExpression
setFallbackValue
in class FunctionExpressionImpl
Copyright © 1996–2021 Geotools. All rights reserved.