Support free tutorials









vogella training Training Books



Java Emitter Template (JET) - Tutorial

Lars Vogel

Version 0.4

01.02.2011

Revision History
Revision 0.1 02.08.2009 Lars
Vogel
created
Revision 0.2 - 0.4 02.12.2009 - 01.02.2011 Lars
Vogel
bug fixing and enhancements

Eclipse JET

This article describes the usage of Eclipse JET to transform Eclipse EMF models into other output. This article is based on Eclipse 3.6 (Helios).


Table of Contents

1. Java Emitter Template (JET)
1.1. Overview
1.2. Prerequisites
2. Installation
3. Create your Jet Project
3.1. Convert project
3.2. JET template editor
3.3. JET directive
3.4. Usage of JET
3.5. Include
4. Support this website
4.1. Thank you
4.2. Questions and Discussion
5. Links and Literature
5.1. Source Code
5.2. JET Resources
5.3. vogella Resources

1. Java Emitter Template (JET)

1.1. Overview

JET is a "model to text" engine which allows to generate (text) output based on an EMF model. For example you can generate SQL, Java, XML, Text, HTML, etc. JET uses a template technology which is very closely related to the Syntax of Java Server Pages (JSPs).

In JET you define templates. These templates will be used to create Java Implementation classes. This process step is called translation. .

The Java classes can then be used to create the final output, e.g. a HTML file. This generated class can be initialized and will create the desired result as a String with the method "generate()". This process step is called generation.

JET has three different types of expressions, e.g. directives, expressions and scriplets. Scriplets are started with <% and ended with %> and can contain any java code. Expressions allow to insert string values within the JET output and the directives defines the settings for the JET template.

The JET compiler creates a Java source file for each JET. The suggestion for the JET templates is to use the following naming schema: ClassName.outputsuffixjet, whereby the outputsuffix determins the output, e.g. java for Java Source or html for HTML files.

1.2. Prerequisites

The following will use the webpage example created in Eclipse EMF Tutorial. Please make sure you have create the EMF model described in this article.

2. Installation

Install EMF via the Eclipse Update manager of Eclipse. Select "Modeling" and install "Java Emitter Templates (JET)". You might have to remove the Group items by category to see this entry.

3. Create your Jet Project

3.1. Convert project

To use JET within a project you have to convert the project to a JET project. Select New -> Other and then the following entry.

Select the project which should be converted. Press finish.

A new folder "templates" will be created. All templates which will be created / saved, will be automatically converted by the JET engine.

Select your project, right-click, select properties, select Jet Settings and maintain the "Source Container" as "src". This is there JET will save the Java class after the template generation.

3.2. JET template editor

The JET project provides an editor for JET templates. Unfortunately this editor does currently not work, please see NPE in JET Editor

3.3. JET directive

The first lines of a JET file must consists of the JET directive. The package parameter defines the Java package in which the Java class is generated. The class parameter defines the name of the generated class and imports defines the necessary imports for the created class. The folder in which the code in generated is defined via the "Source Container".

3.4. Usage of JET

The template you have to create must have the "jet" suffix. For example to create a file web.html the template must be called web.htmljet.

Select the project which should be converted. A new folder "templates" will be created. All templates which will be created / saved here, will be automatically converted by the JET engine.

Select your project, right-click, select properties, select Jet Settings and maintain the "Source Container". This is there JET will save the Java class after the template generation.

Create the following file "Simple2HTML.htmljet" in the template folder and save.

<%@ jet package="generator.website"
class="Simple2HTML" imports ="java.util.Iterator org.eclipse.emf.common.util.EList datamodel.website.*;"
%>
<% Webpage website = (Webpage) argument; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> 
<head> <title> <%=website.getTitle()%> </title>

<meta name="description" content="<%=website.getDescription()%>"> 
<meta name="keywords" content="<%=website.getKeywords()%>">
<meta name="robots" content="index, follow">
<meta name="language" content="en">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1> List of Articles Names</h2>
<%EList<Article> list = website.getArticles();
    for (Iterator<Article> iterator = list.iterator(); iterator.hasNext();) {
  Article article = iterator.next();
%>
<b> 
  <%=article.getName()%>
  </b>
  
  
  Imagine other interesting information about the article.
  <br>
  <% 
}%>




</body>

</html> 

After saving the class Simple2HTML will be created in your source folder in package generator.website. This is because the JET directive has defined this.

<%@ jet package="generator.website" class="Simple2HTML" imports ="java.util.Iterator org.eclipse.emf.common.util.EList datamodel.website.*;" %>

Have a look at the generated class. You can now use the generated class to create HTML output. For example use the following code to create HTML pages which lists the articles of each Webpage.

package writeWebpage;

import generator.website.Simple2HTML;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import datamodel.website.MyWeb;
import datamodel.website.Webpage;

public class SimpleWebTest {
  private static String TARGETDIR = "./output/";

  public static void main(String[] args) {
    EMFModelLoad loader = new EMFModelLoad();
    MyWeb myWeb = loader.load();
    createPages(myWeb);
  }

  private static void createPages(MyWeb myWeb) {
    Simple2HTML htmlpage = new Simple2HTML();
    FileWriter output;
    BufferedWriter writer;
    for (Iterator<Webpage> iterator = myWeb.getPages().iterator(); iterator
        .hasNext();) {
      Webpage page = iterator.next();
      System.out.println("Creating " + page.getName());

      try {
        output = new FileWriter(TARGETDIR + page.getName() + ".html");
        writer = new BufferedWriter(output);
        writer.write(htmlpage.generate(page));
        writer.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
} 

3.5. Include

As with JSPs common elements can be included into the JET templates via the include declaration. For example if your webpages all use the same header, you could declare the header in a separate file and include it <%@ include file = "header.html" %>

4. Support this website

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

4.1. Thank you

Please consider a contribution if this article helped you. It will help to maintain our content and our Open Source activities.

4.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

5. Links and Literature

5.1. Source Code

Source Code of Examples

5.2. JET Resources

http://www.eclipse.org/modeling/m2t/?project=jet#jet Eclipse JET Main page

5.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java, compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put all your files in a distributed version control system