I have created an Ecore metamodel and generated an editor through EMF. To generate my output code I parse the resource set (XMI) when a diagram is created and then map these resources to my own Java classes. These Java classes are responsible for generating my output which is some javascript files based on few template files (.stg).
Everything works fine but my question is where do technologies such as Xtend/Acceleo come in? Do I need any of them? I know that they are template based but since I have my own templates that are rendered through my Java classes why would I need to use Xtend for example? I am totally confused.
Thank you so much
Xtend is a general purpose language. You could call it a "better Java". This is especially true for templating, because Xtend has nice template expressions. So if you want your generator code to be more readable, you could replace some Java classes with Xtend classes. You don't need to rewrite everything, Xtend interoperates seamlessly with Java. Just rewrite the parts that benefit most from Xtend's rich language features.
You are using the eclipse Enterprise Modelling Framework (EMF). This provides you with the ECore meta modell and can generate basic dialog based editors. Basically EMF brings your diagrams from the XMI file format to a standardized object model, similar to a DOM.
Probably you also use the Graphical Editor Framework (GEF) to visualize your diagrams. It takes your DOM and renders it to a graphical representation. Usually you can also modify the graphics using graphical user interface, which will then also change the contents of your DOM.
Parallel to the GEF there also exists a textual editor framework called xText. It brings your DOM to a textual representation. You can then use a text editor to change the textual representation. xText can parse this changed text and change the DOM accordingly. Additionally it generates a nice Eclipse editor with syntax high lightning, code folding, code completion and possibilities for extensions for custom code completion and refactoring.
xText can be augmented by XBase. XBase is a basic programming language that runs on the Java VM. You can then provide a mapping from your ECore artefacts to XBase concepts (e.g. which ECore object represents an if statement or a loop). XBase can then generate Java bytecode and hence convert your ECore model to runnable code.
XTend is a general purpose language. It is often used as a very advanced template engine to to convert your ECore model to text. In contrast to xText there is no way back to convert the generated text back to the ECore DOM. Of course you can use any template engine or even xText/xbase to generate your runnable code according to your needs.
Related
My current project (name it IoTSuite) takes high-level specifications, parses them, and generates code in Java and Android. In this project, I have written ANTLR grammar to parse the high-level specification and I have used StringTemplate for the code generator.
However, due to nice editor support and syntax coloring features, I have used the xtext grammar(same as the ANTLR grammar, but it has been written in xText). Now, I perform the following three steps:
Step 1: I have written xtext grammar as show in the following image.
Step 2: Now, I generate the supporting artifacts from MyDsl.xtext grammar and then run the whole project as Eclipse Application, as shown in the following image.
Step 3: After this, Eclipse application runs that allows me to write high-level specifications, as shown in the following image.
Now, my requirement is - I would like to parse the high-level specification, as shown in the step3, using the ANTLR grammar written in IoTSuite project.
In brief, my requirement is to connect the high-level specification written in xtext with the ANTLR parser. How could I do it? Please let me know in case you need more clarity for the solution.
Xtext generates its own ANTLR grammar. This grammar is also instrumented with code to instantiate the AST for the grammar. The produced parser cannot be exchanged.
What you likely want to do is to invoke your own ANTLR parser when saving a resource. The IGenerator hook could be used to invoke your parser and generator.
Please consider also to use Xtend for code generation, that's much nicer than StringTemplate and integrates smoothly with Xtext infrastructure. Xtext's Statemachine Example shipped with the SDK shows how to do this (and to use the IGenerator interface).
I've been using ZPT in python recently and I love the templating language. I was looking for something similar for Java but couldn't really find anything I liked as well. The closest thing is FreeMarker.
The problem with FreeMarker and the other Java template engines I looked at was their JSP style syntax that allows for non-conforming XML. I was just wondering if there was a Java template engine that is similar to Zope Page Templates such that it's an "attribute" language that requires valid xml.
I think think there quite some of the template engines your looking for:
Cambridge
Thymeleaf
JTP (dead - but exact implementation)
javaTAL (dead - but exact implementation)
Other approaches supporting valid html are:
Snippetory (Not bound to html)
Lift (scala)
FreeMarker has a nasty dependency on AWT. It makes it impossible to use with things like Google App Engine.
I prefer to use StringTemplate for all my Java templating needs. It is the only Java based template system that strictly separates the logic from the template.
StringTemplate is a java template engine (with ports for C#, Python,
Ruby, and Scala) for generating source code, web pages, emails, or any
other formatted text output. StringTemplate is particularly good at
multi-targeted code generators, multiple site skins, and
internationalization/localization
Its distinguishing characteristic is that it strictly enforces
model-view separation unlike other engines. Strict separation makes
websites and code generators more flexible and maintainable; it also
provides an excellent defense against malicious template authors.
Since you are generating XML
Another solution that isn't obvious at first sight is using JAXB. We have a project here that requires us to generate XML, we have very well defined XSD files for the output files, building the objects and marshalling them is super easy and very painless.
The java template engine you will find to be most similar to Chameleon is Thymeleaf.
There is Distal for client based templating.
There are currently two tal implementations for Java that I know of:
Java ZPT
JPT
There is also Apache Velocity. Although it doesn't require that your templates be valid XML. That may be a deal breaker for you given the question. You could likely enforce that rule with extensions to the core classes though.
What about GXP?
There's also LSP and xtempore.
do you know of any tool which creates an AST from a Java program or class and creates an XML representation (Collection or single XML document) from the AST?
kind regards,
Johannes
Not any tools directly, but http://www.antlr.org/ is the defacto tool for building ASTs from any general language. And there exists several grammar files for Java that you can repurpose for your own programs. So grab ANTLR, use the latest Java grammer, and write out the XML representation you want.
Our DMS Software Reengineering Toolkit with its Java Front End can do this directly. You ask DMS to parse the file, and produce an XML dump using a command line switch ++XML.
See What would an AST (abstract syntax tree) for an object-oriented programming language look like?.
As a general rule, we don't recommend this, for several reasons:
XML output for real files is really enormous, and takes a lot of time to write and read
Most people do this because they believe with an XML representation that just a little bit of XSLT will get them what they want
If you intend to modify the code, once you have the XML you pretty much can't regenerate it.
The machinery that DMS provides (attribute grammars, symbol tables, flow analyses, pattern matching and source-to-source transformations, source regeneration from the AST, is what you really want, and you get access to it by using DMS after the parsing step without exporting the XML ever
I want to generate Java Swing Code by processing MS Visio created wireframes. Is there any tool which can support this?
I know a company called Obeo that markets a tool called Acceleo that is specialized into model to text transformations.
Based upon various Eclipse technologies, it allows one to generate code from DSL/DSL.
They have a site allowing one to use already existing engines, as an example the cinematic model, which is precisely what you're looking for, I guess.
Unfortunatly, to use this tool (and in fact all tools in the generation domain, you'll have to transform your Visio diagram into an XMI one, which is not impossible.
I need to read UML diagrams that are serialized into XMI. Is there any library that would allow me to conveniently read UML XMI - by conveniently I mean having some methods to iterate over classes/packages/methods/attributest etc. in model.
I tried EMF, but I am unable to find any tutorial that would show how to import XMI containing UML. I have also found NSUML/NSMDF but the link to documentation is broken.
I don't think what you want exists. All tools importing XMI that I know (including my XMI transformation service: http://modeling-languages.com/content/xmi2-tool-exchanging-uml-models-among-case-tools) work by parsing the whole XMI file and creating the corresponding UML model in main memory. Then you can access to the UML elements by querying the model.
To iterate on the XMI model you can easily use XML libraries to select the pieces of the XMI file you are interested in.
Maybe simply have a look at Transforming XMI to HTML. This should help you to get started with XSLT and XMI.
It depends on the version of UML that you are trying to read. The NSUML package that you found only works with UML 1.3. The NetBeans MetaData Repository (MDR) will read handle UML 1.4 and UML 1.5. It can be used as a standalone library (that's what ArgoUML uses), so you don't need NetBeans. For UML 2.x, I think the Eclipse EMF & UML2 libraries are your only choice. EMF is a dependency, but it's really the UML2 stuff that you want. Like with MDR and NetBeans, you don't need Eclipse. ArgoUML uses the Eclipse libraries for its UML 2.x support.
ArgoUML can import XMI to its UML model and generate code for Java, C++, C#, PHP4 and PHP5.
In order to read XMI/UML using EMF, you will need to find, generate or write ECore model for UML. Once you have one of those, you can generate the in-memory classes for manipulating the UML and leverage the other EMF infrastructure.
But a simpler route may be to use the UML2 infrastructure developed by the Eclipse MDT project. (My guess is that it uses EMF under the hood and that there is an ECore model for UML2 in there somewhere.)
What you are looking for is the Java specification for JMI (“Java Metadata Interface”), which implements MOF for describing Metadata and XMI for transporting the Metadata.
Unfortunately I am currently looking for implementations of JMI myself and suns JMI page seems to be a bit outdated.
The open source implementation by netbeans seems to have been dropped for version 6.