Xpand templates on XSDs

The screen definitions of our application are part of our model as well. They need to be exported to XML to be processed by our screen-interpreter at runtime. Our client is based on Eclipse RCP and we allow to apply hooks to certain steps within the interpretation process. This is required to let the developer implement behavior that differs from the standard behavior of the interpreter. Hooks are registered via an extensions using the name of a model element for identification.

The code within a hook references UI elements as it implements UI behavior. To access an element, it’s name has to be used as well, and that is quite error-prone. The name within the model might change but the code still references the old. So we decided to use Xpand to generate descriptors (it is only a list of String constants to be honest) for the modeled UI elements. The developer may use the constant, so the code will fail to compile if the name of the element changes in the model.

Generating these constants is a pretty easy thing as the XSD for the screen model can simply be used as meta-model.

...
<component class="org.eclipse.xtend.typesystem.xsd.XMLReader">
   <modelSlot value="model" />
   <uri value="${screen.model.project}/model/screen.xml" />
   <metaModel id="screenXMLModel">
      <schemaFile value="${screen.profile.project}/model/screen.xsd" />
      <registerPackagesGlobally value="true" />
   </metaModel>
</component>
...

When activating the XSD metamodel within the project preferences in the Eclipse IDE, the Xpand editor recognizes an XSD in the project’s classpath as metamodel and you may start creating the templates.
Pretty easy and very handy!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>