JATL : Java Anti-Template Language
Java Groovy
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.hgignore
COPYING
README.md
pom.xml

README.md

Java Anti-Template Language (JATL)

Is an extremely lightweight efficient Java library that generates XHTML or XML by using an a elegant fluent styled micro DSL.

I made this while developing my (now defunkt) social taste recommendation engine and is heavily used in my mobile recruiting company.

I find it particularly useful for creating complicated XML/HTML snippets programmatic-ly in Java that you can use as lambdas or helpers in jMustache or Handlebars.java (respectively). Its also very good at creating extremely large XML documents while using very little memory.

Feel free to contact me on twitter: @agentgt.

News

Yes JATL has not been touched for awhile. Not because its not useful or dead but rather there were no changes that were needed.

HOWEVER Java 8 is out and actually works!! Finally the oracle overlords have bequeathed us with elegant deferred logic and sort of traits. Consequently JATL will probably probably be changed dramatically as there were many things people wanted to do that were rather difficult or not very elegant with the anonymous classes.

2.0.0 Planned

  • JDK 8 lambda rewrite - leverage functional and stream programming.

1.0.0 Planned

  • Full Some more HTML5 support (XML style HTML5).
  • Appendable instead of Writer (no need to wrap StringBuilder)
  • Better Indentation strategy documentation
  • Better escaping strategy (see Issue #10)

0.2.3 Released

  • Remove Commons Lang dependency (JATL will have no dependencies)
  • A generic concrete XML Builder and Writer (Issue #15).
  • The HTML 5 escaping is now XML escaping

0.2.2 Released

0.2.1 Released

Issue #10 fixed.

0.2.0 Released

Experimental indentation strategy is in and OSGI support.

Better Indentation Support (more to come later):

Indenter Unit Test Example

Indenter Javadoc... not done yet

Download

Use Maven or its variants/emulators:

<dependency>
    <groupId>com.googlecode.jatl</groupId>
    <artifactId>jatl</artifactId>
    <version>0.2.2</version>
</dependency>

The jars are in the central repository http://repo1.maven.org/maven2/com/googlecode/jatl/jatl.

Inspiration

Are you tired of using !StringBuffer to generate Markup (HTML/XML) but don't want to use a templating language or some XML library?

Do you want to be able to generate HTML like Groovy's Markup Builders or [Haml](http://haml-lang.com/ Ruby's) but with Java.

I know I do.

There are lots of advantages for using Java as templating language instead of something like Velocity, Freemarker, and Jelly. If you are using an IDE such as Eclipse with JATL you can get syntax highlighting, code refactoring and content assistance for free. Not to mention you now have one less language to remember and deal with.

See my posting on stackoverflow that caused me to write this library: http://stackoverflow.com/questions/3583846/java-html-builder-anti-template-library

Examples

    @Test
    public void testExample() throws Exception {
        //From http://codemonkeyism.com/the-best-markup-builder-i-could-build-in-java/
        new Html(writer) {{
            bind("id", "foo");
            bind("coolName", "Awesomo");
            html();
                body();
                    h1().text("Name Games").end();
                    p().id("${id}").text("Hello ${coolName}, and hello").end();
                makeList("Kyle","Stan", "Eric", "${coolName}");
            endAll();
            done();
        }
            Html makeList(String ... names) {
                ul();
                for(String name : names) {
                    li().text(name).end();
                }
                return end();
            }
        };
        String result = writer.getBuffer().toString();
        String expected = "\n" + 
                "<html>\n" + 
                "    <body>\n" + 
                "        <h1>Name Games\n" + 
                "        </h1>\n" + 
                "        <p id=\"foo\">Hello Awesomo, and hello\n" + 
                "        </p>\n" + 
                "        <ul>\n" + 
                "            <li>Kyle\n" + 
                "            </li>\n" + 
                "            <li>Stan\n" + 
                "            </li>\n" + 
                "            <li>Eric\n" + 
                "            </li>\n" + 
                "            <li>Awesomo\n" + 
                "            </li>\n" + 
                "        </ul>\n" + 
                "    </body>\n" + 
                "</html>";
        assertEquals(expected, result);
    }

Notice the double brace after the Html constructor. This is done on purpose. Double braces on anonymous class definitions is how many libraries including JATL achieve a DSL.

For more examples please browse the unit test code.

Features

Efficient

  • Stream/Writer based thus very memory friendly for large documents.
  • No unnecessary template preprocessing (parsing and AST building/walking).
  • No reflection used. Templating languages like Velocity use reflection heavily.
  • Very small footprint and only one dependency: commons-lang which most projects already have.

Flexible and easy to learn

Elegant and Convenient

  • Use either fluent (.method()) style chaining or normal style (method();).
  • No more typing annoying <, >
  • No more grep'ing or searching for templates when they can be right in the controller.
  • The XML/XHTML generated is pretty printed to the way you would expect. No more weird whitespace.
  • Fast variable expansion of ${...}
  • Easy to close tags.

Documentation

Please see Javadoc