From: Svjatoslav Agejenko Date: Wed, 18 Sep 2013 11:33:23 +0000 (+0300) Subject: recursive replace X-Git-Tag: meviz-1.0~79 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=44b11b236b891659a86865c0ff66677fea5233f1;p=meviz.git recursive replace --- diff --git a/src/main/java/eu/svjatoslav/meviz/Main.java b/src/main/java/eu/svjatoslav/meviz/Main.java index f78bf81..a629d1b 100755 --- a/src/main/java/eu/svjatoslav/meviz/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/Main.java @@ -36,6 +36,7 @@ public class Main { modules.add(new eu.svjatoslav.meviz.texttruncate.Main()); modules.add(new eu.svjatoslav.meviz.textsplitter.Main()); modules.add(new eu.svjatoslav.meviz.bomremove.Main()); + modules.add(new eu.svjatoslav.meviz.replace.Main()); } public static void main(final String[] args) throws Exception { diff --git a/src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java new file mode 100755 index 0000000..4a8f336 --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java @@ -0,0 +1,73 @@ +/* + * Meviz - Various tools collection to work with multimedia. + * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + +package eu.svjatoslav.meviz.replace; + +import java.io.File; + +import eu.svjatoslav.commons.commandline.parameterparser.Parameter; +import eu.svjatoslav.commons.commandline.parameterparser.Parser; +import eu.svjatoslav.commons.commandline.parameterparser.arguments.ExistingDirectory; +import eu.svjatoslav.commons.commandline.parameterparser.arguments.StringArgument; + +public class CommandlineHandler { + + Parameter recursiveParameter = new Parameter("Enable recursive mode.", + "-r", "--recursive"); + + Parameter searchForPattern = new Parameter(true, true, false, + new StringArgument(), "String to search for", "-s", + "--search-pattern"); + + Parameter replaceWithPattern = new Parameter(true, true, false, + new StringArgument(), "String to place instead", "-p", + "--replace-pattern"); + + Parameter workingDirectoryParameter = new Parameter(false, true, false, + new ExistingDirectory(), "Working directory.", "-w", + "--working-directory"); + + public Parser initParser() { + + final Parser parser = new Parser(); + parser.addParameter(recursiveParameter); + parser.addParameter(searchForPattern); + parser.addParameter(replaceWithPattern); + parser.addParameter(workingDirectoryParameter); + + return parser; + } + + public CommandlineOptions parseCommandlineArguments(final String[] args) { + + final CommandlineOptions options = new CommandlineOptions(); + + final Parser parser = initParser(); + if (!parser.parse(args)) + return null; + + if (recursiveParameter.isParameterSpecified()) + options.recursive = true; + + if (workingDirectoryParameter.isParameterSpecified()) + options.targetDirectory = workingDirectoryParameter + .getArgumentsAsFiles().get(0); + else + options.targetDirectory = new File(System.getProperty("user.dir")); + + if (searchForPattern.isParameterSpecified()) + options.searchForPattern = searchForPattern.getArgumentAsString(); + + if (replaceWithPattern.isParameterSpecified()) + options.replaceWithPattern = replaceWithPattern + .getArgumentAsString(); + + return options; + } +} diff --git a/src/main/java/eu/svjatoslav/meviz/replace/CommandlineOptions.java b/src/main/java/eu/svjatoslav/meviz/replace/CommandlineOptions.java new file mode 100755 index 0000000..ab46832 --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/replace/CommandlineOptions.java @@ -0,0 +1,25 @@ +/* + * Meviz - Various tools collection to work with multimedia. + * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + +package eu.svjatoslav.meviz.replace; + +import java.io.File; + +public class CommandlineOptions { + + public String searchForPattern; + + public String replaceWithPattern; + + + public boolean recursive = false; + + public File targetDirectory; + +} diff --git a/src/main/java/eu/svjatoslav/meviz/replace/Main.java b/src/main/java/eu/svjatoslav/meviz/replace/Main.java new file mode 100755 index 0000000..3355e92 --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/replace/Main.java @@ -0,0 +1,110 @@ +/* + * Meviz - Various tools collection to work with multimedia. + * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public License + * as published by the Free Software Foundation. + */ + +package eu.svjatoslav.meviz.replace; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import eu.svjatoslav.meviz.Module; + +public class Main implements Module { + + CommandlineHandler commandlineHandler = new CommandlineHandler(); + + CommandlineOptions options; + + + @Override + public String getDescription() { + return "Replace one string to another string in all occurrances and all files recursively."; + } + + @Override + public String getModuleCommand() { + return "replace"; + } + + public void processDirectory(final File directory) { + + for (final File file : directory.listFiles()) + if (file.isDirectory()) { + if (options.recursive) + processDirectory(file); + } else + try { + processFile(file); + } catch (final IOException exception) { + System.out.println("Error processing file: " + + file.getAbsolutePath()); + System.out.println(" exception: " + + exception.getMessage()); + } + + } + + public void processFile(final File file) throws IOException { + + FileReader fileReader = new FileReader(file); + BufferedReader bufferedReader = new BufferedReader(fileReader); + + StringBuffer result = new StringBuffer(); + + boolean contentChanged = false; + + while (true) { + String line = bufferedReader.readLine(); + if (line == null) + break; + + String newLine = line.replace(options.searchForPattern, + options.replaceWithPattern); + + if (!contentChanged) + if (!newLine.equals(line)) + contentChanged = true; + + result.append(newLine); + result.append("\n"); + } + + bufferedReader.close(); + fileReader.close(); + + if (contentChanged) { + FileWriter fileWriter = new FileWriter(file); + fileWriter.write(result.toString()); + fileWriter.close(); + } + + } + + @Override + public void run(final String[] args) throws IOException { + + options = commandlineHandler.parseCommandlineArguments(args); + + if (options == null) { + showCommandlineHelp(); + return; + } + + processDirectory(options.targetDirectory); + + } + + @Override + public void showCommandlineHelp() { + commandlineHandler.initParser().showHelp(); + } + +}