use of org.eclipse.xtext.parser.IEncodingProvider in project xtext-core by eclipse.
the class XtextGenerator method initializeEncoding.
protected void initializeEncoding() {
IResourceServiceProvider.Registry serviceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE;
IResourceServiceProvider serviceProvider = (IResourceServiceProvider) serviceProviderRegistry.getExtensionToFactoryMap().get("xtext");
String encoding = null;
if (grammarEncoding != null) {
encoding = grammarEncoding;
} else {
encoding = configuration.getCode().getEncoding();
}
if (serviceProvider != null && encoding != null) {
IEncodingProvider encodingProvider = serviceProvider.get(IEncodingProvider.class);
if (encodingProvider instanceof IEncodingProvider.Runtime) {
((IEncodingProvider.Runtime) encodingProvider).setDefaultEncoding(encoding);
}
}
}
use of org.eclipse.xtext.parser.IEncodingProvider in project xtext-core by eclipse.
the class JavaIoFileSystemAccessTest method testEncoding.
@Test
public void testEncoding() throws Exception {
File file = null;
FileInputStream fileInputStream = null;
try {
JavaIoFileSystemAccess fileSystemAccess = new JavaIoFileSystemAccess(IResourceServiceProvider.Registry.INSTANCE, new IEncodingProvider() {
@Override
public String getEncoding(URI uri) {
return "ISO-8859-1";
}
});
File tmpDir = configureFileSystemAccess(fileSystemAccess);
String contents = "g�rkenbr�d";
fileSystemAccess.generateFile("test.txt", contents);
file = new File(tmpDir, "test.txt");
assertTrue(file.exists());
fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[512];
int read = fileInputStream.read(buffer);
String utfString = new String(buffer, 0, read, "UTF-8");
assertFalse(equal(contents, utfString));
String isoString = new String(buffer, 0, read, "ISO-8859-1");
assertEquals(contents, isoString);
} finally {
if (fileInputStream != null)
fileInputStream.close();
if (file != null)
file.delete();
}
}
Aggregations