initial commit
[meviz.git] / src / main / java / eu / svjatoslav / meviz / renamer / Main.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public License
7  * as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.meviz.renamer;
11
12 import java.io.File;
13
14 import eu.svjatoslav.commons.string.WildCardMatcher;
15 import eu.svjatoslav.meviz.Module;
16
17 public class Main implements Module {
18
19         CommandlineHandler commandlineHandler = new CommandlineHandler();
20
21         public int processedFilesCount;
22
23         RenamingOptions options;
24
25         /**
26          * Verify that program has enough information to start executing
27          * 
28          * @return true if all is ok, false if errors were found
29          */
30         public boolean checkThatThereIsEnoughDataToProceed() {
31                 if (options.inputPatterns.size() == 0) {
32                         System.out.println("Error: no input patterns given.");
33                         return false;
34                 }
35
36                 if (options.outputPattern == null) {
37                         System.out.println("Error: no output pattern given.");
38                         return false;
39                 }
40
41                 return true;
42         }
43
44         public boolean fileMatchesInputPattern(final File file) {
45                 final String fileName = file.getName();
46
47                 for (final String inputPattern : options.inputPatterns)
48                         if (WildCardMatcher.match(fileName, inputPattern))
49                                 return true;
50
51                 return false;
52         }
53
54         @Override
55         public String getDescription() {
56                 return "Mass rename files according to given pattern.";
57         }
58
59         @Override
60         public String getModuleCommand() {
61                 return "rename";
62         };
63
64         public void handleFile(final File file) {
65                 if (!file.exists()) {
66                         System.out.println("Error: file \"" + file.getAbsolutePath()
67                                         + "\" does not exist.");
68                         return;
69                 }
70
71                 if (file.isDirectory()) {
72                         for (final File subFile : file.listFiles())
73                                 if (subFile.isDirectory()) {
74                                         if (options.recursive)
75                                                 handleFile(subFile);
76                                 } else
77                                         handleFile(subFile);
78                 } else if (file.isFile())
79                         if (fileMatchesInputPattern(file)) {
80
81                                 final StringBuffer targetFilePath = new StringBuffer();
82
83                                 targetFilePath.append(file.getParent());
84                                 targetFilePath.append("/");
85
86                                 for (int i = 0; i < options.outputPattern.length(); i++) {
87
88                                         final char c = options.outputPattern.charAt(i);
89
90                                         if (c == '*')
91                                                 targetFilePath.append(file.getName());
92                                         else if (c == '%') {
93
94                                                 final String processedFileCountString = String
95                                                                 .valueOf(processedFilesCount);
96
97                                                 for (int j = 0; j < (5 - processedFileCountString
98                                                                 .length()); j++)
99                                                         targetFilePath.append(0);
100
101                                                 targetFilePath.append(processedFileCountString);
102                                                 processedFilesCount++;
103                                         } else if (c == '_')
104                                                 targetFilePath.append(" ");
105                                         else
106                                                 targetFilePath.append(c);
107                                 }
108
109                                 final File targetFile = new File(targetFilePath.toString());
110
111                                 if (!options.testOnly) {
112
113                                         if (targetFile.exists())
114                                                 System.out
115                                                                 .println("Renaming aborted because target file: "
116                                                                                 + targetFile.getAbsolutePath()
117                                                                                 + " already exists.");
118                                         else
119                                                 file.renameTo(targetFile);
120
121                                 } else {
122                                         System.out.println("About to rename file: "
123                                                         + file.getAbsolutePath());
124                                         System.out.println("into: " + targetFile.getAbsolutePath());
125                                 }
126                         }
127
128         }
129
130         public void rename() {
131                 handleFile(options.targetDirectory);
132         }
133
134         @Override
135         public void run(final String[] args) {
136
137                 options = commandlineHandler.parseCommandlineArguments(args);
138
139                 if (options == null) {
140                         showCommandlineHelp();
141                         return;
142                 }
143
144                 if (checkThatThereIsEnoughDataToProceed()) {
145
146                         System.out.println("Renaming using input patterns: ");
147                         for (final String inputPattern : options.inputPatterns)
148                                 System.out.println("    " + inputPattern);
149
150                         rename();
151                 } else
152                         showCommandlineHelp();
153         }
154
155         @Override
156         public void showCommandlineHelp() {
157                 commandlineHandler.initParser().showHelp();
158                 System.out.println("");
159                 System.out.println("Output pattern special symbols:");
160                 System.out.println("    % - file number");
161                 System.out.println("    * - original file name");
162         }
163
164 }