Class VarResolverSession

java.lang.Object
org.apache.juneau.commons.svl.VarResolverSession

public class VarResolverSession extends Object
A var resolver session that combines a VarResolver with one or more session objects.

Instances of this class are considered light-weight and fast to construct, use, and discard.

This class contains the workhorse code for var resolution.

Instances of this class are created through the VarResolver.createSession() and VarResolver.createSession(WritableBeanStore) methods.

Notes:
  • This class is not guaranteed to be thread safe.
See Also:
  • Constructor Details

    • VarResolverSession

      public VarResolverSession(VarResolver context, WritableBeanStore beanStore)
      Constructor.
      Parameters:
      context - The VarResolver context object that contains the Vars and context objects associated with that resolver.
      beanStore - The bean store to use for resolving beans needed by vars. Can be null (this session will then have no parent bean store, and only beans explicitly added via bean(Class, Object) will be resolvable).
  • Method Details

    • bean

      public <T> VarResolverSession bean(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.
    • getBean

      public <T> Optional<T> getBean(Class<T> c)
      Returns the bean from the registered bean store.
      Type Parameters:
      T - The value type.
      Parameters:
      c - The bean type.
      Returns:
      The bean.
      Never null.
    • resolve

      public String resolve(String s)
      Resolve all variables in the specified string.
      Parameters:
      s - The string to resolve variables in. Can be null (returns null).
      Returns:
      The new string with all variables resolved, or the same string if no variables were found.
      Returns null if the input was null.
    • resolveHtmlDoc

      Resolves SVL variables for HTML-doc chrome fields that write raw HTML.

      Literal characters from the template (including tags) pass through unchanged. Each SVL substitution is XML-escaped via StringUtils.escapeXml(String) before insert, except the HTML-passthrough vars $W, $C, and $RS.

      Plain resolve(String) is unchanged. Use this only for HTML-doc chrome (aside, header, footer, nav HTML fragments, head, no-results).

      Parameters:
      s - The chrome template. Can be null.
      Returns:
      The resolved chrome string, or null if the input was null.
    • resolveScriptStyle

      Resolves SVL variables for <script> and <style> chrome.

      Request-derived vars ($RQ, $RH, $RF, $RP, $RA, $SA, $R) are left unresolved (same as an unknown var). Other vars including $W, $C, and $RS still resolve.

      Parameters:
      s - The script or style template. Can be null.
      Returns:
      The resolved string, or null if the input was null.
    • compile

      public VarTemplate compile(String input)
      Compiles the given template string into a reusable VarTemplate.

      Equivalent to this.context.compile(input); provided on the session for symmetry with resolve(String). The returned template is bound to the underlying VarResolver, not to this session — see the VarTemplate class javadoc for the compile-binding contract.

      Parameters:
      input - The template string. May be null.
      Returns:
      A new VarTemplate bound to the underlying resolver.
    • resolveSupplier

      Returns a session-bound Supplier that re-resolves the given template against this session each time Supplier.get() is called.

      Unlike VarResolver.resolveSupplier(String) — which opens a fresh session per .get() and is threadsafe to share — this Supplier captures the existing session and reuses it. It is therefore not threadsafe; it inherits the standard "VarResolverSession is not guaranteed to be thread safe" contract.

      Use this variant only when the caller owns the session lifecycle and the perf saving of skipping per-call session construction matters. For shared / cross-thread use cases, prefer VarResolver.resolveSupplier(String).

      Parameters:
      input - The template string. May be null.
      Returns:
      A Supplier<String> bound to this session.
    • resolve

      public String[] resolve(String[] in)
      Resolves the specified strings in the string array.
      Parameters:
      in - The string array containing variables to resolve. Must not be null or a NullPointerException is thrown, though individual elements can be null.
      Returns:
      An array with resolved strings. Elements are null where the corresponding input element was null.
    • resolve

      public <T> T resolve(T o)
      Convenience method for resolving variables in arbitrary objects.

      Supports resolving variables in the following object types:

      • CharSequence
      • Arrays containing values of type CharSequence.
      • Collections containing values of type CharSequence.
        Collection class must have a no-arg constructor.
      • Maps containing values of type CharSequence.
        Map class must have a no-arg constructor.
      Type Parameters:
      T - The value type.
      Parameters:
      o - The object. Can be null (returns null).
      Returns:
      The same object if no resolution was needed, otherwise a new object or data structure if resolution was needed. Returns null if the input was null.
    • resolveTo

      public Writer resolveTo(String s, Writer out) throws IOException
      Resolves variables in the specified string and sends the output to the specified writer.

      More efficient than first parsing to a string and then serializing to the writer since this method doesn't need to construct a large string.

      Parameters:
      s - The string to resolve variables in. Can be null, in which case the writer is left unchanged.
      out - The writer to write to. Must not be null.
      Returns:
      The same writer.
      Throws:
      IOException - Thrown by underlying stream.
    • properties

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getVar

      protected Var getVar(String name)
      Returns the Var with the specified name.
      Parameters:
      name - The var name (e.g. "S"). Must not be null.
      Returns:
      The Var instance, or null if no Var is associated with the specified name.