Skip to main content

Simple Variable Language Basics

The org.apache.juneau.svl package defines an API for a language called "Simple Variable Language".

In a nutshell, Simple Variable Language (or SVL) is text that contains variables of the form $varName{varKey}. It is used extensively in the Config, REST and Microservice APIs.

Most variables can be recursively nested within the varKey (e.g. $FOO{$BAR{xxx},$BAZ{xxx}}) and can return values that themselves contain more variables.

The VarResolver class is used to resolve variables.

The VarResolver.DEFAULT resolver is a reusable instance of this class configured with the following basic variables:

SystemPropertiesVar - $S{key[,default]}EnvVariablesVar - $E{key[,default]}

The following logic variables are also provided:

IfVar - $IF{arg,then[,else]}SwitchVar - $SW{arg,pattern1:then1[,pattern2:then2...]}CoalesceVar - $CO{arg1[,arg2...]}PatternMatchVar - $PM{arg,pattern}PatternReplaceVar - $PR{arg,pattern,replace}PatternExtractVar - $PE{arg,pattern,groupIndex}NotEmptyVar - $NE{arg}UpperCaseVar - $UC{arg}LowerCaseVar - $LC{arg}LenVar - $LN{arg[,delimiter]}SubstringVar - $ST{arg,start[,end]}
Example
// Use the default variable resolver to resolve a string that contains
// $S (system property) variables.
String property = VarResolver.DEFAULT.resolve("The Java home directory is $S{java.home}");

The following shows how variables can be arbitrarily nested...

// Look up a property in the following order:
// 1) MYPROPERTY environment variable.
// 2) 'my.property' system property if environment variable not found.
// 3) 'not found' string if system property not found.
String property = VarResolver.DEFAULT.resolve("$E{MYPROPERTY,$S{my.property,not found}}");