https://github.com/robtimus/string-template-utils
A set of utility classes for working with string templates
https://github.com/robtimus/string-template-utils
java string-template
Last synced: 4 months ago
JSON representation
A set of utility classes for working with string templates
- Host: GitHub
- URL: https://github.com/robtimus/string-template-utils
- Owner: robtimus
- License: apache-2.0
- Created: 2023-10-28T13:33:20.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-01-20T12:51:44.000Z (over 1 year ago)
- Last Synced: 2024-11-17T06:41:50.210Z (6 months ago)
- Topics: java, string-template
- Language: Java
- Homepage: https://robtimus.github.io/string-template-utils/
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# string-template-utils
[](https://search.maven.org/artifact/com.github.robtimus/string-template-utils)
[](https://github.com/robtimus/string-template-utils/actions/workflows/build.yml)
[](https://sonarcloud.io/summary/overall?id=com.github.robtimus%3Astring-template-utils)
[](https://sonarcloud.io/summary/overall?id=com.github.robtimus%3Astring-template-utils)
[](https://snyk.io/test/github/robtimus/string-template-utils)A set of utility classes for working with string templates.
## StringTemplateProcessors
Class `StringTemplateProcessors` can be used to easily construct string template processors that work like the default `STR` template processor, except they apply a function to the template values.
For instance, using [Apache Commons Text](https://commons.apache.org/proper/commons-text/):
```java
StringTemplate.Processor JSON = StringTemplateProcessors
.interpolateMapped(o -> StringEscapeUtils.ESCAPE_JSON.translate(String.valueOf(o)));
String json = JSON."""
{
"id": \{id},
"name": "\{name}"
}
""";
```Or equivalent, to prevent having to call `String.valueOf` explicitly:
```java
StringTemplate.Processor JSON = StringTemplateProcessors
.interpolateMappedAsString(s -> StringEscapeUtils.ESCAPE_JSON.translate(s));
String json = JSON."""
{
"id": \{id},
"name": "\{name}"
}
""";
```In both cases, the `name` property will have a properly escaped value.
## URLEncoderProcessor
Class `URLEncoderProcessor` is a string template processor that uses [URLEncoder](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/net/URLEncoder.html) for encoding template values. This makes it safe to use to create URIs. For example, using a static import:
```java
URI uri = encodeAsURI()."https://host/path/\{id}?url=\{url}";
```If `id` is `id/123` and `url` is `https://example.org?q=1`, the result will be `https://host/path/id%2F123?url=https%3A%2F%2Fexample.org%3Fq%3D1`.