Class ExprLookup

  • All Implemented Interfaces:
    Lookup

    public class ExprLookup
    extends java.lang.Object
    implements Lookup
    Lookup that allows expressions to be evaluated.
     ExprLookup.Variables vars = new ExprLookup.Variables();
     vars.add(new ExprLookup.Variable("String", org.apache.commons.lang.StringUtils.class));
     vars.add(new ExprLookup.Variable("Util", new Utility("Hello")));
     vars.add(new ExprLookup.Variable("System", "Class:java.lang.System"));
     XMLConfiguration config = new XMLConfiguration(TEST_FILE);
     config.setLogger(log);
     ExprLookup lookup = new ExprLookup(vars);
     lookup.setConfiguration(config);
     String str = lookup.lookup("'$[element] ' + String.trimToEmpty('$[space.description]')");
     
    In the example above TEST_FILE contains xml that looks like:
     <configuration>
       <element>value</element>
       <space xml:space="preserve">
         <description xml:space="default">     Some text      </description>
       </space>
     </configuration>
     
    The result will be "value Some text". This lookup uses Apache Commons Jexl and requires that the dependency be added to any projects which use this.
    Since:
    1.7
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ExprLookup.Variable
      The key and corresponding object that will be made available to the JexlContext for use in expressions.
      static class  ExprLookup.Variables
      List wrapper used to allow the Variables list to be created as beans in DefaultConfigurationBuilder.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String CLASS
      Prefix to identify a Java Class object
      private static java.lang.String DEFAULT_PREFIX
      The default prefix for subordinate lookup expressions
      private static java.lang.String DEFAULT_SUFFIX
      The default suffix for subordinate lookup expressions
      private org.apache.commons.jexl3.JexlEngine engine
      The engine.
      private ConfigurationInterpolator interpolator
      The ConfigurationInterpolator used by this object.
      private ConfigurationLogger logger
      The logger used by this instance.
      private java.lang.String prefixMatcher
      The String to use to start subordinate lookup expressions
      private org.apache.commons.text.StringSubstitutor substitutor
      The StringSubstitutor for performing replace operations.
      private java.lang.String suffixMatcher
      The String to use to terminate subordinate lookup expressions
      private ExprLookup.Variables variables
      The variables maintained by this object.
    • Field Detail

      • CLASS

        private static final java.lang.String CLASS
        Prefix to identify a Java Class object
        See Also:
        Constant Field Values
      • DEFAULT_PREFIX

        private static final java.lang.String DEFAULT_PREFIX
        The default prefix for subordinate lookup expressions
        See Also:
        Constant Field Values
      • DEFAULT_SUFFIX

        private static final java.lang.String DEFAULT_SUFFIX
        The default suffix for subordinate lookup expressions
        See Also:
        Constant Field Values
      • substitutor

        private org.apache.commons.text.StringSubstitutor substitutor
        The StringSubstitutor for performing replace operations.
      • engine

        private final org.apache.commons.jexl3.JexlEngine engine
        The engine.
      • prefixMatcher

        private java.lang.String prefixMatcher
        The String to use to start subordinate lookup expressions
      • suffixMatcher

        private java.lang.String suffixMatcher
        The String to use to terminate subordinate lookup expressions
    • Constructor Detail

      • ExprLookup

        public ExprLookup()
        The default constructor. Will get used when the Lookup is constructed via configuration.
      • ExprLookup

        public ExprLookup​(ExprLookup.Variables list)
        Constructor for use by applications.
        Parameters:
        list - The list of objects to be accessible in expressions.
      • ExprLookup

        public ExprLookup​(ExprLookup.Variables list,
                          java.lang.String prefix,
                          java.lang.String suffix)
        Constructor for use by applications.
        Parameters:
        list - The list of objects to be accessible in expressions.
        prefix - The prefix to use for subordinate lookups.
        suffix - The suffix to use for subordinate lookups.
    • Method Detail

      • createContext

        private org.apache.commons.jexl3.JexlContext createContext()
        Creates a new JexlContext and initializes it with the variables managed by this Lookup object.
        Returns:
        the newly created context
      • getInterpolator

        public ConfigurationInterpolator getInterpolator()
        Gets the ConfigurationInterpolator used by this object.
        Returns:
        the ConfigurationInterpolator
        Since:
        2.0
      • getLogger

        public ConfigurationLogger getLogger()
        Gets the logger used by this object.
        Returns:
        the Log
        Since:
        2.0
      • getVariables

        public ExprLookup.Variables getVariables()
        Gets the list of Variables that are accessible within expressions. This method returns a copy of the variables managed by this lookup; so modifying this object has no impact on this lookup.
        Returns:
        the List of Variables that are accessible within expressions.
      • initializeContext

        private void initializeContext​(org.apache.commons.jexl3.JexlContext ctx)
        Initializes the specified context with the variables managed by this Lookup object.
        Parameters:
        ctx - the context to be initialized
      • installSubstitutor

        private void installSubstitutor​(ConfigurationInterpolator ip)
        Creates a StringSubstitutor object which uses the passed in ConfigurationInterpolator as lookup object.
        Parameters:
        ip - the ConfigurationInterpolator to be used
      • lookup

        public java.lang.String lookup​(java.lang.String var)
        Evaluates the expression.
        Specified by:
        lookup in interface Lookup
        Parameters:
        var - The expression.
        Returns:
        The String result of the expression.
      • setInterpolator

        public void setInterpolator​(ConfigurationInterpolator interpolator)
        Sets the ConfigurationInterpolator to be used by this object.
        Parameters:
        interpolator - the ConfigurationInterpolator (may be null)
        Since:
        2.0
      • setLogger

        public void setLogger​(ConfigurationLogger logger)
        Sets the logger to be used by this object. If no logger is passed in, no log output is generated.
        Parameters:
        logger - the Log
        Since:
        2.0
      • setVariablePrefixMatcher

        public void setVariablePrefixMatcher​(java.lang.String prefix)
        Sets the prefix to use to identify subordinate expressions. This cannot be the same as the prefix used for the primary expression.
        Parameters:
        prefix - The String identifying the beginning of the expression.
      • setVariables

        public void setVariables​(ExprLookup.Variables list)
        Add the Variables that will be accessible within expressions.
        Parameters:
        list - The list of Variables.
      • setVariableSuffixMatcher

        public void setVariableSuffixMatcher​(java.lang.String suffix)
        Sets the suffix to use to identify subordinate expressions. This cannot be the same as the suffix used for the primary expression.
        Parameters:
        suffix - The String identifying the end of the expression.