Skip to content

Commit 93b716c

Browse files
committed
Config and Locale handler fixed up a bit more
1 parent e311648 commit 93b716c

18 files changed

+176
-223
lines changed

pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>simplexity</groupId>
8-
<artifactId>CLI-TTS</artifactId>
8+
<artifactId>Console-TTS</artifactId>
99
<version>2.0.0</version>
1010
<packaging>jar</packaging>
1111

@@ -63,11 +63,6 @@
6363
</resources>
6464
</build>
6565
<dependencies>
66-
<dependency>
67-
<groupId>com.typesafe</groupId>
68-
<artifactId>config</artifactId>
69-
<version>1.4.3</version>
70-
</dependency>
7166
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-polly -->
7267
<dependency>
7368
<groupId>com.amazonaws</groupId>

src/main/java/simplexity/Main.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
import simplexity.commands.ExitCommand;
1111
import simplexity.commands.HelpCommand;
1212
import simplexity.commands.ReloadCommand;
13-
import simplexity.config.config.ConfigHandler;
14-
import simplexity.config.config.YmlConfig;
13+
import simplexity.config.ConfigHandler;
14+
import simplexity.config.LocaleHandler;
15+
import simplexity.config.YmlConfig;
1516
import simplexity.httpserver.LocalServer;
1617
import simplexity.util.Logging;
1718

@@ -21,7 +22,8 @@
2122

2223
public class Main {
2324
private static final Logger logger = LoggerFactory.getLogger(Main.class);
24-
private static YmlConfig ymlConfig;
25+
private static YmlConfig config;
26+
private static YmlConfig localeConfig;
2527
private static CommandManager commandManager;
2628
public static PollyHandler pollyHandler;
2729
private static SpeechHandler speechHandler;
@@ -34,14 +36,17 @@ public static void main(String[] args) {
3436
commandManager = new CommandManager();
3537
registerCommands(commandManager);
3638
File file = new File("config/config.yml");
39+
File localeFile = new File("config/locale.yml");
3740
try {
38-
ymlConfig = new YmlConfig(file, "config.yml");
41+
config = new YmlConfig(file, "config.yml");
42+
localeConfig = new YmlConfig(localeFile, "locale.yml");
3943
} catch (IOException e) {
4044
System.out.println("Fatal Error: Config was unable to be generated.");
4145
e.printStackTrace();
4246
return;
4347
}
44-
ConfigHandler.getInstance().reloadValues(ymlConfig);
48+
LocaleHandler.getInstance().reloadMessages();
49+
ConfigHandler.getInstance().reloadValues();
4550
PollySetup.setupPollyAndSpeech();
4651
LocalServer.run();
4752
while (runApp) {
@@ -78,8 +83,12 @@ public static Scanner getScanner() {
7883
return scanner;
7984
}
8085

81-
public static YmlConfig getYmlConfig(){
82-
return ymlConfig;
86+
public static YmlConfig getConfig(){
87+
return config;
88+
}
89+
90+
public static YmlConfig getLocaleConfig(){
91+
return localeConfig;
8392
}
8493

8594
}

src/main/java/simplexity/amazon/PollySetup.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.slf4j.LoggerFactory;
66
import org.slf4j.event.Level;
77
import simplexity.Main;
8-
import simplexity.config.config.ConfigHandler;
9-
import simplexity.config.locale.Message;
8+
import simplexity.config.ConfigHandler;
9+
import simplexity.config.LocaleHandler;
1010
import simplexity.util.Logging;
1111

1212
import java.util.Scanner;
@@ -26,13 +26,13 @@ public static PollyHandler createPollyHandler() {
2626
String awsSecretKey = ConfigHandler.getInstance().getAwsSecretKey();
2727
Region awsRegion = ConfigHandler.getInstance().getAwsRegion();
2828
if (awsAccessID.isEmpty() || awsSecretKey.isEmpty() || awsRegion == null) {
29-
System.out.println(Message.NULL_AWS_CREDENTIALS);
29+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getSaveAwsInfo(), Level.WARN);
3030
return null;
3131
}
3232
try {
3333
pollyHandler = new PollyHandler(awsAccessID, awsSecretKey, awsRegion);
3434
} catch (IllegalArgumentException e) {
35-
System.out.println(Message.NULL_AWS_CREDENTIALS);
35+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getNullAwsCreds(), Level.ERROR);
3636
}
3737
return pollyHandler;
3838
}
@@ -43,9 +43,9 @@ public static void connectToPolly() {
4343
if (Main.getPollyHandler() != null) {
4444
return;
4545
}
46-
Logging.logAndPrint(logger, Message.PLEASE_SAVE_AWS_INFO_IN_CONFIG.getMessage(), Level.INFO);
46+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getSaveAwsInfo(), Level.INFO);
4747
scanner.nextLine();
48-
ConfigHandler.getInstance().reloadValues(Main.getYmlConfig());
48+
ConfigHandler.getInstance().reloadValues();
4949
if (Main.getPollyHandler() != null) {
5050
return;
5151
}

src/main/java/simplexity/amazon/SpeechHandler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
import org.slf4j.LoggerFactory;
1212
import org.slf4j.event.Level;
1313
import simplexity.Main;
14-
import simplexity.config.config.ConfigHandler;
15-
import simplexity.config.config.SpeechEffectRule;
16-
import simplexity.config.locale.Message;
14+
import simplexity.config.ConfigHandler;
15+
import simplexity.config.LocaleHandler;
16+
import simplexity.config.SpeechEffectRule;
1717
import simplexity.util.Logging;
1818

1919
import java.io.InputStream;
@@ -44,7 +44,7 @@ public void processSpeech(String text) {
4444
speechStream = synthesizeSpeech(processedText, voiceId);
4545
}
4646
if (speechStream == null) {
47-
Logging.logAndPrint(logger, Message.GENERAL_ERROR.getMessage().replace("%error%", "Speech stream is null"), Level.ERROR);
47+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getErrorGeneral().replace("%error%", "Speech stream is null"), Level.ERROR);
4848
return;
4949
}
5050

@@ -108,15 +108,15 @@ public void playSpeech(InputStream speechStream) {
108108
player = new AdvancedPlayer(speechStream, FactoryRegistry.systemRegistry().createAudioDevice());
109109
player.play();
110110
} catch (Exception exception) {
111-
Logging.logAndPrint(logger, Message.GENERAL_ERROR.getMessage().replace("%error%", exception.getMessage()), Level.ERROR);
111+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getErrorGeneral().replace("%error%", exception.getMessage()), Level.ERROR);
112112
}
113113
}
114114

115115
/**
116116
* Logs errors during speech synthesis.
117117
*/
118118
private void logSynthesisError(Exception e, String text) {
119-
Logging.logAndPrint(logger, Message.GENERAL_ERROR.getMessage().replace("%error%", e.getMessage()), Level.ERROR);
120-
Logging.logAndPrint(logger, Message.MESSAGE_NOT_PARSABLE.getMessage().replace("%message%", text), Level.ERROR);
119+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getErrorGeneral().replace("%error%", e.getMessage()), Level.ERROR);
120+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getMessageNotParsable().replace("%message%", text), Level.ERROR);
121121
}
122122
}

src/main/java/simplexity/commands/CommandManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55
import org.slf4j.event.Level;
6-
import simplexity.config.locale.Message;
6+
import simplexity.config.LocaleHandler;
77
import simplexity.util.Logging;
88

99
import java.util.HashMap;
@@ -19,7 +19,7 @@ public void registerCommand(Command command) {
1919

2020
public boolean runCommand(String command) {
2121
if (command.startsWith("--") && !commands.containsKey(command)) {
22-
Logging.logAndPrint(logger, Message.UNKNOWN_COMMAND.getMessage().replace("%command%", command), Level.ERROR);
22+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getUnknownCommand().replace("%command%", command), Level.ERROR);
2323
return true;
2424
}
2525
if (!commands.containsKey(command)){

src/main/java/simplexity/commands/ExitCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.slf4j.event.Level;
44
import simplexity.Main;
5-
import simplexity.config.locale.Message;
5+
import simplexity.config.LocaleHandler;
66
import simplexity.httpserver.LocalServer;
77
import simplexity.util.Logging;
88

@@ -14,7 +14,7 @@ public ExitCommand(String name, String usage) {
1414

1515
@Override
1616
public void execute() {
17-
Logging.logAndPrint(logger, Message.SHUTTING_DOWN.getMessage(), Level.INFO);
17+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getFeedbackShuttingDown(), Level.INFO);
1818
Main.runApp = false;
1919
LocalServer.stop();
2020
System.exit(0);

src/main/java/simplexity/commands/HelpCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.slf4j.LoggerFactory;
55
import org.slf4j.event.Level;
66
import simplexity.Main;
7-
import simplexity.config.locale.Message;
7+
import simplexity.config.LocaleHandler;
88
import simplexity.util.Logging;
99

1010
public class HelpCommand extends Command {
@@ -16,9 +16,9 @@ public HelpCommand(String name, String usage) {
1616

1717
@Override
1818
public void execute() {
19-
Logging.logAndPrint(logger, Message.HELP_HEADER.getMessage(), Level.INFO);
19+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getHelpHeader(), Level.INFO);
2020
for (Command command : Main.getCommandManager().getCommands().values()) {
21-
String commandHelpMessage = Message.HELP_COMMAND_MESSAGE.getMessage().replace("%command_name%", command.getName())
21+
String commandHelpMessage = LocaleHandler.getInstance().getHelpCommands().replace("%command_name%", command.getName())
2222
.replace("%command_description%", command.getDescription());
2323
Logging.logAndPrint(logger, commandHelpMessage, Level.INFO);
2424
}

src/main/java/simplexity/commands/ReloadCommand.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import org.slf4j.LoggerFactory;
55
import org.slf4j.event.Level;
66
import simplexity.Main;
7-
import simplexity.config.locale.Message;
7+
import simplexity.config.ConfigHandler;
8+
import simplexity.config.LocaleHandler;
89
import simplexity.httpserver.LocalServer;
910
import simplexity.util.Logging;
1011

@@ -22,7 +23,10 @@ public ReloadCommand(String name, String usage) {
2223
public void execute() {
2324
Logging.log(logger, "Reloading configs", Level.INFO);
2425
try {
25-
Main.getYmlConfig().reloadConfig();
26+
Main.getConfig().reloadConfig();
27+
Main.getLocaleConfig().reloadConfig();
28+
ConfigHandler.getInstance().reloadValues();
29+
LocaleHandler.getInstance().reloadMessages();
2630
} catch (IOException e) {
2731
Logging.logAndPrint(logger, "Fatal Error Reloading Config Files", Level.WARN);
2832
Logging.logAndPrint(logger, e.getStackTrace().toString(), Level.WARN);
@@ -32,6 +36,6 @@ public void execute() {
3236
LocalServer.stop();
3337
Logging.log(logger, "Starting local server", Level.INFO);
3438
LocalServer.run();
35-
Logging.logAndPrint(logger, Message.RELOAD_MESSAGE.getMessage(), Level.ERROR);
39+
Logging.logAndPrint(logger, LocaleHandler.getInstance().getFeedbackReload(), Level.ERROR);
3640
}
3741
}

src/main/java/simplexity/config/AbstractConfig.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/main/java/simplexity/config/config/ConfigHandler.java renamed to src/main/java/simplexity/config/ConfigHandler.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package simplexity.config.config;
1+
package simplexity.config;
22

33
import com.amazonaws.regions.Region;
44
import com.amazonaws.regions.Regions;
55
import com.amazonaws.services.polly.model.VoiceId;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88
import org.slf4j.event.Level;
9+
import simplexity.Main;
910
import simplexity.util.Logging;
1011

1112
import java.util.HashMap;
@@ -25,11 +26,6 @@ public class ConfigHandler {
2526
private Integer serverPort;
2627
private static ConfigHandler instance;
2728

28-
public ConfigHandler() {
29-
super(); //todo figure out a better way to handle the config names
30-
Logging.log(logger, "Initializing AWS config class", Level.INFO);
31-
}
32-
3329
public static ConfigHandler getInstance() {
3430
if (instance == null) {
3531
instance = new ConfigHandler();
@@ -38,13 +34,15 @@ public static ConfigHandler getInstance() {
3834
return instance;
3935
}
4036

41-
public void reloadValues(YmlConfig config) {
37+
public void reloadValues() {
38+
YmlConfig config = Main.getConfig();
4239
reloadSpeechEffects(config);
4340
reloadAwsValues(config);
41+
reloadTextReplace(config);
4442
serverPort = config.getOption("internal-settings.server-port", Integer.class, 3000);
4543
}
4644

47-
public void reloadAwsValues(YmlConfig config){
45+
public void reloadAwsValues(YmlConfig config) {
4846
awsAccessID = config.getOption("aws-api.access-key", String.class, "");
4947
awsSecretKey = config.getOption("aws-api.secret-key", String.class, "");
5048
Regions region = config.getOption("aws-api.region", Regions.class, Regions.US_EAST_1);
@@ -85,7 +83,15 @@ public void reloadSpeechEffects(YmlConfig config) {
8583
}
8684
}
8785

88-
public void reloadTextReplace(YmlConfig config){}
86+
public void reloadTextReplace(YmlConfig config) {
87+
textReplaceRules.clear();
88+
for (String key : config.getKeys("text-replacements")) {
89+
String replacementText = config.getOption("text-replacements." + key, String.class);
90+
if (replacementText == null) continue;
91+
TextReplaceRule replaceRule = new TextReplaceRule(key, replacementText);
92+
textReplaceRules.add(replaceRule);
93+
}
94+
}
8995

9096

9197
public String getAwsAccessID() {

0 commit comments

Comments
 (0)