Search code examples
javaeclipsedslxtextxtend

How to access instance variable of Java in DSL?


I have a data model(Car.java), source template(dsl file) and application program.

Template Class Car.java

public class Car {

    public String name;
    public  int price;

    public Car() {
    }
}

Template file index.template

<body>
    <table>
        <tr>
            <th>Name</th>  
            <th>Price</th>
        </tr>        
        {FOR car : cars}
            <tr>
                <td>{car.name}</td> 
                <td>{car.price}</td>
            </tr>   
    </table>                
</body>

Note: Plain text enriched with template notation elements which are enclosed by { }

Using the template in the program:

Car c1 = new Car("??", 52642);
Car c2 = new Car("??", 29000);
Car c3 = new Car("??", 9000);
List<Car> cars= new ArrayList<>();
cars.add(c1);
...
...

The output should be all the elements of the cars collection.

Problem?

I haven't any access to cars (instance variable) from the application program in a template file. How can I get instance variable cars (not java class like Car) in index.template file?


Solution

  • Have a look about Xbase. In the official documentation: https://www.eclipse.org/Xtext/documentation/305_xbase.html

    Xbase provides integration for all DSLs with the java type system.