Updated copyright message.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / renamer / Main.java
index 8818889..f4fc359 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 -- 2018, 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.renamer;
 
-import java.io.File;
-import java.util.Arrays;
-
 import eu.svjatoslav.commons.string.WildCardMatcher;
 import eu.svjatoslav.meviz.Module;
 
-public class Main implements Module {
-
-       CommandlineHandler commandlineHandler = new CommandlineHandler();
-
-       public int processedFilesCount;
-
-       RenamingOptions options;
-
-       /**
-        * Verify that program has enough information to start executing
-        * 
-        * @return true if all is ok, false if errors were found
-        */
-       public boolean checkThatThereIsEnoughDataToProceed() {
-               if (options.inputPatterns.size() == 0) {
-                       System.out.println("Error: no input patterns given.");
-                       return false;
-               }
-
-               if (options.outputPattern == null) {
-                       System.out.println("Error: no output pattern given.");
-                       return false;
-               }
-
-               return true;
-       }
-
-       public boolean fileMatchesInputPattern(final File file) {
-               final String fileName = file.getName();
-
-               for (final String inputPattern : options.inputPatterns)
-                       if (WildCardMatcher.match(fileName, inputPattern))
-                               return true;
+import java.io.File;
+import java.util.Arrays;
 
-               return false;
-       }
+public class Main implements Module {
 
-       @Override
-       public String getDescription() {
-               return "Mass rename files according to given pattern.";
-       }
+    private final CommandlineHandler commandlineHandler = new CommandlineHandler();
+    private int processedFilesCount;
+    private RenamingOptions options;
+
+    /**
+     * Verify that program has enough information to start executing
+     *
+     * @return true if all is ok, false if errors were found
+     */
+    private boolean checkThatThereIsEnoughDataToProceed() {
+        if (options.inputPatterns.size() == 0) {
+            System.out.println("Error: no input patterns given.");
+            return false;
+        }
+
+        if (options.outputPattern == null) {
+            System.out.println("Error: no output pattern given.");
+            return false;
+        }
+
+        return true;
+    }
+
+    private boolean fileMatchesInputPattern(final File file) {
+        final String fileName = file.getName();
 
-       @Override
-       public String getModuleCommand() {
-               return "rename";
-       };
+        for (final String inputPattern : options.inputPatterns)
+            if (WildCardMatcher.match(fileName, inputPattern))
+                return true;
+
+        return false;
+    }
 
-       private void processDirectory(final File directory) {
-               final File[] directoryContents = directory.listFiles();
+    @Override
+    public String getDescription() {
+        return "Mass rename files according to given pattern.";
+    }
 
-               // sort directory contents alphabetically
-               Arrays.sort(directoryContents);
-
-               for (final File subFile : directoryContents)
-                       if (subFile.isDirectory()) {
-                               if (options.recursive)
-                                       processFileOrDirectory(subFile);
-                       } else
-                               processFileOrDirectory(subFile);
-       }
-
-       private void processFile(final File file) {
-               final StringBuffer targetFilePath = new StringBuffer();
-
-               targetFilePath.append(file.getParent());
-               targetFilePath.append("/");
-
-               for (int i = 0; i < options.outputPattern.length(); i++) {
-
-                       final char c = options.outputPattern.charAt(i);
-
-                       if (c == '*')
-                               targetFilePath.append(file.getName());
-                       else if (c == '%') {
-
-                               final String processedFileCountString = String
-                                               .valueOf(processedFilesCount);
-
-                               for (int j = 0; j < (5 - processedFileCountString.length()); j++)
-                                       targetFilePath.append(0);
-
-                               targetFilePath.append(processedFileCountString);
-                               processedFilesCount++;
-                       } else if (c == '_')
-                               targetFilePath.append(" ");
-                       else
-                               targetFilePath.append(c);
-               }
-
-               final File targetFile = new File(targetFilePath.toString());
-
-               if (!options.testOnly) {
-
-                       if (targetFile.exists())
-                               System.out.println("Renaming aborted because target file: "
-                                               + targetFile.getAbsolutePath() + " already exists.");
-                       else
-                               file.renameTo(targetFile);
-
-               } else {
-                       System.out.println("About to rename file: "
-                                       + file.getAbsolutePath());
-                       System.out.println("into: " + targetFile.getAbsolutePath());
-               }
-       }
-
-       public void processFileOrDirectory(final File file) {
-               if (!file.exists()) {
-                       System.out.println("Error: file \"" + file.getAbsolutePath()
-                                       + "\" does not exist.");
-                       return;
-               }
-
-               if (file.isDirectory())
-                       processDirectory(file);
-               else if (file.isFile())
-                       if (fileMatchesInputPattern(file))
-                               processFile(file);
-
-       }
-
-       public void rename() {
-               processFileOrDirectory(options.targetDirectory);
-       }
-
-       @Override
-       public void run(final String[] args) {
-
-               options = commandlineHandler.parseCommandlineArguments(args);
-
-               if (options == null) {
-                       showCommandlineHelp();
-                       return;
-               }
-
-               if (checkThatThereIsEnoughDataToProceed()) {
-
-                       System.out.println("Renaming using input patterns: ");
-                       for (final String inputPattern : options.inputPatterns)
-                               System.out.println("    " + inputPattern);
-
-                       rename();
-               } else
-                       showCommandlineHelp();
-       }
-
-       @Override
-       public void showCommandlineHelp() {
-               commandlineHandler.parser.showHelp();
-               System.out.println("");
-               System.out.println("Output pattern special symbols:");
-               System.out.println("    % - file number");
-               System.out.println("    * - original file name");
-       }
+    @Override
+    public String getModuleCommand() {
+        return "rename";
+    }
+
+    private void processDirectory(final File directory) {
+        final File[] directoryContents = directory.listFiles();
+
+        // sort directory contents alphabetically
+        Arrays.sort(directoryContents);
+
+        for (final File subFile : directoryContents)
+            if (subFile.isDirectory()) {
+                if (options.recursive)
+                    processFileOrDirectory(subFile);
+            } else
+                processFileOrDirectory(subFile);
+    }
+
+    private void processFile(final File file) {
+        final StringBuilder targetFilePath = new StringBuilder();
+
+        targetFilePath.append(file.getParent());
+        targetFilePath.append("/");
+
+        for (int i = 0; i < options.outputPattern.length(); i++) {
+
+            final char c = options.outputPattern.charAt(i);
+
+            if (c == '*')
+                targetFilePath.append(file.getName());
+            else if (c == '%') {
+
+                final String processedFileCountString = String
+                        .valueOf(processedFilesCount);
+
+                for (int j = 0; j < (5 - processedFileCountString.length()); j++)
+                    targetFilePath.append(0);
+
+                targetFilePath.append(processedFileCountString);
+                processedFilesCount++;
+            } else if (c == '_')
+                targetFilePath.append(" ");
+            else
+                targetFilePath.append(c);
+        }
+
+        final File targetFile = new File(targetFilePath.toString());
+
+        if (!options.testOnly) {
+
+            if (targetFile.exists())
+                System.out.println("Renaming aborted because target file: "
+                        + targetFile.getAbsolutePath() + " already exists.");
+            else
+                file.renameTo(targetFile);
+
+        } else {
+            System.out.println("About to rename file: "
+                    + file.getAbsolutePath());
+            System.out.println("into: " + targetFile.getAbsolutePath());
+        }
+    }
+
+    private void processFileOrDirectory(final File file) {
+        if (!file.exists()) {
+            System.out.println("Error: file \"" + file.getAbsolutePath()
+                    + "\" does not exist.");
+            return;
+        }
+
+        if (file.isDirectory())
+            processDirectory(file);
+        else if (file.isFile())
+            if (fileMatchesInputPattern(file))
+                processFile(file);
+
+    }
+
+    private void rename() {
+        processFileOrDirectory(options.targetDirectory);
+    }
+
+    @Override
+    public void run(final String[] args) {
+
+        options = commandlineHandler.parseCommandlineArguments(args);
+
+        if (options == null) {
+            showCommandlineHelp();
+            return;
+        }
+
+        if (checkThatThereIsEnoughDataToProceed()) {
+
+            System.out.println("Renaming using input patterns: ");
+            for (final String inputPattern : options.inputPatterns)
+                System.out.println("    " + inputPattern);
+
+            rename();
+        } else
+            showCommandlineHelp();
+    }
+
+    @Override
+    public void showCommandlineHelp() {
+        commandlineHandler.parser.showHelp();
+        System.out.println("");
+        System.out.println("Output pattern special symbols:");
+        System.out.println("    % - file number");
+        System.out.println("    * - original file name");
+    }
 
 }