The HTmlFlow is a simple Java library for writing HTML documents in a fluent style into a java.io.PrintStream. For instance, considering a Java class Taskwith three properties: Title, Description and a Priority, then we can produce an HTML document for a Task object in the following way:
//// Creates a new Task object//Taskt1 = newTask("ISEL MPD project", "A Java library for serializing objects in HTML.", Priority.High);
//// Creates and setup an HtmlView object for task details//HtmlView<Task> taskView = newHtmlView<Task>();
taskView.head().title("Task Details");
taskView
.body()
.heading(1, "Task Details")
.hr()
.div()
.text("Title: ").text(t1.getTitle())
.br()
.text("Description: ").text(t1.getDescription());
.br()
.text("Priority: ").text(t1.getPriority().toString());
//// Produces an HTML file document//try{
PrintStreamout = newPrintStream(newFileOutputStream("Task.html"));
taskView.setPrintStream(out).write(1, t1);
Runtime.getRuntime().exec("explorer Task.html");
}finally{
out.close();
}The HtmlFlow also supports binders that enable the same HTML view to be used (or bind) with different object models.
publicstaticvoidmain(String[] args) throwsIOException {
HtmlView<Task> taskView = taskDetailsView();
try{
Taskt1 = newTask("ISEL MPD project", "A Java library for serializing objects in HTML.", Priority.High);
PrintStreamout = newPrintStream(newFileOutputStream("Task1.html"));
taskView.setPrintStream(out).write(1, t1);
Runtime.getRuntime().exec("explorer Task1.html");
}finally{
out.close();
}
try{
Taskt2 = newTask("Special dinner", "Have dinner with someone!", Priority.Normal);
PrintStreamout = newPrintStream(newFileOutputStream("Task2.html"));
taskView.setPrintStream(out).write(1, t2); // Now we can use the same HtmlView with a different taskRuntime.getRuntime().exec("explorer Task2.html");
}finally{
out.close();
}
} privatestaticHtmlView<Task> taskDetailsView(){
HtmlView<Task> taskView = newHtmlView<Task>();
taskView.head().title("Task Details");
taskView
.body()
.heading(1, "Task Details")
.hr()
.div()
.text("Title: ").text(binderGetTitle())
.br()
.text("Description: ").text(binderGetDescription())
.br()
.text("Priority: ").text(binderGetPriority());
returntaskView;
}
privatestaticModelBinder<Task> binderGetTitle(){
returnnewModelBinder<Task>() {publicvoidbind(PrintStreamout, Taskmodel) {
out.print(model.getTitle());
}};
}
privatestaticModelBinder<Task> binderGetDescription(){
returnnewModelBinder<Task>() {publicvoidbind(PrintStreamout, Taskmodel) {
out.print(model.getDescription());
}};
}
privatestaticModelBinder<Task> binderGetPriority(){
returnnewModelBinder<Task>() {publicvoidbind(PrintStreamout, Taskmodel) {
out.print(model.getPriority());
}};
}Finally, an example of producing an HTML table bind to a list of tasks:
publicstaticvoidmain(String[] args) throwsIOException {
HtmlView<Task> taskView = taskDetailsView();
try{
Taskt1 = newTask("ISEL MPD project", "A Java library for serializing objects in HTML.", Priority.High);
Taskt2 = newTask("Special dinner", "Have dinner with someone!", Priority.Normal);
Taskt3 = newTask("Manchester City - Sporting", "1/8 Final UEFA Europa League. VS. Manchester City - Sporting!", Priority.High);
PrintStreamout = newPrintStream(newFileOutputStream("TaskList.html"));
taskListView().setPrintStream(out).write(1, Arrays.asList(t1, t2, t3));
Runtime.getRuntime().exec("explorer TaskList.html");
}finally{
out.close();
}
}
privatestaticHtmlView<Iterable<Task>> taskListView(){
HtmlView<Iterable<Task>> taskView = newHtmlView<Iterable<Task>>();
taskView.head().title("Task List");
HtmlTable<Iterable<Task>> t = taskView.body()
.heading(1, "Task Details")
.hr()
.div()
.table();
HtmlTr<Iterable<Task>> headerRow = t.tr();
headerRow.th().text("Title");
headerRow.th().text("Description");
headerRow.th().text("Priority");
t.trFromIterable(binderGetTitle(), binderGetDescription(), binderGetPriority());
returntaskView;
}