+ /**
+ * Source file to encode
+ */
+ private final File source;
+
+ /**
+ * Target file.
+ */
+ private final File target;
+
+ private final AbstractConverter converter;
+ private final String targetFormat;
+ private boolean useTerminal;
+
+ public EncodingTask(final File source, final File target, final AbstractConverter converter,
+ final String targetFormat) {
+
+ this.source = source;
+ this.target = target;
+ this.converter = converter;
+ this.targetFormat = targetFormat;
+ }
+
+ /**
+ * @return the useTerminal
+ */
+ public boolean doUseTerminal() {
+ return useTerminal;
+ }
+
+ public String encodeApostrophes(final String input) {
+ final StringBuilder result = new StringBuilder();
+
+ for (final char c : input.toCharArray()) {
+ if (c == '\'') {
+ result.append("'\\''");
+ continue;
+ }
+ result.append(c);
+ }
+
+ return result.toString();
+ }
+
+ public void execute(final EncodingOptions encodingOptions) {
+ try {
+ String command = getCommand(encodingOptions);
+
+ if (doUseTerminal())
+ command = "xterm -e '" + encodeApostrophes(command) + "'";
+
+ System.out.println("Executing command: " + command);
+
+ final Runtime runtime = Runtime.getRuntime();
+ final Process process = runtime.exec(new String[]{"/bin/bash", "-c", command});
+
+ process.waitFor();
+
+ } catch (final Exception e) {
+ System.out.println(e.toString());
+ e.printStackTrace();
+ }
+
+ }
+
+ public String getCommand(final EncodingOptions encodingOptions) {
+ return converter.getCommand(source, target, encodingOptions, targetFormat);
+ }
+
+ /**
+ * @param useTerminal the useTerminal to set
+ */
+ public void setUseTerminal(final boolean useTerminal) {
+ this.useTerminal = useTerminal;
+ }