Search code examples
javaphptemplatespresentation

PHP-like presentation language for Java


I have a Java application that currently outputs XML of objects in a JTree. It recursively loops through the nodes and when it finds an object it will print it as a tag with some attributes, and if it is a parent node it will recurse deeper.

What I really want to do is not constrain the output to XML but instead implement a presentation interpreter such as PHP which can access the model in my application but display it in a completely customized manner.

So the requirements are:

  • The applicaton needs to be able to load a template file (PHP or something else)
  • It should then internally interpret this template which will access the Java model
  • Finally it should output the result of combining the data and the template

I'm struggling to understand what is required. I'm not sure template engines are the answer because I need recursive iteration through a tree. I'm not sure whether I am on the right track with a PHP bridge because it seems I need something to execute the PHP externally (like a web browser).

Does anybody have any suggestions about what technology would be suitable for this purpose? I'm looking for a shove in the right direction as I'm really not sure the best way to approach this.

I'm even considering defining my own grammar and scripting language but this feels like reinventing the wheel.

All ideas welcome :)


Solution

  • It sounds like what you want is to transform an XML document. The obvious solution for that is XSLT.

    Yes, there are a number of template engines in Java - Groovy is one of the more popular ones. It can undoubtedly do what you want, though I'm not entirely sure how you'd glue it into your app - it's a web framework. Worth checking out.