BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Using Eclipse Xtext to Simplify Mobile Application Development

Using Eclipse Xtext to Simplify Mobile Application Development

This item in japanese

Daniel Schneller wrote an introduction on Xtext by creating a grammar that provides:

an easy to use and reliable way for configuring navigation through mobile Java applications

In the past, his team hard-coded the application's navigation paths, but because of the growing complexity of the applications, they needed a new solution:

First we thought about an XML based configuration, but this seemed to be a hassle to write (and read) and also would mean we would have to pay the price of parsing it on every application startup.

Recently, they stumbled upon Eclipse Xtext:

[Xtext is] an Eclipse based framework/library for building text based DSLs.
In short, you just provide a grammar description of a new DSL to suit your needs and with – literally – just a few mouse clicks you are provided with a content-assist, syntax-highlight, outline-view-enabled Eclipse editor and optionally a code generator based on that language.

Xtext was originally developed by Sven Efftinge as part of openArchitectureWare and became an integral part of Eclipse this year. It is based on EMF (Eclipse Modeling Framework) and ANTLR.

Daniel created the following grammar:

navigation rules for MyApplication

mappings
{
 map permission
AdminPermission to "privAdmin"
 map permission
DataAccessPermission to "privData"

 map coordinate
Login to "com.danielschneller.myapp.gui.login.LoginController"
in
"com.danielschneller.myapp.login"
 map coordinate
LoginFailed to "com.danielschneller.myapp.gui.login.LoginFailedController"
in
"com.danielschneller.myapp.login"
 
 map coordinate
MainMenu to "com.danielschneller.myapp.gui.menu.MainMenuController"
in
"com.danielschneller.myapp.menu"

 map coordinate
UserAdministration to "com.danielschneller.myapp.gui.admin.UserAdminController"
in
"com.danielschneller.myapp.admin"
 map coordinate
DataLookup to "com.danielschneller.myapp.gui.lookup.LookupController"
in
"com.danielschneller.myapp.lookup"
}


navigations
{
 define navigation USER_LOGON_FAILED
 define navigation USER_LOGON_SUCCESS
 define navigation OK
 define navigation BACK
 define navigation ADMIN
 define navigation DATA_LOOKUP
}


navrules
{
 
from Login
  on navigation USER_LOGON_FAILED
   go to
LoginFailed
  on navigation USER_LOGON_SUCCESS
   go to
MainMenu

 
from LoginFailed
  on navigation OK
   go to
Login

 
from MainMenu
  on navigation ADMIN
   go to
UserAdministration
   
with AdminPermission
  on navigation DATA_LOOKUP
   go to
DataLookup
   
with DataAccessPermission
 
 
from UserAdministration
  on navigation BACK
   go to
MainMenu

 
from DataLookup
  on navigation BACK
   go to
MainMenu

}

 

Xtext lets you define both the syntax and the metamodel behind your grammar at the same time. From there, Xtext generates the code of an Eclipse plugin that will allow developers to create metadata:

The generated editor comes complete with intellisense, color coding, syntax error detection and even the ability to detect broken references, across multiple metadata files.  

Xpand was then used to parse and translate the grammar into a HashMap-based data structure: 

public class NaviRules {

   
private Map navigationRules = new Hashtable();

   
// ...

   
public NaviRules() {
       
NaviDestination naviDest;
        naviDest = new NaviDestination();
        naviDest
.action = "USER_LOGON_FAILED";
        naviDest
.targetClassname = "com.danielschneller.myapp.gui.login.LoginFailedController";
        naviDest
.targetBundleId = "com.danielschneller.myapp.login";
       
        store
("com.danielschneller.myapp.gui.login.LoginController", naviDest);
       
        naviDest = new NaviDestination();
        naviDest
.action = "USER_LOGON_SUCCESS";
        naviDest
.targetClassname = "com.danielschneller.myapp.gui.menu.MainMenuController";
        naviDest
.targetBundleId = "com.danielschneller.myapp.menu";
       
        store
("com.danielschneller.myapp.gui.login.LoginController", naviDest);
       

 
// =============================================================================
       
     
  naviDest = new NaviDestination();
        naviDest
.action = "OK";
        naviDest
.targetClassname = "com.danielschneller.myapp.gui.login.LoginController";
        naviDest
.targetBundleId = "com.danielschneller.myapp.login";
       
        store
("com.danielschneller.myapp.gui.login.LoginFailedController", naviDest);
       
       
// .... and so on ...
   
}
}

Daniel sees several advantages over XML for mobile applications:

No XML parsing necessary on application startup, saving some performance
Validation of the navigation rules ahead of time, preventing parse errors at runtime
No libraries needed to access the information – by putting everything in a simple HashMap we do not have to rely on any non-standard classes whatsoever

Textual DSLs are maturing rapidly and finding a broad range of applicable scenarios on different platforms. Did you already use a textual-DSL in a real world scenario? What for? What is your feedback?

Rate this Article

Adoption
Style

BT