Fixed aspect ratio of WEBM files
[meviz.git] / src / main / java / eu / svjatoslav / meviz / textsplitter / Main.java
index 5d8082e..c955796 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
- * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright (C) 2012 -- 2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
 
 package eu.svjatoslav.meviz.textsplitter;
 
-import java.io.BufferedReader;
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-
 import eu.svjatoslav.commons.file.FilePathParser;
 import eu.svjatoslav.commons.string.String2;
 import eu.svjatoslav.commons.string.WildCardMatcher;
 import eu.svjatoslav.meviz.Module;
 
+import java.io.*;
+
 public class Main implements Module {
 
-       CommandlineHandler commandlineHandler = new CommandlineHandler();
+    private final CommandlineHandler commandlineHandler = new CommandlineHandler();
 
-       TextSplittingOptions options;
+    private TextSplittingOptions options;
 
-       public boolean fileMatchesInputPattern(final File file) {
-               final String fileName = file.getName().toLowerCase();
+    private boolean fileMatchesInputPattern(final File file) {
+        final String fileName = file.getName().toLowerCase();
 
-               for (final String inputPattern : options.fileInputPatterns)
-                       if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
-                               return true;
+        for (final String inputPattern : options.fileInputPatterns)
+            if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
+                return true;
 
-               return false;
-       }
+        return false;
+    }
 
-       @Override
-       public String getDescription() {
-               return "Split text with long lines into multiple shorter lines.";
-       }
+    @Override
+    public String getDescription() {
+        return "Split text with long lines into multiple shorter lines.";
+    }
 
-       @Override
-       public String getModuleCommand() {
-               return "splittext";
-       }
+    @Override
+    public String getModuleCommand() {
+        return "splittext";
+    }
 
-       private File getTargetFile(final File sourceFile) {
-               final StringBuffer targetFilePath = new StringBuffer();
-               targetFilePath.append(sourceFile.getParent());
-               targetFilePath.append("/");
-               targetFilePath.append(FilePathParser
-                               .getFileNameWithoutExtension(sourceFile));
+    private File getTargetFile(final File sourceFile) {
+        final StringBuilder targetFilePath = new StringBuilder();
+        targetFilePath.append(sourceFile.getParent());
+        targetFilePath.append("/");
+        targetFilePath.append(FilePathParser
+                .getFileNameWithoutExtension(sourceFile));
 
-               targetFilePath.append(".splitted");
+        targetFilePath.append(".splitted");
 
-               // add file extension
-               {
-                       final String fileExtension = FilePathParser
-                                       .getFileExtension(sourceFile);
+        // add file extension
+        {
+            final String fileExtension = FilePathParser
+                    .getFileExtension(sourceFile);
 
-                       if (fileExtension.length() > 0)
-                               targetFilePath.append("." + fileExtension);
-               }
-               return new File(targetFilePath.toString());
-       }
+            if (fileExtension.length() > 0)
+                targetFilePath.append("." + fileExtension);
+        }
+        return new File(targetFilePath.toString());
+    }
 
-       public void processDirectory(final File directory) {
+    private void processDirectory(final File directory) {
 
-               for (final File subFile : directory.listFiles())
-                       if (subFile.isDirectory()) {
-                               if (options.recursive)
-                                       processDirectory(subFile);
-                       } else if (fileMatchesInputPattern(subFile))
-                               try {
-                                       processFile(subFile);
-                               } catch (final IOException exception) {
-                                       System.out.println("Error processing file: "
-                                                       + subFile.getAbsolutePath());
-                                       System.out.println("   exception: "
-                                                       + exception.getMessage());
-                               }
+        for (final File subFile : directory.listFiles())
+            if (subFile.isDirectory()) {
+                if (options.recursive)
+                    processDirectory(subFile);
+            } else if (fileMatchesInputPattern(subFile))
+                try {
+                    processFile(subFile);
+                } catch (final IOException exception) {
+                    System.out.println("Error processing file: "
+                            + subFile.getAbsolutePath());
+                    System.out.println("   exception: "
+                            + exception.getMessage());
+                }
 
-       }
+    }
 
-       public void processFile(final File file) throws IOException {
-               final File targetFile = getTargetFile(file);
+    private void processFile(final File file) throws IOException {
+        final File targetFile = getTargetFile(file);
 
-               final BufferedReader bufferedReader = new BufferedReader(
-                               new FileReader(file));
+        final BufferedReader bufferedReader = new BufferedReader(
+                new FileReader(file));
 
-               final BufferedWriter bufferedWriter = new BufferedWriter(
-                               new FileWriter(targetFile));
+        final BufferedWriter bufferedWriter = new BufferedWriter(
+                new FileWriter(targetFile));
 
-               while (true) {
-                       final String readLine = bufferedReader.readLine();
-                       if (readLine == null)
-                               break;
+        while (true) {
+            final String readLine = bufferedReader.readLine();
+            if (readLine == null)
+                break;
 
-                       final String2 cuttableString = new String2(readLine);
+            final String2 cuttableString = new String2(readLine);
 
-                       while (!cuttableString.isEmpty()) {
-                               for (final String pattern : options.textSplitPatterns)
-                                       if (WildCardMatcher.match(cuttableString.toString(),
-                                                       pattern))
-                                               bufferedWriter.write("\n");
+            while (!cuttableString.isEmpty()) {
+                for (final String pattern : options.textSplitPatterns)
+                    if (WildCardMatcher.match(cuttableString.toString(),
+                            pattern))
+                        bufferedWriter.write("\n");
 
-                               final String character = cuttableString.cutLeft(1);
-                               bufferedWriter.append(character);
-                       }
+                final String character = cuttableString.trimLeft(1);
+                bufferedWriter.append(character);
+            }
 
-                       bufferedWriter.write("\n");
-               }
+            bufferedWriter.write("\n");
+        }
 
-               bufferedReader.close();
-               bufferedWriter.close();
-       }
+        bufferedReader.close();
+        bufferedWriter.close();
+    }
 
-       @Override
-       public void run(final String[] args) throws IOException {
+    @Override
+    public void run(final String[] args) throws IOException {
 
-               options = commandlineHandler.parseCommandlineArguments(args);
+        options = commandlineHandler.parseCommandlineArguments(args);
 
-               if (options == null) {
-                       showCommandlineHelp();
-                       return;
-               }
-
-               processDirectory(options.targetDirectory);
+        if (options == null) {
+            showCommandlineHelp();
+            return;
+        }
+
+        processDirectory(options.targetDirectory);
 
-       }
-
-       @Override
-       public void showCommandlineHelp() {
-               commandlineHandler.parser.showHelp();
-       }
+    }
+
+    @Override
+    public void showCommandlineHelp() {
+        commandlineHandler.parser.showHelp();
+    }
 
 }