From 89ab17829bee87446e32fc9d26872186896551bc Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Sat, 30 Mar 2019 16:12:45 -0700 Subject: [PATCH] + Added file utils and updated localizer format --- src/core/Localizer.java | 82 ++++++++++++---------------------------- src/utils/FileUtils.java | 49 ++++++++++++++++++++++++ src/utils/LogFilter.java | 2 +- 3 files changed, 74 insertions(+), 59 deletions(-) create mode 100644 src/utils/FileUtils.java diff --git a/src/core/Localizer.java b/src/core/Localizer.java index fe1973f..61bd487 100644 --- a/src/core/Localizer.java +++ b/src/core/Localizer.java @@ -3,17 +3,17 @@ package core; import java.io.File; import java.io.IOException; import java.io.PrintWriter; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; -import java.util.stream.Stream; import org.ini4j.InvalidFileFormatException; import org.ini4j.Profile.Section; import org.ini4j.Wini; +import utils.FileUtils; +import utils.GlobalLog; +import utils.LogFilter; + // A quick-and-dirty localization tool that scrapes the project for calls to itself, then // generates/updates a file externally (phrases.config) with all the stub values as keys that // can then be localized. @@ -24,69 +24,34 @@ public class Localizer public final static String functionName = "Localizer.Stub"; // Local translation - private static HashMap translated = new HashMap(); + private static HashMap translated = new HashMap(); // Logging - private static void Log(String str) { System.out.println("[Log] " + str); } - private static void Warn(String str) { System.out.println("[Warn] " + str); } - private static void Error(String str) { System.err.println("[Error] " + str); } + private static void Log(String str) { GlobalLog.Log(LogFilter.Strings, str); } + private static void Warn(String str) { GlobalLog.Warn(LogFilter.Strings, str); } + private static void Error(String str) { GlobalLog.Error(LogFilter.Strings, str); } - // Util: Reads all lines from a file as a string - private static String ReadContent(Path filePath) - { - StringBuilder contentBuilder = new StringBuilder(); - - try - { - Stream stream = Files.lines(filePath, StandardCharsets.UTF_8); - - stream.forEach(str -> contentBuilder.append(str).append("\n")); - stream.close(); - } - catch (IOException e) - { - e.printStackTrace(); - } - - return contentBuilder.toString(); - } - - // Recursively acquires all files at and below the specified directory, returning them as an arraylist of paths. - public static ArrayList AcquireAllFiles(String startingDir) - { - ArrayList items = new ArrayList(); - - try - { - Files.find(Paths.get(startingDir), 999, (path, attributes) -> attributes.isRegularFile()).forEach(items::add); - } - catch (IOException e) - { - e.printStackTrace(); - } - - return items; - } - - public static class LocPair + // Structure used for holding a pair of strings and any other info we need + // about localized information that is being looked up. + private static class LocInfo { public String file; public String phrase; - public LocPair(String f, String p) + public LocInfo(String f, String p) { this.file = f; this.phrase = p; } - } - + } + // Do processing on each path in the scraped directory here, assuming it's .java - public static void StripForContents(Path path, ArrayList strings) + public static void StripForContents(Path path, ArrayList strings) { String filename = path.getFileName().toString(); if(filename.contains(".java")) { - String contents = ReadContent(path); + String contents = FileUtils.ReadContent(path); String[] split = contents.split(functionName); // Identify all localizer function calls @@ -104,7 +69,7 @@ public class Localizer { String toLocalize = split[i].substring(2, loc - 1); - strings.add(new LocPair(filename.substring(0, filename.lastIndexOf('.')), toLocalize)); + strings.add(new LocInfo(filename.substring(0, filename.lastIndexOf('.')), toLocalize)); Log("Found stubbed phrase in " + path + " : " + toLocalize); } @@ -143,6 +108,7 @@ public class Localizer file.createNewFile(); Wini ini = new Wini(file); + for(String str : ini.keySet()) { // Store everything in all .ini sections @@ -150,7 +116,7 @@ public class Localizer for(String s : sec.keySet()) { if(!translated.containsKey(s)) - translated.put(s, new LocPair(sec.getName(), sec.get(s, String.class))); + translated.put(s, new LocInfo(sec.getName(), sec.get(s, String.class))); } } } @@ -181,7 +147,7 @@ public class Localizer for(String s : translated.keySet()) { - LocPair toWrite = translated.get(s); + LocInfo toWrite = translated.get(s); ini.put(toWrite.file, s, toWrite.phrase); } @@ -201,14 +167,14 @@ public class Localizer // This stubs out phrases to be localized. public static void ScrapeAll() { - ArrayList localizeList = new ArrayList(); - AcquireAllFiles(".\\src").forEach((path) -> StripForContents(path, localizeList)); + ArrayList localizeList = new ArrayList(); + FileUtils.AcquireAllFiles(".\\src").forEach((path) -> StripForContents(path, localizeList)); - for(LocPair toStub : localizeList) + for(LocInfo toStub : localizeList) { if(!translated.containsKey(toStub.phrase)) { - translated.put(toStub.phrase, new LocPair(toStub.file, "")); + translated.put(toStub.phrase, new LocInfo(toStub.file, "")); Log("Found new stubbed phrase '" + toStub.phrase + "' in " + toStub.file); } } diff --git a/src/utils/FileUtils.java b/src/utils/FileUtils.java new file mode 100644 index 0000000..4d3ef48 --- /dev/null +++ b/src/utils/FileUtils.java @@ -0,0 +1,49 @@ +package utils; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.stream.Stream; + +public class FileUtils +{ + // Reads all lines from a file as a string + public static String ReadContent(Path filePath) + { + StringBuilder contentBuilder = new StringBuilder(); + + try + { + Stream stream = Files.lines(filePath, StandardCharsets.UTF_8); + + stream.forEach(str -> contentBuilder.append(str).append("\n")); + stream.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + + return contentBuilder.toString(); + } + + // Recursively acquires all files at and below the specified directory, returning them as an arraylist of paths. + public static ArrayList AcquireAllFiles(String startingDir) + { + ArrayList items = new ArrayList(); + + try + { + Files.find(Paths.get(startingDir), 999, (path, attributes) -> attributes.isRegularFile()).forEach(items::add); + } + catch (IOException e) + { + e.printStackTrace(); + } + + return items; + } +} diff --git a/src/utils/LogFilter.java b/src/utils/LogFilter.java index 833c440..eeb9d3a 100644 --- a/src/utils/LogFilter.java +++ b/src/utils/LogFilter.java @@ -2,7 +2,7 @@ package utils; public enum LogFilter { - Debug(0), Command(1), Core(2), Database(4), Response(8), Network(16); //8, 16, 32... (flags, so we can | together later) + Debug(0), Command(1), Core(2), Database(4), Response(8), Network(16), Strings(32); //8, 16, 32... (flags, so we can | together later) private final int value; private LogFilter(int value)