2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, 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 java.io.BufferedReader;
14 import java.io.FileReader;
15 import java.io.FileWriter;
16 import java.io.IOException;
18 import eu.svjatoslav.meviz.Module;
20 public class Main implements Module {
22 CommandlineHandler commandlineHandler = new CommandlineHandler();
24 CommandlineOptions options;
28 public String getDescription() {
29 return "Replace one string to another string in all occurrances and all files recursively.";
33 public String getModuleCommand() {
37 public void processDirectory(final File directory) {
39 for (final File file : directory.listFiles())
40 if (file.isDirectory()) {
41 if (options.recursive)
42 processDirectory(file);
46 } catch (final IOException exception) {
47 System.out.println("Error processing file: "
48 + file.getAbsolutePath());
49 System.out.println(" exception: "
50 + exception.getMessage());
55 public void processFile(final File file) throws IOException {
57 FileReader fileReader = new FileReader(file);
58 BufferedReader bufferedReader = new BufferedReader(fileReader);
60 StringBuffer result = new StringBuffer();
62 boolean contentChanged = false;
65 String line = bufferedReader.readLine();
69 String newLine = line.replace(options.searchForPattern,
70 options.replaceWithPattern);
73 if (!newLine.equals(line))
74 contentChanged = true;
76 result.append(newLine);
80 bufferedReader.close();
84 FileWriter fileWriter = new FileWriter(file);
85 fileWriter.write(result.toString());
92 public void run(final String[] args) throws IOException {
94 options = commandlineHandler.parseCommandlineArguments(args);
96 if (options == null) {
97 showCommandlineHelp();
101 processDirectory(options.targetDirectory);
106 public void showCommandlineHelp() {
107 commandlineHandler.initParser().showHelp();