Earlier, I had blogged about Character
Encoding in Eclipse. Now I would like to write about how we can
configure encoding in Xtext at various levels.
Design time encoding
Xtext generates the DSL artifacts like model, ui, test plugins and by default they will be encoded with platform defaults. However, Xtext provides the mechanism to provide encoding type to be used for encoding the generated plugins by adding an entry encoding = "UTF-8" in the Generator fragment in *.mwe2 file as shown below
Xtext generates the DSL artifacts like model, ui, test plugins and by default they will be encoded with platform defaults. However, Xtext provides the mechanism to provide encoding type to be used for encoding the generated plugins by adding an entry encoding = "UTF-8" in the Generator fragment in *.mwe2 file as shown below
In 2.8.4 version and above
component = Generator {
component = Generator {
encoding = "UTF-8"
pathRtProject = runtimeProject
...
}
In Earlier version, the xtextGenerator fragment to be changed
In Earlier version, the xtextGenerator fragment to be changed
component = xtextGenerator {
encoding = "UTF-8"
pathRtProject = runtimeProject
...
}
Encoding at Language Runtime
Xtext is highly configurable through its mechanism of providing binding mechanism. One can implement the own encoding provider by implementing the IEncodingProvider interface of org.eclipse.xtext.parser. IEncodingProvider provides single api getEncoding(URI) to provide encoding for the given URI.
package org.eclipse.mydsl.encoding;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.parser.IEncodingProvider;
public class MyDslEncodingProvider implements IEncodingProvider {
@Override
public String getEncoding(URI uri) {
return "UTF-8";
}
}
Now, we need to bind this newly implemented EncodingProvider in both model and ui generated artifacts through *RuntimeModule.java and *UIModule.java, which is usually present in src folder of dsl and ui plugin respectively
1. Runtime Binding
@Override
public void configureRuntimeEncodingProvider(Binder binder) {
binder.bind(IEncodingProvider.class)
.annotatedWith(DispatchingProvider.Runtime.class)
.to(MyDslEncodingProvider.class);
}
2. UI Binding
@Override
public void configureUiEncodingProvider(Binder binder) {
binder.bind(IEncodingProvider.class)
.annotatedWith(DispatchingProvider.Ui.class)
.to(MyDslEncodingProvider.class);
}
So now xtext uses the MyDslEncodingProvider as the default provider during creation or saving of resource. Even this could be overridden like as shown below
Map<?,?> options = new HashMap();
options.put(XtextResource.OPTION_ENCODING, "UTF-8");
myXtextResource.load(options);
options.put(XtextResource.OPTION_ENCODING, "ISO-8859-1");
myXtextResource.save(options);
Xtext is highly configurable through its mechanism of providing binding mechanism. One can implement the own encoding provider by implementing the IEncodingProvider interface of org.eclipse.xtext.parser. IEncodingProvider provides single api getEncoding(URI) to provide encoding for the given URI.
package org.eclipse.mydsl.encoding;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.parser.IEncodingProvider;
public class MyDslEncodingProvider implements IEncodingProvider {
@Override
public String getEncoding(URI uri) {
return "UTF-8";
}
}
Now, we need to bind this newly implemented EncodingProvider in both model and ui generated artifacts through *RuntimeModule.java and *UIModule.java, which is usually present in src folder of dsl and ui plugin respectively
1. Runtime Binding
@Override
public void configureRuntimeEncodingProvider(Binder binder) {
binder.bind(IEncodingProvider.class)
.annotatedWith(DispatchingProvider.Runtime.class)
.to(MyDslEncodingProvider.class);
}
2. UI Binding
@Override
public void configureUiEncodingProvider(Binder binder) {
binder.bind(IEncodingProvider.class)
.annotatedWith(DispatchingProvider.Ui.class)
.to(MyDslEncodingProvider.class);
}
So now xtext uses the MyDslEncodingProvider as the default provider during creation or saving of resource. Even this could be overridden like as shown below
Map<?,?> options = new HashMap();
options.put(XtextResource.OPTION_ENCODING, "UTF-8");
myXtextResource.load(options);
options.put(XtextResource.OPTION_ENCODING, "ISO-8859-1");
myXtextResource.save(options);
No comments:
Post a Comment