Tuesday, February 10, 2009

Add branding to the XML files you generate from your EMF model

What if you want to suggest that an XML file is generated using your product?
If you look at some of these commercial tools they add a comment which proudly says "used XXX v a.b.c to generate this file." Just out of curiosity I wanted to do the same using EMF resource save api.

Guess what! It is possible to do such a thing with EMF resource api. There's this interface called ResourceHandler inside org.eclipse.emf.ecore.xmi.XMLResource.java which can be implemented as required. I have provided an example for your benifit.

public class MyResourceHandler implements ResourceHandler {

* you have the file handle of the XML!
public void postSave(XMLResource resource, OutputStream outputStream,
Map options) {
// append your comment here.
byte[] trailcomment = "some comment";
// enclose it in XML comment tags.
try {
catch (IOException e) {

// may be log something here.
// do NOT close the stream here. EMF resource save will eventually do it for you.

You can find the link to the EMF newsgroup thread here.

No comments: