Had fight with maven. It decided to block HTTP repositories.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / EncodingTask.java
1 /*
2  * Meviz - Various tools collection to work with multimedia. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5
6
7 package eu.svjatoslav.meviz.encoder;
8
9 import eu.svjatoslav.meviz.encoder.converters.AbstractConverter;
10
11 import java.io.File;
12
13 public class EncodingTask {
14
15     /**
16      * Source file to encode
17      */
18     private final File source;
19
20     /**
21      * Target file.
22      */
23     private final File target;
24
25     private final AbstractConverter converter;
26     private final String targetFormat;
27     private boolean useTerminal;
28
29     public EncodingTask(final File source, final File target, final AbstractConverter converter,
30                         final String targetFormat) {
31
32         this.source = source;
33         this.target = target;
34         this.converter = converter;
35         this.targetFormat = targetFormat;
36     }
37
38     /**
39      * @return the useTerminal
40      */
41     private boolean doUseTerminal() {
42         return useTerminal;
43     }
44
45     private String encodeApostrophes(final String input) {
46         final StringBuilder result = new StringBuilder();
47
48         for (final char c : input.toCharArray()) {
49             if (c == '\'') {
50                 result.append("'\\''");
51                 continue;
52             }
53             result.append(c);
54         }
55
56         return result.toString();
57     }
58
59     public void execute(final EncodingOptions encodingOptions) {
60         try {
61             String command = getCommand(encodingOptions);
62
63             if (doUseTerminal())
64                 command = "xterm -e '" + encodeApostrophes(command) + "'";
65
66             System.out.println("Executing command: " + command);
67
68             final Runtime runtime = Runtime.getRuntime();
69             final Process process = runtime.exec(new String[]{"/bin/bash", "-c", command});
70
71             process.waitFor();
72
73         } catch (final Exception e) {
74             System.out.println(e.toString());
75             e.printStackTrace();
76         }
77
78     }
79
80     private String getCommand(final EncodingOptions encodingOptions) {
81         return converter.getCommand(source, target, encodingOptions, targetFormat);
82     }
83
84     /**
85      * @param useTerminal the useTerminal to set
86      */
87     public void setUseTerminal(final boolean useTerminal) {
88         this.useTerminal = useTerminal;
89     }
90 }