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