
Contents |
There are two possibilities to change the highlighting:
EMFText_Concrete_Syntax_Zoo_Text_Adventure
Note that some tokens are available by default (e.g., "TEXT" or "QUOTED_34_34" - for ['"','"']). You can see these tokens in the outline view.
This is simple: Open the file in the text editor generated for your language. Use File > Save as... and save the file at a new location with the extension "xmi". Done.
If you have two (or more) text syntaxes defined for your metamodel, which would have different file extensions, File > Save as... can also be used to convert models from one syntax to another.
If you want to do it programmatically, you can use code like this:
// If you work outside Eclipse, you first have to register the different
// resource factories with EMF:
//register <DSL>ResourceFactory for "dslExtension" file extension
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
).put("dslExtension", new <DSL>ResourceFactory());
//register XMI resource factory for all other extensions
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap(
).put(Resource.Factory.Registry.DEFAULT_EXTENSION,
new XMIResourceFactoryImpl());
// Then you can load and save resources in the different
// formats using a resource set:
ResourceSet rs = new ResourceSetImpl();
//load xmi resource
Resource xmiResource = rs.getResource(
URI.createFileURI("MyFile.xmi"),true);
//create an empty DSL resource (will be an instance of <DSL>Resource)
Resource dslResource = rs.createResource(
URI.createFileURI("FileToPrintTo.dslExtension"));
//transfer content from XMI to DSL resource
dslResource.getContents().addAll(xmiResource.getContents());
//save the dsl resource (this will automatically use the <DSL>Printer)
dslResource.save(null);
EMFText resources can be loaded without running Eclipse. To do so, the generated resource factory must be explicitly registered with the Ecore resource set. The follwing code shows how to load an EMFText resource and print all objects to System.out:
import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
public class UseDSLOutsideEclipse {
public static void main(String[] args) throws IOException {
MyGeneratedResourceFactory factory = new MyGeneratedResourceFactory();
ResourceSet rs = new ResourceSetImpl();
rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
"myFileExtension", factory
);
Resource resource = rs.getResource(URI.createFileURI(
"MyFile.myFileExtension"), true);
Iterator<EObject> i = resource.getAllContents();
while (i.hasNext()) {
Object o = i.next();
System.out.println(o);
}
}
}
EMFText implements the EMF resource API. Hence, in principle it is possible to transform models parsed by EMFText using MWE workflows and transformations based on Xpand and Xtend. By default, MWE workflows are not aware of generated resource factories. To register parsers generated by EMFText you may subclass the MWE Reader as follows:
public class Reader extends org.eclipse.emf.mwe.utils.Reader {
public Reader(){
super();
//register your resource factory
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
"<YOUR_FILE_EXTENSION>",
new <YOUR_LANGUAGE_NAME>ResourceFactory()
);
//register all ecore models needed for reference resolving as follows:
EPackage.Registry.INSTANCE.put(
"<METAMODEL_NAMESPACE_URI>",
<METAMODEL_PACKAGE_NAME>Impl.init()
);
}
}
Note for EMFText 1.2.0: To parse xpt files, Xpand (as shipped with MWE 0.7.2) uses a version of the ANTLR runtime which is incompatible with the one used in EMFText 1.2.0. We have fixed this issue in later releases. If you really need to stick with EMFText 1.2.0, As a workaround, you may renamed the package names of the generated ANTLR runtime plugin to org.emftext.commons.antlr3_1_1.