JLoom
Overview
Introduction
Commands
Hello World Examples
Installation / Usage
Eclipse Plugin
Download
|
Simple Hello World
Examples can be much more useful than tons of text.
Here is a simple "Hello Word" example template:
HelloWorld.jloom
|
<%@ jloom template /%>
<%@ main (String name) %>
Hello <%= name %>!
<%/ main %>
|
Note that the template defines one String parameter,
which is inserted in the salutation.
This template has to be translated into the generator class.
If you are using the Eclipse plugin, this is done automatically immediately
after saving the template.
Otherwise you have to translate it manually: provided that you have the
binary JLoom JAR and the template in the same directory
(you need at least v0.10), you type:
java -jar jloom-0.10.0.jar HelloWorld
A file called HelloWorld_JLoom.java - the generator class - will be created:
HelloWorld_JLoom.java
|
/* JLoom 1.4.0 generator class */
/* AUTO-GENERATED */
/* Do not edit! Changes will be overwritten! */
package net.gereon.jloom.homepage.examples;
import java.io.*;
import net.gereon.jloom.core.*;
import net.gereon.jloom.util.*;
import static net.gereon.jloom.util.GenerationTools.*;
public class HelloWorld_JLoom extends net.gereon.jloom.core.GeneratorBase {
public void generateMain(GenerationContext context, String name) throws IOException {
newLine(context);
print(context, "Hello ");
print(context, "" + (name));
println(context, "!");
}
}
/* TRANSLATION MAPPING:
[26 287 c] [51 456 c] [61 508 c] [72 541 c] */
|
The generateMain() method of the generator class declares two parameters.
The first parameter stores information about the generation process and
is mandatory for all generator methods.
All subsequent parameters correspond to the parameters of the template,
in this case one String parameter.
You can call the generateMain() method to generate the text,
here is an example code:
HelloWorld.java
|
package net.gereon.jloom.homepage.examples;
import net.gereon.jloom.core.*;
public class HelloWorld
{
public static void main(String[] args) throws Exception
{
// create a GenerationContext, configure output to System.out
GenerationContext context = new SimpleGenerationContext(System.out, "\n", " ");
// create an instance of the generator class
HelloWorld_JLoom generator = new HelloWorld_JLoom();
// generate output to System.out
generator.generateMain(context, "World");
}
}
|
When you run the above code it writes the generated text output to the console:
Hello World!
Advanced Hello Big World
Here is an advanced example template to show the basic syntax elements of JLoom.
Smaller examples can be found in
Introduction and
Commands.
HelloBigWorld.jloom
|
<%@ jloom template /%>
<%@ import java.util.* /%>
<%!
// This is a declaration:
// Java code which can contain declarations of methods and fields.
// It is only one such declaration-tag allowed and it has to
// be before the main-command-tag
private final static String TITLE = "Hello Big World Example";
private final static String END_NAME = "END";
public String getTimeString()
{
Calendar c = new GregorianCalendar();
c.setTime(new Date());
return "sometime in " + c.get(Calendar.YEAR);
}
%>
<%@ main (String firstName, String lastName) %>
<%@ if (!firstName.equals(END_NAME)) %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> <%= TITLE %> </title>
</head>
<body>
<%/ if %>
<h1>
<%= TITLE %>
</h1>
<%@ for (int i=1; i<=3; i++) %>
<b> Hello <%= firstName %> <%= lastName %> No. <%= i %> ! </b>
<br>
<%/ for %>
<br>
now the scriptlet is executed...
<br>
<br>
<%
// This is a scriptlet:
// Java code which is executed while executing the template.
if (firstName.equals(END_NAME)) {
return; // to end the recursion in this stupid example
}
%>
<%@ exec HelloBigWorld (END_NAME, "") /%>
<small> generated <%= getTimeString() %> </small>
</body>
</html>
<%/ main %>
|
When you look at this template, you will find all basic JLoom syntax elements,
like scriptlets, expressions, and miscellaneous commands and macros.
All these have the same basic tag-structure:
<%...%>
The template calls itself to demonstrate, how to call other templates:
<%@ exec HelloBigWorld (END_NAME, "") /%>
HelloBigWorld.jloom will be translated by JLoom into the following Java class.
Because this class is generated by JLoom, you don't have do worry about its code,
the code is shown here just to give you an impression how JLoom works:
HelloBigWorld_JLoom.java
|
/* JLoom 1.4.0 generator class */
/* AUTO-GENERATED */
/* Do not edit! Changes will be overwritten! */
package net.gereon.jloom.homepage.examples;
import java.io.*;
import net.gereon.jloom.core.*;
import net.gereon.jloom.util.*;
import static net.gereon.jloom.util.GenerationTools.*;
import java.util.*;
public class HelloBigWorld_JLoom extends net.gereon.jloom.core.GeneratorBase {
// This is a declaration:
// Java code which can contain declarations of methods and fields.
// It is only one such declaration-tag allowed and it has to
// be before the main-command-tag
private final static String TITLE = "Hello Big World Example";
private final static String END_NAME = "END";
public String getTimeString()
{
Calendar c = new GregorianCalendar();
c.setTime(new Date());
return "sometime in " + c.get(Calendar.YEAR);
}
public void generateMain(GenerationContext context, String firstName, String lastName) throws IOException {
newLine(context);
if (!firstName.equals(END_NAME)) {
println(context, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
println(context, "<html>");
println(context, "<head>");
print(context, "<title> ");
print(context, "" + (TITLE));
println(context, " </title>");
println(context, "</head>");
println(context, "<body>");
}
newLine(context);
println(context, "<h1>");
print(context, "" + (TITLE));
newLine(context);
println(context, "</h1>");
newLine(context);
for (int i=1; i<=3; i++) {
print(context, "<b> Hello ");
print(context, "" + (firstName));
print(context, " ");
print(context, "" + (lastName));
print(context, " No. ");
print(context, "" + (i));
println(context, " ! </b>");
println(context, "<br>");
}
newLine(context);
println(context, "<br>");
println(context, "now the scriptlet is executed...");
println(context, "<br>");
println(context, "<br>");
newLine(context);
// This is a scriptlet:
// Java code which is executed while executing the template.
if (firstName.equals(END_NAME)) {
return; // to end the recursion in this stupid example
}
newLine(context);
{
HelloBigWorld_JLoom _generator_60 = new HelloBigWorld_JLoom();
_generator_60.generateMain(context, END_NAME, "");
_generator_60.generateTail(context);
}
print(context, " ");
newLine(context);
newLine(context);
print(context, "<small> generated ");
print(context, "" + (getTimeString()));
println(context, " </small>");
newLine(context);
println(context, "</body>");
println(context, "</html>");
}
}
/* TRANSLATION MAPPING:
[26 287 c] [118 307 c] [123 391 s] [150 419 s] [218 488 s]
[280 551 s] [315 587 s] [317 590 s] [384 658 s] [431 706 s]
[433 709 s] [435 712 s] [466 744 s] [469 748 s] [510 790 s]
[536 817 s] [585 867 s] [666 869 c] [713 981 c] [747 1003 c]
[786 1042 c] [877 1237 c] [889 1273 c] [926 1385 c] [1236 1437 c]
[1248 1471 c] [1290 1546 c] [1321 1577 c] [1333 1613 c] [1349 1653 c]
[1350 1680 c] [1365 1719 c] [1370 1750 c] [1378 1782 c] [1403 1855 c]
[1461 2047 c] [1465 2051 s] [1490 2079 s] [1552 2144 s] [1554 2149 s]
[1589 2187 s] [1647 2248 s] [1652 2250 c] [1688 2272 c] [1729 2453 c]
[1755 2479 c] [1777 2565 c] [1799 2609 c] */
|
You can use the generator class (in HelloBigWorld_JLoom.java) to generate HTML Pages
(as defined by the template HelloBigWorld.jloom).
The template HelloBigWorld.jloom declares two String parameters:
<%@ main (String firstName, String lastName) %>
Therefore the corresponding method generateMain() in HelloBigWorld_JLoom.java
expects two String arguments (besides a GenerationContext).
This is the most important feature of JLoom, because it allows you
to modularize your code into encapsulated templates.
The following code generates the HTML Page:
HelloBigWorld.java
|
package net.gereon.jloom.homepage.examples;
import java.io.*;
import net.gereon.jloom.core.*;
import net.gereon.jloom.homepage.core.Web;
public class HelloBigWorld
{
public static void main(String[] args) throws Exception
{
File file = new File(Web.EXAMPLES_PATH + "/HelloBigWorld.html");
Writer out = new FileWriter(file);
try {
GenerationContext context = new SimpleGenerationContext(out, "\n", " ");
new HelloBigWorld_JLoom().generateMain(context, "Big", "World");
}
finally {
out.close();
}
}
}
|
HelloBigWorld.java calls generateMain() in HelloBigWorld_JLoom.java
and writes the output to HelloBigWorld.html.
So finally, this is the produced output:
HelloBigWorld.html
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> Hello Big World Example </title>
</head>
<body>
<h1>
Hello Big World Example
</h1>
<b> Hello Big World No. 1 ! </b>
<br>
<b> Hello Big World No. 2 ! </b>
<br>
<b> Hello Big World No. 3 ! </b>
<br>
<br>
now the scriptlet is executed...
<br>
<br>
<h1>
Hello Big World Example
</h1>
<b> Hello END No. 1 ! </b>
<br>
<b> Hello END No. 2 ! </b>
<br>
<b> Hello END No. 3 ! </b>
<br>
<br>
now the scriptlet is executed...
<br>
<br>
<small> generated sometime in 2007 </small>
</body>
</html>
|
Show the HTML page
Ok, this was a stupid example just for demonstration purpose.
An example for real world usage are these JLoom webpages -
yes, this is what you are currently reading, more precisely the entire
content of the JLoom homepage is generated out of JLoom templates.
You can download these templates on Sourceforge.
You can also browse these templates online in the
Homepage Template Browser.
When you take a look at these templates -
each of it is extremely simple because of modularization and elimination of
redundant markup -
you will (hopefully) see, that JLoom can be very helpful for generating static HTML content.
Of course it is much more useful for dynamic content.
Another real world example is JLoom itself, because most template
commands are implemented as macros which are JLoom templates.
You can look at these macros to learn about JLoom templates,
you will find them in the sources in the package
net.gereon.jloom.macros.
|