Improve ConfigUtils.addPhrases and ConfigUtils.createSqlConnection methods
This commit is contained in:
parent
3c6c655ff2
commit
06388d4f72
1 changed files with 19 additions and 5 deletions
|
|
@ -2,27 +2,41 @@ package org.zhdev.util;
|
|||
|
||||
import org.zhdev.config.ConfigSection;
|
||||
import org.zhdev.language.Language;
|
||||
import org.zhdev.sql.*;
|
||||
import org.zhdev.sql.H2SqlConnection;
|
||||
import org.zhdev.sql.MySqlConnection;
|
||||
import org.zhdev.sql.SqlConnection;
|
||||
import org.zhdev.sql.SqliteConnection;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ConfigUtils {
|
||||
public static void addPhrases(Language language, ConfigSection config) {
|
||||
public static void addPhrases(Language language, ConfigSection config, Function<String, String> function) {
|
||||
for (String i : config) {
|
||||
ConfigSection section = config.getSection(i);
|
||||
if (section == null) continue;
|
||||
for (String j : section) {
|
||||
language.addPhrase(i, j, section.getString(j));
|
||||
String phrase = section.getString(j);
|
||||
if (phrase == null) {
|
||||
phrase = String.join("\n", CollectionUtils.mapToString(section.getList(j, Collections.emptyList()), ArrayList::new));
|
||||
}
|
||||
language.addPhrase(i, j, function.apply(phrase));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void addPhrases(Language language, ConfigSection config) {
|
||||
addPhrases(language, config, s -> s);
|
||||
}
|
||||
|
||||
public static SqlConnection createSqlConnection(ConfigSection config, String pathPrefix) {
|
||||
String type = config.getString("type", "none").toLowerCase();
|
||||
SqlConnection connection;
|
||||
switch (type) {
|
||||
case "h2": {
|
||||
String path = config.getString("path");
|
||||
String path = config.getString("path", "storage.h2");
|
||||
String username = config.getString("username");
|
||||
String password = config.getString("password");
|
||||
connection = new H2SqlConnection(File.separatorChar + path, username, password);
|
||||
|
|
@ -39,7 +53,7 @@ public class ConfigUtils {
|
|||
break;
|
||||
}
|
||||
case "sqlite": {
|
||||
String path = config.getString("path");
|
||||
String path = config.getString("path", "database.db");
|
||||
connection = new SqliteConnection(pathPrefix + File.separatorChar + path);
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue