HTML Generator in Java – renderSnake

19. March, 2011

There is another HTML generator: renderSnake. Here is how it looks:

public class Logo implements Renderable {
    public void renderOn(HtmlCanvas html) throws IOException { //@formatter:off
        html
            .div(id("logo"))
                .div(id("logo_text"))



                    .h1()
                        .a(href("index.html"))
                            .write("render")
                            .style().write(".snake { color:yellow; }")._style()



                            .span(class_("snake")).write("S")._span()
                            .write("nake")
                        ._a()
                    ._h1()
                    .h2().write("lean and mean HTML page writing machine")._h2()._div()



                ._div();
    }
}

I’m not sure about the “_” versions but it sure looks better than JSP 🙂

Related articles: Simple HTML Output From Java Using renderSnake


Code Generators

26. August, 2009

Code generators can save a lot of time and effort which leads to the question: Why isn’t everyone using them? A lot of code in any project is repetition.

The main reason: With todays IDEs, you can’t debug generated code. Well, you can debug the result of the code generation but when hunting bugs in generated code, chances are that you want to hunt them in the input and/or the transformer and no IDE has a notion of “this line of code came from these inputs, so show them as well”.

Example: You need POJOs which map database tables. Easy enough: You create a small program that connects to the database, maps the types in the tables to types in your favorite programming language (or rather the one you have to use for your project) and dump some code.

But unlike C/C++, modern languages don’t have a #line directive which says “this piece of code was originally part of another file” let alone “this information came from the database XXX”. So when there is some problem, you need to dig through the layers of the code generator, the templates and the input (the database) yourself, running the transformation in your head. Bad.

Approaches like MDD will keep failing until they support this.