recursive replace
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 18 Sep 2013 11:33:23 +0000 (14:33 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 18 Sep 2013 11:33:23 +0000 (14:33 +0300)
src/main/java/eu/svjatoslav/meviz/Main.java
src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java [new file with mode: 0755]
src/main/java/eu/svjatoslav/meviz/replace/CommandlineOptions.java [new file with mode: 0755]
src/main/java/eu/svjatoslav/meviz/replace/Main.java [new file with mode: 0755]

index f78bf81..a629d1b 100755 (executable)
@@ -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.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 {
        }
 
        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 (executable)
index 0000000..4a8f336
--- /dev/null
@@ -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 (executable)
index 0000000..ab46832
--- /dev/null
@@ -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 (executable)
index 0000000..3355e92
--- /dev/null
@@ -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();
+       }
+
+}