|
Project Information
|
This project has one aim, to build HTML pages with objects. The idea is to provide a system where using text strings to build HTML documents is no longer necessary, with the entire structure composed of objects. The resulting HTML is well formatted and properly structured, removing some of the common and unnecessary errors such as missing or badly closed tags etc. HTMLDocument html = new HTMLDocument();
Head head = html.head();
Body body = html.body();
head.title("Hello World Page");
body.h1("Hello World!");
String text = html.toString();The above code block results in the following html ... <html>
<head>
<title>Hello World Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>One aim of the API is to include as many convenience methods as possible, without degrading the performance or ease of use of the API. In the above example note the call to create the header tag. body.h1("Hello World!");Within the Body object, this method creates a new Header1 object and adds to its text content. An alternative is to create the Header1 object and add it to the Body manually, giving access to the Header1 object to set attributes like "class" or "id" or add a "style" for example. Header1 header = new Header1();
header.id("header-style");
header.text("Hello World");
body.add(header);All objects can be accessed in this way, the convenience methods simply exist to help speed up writing small websites and keep the code length to a minimum where possible. Perhaps this APIs most useful application is when converting objects to HTML. If you have a POJO and need to provide an HTML representation of it, perhaps in the toString() method, you currently need to build the HTML using StringBuilders. This is a dirty approach as you cannot guarantee the structure of the HTML it produces. Using this API provides a very clean approach to solving this issue. /**
* A Map containing the costs of named items.
*/
public class CostMap {
/** A mapping from names to their associated costs. */
private Map<String, Double> nameToCostMap = new HashMap<String, Double>();
...
@Override
public String toString() {
Table table = new Table();
Row title = table.tr();
title.cell().b("Item");
title.cell().b("Cost");
for (Map.Entry<String, Double> entry : nameToCostMap.entrySet()) {
Row row = table.tr();
row.cell().text(entry.getKey());
row.cell().text(entry.getValue());
}
return table.toString();
}
}
|