2

I am working on a java-based framework for a Home Automation System. One of the requirements for this home automation system is that developers should be able to easily extend the functionality. I thought that a great way to simplify things would be to allow developers to create a simple XML file that describes the features of their peripheral; namely the commands that it can handle and the events that it can broadcast. (Similar to the WHEN and THEN events in systems like Wig Wag: http://www.kickstarter.com/projects/wigwag/wigwag-scan-it-control-it-rule-it-share-it)

I would like to parse the XML file that the developer provides and automatically generate Java code to interface with the rest of the system, sort of like what Google does with their R.java file in the Android SDK. (I realize that this is handled by APT and not Eclipse) Some solutions I have looked at:

  • Eclipse Java Emitter Templates (JET) - JET is very complicated to use and the documentation is very poor/nonexistant. I got it to produce a Java Implementation file with a generate() method, but I cannot find out how to get the IDE to generate the Java code I defined in the template. I tried this tutorial: https://weblogs.java.net/blog/survivant/archive/2009/01/template_code_g.html but I could not get the Car example to work; I get this error:

Errors occurred during the build.

Errors running builder 'JET Builder' on project 'JETTest'.

Bad directive in 'main.jet' at line 1 column 1

Bad directive in 'main.jet' at line 1 column 1

Bad directive in 'main.jet' at line 1 column 1

Bad directive in 'main.jet' at line 1 column 1

  • JAXB - This appears to be more for marshalling/unmarshalling Java objects, and does not solve my problem
  • ANT Compiler - I got this to generate a class, but it is not quite as flexible I want (e.g. I want to be able to get the package name from the developer-defined XML, but the output directory is statically assigned in the ANT build.xml file.)
  • Apache Velocity - glanced at it but I was already frustrated about JET and it did not appear to be a great solution upon cursory inspection. (I'd be happy to be proven wrong :))

The ANT solution is so far looking best to me because it actually works, but I like JET because it seems to work the smoothest.

Now my question. Should I just go with ANT, or is JET that much better? Furthermore, if JET is that much better, can somebody PLEASE point out a simple tutorial that is not completely out of date?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 2
    If it's being defined by a developer then let them expose their functionality through a plugin. Or use Groovy/JavaScript/etc and let the whole thing be done through actual code. Or etc. If all you want is to generate classes, *any* templating mechanism should work, e.g., Velocity, FreeMarker, Mustache, whatever. Your question is too broad, opinion-based, and doesn't contain enough information to help figure out what's wrong with your current solution, why Velocity isn't a good solution, etc. – Dave Newton Jan 23 '14 at 18:43
  • Do you actually need to generate Java source, or would it suffice to have bytecode emitted at runtime? – Mike Strobel Jan 23 '14 at 19:24

1 Answers1

0

I look at it this way - Use the framework or tool which is easier and simpler to use. More importantly has good documentation. So ANT looks like your best bet.

However may I suggest a different alternative - Groovy Templates. http://groovy.codehaus.org/Groovy+Templates

Does the same job but is really simple to use and has good documentation. We use this in our company to transform orders from some really old systems.

Dinesh Arora
  • 2,115
  • 3
  • 24
  • 30