Search code examples
javajspservletssitebricks

How is annotations support in jsp implemented in sitebricks?


Here is an example from the SiteBricks user guide:

<html>
<body>
    @ShowIf(true)     <----- I'm impressed with this line
    <p>${message} from Sitebricks!</p>
</body>
</html>

I'm curious how is it implemented? (I mean how and at which entry point sitebricks creators managed to enhance transforming jsp to servlet?)

Thanks for any ideas!


Solution

  • There is no filter doing this. We have our own templating logic that uses what is known as a recursive descent parser.

    It's actually a non-trivial problem to accomplish this kind of annotation parsing. What we do first is to slurp the entire HTML as a DOM-like tree using Jsoup. Jsoup takes care of normalizing the HTML into an XML-like structure (not exactly, though).

    We then descend this tree and build a parallel tree of "Renderers" as Java objects. Each renderer has the ability to do something specific, i.e. spit out HTML, Show If the value is true etc. Given that the renderer tree matches the HTML tree, if ShowIf returns false, we elide the entire branch below it too.

    There are some more complex things going on under the hood for page-embedding and so on, but this is the gist of it. Thanks for the kind words!

    Dhanji, creator, Sitebricks & Guice Servlet.