Class DefaultLineExpander

  • All Implemented Interfaces:
    LineExpander

    public class DefaultLineExpander
    extends java.lang.Object
    implements LineExpander
    Default implementation of LineExpander that provides sane defaults for variable expansion.

    Supported expansions:

    • $VAR and ${VAR} ? variable expansion from session variables, then System.getenv()
    • ~ at word start ? expands to user.home system property
    • Single-quoted regions ('...') are not expanded
    • Double-quoted regions ("...") expand variables

    Advanced braced forms:

    • ${VAR:-default} ? use default if VAR is unset or empty
    • ${VAR:=default} ? assign default if VAR is unset or empty
    • ${VAR:+alt} ? use alt if VAR is set and non-empty
    • ${VAR:?error} ? error if VAR is unset or empty

    Deliberately not included (subclass for these):

    • ${VAR//pattern/replacement} ? pattern substitution
    • Glob expansion (*, ?)

    Subclasses can override resolve(String, CommandSession) to customize how variable names are resolved.

    Since:
    4.0
    See Also:
    LineExpander
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultLineExpander()
      Creates a new DefaultLineExpander.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String expand​(java.lang.String line, CommandSession session)
      Expands variables and expressions in the given command line.
      protected java.lang.String expandBracedExpression​(java.lang.String expr, CommandSession session)
      Expands a braced expression such as VAR, VAR:-default, VAR:=default, VAR:+alt, or VAR:?error.
      protected java.lang.String resolve​(java.lang.String name, CommandSession session)
      Resolves a variable name to its value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DefaultLineExpander

        public DefaultLineExpander()
        Creates a new DefaultLineExpander.
    • Method Detail

      • expand

        public java.lang.String expand​(java.lang.String line,
                                       CommandSession session)
        Description copied from interface: LineExpander
        Expands variables and expressions in the given command line.
        Specified by:
        expand in interface LineExpander
        Parameters:
        line - the command line to expand
        session - the current command session (for variable lookup)
        Returns:
        the expanded command line
      • expandBracedExpression

        protected java.lang.String expandBracedExpression​(java.lang.String expr,
                                                          CommandSession session)
        Expands a braced expression such as VAR, VAR:-default, VAR:=default, VAR:+alt, or VAR:?error.
        Parameters:
        expr - the expression inside the braces (without ${ })
        session - the command session
        Returns:
        the expanded value, or null if the variable is unknown and no modifier applies
      • resolve

        protected java.lang.String resolve​(java.lang.String name,
                                           CommandSession session)
        Resolves a variable name to its value.

        The default implementation checks session variables first, then falls back to System.getenv().

        Subclasses can override this to provide custom resolution (e.g., Groovy evaluation, default values).

        Parameters:
        name - the variable name
        session - the current command session
        Returns:
        the resolved value as a string, or null if not found