如果您有一个 XMI 并且只想从中生成代码,那么您根本不需要 Xtext。只需从一个 Java 项目开始(我会使用一个插件项目,以重用依赖项管理)并开始编码:
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.resource.Resource$Factory$Registry
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
class MyCodeGenerator {
def static void main(String[] args) {
new MyCodeGenerator().generate("mymodel.xmi")
}
def generate(String file) {
doEMFSetup
val resourceSet = new ResourceSetImpl
val resource = resourceSet.getResource(URI.createURI(file), true)
for (content : resource.contents) {
generateCode(content)
}
}
def dispatch generateCode(MySpecialType it) '''
public class «name» {
«FOR member : members»
«ENDFOR»
}
'''
def dispatch generateCode(MyMember it) '''
private «type» «name»;
...
'''
def doEMFSetup() {
// EPackage$Registry.INSTANCE.put(MyPackage.eINSTANCE.nsURI, MyPackage.eINSTANCE)
Resource$Factory.Registry.INSTANCE.extensionToFactoryMap.put("xmi", new XMIResourceFactoryImpl);
}
}
您需要添加到 Manifest 的依赖项:
Require-Bundle: org.eclipse.xtend.lib,
com.google.guava,
org.eclipse.xtext.xbase.lib,
org.eclipse.emf.common,
org.eclipse.emf.ecore,
org.eclipse.emf.ecore.xmi