Trying to generate DSL files using Java [message #1777723] |
Sat, 02 December 2017 18:09  |
Eclipse User |
|
|
|
Hi !
I need to create a program able to generate files following a DSL i made using Xtext. I have some problems saving the file and I can't have it using my DSL syntax. The only thing I manage to obtain is a XMI file containing the data of the object.
My project is focusing on charts so my DSL extension is ".chart".
This part is where I instantiate a Java object containing the data :
ChartPackage.eINSTANCE.eClass();
ChartFactory factory = ChartFactory.eINSTANCE;
Chart myChart = factory.createChart();
myChart.setTitle("Population of countries");
Data data = factory.createData();
data.setType("Population (millions)");
DataEntry entry1 = factory.createDataEntry();
entry1.setName("France");
entry1.getValues().add(66);
data.getEntries().add(entry1);
DataEntry entry2 = factory.createDataEntry();
entry2.setName("Canada");
entry2.getValues().add(31);
data.getEntries().add(entry2);
DataEntry entry3 = factory.createDataEntry();
entry3.setName("USA");
entry3.getValues().add(300);
data.getEntries().add(entry3);
myChart.setData(data);
And then I am able to create an XMI file using this :
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("chart", new XMIResourceFactoryImpl());
ResourceSet resSet = new ResourceSetImpl();
Resource resource = resSet.createResource(URI
.createURI("charts/test.chart"));
resource.getContents().add(myChart);
try {
resource.save(Collections.EMPTY_MAP);
System.out.println("File saved!");
} catch (IOException e) {
e.printStackTrace();
}
The problem is that I want the data in my DSL syntax, not in XMI. I tried this code I found on the forum but it didn't work. The s variable is supposed to contains the text of the object following my syntax ;
new ChartStandaloneSetup().createInjectorAndDoEMFRegistration();
Injector injector = Guice.createInjector(new ChartRuntimeModule());
Serializer serializer = injector.getInstance(Serializer.class);
String s = serializer.serialize(myChart);
When executing the previous code I have the following exception (ExecutionPerso is the name of the class where I have my main method) :
Exception in thread "main" java.lang.RuntimeException: No Context for Chart could be found
at org.eclipse.xtext.serializer.impl.Serializer.getIContext(Serializer.java:165)
at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:126)
at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:178)
at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:55)
at ExecutionPerso.main(ExecutionPerso.java:62)
I also tried to write it that way :
Injector injector = new ChartStandaloneSetup().createInjectorAndDoEMFRegistration();
ResourceSet rs = injector.getInstance(ResourceSet.class);
Resource r = rs.createResource(URI.createURI("charts/test2.chart"));
r.getContents().add(myChart);
try {
r.save(null);
} catch (IOException e) {
e.printStackTrace();
}
But I have the following exception :
Exception in thread "main" java.lang.NullPointerException: Invalid context: Chart returns Chart
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
at org.eclipse.xtext.serializer.sequencer.AbstractSyntacticSequencer.init(AbstractSyntacticSequencer.java:442)
at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:111)
at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:128)
at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:192)
at org.eclipse.xtext.resource.XtextResource.doSave(XtextResource.java:386)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1430)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:999)
at ExecutionPerso.main(ExecutionPerso.java:54)
Can you help me find what is the problem please ? Thank you !
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04300 seconds