Skip to content

noncat-lang/java-template-engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Central Unit Test Semgrep Dependency Check Markdown Link Check

Java Template Engine

Java Template Engine demonstrates one concept of Noncat within the Java programming language.

Java Template Engine uses the well known idea of templates to solve the not well known problem of defining languages for program input and output. By defining these languages programs are able to reject malicious input and create well formed output to prevent input based vulnerabilities. Java Template Engine helps you to do exactly this without the need to write a grammar.

Using Java Template Engine one can only define regular languages, hence it is not possible to define all language features of context free languages, e.g., HTML or SQL.

Usage

TODO

Format / Unparse

createOutput("new,World");
String createOutput(String name) {
  Token token = Token.of("[a-zA-Z ]+").withEncoding(Encoding.of(",", " "));
  Template template = Template.of("Hello ${name}!")
      .withToken("name", token);

  Values values = Values.of("name", name);
  return template.format(values);
}
Hello new World!

Parse

handleInput("Hello new World!");
Optional<String> handleInput(String input) {
  Token token = Token.of("[a-zA-Z ]+").withDecoding(Decoding.of(" ", ","));
  Template template = Template.of("Hello ${name}!")
      .withToken("name", token);

  Values data = template.parse(input);
  return data.get("name");
}
Optional[new,World]