JET : Java Emitter Templates Arthur J. Wanner, Jr. Frankie Kwok Outline What is M2T? What is JET? How JET works: Models & Templates Small Hello World Example Tutorial JET language and syntax PathwayMATE code generation example What is M2T? (Model-To-Text) Starts for a “model” – some sort of structured document Examples: XML, UML, EMF-based models During execution, model is traversed and “templates” are executed, creating strings (which can be used to output to stdout, files, etc.) The Idea of JET Advantages of JET Java as a powerful template language Skeletons for customized generator classes Arbitrary source model (e.g. models, arrays / lists, files, …) Arbitrary text output (e.g. text-reports, code, xml, …) Disadvantages of JET Bound to Eclipse Compilation of generator classes required Produces a .JETEmitter project for internal purposes If a template is changed, the generator class is overwritten Hard to debug No syntax check for templates (JET editor with syntax highlighting etc. still buggy) Transformation logic and output are mixed up Optimized for text output generating models is more complicated Hello World Example Create a new Project in Eclipse Convert the Project to a JET project In Navigator, right click on the project... New > Other... > Java Emitter Templates > Convert Projects to JET Projects This will create a 'templates' top-level dir In the templates dir create a new file called helloworld.txtjet <%@ jet package="hello"class="HelloWorldTemplate" %> Hello, <%=argument%>! Hello World Example continued... When the template file is saved it creates a new template implementation class The implementation class name is taken from the class property and is located in the package specified by the package property <%@ jet package="hello"class="HelloWorldTemplate" %> Hello, <%=argument%>! Hello World Example continued... The implementation class is to be used by your code to output the generated text... The generate method takes an object parameter named 'argument' which can safely be used as a string in your template. package hello; public class HelloWorld { public static void main(String args[]) { HelloWorldTemplate hw = new HelloWorldTemplate(); System.out.println(hw.generate("World")); } } JET Language Directives <%@ … %> There are two types of directives: jet (must be in 1st line!) parameters: package, class, imports, startTag, endTag, skeleton, nlString include (optional) parameters: file, fail <%@ jet package="hello" class="HelloWorldTemplate" %> Hello, <%=argument%>! JET Language Include Directive The include directive is used to substitute text and/or code at template translation-time. The <%@ include file="urlSpec" %> directive inserts the text of the specified resource into the jet template file. The included file may have JET scripting elements which will also be processed. The value of this attribute is the URI of the location of the file to include. This URI can be either an absolute path or a relative path. Relative URIs are always interpreted as relative to the folder of the template that contains the include directive. Example: The following example requests the inclusion, at translation time, of a copyright file. <%@ include file="copyright.jet" %> JET Language Expressions Arbitrary java expressions that output strings <%= … %> <%@ jet package="hello" class="HelloWorldTemplate" %> Hello, <%=argument%>! <%=new java.util.Date()%> JET Language Scriptlets Arbitrary java code <% … %> for loops <%@ jet package="hello" class="HelloWorldTemplate" %> <% for (int i = 0; i < 5; i++) { %> Hello, <%=argument%>! <%}%> JET Language Predefined variables argument object to which the transformation is applied to can be any object but by default it's cast to a string <%@ jet package="hello" imports="pack.Person" class="PersonTemplate"%> <% Person person = (Person) argument; %> Name = <%= person.getName(); %> Height = <%= person.getHeight(); %> Eye Color = <%= person.getEyeColor(); %> JET Language Predefined variables stringBuffer output of the transformation arbitrary text can be passed to the stringBuffer used within scriplets <%@ jet package="hello" class="HelloWorldTemplate" %> <%String hw = "Hello " + argument + NL; %> <%for(int i = 0; i < 3; i++, stringBuffer.append(hw))%> <%;%> JMerge JMerge is a technology developed by the Eclipse Modeling Framework (EMF) so that code generators that produce Java code can permit user modification of that code, while still permitting the code generator to be re-executed. A contract between Code Generator and the Consumer The Code Generator shall only update Java elements with an intact @generated tag. The Code Generator may remove Java elements with an intact @generated tag this is no longer specified by the code generator input. JET : Java Emitter Templates Arthur J. Wanner, Jr. Frankie Kwok PathwayMATE Tour......
© Copyright 2024 ExpyDoc