Skip to content
Permalink
Browse files

Change SystemKeys to an enum

  • Loading branch information...
joeha480 committed Apr 8, 2019
1 parent 9e51b8c commit 6a286ce5117ab28697863c7ce987b9916fc98837
Showing with 52 additions and 44 deletions.
  1. +13 −13 src/org/daisy/dotify/Dotify.java
  2. +25 −17 src/org/daisy/dotify/SystemKeys.java
  3. +14 −14 src/org/daisy/dotify/cli/Convert.java
@@ -48,16 +48,16 @@
private static final HashMap<String, String> extensionBindings;
static {
extensionBindings = new HashMap<String, String>();
extensionBindings.put("txt", SystemKeys.FORMATTED_TEXT_FORMAT);
extensionBindings.put("txt", SystemKeys.FORMATTED_TEXT_FORMAT.getKey());
}

private final boolean writeTempFiles;
private final boolean keepTempFilesOnSuccess;
// hide default constructor to disable instantiation.
private Dotify(Map<String, String> params) {
// get parameters
writeTempFiles = "true".equals(params.get(SystemKeys.WRITE_TEMP_FILES));
keepTempFilesOnSuccess = !("false".equals(params.get(SystemKeys.KEEP_TEMP_FILES_ON_SUCCESS)));
writeTempFiles = "true".equals(params.get(SystemKeys.WRITE_TEMP_FILES.getKey()));
keepTempFilesOnSuccess = !("false".equals(params.get(SystemKeys.KEEP_TEMP_FILES_ON_SUCCESS.getKey())));
}

public static void run(File input, File output, String locale, Map<String, String> params) throws IOException, InternalTaskException {
@@ -87,7 +87,7 @@ public static void run(File inputFile, File output, FilterLocale context, Map<St
map.putAll(params);

AnnotatedFile ai = IdentityProvider.newInstance().identify(inputFile);
map.put(SystemKeys.INPUT, ai.getFile().getAbsolutePath());
map.put(SystemKeys.INPUT.getKey(), ai.getFile().getAbsolutePath());

String inputFormat = getFormatString(ai);
if (inputFormat!=null) {
@@ -110,9 +110,9 @@ public static void run(File inputFile, File output, FilterLocale context, Map<St
}
}

map.put(SystemKeys.INPUT_FORMAT, inputFormat);
map.put(SystemKeys.INPUT_FORMAT.getKey(), inputFormat);

String outputformat = params.get(SystemKeys.OUTPUT_FORMAT);
String outputformat = params.get(SystemKeys.OUTPUT_FORMAT.getKey());
if (outputformat==null || "".equals(outputformat)) {
int indx = output.getName().lastIndexOf('.');
if (indx>-1) {
@@ -127,18 +127,18 @@ public static void run(File inputFile, File output, FilterLocale context, Map<St
throw new IllegalArgumentException("Cannot detect file format for output file. Please specify output format.");
}
}
map.put(SystemKeys.OUTPUT_FORMAT, outputformat.toLowerCase());
map.put(SystemKeys.OUTPUT_FORMAT.getKey(), outputformat.toLowerCase());

map.put(SystemKeys.SYSTEM_NAME, SystemProperties.SYSTEM_NAME);
map.put(SystemKeys.SYSTEM_BUILD, SystemProperties.SYSTEM_BUILD);
map.put(SystemKeys.SYSTEM_RELEASE, SystemProperties.SYSTEM_RELEASE);
map.put(SystemKeys.SYSTEM_NAME.getKey(), SystemProperties.SYSTEM_NAME);
map.put(SystemKeys.SYSTEM_BUILD.getKey(), SystemProperties.SYSTEM_BUILD);
map.put(SystemKeys.SYSTEM_RELEASE.getKey(), SystemProperties.SYSTEM_RELEASE);
map.put("conversionDate", new Date().toString());

map.put(SystemKeys.INPUT_URI, ai.getFile().toURI().toString());
map.put(SystemKeys.INPUT_URI.getKey(), ai.getFile().toURI().toString());

// Add default values for optional parameters

final String tempFilesDirectory = params.get(SystemKeys.TEMP_FILES_DIRECTORY);
final String tempFilesDirectory = params.get(SystemKeys.TEMP_FILES_DIRECTORY.getKey());

// Load additional settings from file
if (map.get("config")==null || "".equals(map.get("config"))) {
@@ -157,7 +157,7 @@ public static void run(File inputFile, File output, FilterLocale context, Map<St
String setup = map.remove("preset");
Map<String, Object> rp = d.loadSetup(map, setup);

boolean shouldPrintOptions = "true".equalsIgnoreCase(map.getOrDefault(SystemKeys.LIST_OPTIONS, "false"));
boolean shouldPrintOptions = "true".equalsIgnoreCase(map.getOrDefault(SystemKeys.LIST_OPTIONS.getKey(), "false"));
// Run tasks
try {
TaskSystem ts = TaskSystemFactoryMaker.newInstance().newTaskSystem(inputFormat, outputformat, context.toString());
@@ -4,65 +4,73 @@
* Provides common property keys used in the system.
* @author Joel Håkansson
*/
public interface SystemKeys {
public enum SystemKeys {
/**
* Defines a key for the system name
*/
public static final String SYSTEM_NAME = "systemName";
SYSTEM_NAME("systemName"),
/**
* Defines a key for the system build
*/
public static final String SYSTEM_BUILD = "systemBuild";
SYSTEM_BUILD("systemBuild"),
/**
* Defines a key for the system release
*/
public static final String SYSTEM_RELEASE = "systemRelease";
SYSTEM_RELEASE("systemRelease"),
/**
* Defines a key for the input file path
*/
public static final String INPUT = "input";
INPUT("input"),

public static final String INPUT_FORMAT = "inputFormat";
INPUT_FORMAT("inputFormat"),
/**
* Defines a key for the input uri
*/
public static final String INPUT_URI = "input-uri";
INPUT_URI("input-uri"),
/**
* Defines a key for the output format
*/
public static final String OUTPUT_FORMAT = "outputFormat";
OUTPUT_FORMAT("outputFormat"),

public static final String TEMPLATE = "template";
TEMPLATE("template"),



public static final String PEF_FORMAT = "pef";
public static final String FORMATTED_TEXT_FORMAT = "formatted-text";
public static final String OBFL_FORMAT = "obfl";
PEF_FORMAT("pef"),
FORMATTED_TEXT_FORMAT("formatted-text"),
OBFL_FORMAT("obfl"),

/**
* Defines a key for the configuration
*/
public static final String CONFIGURATION = "configuration";
CONFIGURATION("configuration"),
/**
* Defines a key for the temp files.
* Corresponding value should be the string "true" or "false"
*/
public static final String WRITE_TEMP_FILES = "writeTempFiles";
WRITE_TEMP_FILES("writeTempFiles"),
/**
* Defines a key for keeping temp files on success
* Corresponding value should be the string "true" or "false"
*/
public static final String KEEP_TEMP_FILES_ON_SUCCESS = "keepTempFilesOnSuccess";
KEEP_TEMP_FILES_ON_SUCCESS("keepTempFilesOnSuccess"),
/**
* Defines a key for the temp files directory.
* Corresponding value should be a string containing a file path
*/
public static final String TEMP_FILES_DIRECTORY = "tempFilesDirectory";
TEMP_FILES_DIRECTORY("tempFilesDirectory"),

/**
* Defines a key for listing the conversion options.
*
*/
public static final String LIST_OPTIONS = "listOptions";
LIST_OPTIONS("listOptions");
private final String key;
SystemKeys(String key) {
this.key = key;
}

public String getKey() {
return key;
}
}
@@ -71,7 +71,7 @@ public Convert() {
this.optionalArgs = new ArrayList<OptionalArgument>();
this.switches = new SwitchMap.Builder()
.addSwitch(new SwitchArgument('w', WATCH_KEY, WATCH_KEY, "" + DEFAULT_POLL_TIME, "Keeps the conversion in sync by watching the input file for changes and rerunning the conversion automatically when the input is modified."))
.addSwitch(new SwitchArgument('o', SystemKeys.LIST_OPTIONS, SystemKeys.LIST_OPTIONS, "true", "Lists additional options as the conversion runs."))
.addSwitch(new SwitchArgument('o', SystemKeys.LIST_OPTIONS.getKey(), SystemKeys.LIST_OPTIONS.getKey(), "true", "Lists additional options as the conversion runs."))
.addSwitch(new SwitchArgument('c', CONFIG_KEY, META_KEY, CONFIG_KEY, "Lists known configurations."))
.build();
this.parser = CommandParser.create(this);
@@ -130,25 +130,25 @@ public static void main(String[] args) throws InternalTaskException, IOException
//File output = new File(args[1]);
final HashMap<String, String> props = new HashMap<String, String>();
//props.put("debug", "true");
//props.put(SystemKeys.TEMP_FILES_DIRECTORY, TEMP_DIR);
//props.put(SystemKeys.TEMP_FILES_DIRECTORY.getKey(), TEMP_DIR);

props.putAll(result.getOptional());

if (input.isDirectory() && output.isDirectory()) {
if (result.getOptional().get(WATCH_KEY)!=null) {
logger.warning("'" + WATCH_KEY + "' is not implemented for batch mode.");
}
if ("true".equals(props.get(SystemKeys.WRITE_TEMP_FILES))) {
if ("true".equals(props.get(SystemKeys.WRITE_TEMP_FILES.getKey()))) {
ExitCode.ILLEGAL_ARGUMENT_VALUE.exitSystem("Cannot write debug files in batch mode.");
}
String format = props.get(SystemKeys.OUTPUT_FORMAT);
String format = props.get(SystemKeys.OUTPUT_FORMAT.getKey());
if (format==null) {
ExitCode.MISSING_ARGUMENT.exitSystem(SystemKeys.OUTPUT_FORMAT + " must be specified in batch mode.");
} else if (format.equals(SystemKeys.PEF_FORMAT)) {
ExitCode.MISSING_ARGUMENT.exitSystem(SystemKeys.OUTPUT_FORMAT.getKey() + " must be specified in batch mode.");
} else if (format.equals(SystemKeys.PEF_FORMAT.getKey())) {
format = "pef";
} else if (format.equals(SystemKeys.FORMATTED_TEXT_FORMAT)) {
} else if (format.equals(SystemKeys.FORMATTED_TEXT_FORMAT.getKey())) {
format = "txt";
} else if (format.equals(SystemKeys.OBFL_FORMAT)) {
} else if (format.equals(SystemKeys.OBFL_FORMAT.getKey())) {
format = "obfl";
} else {
ExitCode.ILLEGAL_ARGUMENT_VALUE.exitSystem("Unknown output format.");
@@ -300,18 +300,18 @@ public String getDescription() {

{
ArrayList<Definition> vals = new ArrayList<Definition>();
vals.add(new Definition(SystemKeys.PEF_FORMAT, "write result in PEF-format"));
vals.add(new Definition(SystemKeys.FORMATTED_TEXT_FORMAT, "write result as text"));
//vals.add(new Definition(SystemKeys.OBFL_FORMAT, "write result in OBFL-format (bypass formatter)"));
optionalArgs.add(new OptionalArgument(SystemKeys.OUTPUT_FORMAT, "Specifies output format", vals, "[detect]"));
vals.add(new Definition(SystemKeys.PEF_FORMAT.getKey(), "write result in PEF-format"));
vals.add(new Definition(SystemKeys.FORMATTED_TEXT_FORMAT.getKey(), "write result as text"));
//vals.add(new Definition(SystemKeys.OBFL_FORMAT.getKey(), "write result in OBFL-format (bypass formatter)"));
optionalArgs.add(new OptionalArgument(SystemKeys.OUTPUT_FORMAT.getKey(), "Specifies output format", vals, "[detect]"));
}
{
ArrayList<Definition> vals = new ArrayList<Definition>();
vals.add(new Definition("true", "outputs temp files"));
vals.add(new Definition("false", "does not output temp files"));
optionalArgs.add(new OptionalArgument(SystemKeys.WRITE_TEMP_FILES, "Writes temp files", vals, "false"));
optionalArgs.add(new OptionalArgument(SystemKeys.WRITE_TEMP_FILES.getKey(), "Writes temp files", vals, "false"));
}
optionalArgs.add(new OptionalArgument(SystemKeys.TEMP_FILES_DIRECTORY, "Path to temp files directory", DefaultTempFileWriter.TEMP_DIR));
optionalArgs.add(new OptionalArgument(SystemKeys.TEMP_FILES_DIRECTORY.getKey(), "Path to temp files directory", DefaultTempFileWriter.TEMP_DIR));
optionalArgs.add(new OptionalArgument(PEFConverterFacade.KEY_TABLE, "If specified, an ASCII-braille file (.brl) is generated in addition to the PEF-file using the specified braille code table", brailleInfo.getDefinitionList(), ""));
}
return optionalArgs;

0 comments on commit 6a286ce

Please sign in to comment.
You can’t perform that action at this time.