Move charset loading to fix issue with static fields
This commit is contained in:
@@ -13,10 +13,14 @@ package constants.string;
|
||||
* CharsetConstants
|
||||
*/
|
||||
|
||||
import com.esotericsoftware.yamlbeans.YamlReader;
|
||||
import config.YamlConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
@@ -55,8 +59,22 @@ public class CharsetConstants {
|
||||
}
|
||||
}
|
||||
|
||||
private static String loadCharsetFromConfig() {
|
||||
try {
|
||||
YamlReader reader = new YamlReader(new FileReader(YamlConfig.CONFIG_FILE_NAME, StandardCharsets.US_ASCII));
|
||||
reader.getConfig().readConfig.setIgnoreUnknownProperties(true);
|
||||
StrippedYamlConfig charsetConfig = reader.read(StrippedYamlConfig.class);
|
||||
reader.close();
|
||||
return charsetConfig.server.CHARSET;
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new RuntimeException("Could not read config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Could not successfully parse charset from config file " + YamlConfig.CONFIG_FILE_NAME + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static Charset loadCharset() {
|
||||
String configCharset = YamlConfig.loadCharset();
|
||||
String configCharset = loadCharsetFromConfig();
|
||||
if (configCharset != null) {
|
||||
Language language = Language.fromCharset(configCharset);
|
||||
return Charset.forName(language.getCharset());
|
||||
@@ -64,4 +82,12 @@ public class CharsetConstants {
|
||||
|
||||
return StandardCharsets.US_ASCII;
|
||||
}
|
||||
|
||||
private static class StrippedYamlConfig {
|
||||
public StrippedServerConfig server;
|
||||
|
||||
private static class StrippedServerConfig {
|
||||
public String CHARSET;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user