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?
Community comments
what kind of mobile platform it support?
by steve zhang,
Re: what kind of mobile platform it support?
by Jean-Jacques Dubray,
Nice
by Sebastian Zarnekow,
Re: Nice
by Jean-Jacques Dubray,
what kind of mobile platform it support?
by steve zhang,
Your message is awaiting moderation. Thank you for participating in the discussion.
I am interested in this concept, but what kind of platform it support right now?
iPhone, android, j2ME. It seems the DSL generates the java code.
Re: what kind of mobile platform it support?
by Jean-Jacques Dubray,
Your message is awaiting moderation. Thank you for participating in the discussion.
Steve:
Xtext / Xpand is actually a framework from which you can generate any artefact you want (code, config files, documentation...). I successfully built an Objective-C code generator with it.
I found Daniel's article interesting because it shows how easy it is do that by providing an end-to-end scenario.
Nice
by Sebastian Zarnekow,
Your message is awaiting moderation. Thank you for participating in the discussion.
Jean,
thanks for the article on Xtext. It's always good to see how people use the technologies and frameworks, that I helped to develop.
Just one remark: The Xtext prototype which was part of the openArchitectureWare project was developed by Sven Efftinge [1]. When Xtext became a project at Eclipse it was completely rewritten by a team of great guys around Sven [2]. It's us, who share the vision about Xtext and its future.
Regards,
Sebastian
[1] blog.efftinge.de/
[2] www.eclipse.org/projects/project_summary.php?pr...
Re: Nice
by Jean-Jacques Dubray,
Your message is awaiting moderation. Thank you for participating in the discussion.
Sorry about that, I was under the impression that Markus was behind it. I appreciate the clarification and I apologize for not looking into it further. I'll correct the article.