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