updated meviz to work with new commandline parsing API
[meviz.git] / src / main / java / eu / svjatoslav / meviz / replace / 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.replace;
11
12 import java.io.BufferedReader;
13 import java.io.File;
14 import java.io.FileReader;
15 import java.io.FileWriter;
16 import java.io.IOException;
17
18 import eu.svjatoslav.meviz.Module;
19
20 public class Main implements Module {
21
22         CommandlineHandler commandlineHandler = new CommandlineHandler();
23
24         CommandlineOptions options;
25
26         @Override
27         public String getDescription() {
28                 return "Replace one string to another string in all occurrances and all files recursively.";
29         }
30
31         @Override
32         public String getModuleCommand() {
33                 return "replace";
34         }
35
36         public void processDirectory(final File directory) {
37
38                 for (final File file : directory.listFiles())
39                         if (file.isDirectory()) {
40                                 if (options.recursive)
41                                         processDirectory(file);
42                         } else
43                                 try {
44                                         processFile(file);
45                                 } catch (final IOException exception) {
46                                         System.out.println("Error processing file: "
47                                                         + file.getAbsolutePath());
48                                         System.out.println("   exception: "
49                                                         + exception.getMessage());
50                                 }
51
52         }
53
54         public void processFile(final File file) throws IOException {
55
56                 final FileReader fileReader = new FileReader(file);
57                 final BufferedReader bufferedReader = new BufferedReader(fileReader);
58
59                 final StringBuffer result = new StringBuffer();
60
61                 boolean contentChanged = false;
62
63                 while (true) {
64                         final String line = bufferedReader.readLine();
65                         if (line == null)
66                                 break;
67
68                         final String newLine = line.replace(options.searchForPattern,
69                                         options.replaceWithPattern);
70
71                         if (!contentChanged)
72                                 if (!newLine.equals(line))
73                                         contentChanged = true;
74
75                         result.append(newLine);
76                         result.append("\n");
77                 }
78
79                 bufferedReader.close();
80                 fileReader.close();
81
82                 if (contentChanged) {
83                         final FileWriter fileWriter = new FileWriter(file);
84                         fileWriter.write(result.toString());
85                         fileWriter.close();
86                 }
87
88         }
89
90         @Override
91         public void run(final String[] args) throws IOException {
92
93                 options = commandlineHandler.parseCommandlineArguments(args);
94
95                 if (options == null) {
96                         showCommandlineHelp();
97                         return;
98                 }
99
100                 processDirectory(options.targetDirectory);
101
102         }
103
104         @Override
105         public void showCommandlineHelp() {
106                 commandlineHandler.parser.showHelp();
107         }
108
109 }