This topic tells you about some common Spring Expression Language (SpEL) use cases in Application Accelerator.
For more information, see Spring Expression Language documentation.
You can reference all the values added as options in the accelerator
section from the YAML file as variables in the engine
section of accelerator.axl
. You can access the value using the syntax #OPTION-NAME
.
Example accelerator.yaml
:
options:
- name: foo
dataType: string
inputType: text
...
Example accelerator.axl
:
engine {
Include({"some/file.txt"})
ReplaceText(substitutions: {{text: 'bar', with: #foo}})
}
This sample replaces every occurrence of the text bar
in the file some/file.txt
with the contents of the foo
option.
Some variables are made available to the model by the engine, including:
artifactId
is a built-in value derived from the projectName
passed in from the UI with spaces replaced by “_”. If that value is empty, it is set to app
.files
is a helper object that currently exposes the contentsOf(<path>)
method. For more information, see ReplaceText.camel2Kebab
and other variations of the form xxx2Yyyy
is a series of helper functions for dealing with changing case of words. For more information, see ReplaceText.You can use Boolean options for conditionals in your transformations.
Example accelerator.yaml
:
options:
- name: numbers
inputType: select
choices:
- text: First Option
value: first
- text: Seconf Option
value: second
defaultValue: first
Example accelerator.axl
:
engine {
if (#numbers == 'first') {
Include({"some/file.txt"})
ReplaceText({{text: "bar", with: #foo}})
}
}
This replaces the text only if the selected option is the first one.
String templates are available in Application Accelerator using backticks. These are useful, for example, when using RewritePath
.
Example accelerator.yaml
:
options:
- name: renameTo
dataType: string
inputType: text
...
Example accelerator.axl
:
engine {
Include({"some/file.txt"})
RewritePath(rewriteTo: `somewhere/#{#renameTo}.txt`)
}
Regular expressions allow you to use patterns as a matcher for strings. Here is an example of what you can do with them.
Example accelerator.yaml
:
options:
- name: foo
dataType: string
inputType: text
defaultValue: abcZ123
...
Example accelerator.axl
:
engine {
if (#foo.matches('[a-z]+Z\d+')) {
Include({"some/file.txt"})
ReplaceText({{text: "bar", with: #foo}})
}
}
This example uses RegEx to match a string of letters that ends with a capital Z and any number of digits. If this condition is fulfilled, the text is replaced in the file, file.txt
.
Options with a dataType
of [string]
come out as an array of strings.
To use them and, for example, format the result as a bulleted list, you can use the Java static String.join()
method.
Example accelerator.yaml
:
accelerator:
options:
- name: meals
dataType: [string]
inputType: checkbox
choices:
- value: fish
- value: chips
- value: BLT
...
Example accelerator.axl
:
engine {
ReplaceText({{text: recipe, with: ' * ' + T(java.lang.String).join('\n * ', #meals) }})
}