Snippetory
a code generation platform
It's said: Progress is the way from the primitive via the complicated towards the simple.
In that sense
Snippetory can be considered simple
.
So, what do you need to know, to start?
Integration
First you need a dependency:
<groupId>org.jproggy</groupId>
<artifactId>snippetory-core</artifactId>
<version>0.9.7</version>
And don't hesitate: It uses the Apache Public License 2.0.
A Template
Then you need a template. It's part of your source, so maybe you put it in your sources. Then you can easily load it via resource loading.
$platform is
<ul>
$traits{
<li>$trait</li>
}$
</ul>
The Renderer
Lastly you need to bind it:
Template tpl = Repo.readResource("myTemplate.html")
.encoding(Encodings.html)
.syntax(Syntaxes.FLUYT)
.parse()
.set("platform", "Snippetory");
Stream.of("visionary", "open", "source").forEach(
t -> tpl.get("traits").set("trait", t).render()
);
tpl.render(System.out);
Out there, there are a number of projects, where every thing is said now. That on one hand means: Yes, you're good to go for pretty complex projects. But of course, more knowledge helps if thing get rough. Once things get bigger you might ask how to organize your java code what encodings can do for you.
Intact Templates
Or you might look into the intact template:
package /*$package(*/org.jproggy.sample/*)*/;
public class $Name$Bean {
// $properties{
private /*$type(*/int/*)*/ $name$;
public /*$type(*/int/*)*/ get$Name$() {
return $name$;
}
public void set$Name$(/*$type(*/int/*)*/ $name$) {
this.$name$ = $name$;
}
// }$
}
That allows you to take advantage of your Java editor and, if you tell your IDE to handle the templates as Java, refactoring tools can affect them as well. Even unit tests for the templates are possible. But generally the strong separation of concerns makes Snippetory pretty strong in testing.
Different Snippetory syntaxes allow for intact templates for a number of different output language like HTML, CSS, SQL and a lot more... Learn more about intact templates
SQL: Prepared statements
Talking about SQL: The snippetory-sql module takes this one step further!
SELECT *
FROM tblTest1
WHERE value = :mandatory
/*${*/ OR value = :opt1/*}$*/
/*${*/ OR value = :opt2/*}$*/
While that template is possible with snippetory-core as well, with snippetory-sql this will create prepared statements. And there's a smooth API to directly load stuff via such an SQL template:
Statement stmt = new SqlContext()
.uriResolver(UriResolver.resource("org/jproggy/sample"))
.connections(() -> connection)
.getRepository("DbAccessRepo.sql")
.get("statement1");
List<Object[]> result = stmt.set("mandatory", "test")
.list(SQL.objects());