Skip to content

Commit

Permalink
Merge pull request #39 from tchristofferson/develop
Browse files Browse the repository at this point in the history
Fix more UTF8 issues
  • Loading branch information
tchristofferson authored Dec 31, 2024
2 parents 96e9f05 + 6c5e298 commit c1cd3eb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 8
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '8'
java-version: '21'
distribution: 'temurin'
cache: maven

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-core</artifactId>
<version>5.14.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
38 changes: 24 additions & 14 deletions src/main/java/com/tchristofferson/configupdater/ConfigUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -20,6 +22,7 @@ public class ConfigUpdater {

//Used for separating keys in the keyBuilder inside parseComments method
private static final char SEPARATOR = '.';
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

/**
* Update the YAML file inside the plugin folder, only if it does not match the file from the JAR.
Expand Down Expand Up @@ -50,8 +53,8 @@ public static void update(Plugin plugin, String resourceName, File toUpdate, Str
public static void update(Plugin plugin, String resourceName, File toUpdate, List<String> ignoredSections) throws IOException {
Preconditions.checkArgument(toUpdate.exists(), "The toUpdate file doesn't exist!");

FileConfiguration defaultConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource(resourceName), StandardCharsets.UTF_8));
FileConfiguration currentConfig = YamlConfiguration.loadConfiguration(Files.newBufferedReader(toUpdate.toPath(), StandardCharsets.UTF_8));
FileConfiguration defaultConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(plugin.getResource(resourceName), DEFAULT_CHARSET));
FileConfiguration currentConfig = YamlConfiguration.loadConfiguration(Files.newBufferedReader(toUpdate.toPath(), DEFAULT_CHARSET));
Map<String, String> comments = parseComments(plugin, resourceName, defaultConfig);
Map<String, String> ignoredSectionsValues = parseIgnoredSections(toUpdate, comments, ignoredSections == null ? Collections.emptyList() : ignoredSections);
// will write updated config file "contents" to a string
Expand All @@ -60,8 +63,8 @@ public static void update(Plugin plugin, String resourceName, File toUpdate, Lis
String value = writer.toString(); // config contents

Path toUpdatePath = toUpdate.toPath();
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), StandardCharsets.UTF_8))) { // if updated contents are not the same as current file contents, update
Files.write(toUpdatePath, value.getBytes(StandardCharsets.UTF_8));
if (!value.equals(new String(Files.readAllBytes(toUpdatePath), DEFAULT_CHARSET))) { // if updated contents are not the same as current file contents, update
Files.write(toUpdatePath, value.getBytes(DEFAULT_CHARSET));
}
}

Expand All @@ -76,8 +79,8 @@ public static void update(Plugin plugin, String resourceName, File toUpdate, Lis
* @throws IOException if an I/O error occurs while writing the data to the BufferedWriter.
*/
private static void write(FileConfiguration defaultConfig, FileConfiguration currentConfig, BufferedWriter writer, Map<String, String> comments, Map<String, String> ignoredSectionsValues) throws IOException {
//Used for converting objects to yaml, then cleared
FileConfiguration parserConfig = new YamlConfiguration();
//Used for converting objects to yaml
Yaml yaml = getYamlWriter();

for (String fullKey : defaultConfig.getKeys(true)) {
String indents = KeyUtils.getIndents(fullKey, SEPARATOR);
Expand All @@ -100,7 +103,7 @@ private static void write(FileConfiguration defaultConfig, FileConfiguration cur
writeConfigurationSection(writer, indents, trailingKey, (ConfigurationSection) currentValue);
continue;
}
writeYamlValue(parserConfig, writer, indents, trailingKey, currentValue);
writeYamlValue(yaml, writer, indents, trailingKey, currentValue);
}

String danglingComments = comments.get(null);
Expand All @@ -123,7 +126,7 @@ private static void write(FileConfiguration defaultConfig, FileConfiguration cur
private static Map<String, String> parseComments(Plugin plugin, String resourceName, FileConfiguration defaultConfig) throws IOException {
//keys are in order
List<String> keys = new ArrayList<>(defaultConfig.getKeys(true));
BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(resourceName), StandardCharsets.UTF_8));
BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getResource(resourceName), DEFAULT_CHARSET));
Map<String, String> comments = new LinkedHashMap<>();
StringBuilder commentBuilder = new StringBuilder();
KeyBuilder keyBuilder = new KeyBuilder(defaultConfig, SEPARATOR);
Expand Down Expand Up @@ -196,7 +199,7 @@ private static Map<String, String> parseIgnoredSections(File toUpdate, Map<Strin
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(new YamlConstructor(), new YamlRepresenter(), options);

Map<Object, Object> root = (Map<Object, Object>) yaml.load(new FileReader(toUpdate));
Map<Object, Object> root = (Map<Object, Object>) yaml.load(new InputStreamReader(new FileInputStream(toUpdate), DEFAULT_CHARSET));
ignoredSections.forEach(section -> {
String[] split = section.split("[" + SEPARATOR + "]");
String key = split[split.length - 1];
Expand Down Expand Up @@ -410,19 +413,18 @@ private static Object getKeyAsObject(String key, Map<Object, Object> sectionCont
/**
* Writes the current value with the provided trailing key to the provided writer.
*
* @param parserConfig The parser configuration to use for writing the YAML value.
* @param yamlWriter The {@link Yaml} object used for converting to yaml
* @param bufferedWriter The writer to write the value to.
* @param indents The string representation of the indentation.
* @param trailingKey The trailing key for the YAML value.
* @param currentValue The current value to write as YAML.
* @throws IOException If an I/O error occurs while writing the YAML value.
*/
private static void writeYamlValue(final FileConfiguration parserConfig, final BufferedWriter bufferedWriter, final String indents, final String trailingKey, final Object currentValue) throws IOException {
parserConfig.set(trailingKey, currentValue);
String yaml = parserConfig.saveToString();
private static void writeYamlValue(final Yaml yamlWriter, final BufferedWriter bufferedWriter, final String indents, final String trailingKey, final Object currentValue) throws IOException {
Map<String, Object> map = Collections.singletonMap(trailingKey, currentValue);
String yaml = yamlWriter.dump(map);
yaml = yaml.substring(0, yaml.length() - 1).replace("\n", "\n" + indents);
final String toWrite = indents + yaml + "\n";
parserConfig.set(trailingKey, null);
bufferedWriter.write(toWrite);
}

Expand Down Expand Up @@ -466,4 +468,12 @@ private static void writeConfigurationSection(final BufferedWriter bufferedWrite
bufferedWriter.write(" {}\n");
}
}

private static Yaml getYamlWriter() {
DumperOptions dumperOptions = new DumperOptions();
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setAllowUnicode(true);

return new Yaml(dumperOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.junit.Test;
import org.mockito.stubbing.Answer;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
Expand All @@ -24,10 +22,11 @@
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Matchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

//JUnit tests should be run with -Dfile.encoding=GBK to make sure UTF8 is used everywhere
public class ConfigUpdaterTest {

private static final String FILE_NAME = "config.yml";
Expand Down Expand Up @@ -69,7 +68,7 @@ public void testUpdateMethodToCheckIfFilesAreSameAfter() throws IOException, URI
public void testUpdateMethodToMakeSureIgnoredSectionsAreHandledCorrectly() throws IOException, InvalidConfigurationException {
File toUpdate = new File(FILE_NAME);

FileConfiguration config = YamlConfiguration.loadConfiguration(toUpdate);
FileConfiguration config = YamlConfiguration.loadConfiguration(new BufferedReader(new InputStreamReader(Files.newInputStream(toUpdate.toPath()), StandardCharsets.UTF_8)));
config.set("a-section-with-ignored-sections.sub-ignored.ignored.value3", 3);
config.set("a-section-with-ignored-sections.sub-ignored.ignored2.value", 1);

Expand Down Expand Up @@ -141,8 +140,10 @@ public void testIgnoredSectionKeysAreStillValidAfterUpdate() throws IOException
}

private void saveDefaultConfig(File toUpdate) throws IOException, URISyntaxException {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(Files.newBufferedReader(getResourcePath(), StandardCharsets.UTF_8));
configuration.save(toUpdate);
byte[] bytes = Files.readAllBytes(getResourcePath());
BufferedWriter writer = Files.newBufferedWriter(toUpdate.toPath(), StandardCharsets.UTF_8);
writer.write(new String(bytes, StandardCharsets.UTF_8));
writer.close();
}

private Path getResourcePath() throws URISyntaxException {
Expand Down

0 comments on commit c1cd3eb

Please # to comment.