mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-17 01:08:56 +00:00
[generator] consider encoding for JavaIoFileSystemAccess
https://bugs.eclipse.org/bugs/show_bug.cgi?id=368019
This commit is contained in:
parent
f494de41cf
commit
e306066edf
2 changed files with 99 additions and 16 deletions
|
@ -8,10 +8,15 @@
|
|||
package org.eclipse.xtext.generator;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.xtext.parser.IEncodingProvider;
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
/**
|
||||
* @author Sven Efftinge - Initial contribution and API
|
||||
|
@ -19,11 +24,28 @@ import org.eclipse.emf.common.util.URI;
|
|||
*/
|
||||
public class JavaIoFileSystemAccess extends AbstractFileSystemAccess {
|
||||
|
||||
@Inject
|
||||
private IEncodingProvider encodingProvider;
|
||||
|
||||
@Inject
|
||||
private IResourceServiceProvider.Registry registry;
|
||||
|
||||
public JavaIoFileSystemAccess() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.3
|
||||
*/
|
||||
public JavaIoFileSystemAccess(IResourceServiceProvider.Registry registry, IEncodingProvider encodingProvider) {
|
||||
this.registry = registry;
|
||||
this.encodingProvider = encodingProvider;
|
||||
}
|
||||
|
||||
public void generateFile(String fileName, String outputConfigName, CharSequence contents) {
|
||||
File file = getFile(fileName, outputConfigName);
|
||||
try {
|
||||
createFolder(file.getParentFile());
|
||||
FileWriter writer = new FileWriter(file);
|
||||
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), getEncoding(getURI(fileName, outputConfigName)));
|
||||
writer.append(postProcess(fileName, outputConfigName, contents));
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
|
@ -31,6 +53,17 @@ public class JavaIoFileSystemAccess extends AbstractFileSystemAccess {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.3
|
||||
*/
|
||||
protected String getEncoding(URI fileURI) {
|
||||
IResourceServiceProvider resourceServiceProvider = registry.getResourceServiceProvider(fileURI);
|
||||
if(resourceServiceProvider != null)
|
||||
return resourceServiceProvider.getEncodingProvider().getEncoding(fileURI);
|
||||
else
|
||||
return encodingProvider.getEncoding(fileURI);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 2.1
|
||||
*/
|
||||
|
|
|
@ -7,10 +7,14 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.xtext.generator;
|
||||
|
||||
import static org.eclipse.xtext.util.Strings.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
|
||||
import org.eclipse.xtext.parser.IEncodingProvider;
|
||||
import org.eclipse.xtext.resource.IResourceServiceProvider;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -19,26 +23,72 @@ import org.junit.Test;
|
|||
*/
|
||||
public class JavaIoFileSystemAccessTest extends Assert {
|
||||
|
||||
@Test public void testDirsAreCreated() throws Exception {
|
||||
@Test
|
||||
public void testDirsAreCreated() throws Exception {
|
||||
File dir = null;
|
||||
File file = null;
|
||||
try {
|
||||
JavaIoFileSystemAccess fileSystemAccess = new JavaIoFileSystemAccess(
|
||||
IResourceServiceProvider.Registry.INSTANCE, new IEncodingProvider.Runtime());
|
||||
File tmpDir = configureFileSystemAccess(fileSystemAccess);
|
||||
fileSystemAccess.generateFile("tmp/X", "XX");
|
||||
dir = new File(tmpDir, "tmp");
|
||||
assertTrue(dir.exists());
|
||||
assertTrue(dir.isDirectory());
|
||||
file = new File(dir, "X");
|
||||
assertTrue(file.exists());
|
||||
assertTrue(file.isFile());
|
||||
} finally {
|
||||
try {
|
||||
if (file != null)
|
||||
file.delete();
|
||||
} finally {
|
||||
if (dir != null)
|
||||
dir.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected File configureFileSystemAccess(JavaIoFileSystemAccess fileSystemAccess) {
|
||||
String tempDir = System.getProperties().getProperty("java.io.tmpdir") + File.separator;
|
||||
File tmpDir = new File(tempDir);
|
||||
JavaIoFileSystemAccess fileSystemAccess = new JavaIoFileSystemAccess();
|
||||
fileSystemAccess.setOutputPath(tempDir);
|
||||
fileSystemAccess.generateFile("tmp/X", "XX");
|
||||
File dir = new File(tmpDir, "tmp");
|
||||
assertTrue(dir.exists());
|
||||
assertTrue(dir.isDirectory());
|
||||
File file = new File(dir, "X");
|
||||
assertTrue(file.exists());
|
||||
assertTrue(file.isFile());
|
||||
file.delete();
|
||||
dir.delete();
|
||||
return tmpDir;
|
||||
}
|
||||
|
||||
@Test public void testURI() throws Exception {
|
||||
|
||||
@Test
|
||||
public void testURI() throws Exception {
|
||||
JavaIoFileSystemAccess fileSystemAccess = new JavaIoFileSystemAccess();
|
||||
fileSystemAccess.setOutputPath("testOutput", "/testDir");
|
||||
URI uri = fileSystemAccess.getURI("testFile", "testOutput");
|
||||
assertEquals("file:/testDir/testFile", uri.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncoding() throws Exception {
|
||||
File file = null;
|
||||
try {
|
||||
JavaIoFileSystemAccess fileSystemAccess = new JavaIoFileSystemAccess(
|
||||
IResourceServiceProvider.Registry.INSTANCE, new IEncodingProvider() {
|
||||
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 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 (file != null)
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue