Skip to content
Gabor Szarnyas edited this page Oct 30, 2016 · 4 revisions

Howto

Serializing Xtext models

The related StackOverflow post pretty much gets it right.

A similar Xtend code is the following:

def static void save(Cypher cypher, String filename) {
  EcoreUtil.resolveAll(cypher)

  Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(CypherUtil.MODEL_EXTENSION,
    new XMIResourceFactoryImpl())

  val resourceSet = new ResourceSetImpl
  val filePath = filename + "." + CypherUtil.MODEL_EXTENSION
  val uri = URI.createFileURI(filePath)
  val resource = resourceSet.createResource(uri)
  resource.contents.add(cypher)

  val saveOptions = new HashMap<String, Object>
  saveOptions.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
  saveOptions.put(XMIResource.OPTION_ENCODING, "UTF-8");
  resource.save(saveOptions)
}

For me, the generated models could not be opened with the Sample Reflective Ecore Model Editor. This however did not work for me as it threw an org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri ... exception.

Theoretically, adding the OPTION_SCHEMA_LOCATION option should have helped, but it had absolutely no effect (see the Eclipse forum).

The solution was to have the Xtext project as a plug-in project in the workspace. You do not have to install it, the only restriction is that the project has to be free from errors & opened.

The EcoreUtil.resolveAll(...) call is important, else the model may contain unresolved references.

Gradle Xtext web does not work

If you press Ctrl + space, the following error appears on the Gradle output:

1) No implementation for org.eclipse.xtext.preferences.IPreferenceValuesProvider annotated with @org.eclipse.xtext.formatting2.FormatterPreferences() was bound.
  while locating org.eclipse.xtext.preferences.IPreferenceValuesProvider annotated with @org.eclipse.xtext.formatting2.FormatterPreferences()
    for field at org.eclipse.xtext.web.server.XtextServiceDispatcher.formatterPreferencesProvider(XtextServiceDispatcher.java:98)
  while locating org.eclipse.xtext.web.server.XtextServiceDispatch
Clone this wiki locally