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...

    
     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
    • Constructor Detail

      • EnvFunction

        public EnvFunction()
        Create a new instance of this function.
    • 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 input Map is copied.
        Parameters:
        values - the lookup table; if null 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 input Map is copied.
        Parameters:
        values - the lookup table; if null 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 name
        value - 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 use removeGlobalValue(String)
        Parameters:
        name - the name
        value - 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 interface Expression
        Overrides:
        evaluate in class FunctionExpressionImpl
        Returns:
        the variable value or null if the variable was not found
      • 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 interface FunctionExpression
        Overrides:
        setFallbackValue in class FunctionExpressionImpl