Search in sources :

Example 1 with IEncodingProvider

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);
        }
    }
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IEncodingProvider(org.eclipse.xtext.parser.IEncodingProvider)

Example 2 with IEncodingProvider

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();
    }
}
Also used : IEncodingProvider(org.eclipse.xtext.parser.IEncodingProvider) File(java.io.File) URI(org.eclipse.emf.common.util.URI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

IEncodingProvider (org.eclipse.xtext.parser.IEncodingProvider)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 URI (org.eclipse.emf.common.util.URI)1 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1 Test (org.junit.Test)1