public interface VariableSubstituter
This interface defines the methods for a class that does inline variable substitution, converting strings containing variable references into strings where the variables are replaced with their corresponding values. It defines the semantics of variable substitution, but it does not define the syntax. Each subclass defines its own syntax (and logic) for recognizing and replacing a variable reference within a string.
The VariableSubstituter interface defines the minimum functionality and behavior of classes that provide inline string variable substitution. A VariableSubstituter object replaces variable references in a string with the variables' values. Examples of variable references include (but are not limited to) some of the following:
The value of HOME is %HOME% # Windows-style variable reference The value of HOME is $HOME # Unix shell-style variable reference The value of HOME is %(HOME)s # Python ConfigParser syntax
A VariableSubstituter object parses a string passed to
it. It copies literal text from the source string to the result string;
when it encounters a variable reference, it resolves the variable's
value through a separate VariableDereferencer
object, and
substitutes the resulting value in place of the variable reference in
the result string. Each individual VariableSubstituter
class uses its own variable reference syntax. For example, the
UnixShellVariableSubstituter
class recognizes variable
references using the traditional UNIX Bourne Shell variable syntax. The
WindowsCmdVariableSubstituter
class recognizes %-delimited
variable references, the way the Windows cmd.exe program
does.
A VariableSubstituter object dereferences a variable's value by calling the getVariableValue() method in a VariableDereferencer object. One such object might choose to dereference variables by looking them up in a Properties object. Another might resolve variables via hard-coded method calls. Yet another might use a symbol table built by a parser. The variable substitution logic is separated from the variable dereferencing logic; the same variable substitution object (with its specific variable syntax) can resolve variables from a number of different sources.
VariableDereferencer
Modifier and Type | Method and Description |
---|---|
boolean |
getAbortOnSyntaxError()
Get the value of the flag that controls whether the
substitute() methods will abort when they encounter a
syntax error.
|
boolean |
getAbortOnUndefinedVariable()
Get the value of the flag that controls whether the
substitute() methods will abort when they encounter an
undefined variable.
|
void |
setAbortOnSyntaxError(boolean enable)
Set or clear the flag that controls whether the
substitute() methods will abort when they encounter a
syntax error.
|
void |
setAbortOnUndefinedVariable(boolean enable)
Set or clear the flag that controls whether the substitute()
methods will abort when they encounter an undefined variable.
|
java.lang.String |
substitute(java.lang.String s,
VariableDereferencer deref,
java.lang.Object context)
Substitute all variable references in the supplied string,
returning the new String, according to the variable syntax defined
by the implementing class.
|
java.lang.String |
substitute(java.lang.String s,
VariableDereferencer deref,
VariableNameChecker nameChecker,
java.lang.Object context)
Substitute all variable references in the supplied string,
returning the new String, according to the variable syntax defined
by the implementing class.
|
java.lang.String substitute(java.lang.String s, VariableDereferencer deref, java.lang.Object context) throws VariableSubstitutionException
Substitute all variable references in the supplied string, returning the new String, according to the variable syntax defined by the implementing class. This method uses a supplied VariableDereferencer object to resolve variable values. Note that this method throws no exceptions. Syntax errors in the variable references are silently ignored. Variables that have no value are substituted as the empty string. The VariableSubstituter class enforces its own notion of what characters are legal in a variable name. If you want more control over the legal characters, use the second substitute method.
s
- the string containing possible variable referencesderef
- the VariableDereferencer object
to use to resolve the variables' values.context
- an optional context object, passed through unmodified
to the deref object's
VariableDereferencer.getVariableValue(java.lang.String, java.lang.Object)
method.
This object can be anything at all (and, in fact, may
be null if you don't care.) It's primarily useful
for passing context information from the caller to
the (custom) VariableDereferencer.UndefinedVariableException
- undefined variable, and
getAbortOnUndefinedVariable()
returns trueVariableSyntaxException
- syntax error, and
getAbortOnSyntaxError()
returns trueVariableSubstitutionException
- substitution errorsubstitute(String,VariableDereferencer,VariableNameChecker,Object)
,
VariableDereferencer.getVariableValue(String,Object)
java.lang.String substitute(java.lang.String s, VariableDereferencer deref, VariableNameChecker nameChecker, java.lang.Object context) throws VariableSubstitutionException
Substitute all variable references in the supplied string,
returning the new String, according to the variable syntax defined
by the implementing class. This method uses a supplied
VariableDereferencer object to resolve variable values.
Note that this method throws no exceptions. Syntax errors in the
variable references are silently ignored. Variables that have no
value are substituted as the empty string. If the
nameChecker parameter is not null, this method calls its
VariableNameChecker.legalVariableCharacter(char)
method to
determine whether a given character is a legal part of a variable
name. If nameChecker is null, then this method uses its
own notion of legal variable name characters.
s
- the string containing possible variable referencesderef
- the VariableDereferencer object
to use to resolve the variables' values.nameChecker
- the VariableNameChecker object to be
used to check for legal variable name characters,
or nullcontext
- an optional context object, passed through
unmodified to the deref object's
VariableDereferencer.getVariableValue(java.lang.String, java.lang.Object)
method. This object can be anything at all (and,
in fact, may be null if you don't care.) It's
primarily useful for passing context information
from the caller to the
VariableDereferencer.VariableSubstitutionException
- substitution errorUndefinedVariableException
- undefined variable, and
getAbortOnUndefinedVariable()
returns trueVariableSyntaxException
- syntax error, and
getAbortOnSyntaxError()
returns truesubstitute(String,VariableDereferencer,Object)
,
VariableDereferencer.getVariableValue(String,Object)
boolean getAbortOnUndefinedVariable()
UndefinedVariableException
.setAbortOnUndefinedVariable(boolean)
void setAbortOnUndefinedVariable(boolean enable)
UndefinedVariableException
. The flag defaults to false.enable
- true to enable the "abort on undefined variable"
flag, false to disable it.getAbortOnUndefinedVariable()
boolean getAbortOnSyntaxError()
VariableSyntaxException
.setAbortOnUndefinedVariable(boolean)
void setAbortOnSyntaxError(boolean enable)
VariableSyntaxException
. The flag defaults to false.enable
- true to enable the "abort on syntax error"
flag, false to disable it.getAbortOnUndefinedVariable()