Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [XPAND/XTEND] defining aliases
[XPAND/XTEND] defining aliases [message #532458] Mon, 10 May 2010 07:27 Go to next message
MaximeLecourt  is currently offline MaximeLecourt Friend
Messages: 108
Registered: February 2010
Location: France
Senior Member
Hi,

for my m2t project I'm thinking about using aliases, which would depend on the language I generate code in.

As an exemple, if i were to generate SQL, I can't use the words "create", "table", "alter", "group" as names.

So I'm thinking of implementing a JAVA extension that will read a file with the aliases (unless someone has a better idea).

I will write that file in XML, and as the workflow is already written in XML, is there already a parser in the Xpand/Xtend project I could use, instead of writing my own ?

for the file, it will look like a succession of
<name value="create" replace="creates">

Regards,

Maxime


One day I shall master M2T, but that day has yet to come...
Re: [XPAND/XTEND] defining aliases [message #532475 is a reply to message #532458] Mon, 10 May 2010 08:10 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Hi Maxime,

sorry, I don't get the point why you should not be able to use those names. Names that you choose within the model, for templates or whatever do not interfer with language concepts of the target language. The only names that you are don't allow to use are Xpand/Xtend keywords, and latter could also be escaped with ^. OK, "create" is such a conflicting keyword, so the escaping hint might be what you searched for.

Regards,
~Karsten


Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Re: [XPAND/XTEND] defining aliases [message #532551 is a reply to message #532458] Mon, 10 May 2010 12:54 Go to previous messageGo to next message
MaximeLecourt  is currently offline MaximeLecourt Friend
Messages: 108
Registered: February 2010
Location: France
Senior Member
using a metamodel I created, I generate SQL code.

So in my model, I should not use any of the SQL keywords, else when I try tu use the generated text, I'll get an error.

So I decided to create an extension, that will read a beforehand prepared file, and replace occurences in generated code (without modifying the model).

My question was, first is there something already implemented in the Xpand project to do this ?
Or, if not, is there a way to read xml files using Xpand (instead of using Sax, JDom, or StAx) ?


I thought there might already be a way to read an xml file, as the workflow is in xml.


One day I shall master M2T, but that day has yet to come...
Re: [XPAND/XTEND] defining aliases [message #533817 is a reply to message #532458] Mon, 17 May 2010 08:16 Go to previous message
MaximeLecourt  is currently offline MaximeLecourt Friend
Messages: 108
Registered: February 2010
Location: France
Senior Member
I'll post my solution, in case it helps someone.
I'm using the Singleton Pattern.

using that xml file :
<?xml version="1.0" encoding="UTF-8"?>
<option>
<package name="Company" value="company"/>
<package name="Site" value="company"/>
<package name="Group" value="user"/>
<package name="User" value="user"/>
<package name="Building" value="building"/>
</option>


And this Java file
package metamodel;

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import java.util.List;
import java.util.Iterator;

public class Specific {
    
    private static org.jdom.Document document;
    private static Specific instance;
    private static Element racine;
    
    public static Specific getInstance(String name){
        if(null==instance){
            instance = new Specific(name);
        }
        return instance;
    }
    
    private Specific(String name){
        SAXBuilder sxb= new SAXBuilder();
            try {
                document = sxb.build(new File(name));
            } catch (JDOMException e) {
                System.out.println("Catched JDOMEXception");
            } catch (IOException e) {
                System.out.println("Catched IOException");
            }
        racine = document.getRootElement();
    }
    
    public static String getOption(String tag, String name){
        String result = name;
        
        List listOfTag = racine.getChildren(tag);
        Iterator i = listOfTag.iterator();
        
        while(i.hasNext()){
            Element elt = (Element) i.next();
            if(elt.getAttributeValue("name").equals(name)){
                result = elt.getAttributeValue("value");
                break;
            }
        }
        
        return result;
    }

}


String alias(String name):
	getOption("alias", name);

String package(String name):
	getOption("package", name);
	
String getOption(String tag, String name) :
	JAVA metamodel.Specific.getOption(java.lang.String, java.lang.String);
	
String setInstance(String name):
	JAVA metamodel.Specific.getInstance(java.lang.String);
	



Now, at the start of my generation, I call getInstance, and load the xml file in memory.

During the text generation, the classes whose names are in file will be placed in package with given value.

Other classes will be placed in a package that has the class name.

Edit : I forgot the Xtend Extensions.


One day I shall master M2T, but that day has yet to come...

[Updated on: Mon, 17 May 2010 08:20]

Report message to a moderator

Previous Topic:Extend file
Next Topic:[Acceleo] Java application runner
Goto Forum:
  


Current Time: Fri Apr 26 17:19:59 GMT 2024

Powered by FUDForum. Page generated in 0.03157 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top