Class VarResolver

java.lang.Object
org.apache.juneau.svl.VarResolver

public class VarResolver extends Object
Utility class for resolving variables of the form "$X{key}" in strings.

Variables are of the form $X{key}, where X can consist of zero or more ASCII characters.
The variable key can contain anything, even nested variables that get recursively resolved.

Variables are defined through the VarResolver.Builder.vars(Class[]) method.

The Var interface defines how variables are converted to values.

Example:

public class SystemPropertiesVar extends SimpleVar { // Must have a no-arg constructor! public SystemPropertiesVar() { super("S"); } @Override public String resolve(VarResolverSession session, String value) { return System.getProperty(value); } } // Create a variable resolver that resolves system properties (e.g. "$S{java.home}") VarResolver varResolver = VarResolver.create().vars(SystemPropertiesVar.class).build(); // Use it! System.out.println(varResolver.resolve("java.home is set to $S{java.home}"));

See Also:
  • Field Details

  • Constructor Details

  • Method Details

    • create

      public static VarResolver.Builder create()
      Instantiates a new clean-slate VarResolver.Builder object.
      Returns:
      A new VarResolver.Builder object.
    • copy

      Returns a new builder object using the settings in this resolver as a base.
      Returns:
      A new var resolver builder.
    • getVarMap

      protected Map<String,Var> getVarMap()
      Returns an unmodifiable map of Vars associated with this context.
      Returns:
      A map whose keys are var names (e.g. "S") and values are Var instances.
    • getVars

      protected Var[] getVars()
      Returns an array of variables define in this variable resolver context.
      Returns:
      A new array containing the variables in this context.
    • addBean

      public <T> VarResolver addBean(Class<T> c, T value)
      Adds a bean to this session.
      Type Parameters:
      T - The bean type.
      Parameters:
      c - The bean type.
      value - The bean.
      Returns:
      This object .
    • createSession

      Creates a new resolver session with no session objects.
      Returns:
      A new resolver session.
    • createSession

      Same as createSession() except allows you to specify a bean store for resolving beans.
      Parameters:
      beanStore - The bean store to associate with this session.
      Returns:
      A new resolver session.
    • resolve

      public String resolve(String s)
      Resolve variables in the specified string.

      This is a shortcut for calling createSession(null).resolve(s);.
      This method can only be used if the string doesn't contain variables that rely on the existence of session variables.

      Parameters:
      s - The input string.
      Returns:
      The string with variables resolved, or the same string if it doesn't contain any variables to resolve.
    • resolveTo

      public void resolveTo(String s, Writer w) throws IOException
      Resolve variables in the specified string and sends the results to the specified writer.

      This is a shortcut for calling createSession(null).resolveTo(s, w);.
      This method can only be used if the string doesn't contain variables that rely on the existence of session variables.

      Parameters:
      s - The input string.
      w - The writer to send the result to.
      Throws:
      IOException - Thrown by underlying stream.