Disabled terminal commandline option.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 9 Nov 2013 22:04:43 +0000 (00:04 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 9 Nov 2013 22:04:43 +0000 (00:04 +0200)
Correctly handle aphostrophes in the encoded file names.

src/main/java/eu/svjatoslav/meviz/encoder/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/encoder/Encoder.java
src/main/java/eu/svjatoslav/meviz/encoder/EncodingOptions.java
src/main/java/eu/svjatoslav/meviz/encoder/EncodingPlan.java
src/main/java/eu/svjatoslav/meviz/encoder/EncodingTask.java

index 870d476..b6e10ce 100755 (executable)
@@ -22,9 +22,6 @@ public class CommandlineHandler {
                        .createStringParameter("Encoding output format.")
                        .addAliases("-o", "--output-format").setMandatory();
 
-       NullParameter terminalParameter = parser.createNullParameter(
-                       "Enable popup terminal.").addAliases("--terminal");
-
        NullParameter deinterlaceParameter = parser.createNullParameter(
                        "Deinterlace video.").addAliases("--deinterlace", "-d");
 
@@ -67,9 +64,6 @@ public class CommandlineHandler {
                if (deinterlaceParameter.isParameterSpecified())
                        options.deinterlace = true;
 
-               if (terminalParameter.isParameterSpecified())
-                       options.terminal = true;
-
                if (testParameter.isParameterSpecified())
                        options.testOnly = true;
 
index 420fa49..879c6da 100755 (executable)
@@ -91,8 +91,7 @@ public class Encoder implements Module {
                                                        final EncodingTask encodingTask = new EncodingTask(
                                                                        sourceFile, targetFile, chosenFormat);
 
-                                                       if (encodingOptions.terminal
-                                                                       || chosenFormat.isTerminalMandatory())
+                                                       if (chosenFormat.isTerminalMandatory())
                                                                encodingTask.setUseTerminal(true);
                                                        encodingPlan.scheduleTask(encodingTask);
                                                }
index 9f14546..463b48a 100755 (executable)
@@ -23,8 +23,6 @@ public class EncodingOptions {
 
        public boolean recursive;
 
-       public boolean terminal;
-
        public boolean testOnly;
 
        public File workingDirectory = new File(System.getProperty("user.dir"));
index f23fbdb..e4c75c3 100755 (executable)
@@ -14,37 +14,18 @@ import java.util.ArrayList;
 
 public class EncodingPlan {
 
-    public ArrayList<EncodingTask> encodingTasks = new ArrayList<EncodingTask>();
+       public ArrayList<EncodingTask> encodingTasks = new ArrayList<EncodingTask>();
 
-    public void execute(final EncodingOptions encodingOptions) throws IOException {
-        for (final EncodingTask task : encodingTasks) {
+       public void execute(final EncodingOptions encodingOptions)
+                       throws IOException {
 
-            try {
-                String command = task.getCommand(encodingOptions);
+               for (final EncodingTask task : encodingTasks)
+                       task.execute(encodingOptions);
 
-                if (task.doUseTerminal()) {
-                    command = "xterm -e '" + command + "'";
-                }
+       }
 
-                System.out.println("Executing command: " + command);
-
-                final Runtime run = Runtime.getRuntime();
-                Process pr;
-                pr = run.exec(new String[] { "/bin/bash", "-c", command });
-
-                pr.waitFor();
-
-            } catch (final Exception e) {
-                System.out.println(e.toString());
-                e.printStackTrace();
-            }
-
-        }
-
-    }
-
-    public void scheduleTask(final EncodingTask encodingTask) {
-        encodingTasks.add(encodingTask);
-    };
+       public void scheduleTask(final EncodingTask encodingTask) {
+               encodingTasks.add(encodingTask);
+       };
 
 }
index 9db5d92..54811c2 100755 (executable)
@@ -15,45 +15,81 @@ import eu.svjatoslav.meviz.encoder.converters.Converter;
 
 public class EncodingTask {
 
-    /**
-     * Source file to encode
-     */
-    private final File source;
-
-    /**
-     * Target file.
-     */
-    private final File target;
-
-    private final Converter converter;
-
-    private boolean useTerminal;
-
-    public EncodingTask(final File source, final File destination,
-            final eu.svjatoslav.meviz.encoder.converters.Converter converter) {
-
-        this.source = source;
-        target = destination;
-        this.converter = converter;
-
-    }
-
-    /**
-     * @return the useTerminal
-     */
-    public boolean doUseTerminal() {
-        return useTerminal;
-    }
-
-    public String getCommand(final EncodingOptions encodingOptions) {
-        return converter.getCommand(source, target, encodingOptions);
-    }
-
-    /**
-     * @param useTerminal
-     *            the useTerminal to set
-     */
-    public void setUseTerminal(final boolean useTerminal) {
-        this.useTerminal = useTerminal;
-    }
+       /**
+        * Source file to encode
+        */
+       private final File source;
+
+       /**
+        * Target file.
+        */
+       private final File target;
+
+       private final Converter converter;
+
+       private boolean useTerminal;
+
+       public EncodingTask(final File source, final File destination,
+                       final eu.svjatoslav.meviz.encoder.converters.Converter converter) {
+
+               this.source = source;
+               target = destination;
+               this.converter = converter;
+
+       }
+
+       /**
+        * @return the useTerminal
+        */
+       public boolean doUseTerminal() {
+               return useTerminal;
+       }
+
+       public String encodeApostrophes(final String input) {
+               final StringBuffer result = new StringBuffer();
+
+               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 run = Runtime.getRuntime();
+                       Process pr;
+                       pr = run.exec(new String[] { "/bin/bash", "-c", command });
+
+                       pr.waitFor();
+
+               } catch (final Exception e) {
+                       System.out.println(e.toString());
+                       e.printStackTrace();
+               }
+
+       }
+
+       public String getCommand(final EncodingOptions encodingOptions) {
+               return converter.getCommand(source, target, encodingOptions);
+       }
+
+       /**
+        * @param useTerminal
+        *            the useTerminal to set
+        */
+       public void setUseTerminal(final boolean useTerminal) {
+               this.useTerminal = useTerminal;
+       }
 }