Class EnvFunction
-
- All Implemented Interfaces:
Expression
,Function
,FunctionExpression
,Factory
public class EnvFunction extends FunctionExpressionImpl
Provides local to thread and global thread-independent lookup tables of named variables allowing externally defined values to be access within a SLD document.Example: in the application, prior to rendering...
Then, in the SLD document we can refer to this variable using the "env" functionEnvFunction.setGlobalValue("foo", 42);
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.... <FeatureTypeStyle> <Rule> <Filter> <PropertyIsEqualTo> <PropertyName>FooValue</PropertyName> <Function name="env"> <literal>foo</literal> </Function> </PropertyIsEqualTo> </Filter> ...
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:
The same approach can be used programmatically...<!-- 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>
if the value for a key is null its possible to check it with isNull:// set argument to set a default return value of 0 FilterFactory ff = ... ff.function("env", ff.literal("foo"), ff.literal(0));
and within SLD:EnvFunction.setGlobalValue("foo", null); boolean isNull = ff.isNull(ff.function("env", ff.literal("foo"))).evaluate(null); ...
<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
Fields Modifier and Type Field Description static FunctionName
NAME
-
Fields inherited from class FunctionExpressionImpl
fallback, functionName, name, params
-
Fields inherited from interface Expression
NIL
-
-
Constructor Summary
Constructors Constructor Description EnvFunction()
Create a new instance of this function.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 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 Detail
-
NAME
public static FunctionName NAME
-
-
Method Detail
-
setLocalValues
public static void setLocalValues(Map<String,Object> values)
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
public static Map<String,Object> 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
public 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. 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
public static void setLocalValue(String name, Object value)
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
public static void removeLocalValue(String name)
Remove a named value from the local (to this thread) lookup table.- Parameters:
name
- the name to remove from local lookup table
-
setGlobalValue
public static void setGlobalValue(String name, Object value)
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
public static void removeGlobalValue(String name)
Remove a named value from the global (accessible from any thread) lookup table.- Parameters:
name
- the name to remove from global
-
evaluate
public Object evaluate(Object feature)
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
public void setParameters(List<Expression> params)
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
public void setFallbackValue(Literal fallback)
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
-
-