Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Inserting Comments at the Top of an XText Resource at Serialization Time
Inserting Comments at the Top of an XText Resource at Serialization Time [message #1047610] Tue, 23 April 2013 12:47 Go to next message
Ronan B is currently offline Ronan B
Messages: 247
Registered: July 2009
Senior Member
Hi,
I'm generating an XText resource as an output of an ATL transformation. I want to be able to put in a comment at the top of the output file to give the user some details about the version of the transformer used to generate the output file.

I'm finding it very hard to figure out how to do this in Xtext. The forum/manual talk about comments but it is normally about preservation rather than insertion. Any advise is appreciated. Usualy, when not using Xtext, I just edit the Ecore GenModel class that extends EFactoryImpl and add comments in at the right spot as Ecore can serialize them for me e.g. In the createDocumentRoot() method if using an XML based serializer.

Thanks!
Ronan
Re: Inserting Comments at the Top of an XText Resource at Serialization Time [message #1051199 is a reply to message #1047610] Sun, 28 April 2013 11:22 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan Koehnlein
Messages: 656
Registered: July 2009
Senior Member
A comment does not have a representation in the semantic model,
therefore it cannot be added to the semantic model.

Usually Xtext stores the textual representation in a parallel model (the
node model), but this one does not exist if you create the resource as a
semantic EMF model only. The node model is only created by the parser
which you don't use in your scenario at all.

Possible solutions:
1) Implement a post processor, inserting the comments into the saved
models (reopen as text files, add comment, save)
2) Instead of calling Resource.save(Map<?,?>) call
Resource.save(OutputStream, Map<?,?>) and write the comment into the
output stream before.
3) Serialize the semantic model, parse the resulting string again and
then add the comment textually, e.g. using XtextResource.update(0,0, "//
my comment\n").
3) Write and bind your own XtextResource or ISerializer and hook into
the doSave or serialize methods.


Am 23.04.13 14:47, schrieb Ronan B:
> Hi,
> I'm generating an XText resource as an output of an ATL transformation.
> I want to be able to put in a comment at the top of the output file to
> give the user some details about the version of the transformer used to
> generate the output file.
> I'm finding it very hard to figure out how to do this in Xtext. The
> forum/manual talk about comments but it is normally about preservation
> rather than insertion. Any advise is appreciated. Usualy, when not using
> Xtext, I just edit the Ecore GenModel class that extends EFactoryImpl
> and add comments in at the right spot as Ecore can serialize them for me
> e.g. In the createDocumentRoot() method if using an XML based serializer.
>
> Thanks!
> Ronan


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Inserting Comments at the Top of an XText Resource at Serialization Time [message #1052051 is a reply to message #1051199] Mon, 29 April 2013 14:56 Go to previous message
Ronan B is currently offline Ronan B
Messages: 247
Registered: July 2009
Senior Member
Hi Jan,
Thanks! Ok, I thought there might be some special approach to this and didn't want to come up with a homebrew. Your #1 approach makes the most sense for me as I have easy access to the File. For future referene I do the following:

public File addToolVersionsHeader(File file) {

//ensure there is a file before manipulating it
if(file.exists())
{
try {
//construct our custom header details
String headerData = String.format("/*%n" +
" Copyright (c) "+java.util.Calendar.getInstance().get(java.util.Calendar.YEAR)+" My Company.%n" +
" All rights reserved%n" +
" My tool generated this file on "+new java.util.Date().toLocaleString() +" using %n" +
" * my plugin name " +Activator.getVersion() + "%n"+
"*/%n");
//combine the header with existing file contents and store in original file
FileUtils.writeStringToFile(file, headerData.concat(FileUtils.readFileToString(file)));
}
catch (IOException e) {
e.printStackTrace();
}
}
return file;

Previous Topic:Building an Eclipse Product for XText
Next Topic:Following the tutorial, confused on how I actually generate any Java code
Goto Forum:
  


Current Time: Wed Oct 22 05:02:38 GMT 2014

Powered by FUDForum. Page generated in 0.28047 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software