Console console = System.console();
if (console == null) {
System.out.println("This application can only be used from the command line.");
System.exit(-1);
}
IConverter converter = asConverter(args);
try {
Logger logger = LoggerFactory.getLogger(StandaloneClient.class);
sayHello(converter, logger, console);
DocumentType[] documentTypes = configureConversion(console, converter.getSupportedConversions());
console.printf("Enter '<source> [-> <target>]' for converting a file. Enter '\\q' for exiting this application.%n");
String argument;
do {
console.printf("> ");
argument = console.readLine();
if (argument != null) {
if (argument.equals("\\q")) {
break;
} else if (argument.equals("\\f")) {
documentTypes = configureConversion(console, converter.getSupportedConversions());
} else if (argument.trim().equals("")) {
continue;
}
int targetIndex = argument.indexOf("->");
String source = targetIndex == -1 ? argument : argument.substring(0, targetIndex);
File sourceFile = normalize(source);
if (!sourceFile.isFile()) {
console.printf("Input file does not exist: %s%n", sourceFile);
continue;
}
String target = targetIndex == -1 ? source + "." + extensionFor(documentTypes[1]) : argument.substring(targetIndex + 1);
File targetFile = normalize(target);
converter.convert(sourceFile).as(documentTypes[0])
.to(targetFile, new LoggingFileConsumer(sourceFile, logger)).as(documentTypes[1])
.schedule();
console.printf("Scheduled conversion: %s -> %s%n", sourceFile, targetFile);
logger.info("Converting {} to {}", sourceFile, targetFile);
} else {
logger.error("Error when reading from console.");
}
} while (argument != null);
sayGoodbye(converter, logger);
} finally {
converter.shutDown();
}
console.printf("The connection was successfully closed. Goodbye!%n");
} catch (Exception e) {
LoggerFactory.getLogger(StandaloneClient.class).error("The document conversion client terminated with an unexpected error", e);
System.err.println(String.format("Error: %s", e.getMessage()));