2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012 -- 2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.replace;
12 import eu.svjatoslav.meviz.Module;
16 public class Main implements Module {
18 private final CommandlineHandler commandlineHandler = new CommandlineHandler();
20 private CommandlineOptions options;
23 public String getDescription() {
24 return "Replace one string to another string in all occurrances and all files recursively.";
28 public String getModuleCommand() {
32 private void processDirectory(final File directory) {
34 for (final File file : directory.listFiles())
35 if (file.isDirectory()) {
36 if (options.recursive)
37 processDirectory(file);
41 } catch (final IOException exception) {
42 System.out.println("Error processing file: "
43 + file.getAbsolutePath());
44 System.out.println(" exception: "
45 + exception.getMessage());
50 private void processFile(final File file) throws IOException {
52 final FileReader fileReader = new FileReader(file);
53 final BufferedReader bufferedReader = new BufferedReader(fileReader);
55 final StringBuilder result = new StringBuilder();
57 boolean contentChanged = false;
60 final String line = bufferedReader.readLine();
64 final String newLine = line.replace(options.searchForPattern,
65 options.replaceWithPattern);
68 if (!newLine.equals(line))
69 contentChanged = true;
71 result.append(newLine);
75 bufferedReader.close();
79 final FileWriter fileWriter = new FileWriter(file);
80 fileWriter.write(result.toString());
87 public void run(final String[] args) throws IOException {
89 options = commandlineHandler.parseCommandlineArguments(args);
91 if (options == null) {
92 showCommandlineHelp();
96 processDirectory(options.targetDirectory);
101 public void showCommandlineHelp() {
102 commandlineHandler.parser.showHelp();