= Migrated to CSV

This commit is contained in:
Matthew Cech
2019-06-27 01:00:13 -07:00
parent 610fbdc809
commit b34e9795dc
12 changed files with 145 additions and 593 deletions
+82 -46
View File
@@ -2,41 +2,58 @@ package core;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import dataStructures.TaggedPairStore;
import utils.GlobalLog;
import utils.LogFilter;
import utils.io.FileMonitor;
import utils.io.FileUtils;
// A quick-and-dirty localization tool that scrapes the project for calls to itself, then
// generates/updates a file externally with all the stub values as keys that are localized.
public abstract class BaseLocFile
public abstract class BaseLocFile implements IConfigSection
{
// Filename
public final String functionName; // Example: "Localizer.Stub";
// Local translation storage
protected TaggedPairStore stringStore;
// Variables
protected final String headerName; // Example: Loc Strings
protected final String functionName; // Example: "Localizer.Stub";
protected Map<String, String> localized;
// Logging
private void log(String str) { GlobalLog.log(LogFilter.Strings, str); }
private void warn(String str) { GlobalLog.warn(LogFilter.Strings, str); }
private void error(String str) { GlobalLog.error(LogFilter.Strings, str); }
// File monitoring
protected FileMonitor fileMonitor;
// Constructor
public BaseLocFile(String functionName)
public BaseLocFile(String headerName, String functionName)
{
this.headerName = headerName;
this.functionName = functionName;
localized = new HashMap<String, String>();
}
//////////////////////////////////////////////////
// First Step: Populating with existing strings //
//////////////////////////////////////////////////
private void buildLocalized(List<ConfigItem> pairs)
{
for(ConfigItem item : pairs)
{
localized.put(item.key, item.value);
}
}
//////////////////////////////////////////
// Second Step: Scraping existing files //
//////////////////////////////////////////
// Structure used for holding a pair of strings and any other info we need
// about localized information that is being looked up.
@SuppressWarnings("unused")
private class LocInfo
{
public String file;
@@ -49,8 +66,21 @@ public abstract class BaseLocFile
}
}
// Try to perform stripping java files for contents to localize
private void tryStripSpecified(Path path, ArrayList<LocInfo> toFill)
{
try
{
stripForContents(path, toFill);
}
catch(Exception e)
{
error("issue with file: " + path.toString());
}
}
// Do processing on each path in the scraped directory here, assuming it's .java
public void stripForContents(Path path, ArrayList<LocInfo> strings)
private void stripForContents(Path path, ArrayList<LocInfo> strings)
{
String filename = path.getFileName().toString();
if(filename.contains(".java"))
@@ -68,8 +98,8 @@ public abstract class BaseLocFile
{
if(noWhitespace.charAt(noWhitespace.indexOf(")") - 1) == '"' && split[i].charAt(loc - 2) != '\\')
{
// At this point, we find the first ), then verify there's a ") behind it, and that
// the " is not an escaped character.
// At this point we find the first ), then verify there's a ") behind it,
// and that the " is not an escaped character.
try
{
String toLocalize = split[i].substring(2, loc - 1);
@@ -88,61 +118,67 @@ public abstract class BaseLocFile
}
}
// Nothing for now, but in the future will return a parsed and localized version of
// the string in question if one can be found. If the localized string is empty,
// Returns the value. If the localized string is empty,
// returns a the key instead which is the default phrase.
public String getKey(String input)
{
if(stringStore == null)
if(localized == null)
{
return input;
}
String value = stringStore.getKey(input);
String value = localized.get(input);
if(value == null || value.trim().length() < 1)
{
return input;
}
return value;
}
// Update localization from the disk on file. Creates the file if it doesn't exist.
// This file is internally formatted as an ini file.
public void updateLocFromString(String fileContents)
{
log("Attempting to read localization file contents");
stringStore = new TaggedPairStore(fileContents);
}
private void tryStripSpecified(Path path, ArrayList<LocInfo> toFill)
{
try
{
stripForContents(path, toFill);
}
catch(Exception e)
{
error("issue with file: " + path.toString());
}
}
// Scrape the project and generate all the possible localizeable phrases.
// This stubs out phrases to be localized.
// This stubs out phrases to be localized, by default placing the key in as the value.
public void scrapeAll()
{
ArrayList<LocInfo> localizeList = new ArrayList<LocInfo>();
FileUtils.acquireAllFiles(Constants.SourceDirectory).forEach((path) -> tryStripSpecified(path, localizeList));
for(LocInfo toStub : localizeList)
stringStore.addKeyValue(toStub.file, toStub.phrase, toStub.phrase);
{
localized.putIfAbsent(toStub.phrase, toStub.phrase);
}
}
// Converts this to a string
public String toString()
///////////////////////////////////
// IConfigSection Implementation //
///////////////////////////////////
@Override
public String getSectionTitle()
{
return stringStore.toString();
return headerName;
}
public List<ConfigItem> toConfigList();
@Override
public void consume(List<ConfigItem> pairs)
{
localized.clear();
buildLocalized(pairs);
scrapeAll();
}
@Override
public List<ConfigItem> produce()
{
List<ConfigItem> items = new Vector<ConfigItem>();
for(Entry<String, String> entry : localized.entrySet())
{
items.add(new ConfigItem(headerName, entry.getKey(), entry.getValue()));
}
items.sort((item1, item2) -> item1.key.compareToIgnoreCase(item2.key));
return items;
}
}