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