Had fight with maven. It decided to block HTTP repositories.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / renamer / Main.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.renamer;
8
9 import eu.svjatoslav.commons.string.GlobMatcher;
10 import eu.svjatoslav.meviz.Module;
11
12 import java.io.File;
13 import java.util.Arrays;
14
15 public class Main implements Module {
16
17     private final CommandlineHandler commandlineHandler = new CommandlineHandler();
18     private int processedFilesCount;
19     private RenamingOptions options;
20
21     /**
22      * Verify that program has enough information to start executing
23      *
24      * @return true if all is ok, false if errors were found
25      */
26     private boolean checkThatThereIsEnoughDataToProceed() {
27         if (options.inputPatterns.size() == 0) {
28             System.out.println("Error: no input patterns given.");
29             return false;
30         }
31
32         if (options.outputPattern == null) {
33             System.out.println("Error: no output pattern given.");
34             return false;
35         }
36
37         return true;
38     }
39
40     private boolean fileMatchesInputPattern(final File file) {
41         final String fileName = file.getName();
42
43         for (final String inputPattern : options.inputPatterns)
44             if (GlobMatcher.match(fileName, inputPattern))
45                 return true;
46
47         return false;
48     }
49
50     @Override
51     public String getDescription() {
52         return "Mass rename files according to given pattern.";
53     }
54
55     @Override
56     public String getModuleCommand() {
57         return "rename";
58     }
59
60     private void processDirectory(final File directory) {
61         final File[] directoryContents = directory.listFiles();
62
63         // sort directory contents alphabetically
64         Arrays.sort(directoryContents);
65
66         for (final File subFile : directoryContents)
67             if (subFile.isDirectory()) {
68                 if (options.recursive)
69                     processFileOrDirectory(subFile);
70             } else
71                 processFileOrDirectory(subFile);
72     }
73
74     private void processFile(final File file) {
75         final StringBuilder targetFilePath = new StringBuilder();
76
77         targetFilePath.append(file.getParent());
78         targetFilePath.append("/");
79
80         for (int i = 0; i < options.outputPattern.length(); i++) {
81
82             final char c = options.outputPattern.charAt(i);
83
84             if (c == '*')
85                 targetFilePath.append(file.getName());
86             else if (c == '%') {
87
88                 final String processedFileCountString = String
89                         .valueOf(processedFilesCount);
90
91                 for (int j = 0; j < (5 - processedFileCountString.length()); j++)
92                     targetFilePath.append(0);
93
94                 targetFilePath.append(processedFileCountString);
95                 processedFilesCount++;
96             } else if (c == '_')
97                 targetFilePath.append(" ");
98             else
99                 targetFilePath.append(c);
100         }
101
102         final File targetFile = new File(targetFilePath.toString());
103
104         if (!options.testOnly) {
105
106             if (targetFile.exists())
107                 System.out.println("Renaming aborted because target file: "
108                         + targetFile.getAbsolutePath() + " already exists.");
109             else
110                 file.renameTo(targetFile);
111
112         } else {
113             System.out.println("About to rename file: "
114                     + file.getAbsolutePath());
115             System.out.println("into: " + targetFile.getAbsolutePath());
116         }
117     }
118
119     private void processFileOrDirectory(final File file) {
120         if (!file.exists()) {
121             System.out.println("Error: file \"" + file.getAbsolutePath()
122                     + "\" does not exist.");
123             return;
124         }
125
126         if (file.isDirectory())
127             processDirectory(file);
128         else if (file.isFile())
129             if (fileMatchesInputPattern(file))
130                 processFile(file);
131
132     }
133
134     private void rename() {
135         processFileOrDirectory(options.targetDirectory);
136     }
137
138     @Override
139     public void run(final String[] args) {
140
141         options = commandlineHandler.parseCommandlineArguments(args);
142
143         if (options == null) {
144             showCommandlineHelp();
145             return;
146         }
147
148         if (checkThatThereIsEnoughDataToProceed()) {
149
150             System.out.println("Renaming using input patterns: ");
151             for (final String inputPattern : options.inputPatterns)
152                 System.out.println("    " + inputPattern);
153
154             rename();
155         } else
156             showCommandlineHelp();
157     }
158
159     @Override
160     public void showCommandlineHelp() {
161         commandlineHandler.parser.showHelp();
162         System.out.println("");
163         System.out.println("Output pattern special symbols:");
164         System.out.println("    % - file number");
165         System.out.println("    * - original file name");
166     }
167
168 }