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;
27 public String getDescription() {
28 return "Replace one string to another string in all occurrances and all files recursively.";
32 public String getModuleCommand() {
36 public void processDirectory(final File directory) {
38 for (final File file : directory.listFiles())
39 if (file.isDirectory()) {
40 if (options.recursive)
41 processDirectory(file);
45 } catch (final IOException exception) {
46 System.out.println("Error processing file: "
47 + file.getAbsolutePath());
48 System.out.println(" exception: "
49 + exception.getMessage());
54 public void processFile(final File file) throws IOException {
56 final FileReader fileReader = new FileReader(file);
57 final BufferedReader bufferedReader = new BufferedReader(fileReader);
59 final StringBuffer result = new StringBuffer();
61 boolean contentChanged = false;
64 final String line = bufferedReader.readLine();
68 final String newLine = line.replace(options.searchForPattern,
69 options.replaceWithPattern);
72 if (!newLine.equals(line))
73 contentChanged = true;
75 result.append(newLine);
79 bufferedReader.close();
83 final FileWriter fileWriter = new FileWriter(file);
84 fileWriter.write(result.toString());
91 public void run(final String[] args) throws IOException {
93 options = commandlineHandler.parseCommandlineArguments(args);
95 if (options == null) {
96 showCommandlineHelp();
100 processDirectory(options.targetDirectory);
105 public void showCommandlineHelp() {
106 commandlineHandler.parser.showHelp();