Thursday, October 1, 2009

Generate code with the right encoding

Up until now, Acceleo allowed you to define the encoding in which the module itself should be read in the form of a comment at the very beginning of the module :However, this only affects how Acceleo reads your template file; it will not affect how Acceleo generates files from this module. So if you want to generate an XML file, and this XML file needs be encoded in a specific encoding which is not you system's default, you're stuck :p. Or at least that was true until M2.

Let's take this basic module as an example :This simple module is entirely composed of static text; its only purpose is to generate an ant file that creates a zip from a directory in a directory named "échantillons"... yes, that's an "é". If This file is generated under windows, no problem : the default windows encoding is the same as ISO-8859-1 as far as the "é" is concerned. What if this file is generated by one of your coworker who happens to be working under linux? Here's what would be generated :
And that'd could be a tragedy if the error isn't seen before someone tries and run this ant build... and sees that you generated under something else that windows! Imagine all the chocolate needed to atone for that blasphemy! Acceleo now proposes a way to define once and for all the encoding that must be used to generate a given file, and all you need is a single parameter for the file block :Take note that this is not a behaviour described by the OMG MOFM2T specification (that sadly left all encoding issues aside). As suche this feature might change form in the future, monitor the related OMG issue if you're interested by this field.

1 comment: