<dependency>
<groupId>eu.svjatoslav</groupId>
<artifactId>svjatoslavcommons</artifactId>
- <version>1.0-SNAPSHOT</version>
+ <version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
modules.add(new eu.svjatoslav.meviz.htmlindexer.Main());
modules.add(new eu.svjatoslav.meviz.renamer.Main());
modules.add(new eu.svjatoslav.meviz.grabmemcard.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());
}
Parameter recursiveParameter = new Parameter("Enable recursive mode.",
"-r", "--recursive");
- Parameter inputPatternParameter = new Parameter(true, true, true,
+ Parameter fileInputPatternsParameter = new Parameter(true, true, true,
new StringArgument(), "File input pattern.", "-i",
"--input-pattern");
+ Parameter splitPatternsParameter = new Parameter(true, true, true,
+ new StringArgument(), "File split regular expression.", "-s",
+ "--split-pattern");
+
Parameter workingDirectoryParameter = new Parameter(false, true, true,
new ExistingDirectory(), "Working directory.", "-w",
"--working-directory");
final Parser parser = new Parser();
parser.addParameter(recursiveParameter);
- parser.addParameter(inputPatternParameter);
+ parser.addParameter(fileInputPatternsParameter);
+ parser.addParameter(splitPatternsParameter);
parser.addParameter(workingDirectoryParameter);
return parser;
else
options.targetDirectory = new File(System.getProperty("user.dir"));
- if (inputPatternParameter.isParameterSpecified())
- options.inputPatterns.addAll(inputPatternParameter
+ if (fileInputPatternsParameter.isParameterSpecified())
+ options.fileInputPatterns.addAll(fileInputPatternsParameter
+ .getArgumentsAsStrings());
+
+ if (splitPatternsParameter.isParameterSpecified())
+ options.textSplitPatterns.addAll(splitPatternsParameter
.getArgumentsAsStrings());
return options;
public class Main implements Module {
- public static final int targetLineLength = 200;
-
- public static final int maxSplittedLines = 3;
-
- public static final String splitLinePrefix = " ";
-
CommandlineHandler commandlineHandler = new CommandlineHandler();
TextSplittingOptions options;
public boolean fileMatchesInputPattern(final File file) {
final String fileName = file.getName().toLowerCase();
- for (final String inputPattern : options.inputPatterns)
+ for (final String inputPattern : options.fileInputPatterns)
if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
return true;
targetFilePath.append(FilePathParser
.getFileNameWithoutExtension(sourceFile));
- targetFilePath.append(".fixedLengh");
+ targetFilePath.append(".splitted");
// add file extension
{
if (readLine == null)
break;
- if (readLine.length() <= targetLineLength)
- bufferedWriter.write(readLine + "\n");
- else {
-
- final CuttableString cuttableString = new CuttableString(
- readLine);
+ final CuttableString cuttableString = new CuttableString(readLine);
- bufferedWriter.write(cuttableString.cutLeft(targetLineLength)
- + "\n");
+ while (!cuttableString.isEmpty()) {
+ for (final String pattern : options.textSplitPatterns)
+ if (WildCardMatcher.match(cuttableString.getValue(),
+ pattern))
+ bufferedWriter.write("\n");
- int splittedLinesCount = 0;
-
- while (!cuttableString.isEmpty()) {
- splittedLinesCount++;
- if (splittedLinesCount >= maxSplittedLines)
- break;
-
- bufferedWriter.write(splitLinePrefix
- + cuttableString.cutLeft(targetLineLength
- - splitLinePrefix.length()) + "\n");
- }
+ final String character = cuttableString.cutLeft(1);
+ bufferedWriter.append(character);
}
+ bufferedWriter.write("\n");
}
bufferedReader.close();
public class TextSplittingOptions {
- public ArrayList<String> inputPatterns = new ArrayList<String>();
+ public ArrayList<String> fileInputPatterns = new ArrayList<String>();
- public boolean recursive = false;
+ public ArrayList<String> textSplitPatterns = new ArrayList<String>();
- public boolean testOnly = false;
+ public boolean recursive = false;
- public String outputPattern;
-
- public File targetDirectory;
+ public File targetDirectory;
}
--- /dev/null
+/*
+ * 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.texttruncate;
+
+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 inputPatternParameter = new Parameter(true, true, true,
+ new StringArgument(), "File input pattern.", "-i",
+ "--input-pattern");
+
+ Parameter workingDirectoryParameter = new Parameter(false, true, true,
+ new ExistingDirectory(), "Working directory.", "-w",
+ "--working-directory");
+
+ public Parser initParser() {
+
+ final Parser parser = new Parser();
+ parser.addParameter(recursiveParameter);
+ parser.addParameter(inputPatternParameter);
+ parser.addParameter(workingDirectoryParameter);
+
+ return parser;
+ }
+
+ public TextTruncatingOptions parseCommandlineArguments(final String[] args) {
+
+ final TextTruncatingOptions options = new TextTruncatingOptions();
+
+ 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 (inputPatternParameter.isParameterSpecified())
+ options.inputPatterns.addAll(inputPatternParameter
+ .getArgumentsAsStrings());
+
+ return options;
+ }
+}
--- /dev/null
+/*
+ * 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.texttruncate;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import eu.svjatoslav.commons.file.FilePathParser;
+import eu.svjatoslav.commons.string.CuttableString;
+import eu.svjatoslav.commons.string.WildCardMatcher;
+import eu.svjatoslav.meviz.Module;
+
+public class Main implements Module {
+
+ public static final int targetLineLength = 200;
+
+ public static final int maxSplittedLines = 3;
+
+ public static final String splitLinePrefix = " ";
+
+ CommandlineHandler commandlineHandler = new CommandlineHandler();
+
+ TextTruncatingOptions options;
+
+ public boolean fileMatchesInputPattern(final File file) {
+ final String fileName = file.getName().toLowerCase();
+
+ for (final String inputPattern : options.inputPatterns)
+ if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
+ return true;
+
+ return false;
+ }
+
+ @Override
+ public String getDescription() {
+ return "Truncate text with long lines into multiple shorter lines.";
+ }
+
+ @Override
+ public String getModuleCommand() {
+ return "trunctext";
+ }
+
+ private File getTargetFile(final File sourceFile) {
+ final StringBuffer targetFilePath = new StringBuffer();
+ targetFilePath.append(sourceFile.getParent());
+ targetFilePath.append("/");
+ targetFilePath.append(FilePathParser
+ .getFileNameWithoutExtension(sourceFile));
+
+ targetFilePath.append(".truncated");
+
+ // add file extension
+ {
+ final String fileExtension = FilePathParser
+ .getFileExtension(sourceFile);
+
+ if (fileExtension.length() > 0)
+ targetFilePath.append("." + fileExtension);
+ }
+ return new File(targetFilePath.toString());
+ }
+
+ public void processDirectory(final File directory) {
+
+ for (final File subFile : directory.listFiles())
+ if (subFile.isDirectory()) {
+ if (options.recursive)
+ processDirectory(subFile);
+ } else if (fileMatchesInputPattern(subFile))
+ try {
+ processFile(subFile);
+ } catch (final IOException exception) {
+ System.out.println("Error processing file: "
+ + subFile.getAbsolutePath());
+ System.out.println(" exception: "
+ + exception.getMessage());
+ }
+
+ }
+
+ public void processFile(final File file) throws IOException {
+ final File targetFile = getTargetFile(file);
+
+ final BufferedReader bufferedReader = new BufferedReader(
+ new FileReader(file));
+
+ final BufferedWriter bufferedWriter = new BufferedWriter(
+ new FileWriter(targetFile));
+
+ while (true) {
+ final String readLine = bufferedReader.readLine();
+ if (readLine == null)
+ break;
+
+ if (readLine.length() <= targetLineLength)
+ bufferedWriter.write(readLine + "\n");
+ else {
+
+ final CuttableString cuttableString = new CuttableString(
+ readLine);
+
+ bufferedWriter.write(cuttableString.cutLeft(targetLineLength)
+ + "\n");
+
+ int splittedLinesCount = 0;
+
+ while (!cuttableString.isEmpty()) {
+ splittedLinesCount++;
+ if (splittedLinesCount >= maxSplittedLines)
+ break;
+
+ bufferedWriter.write(splitLinePrefix
+ + cuttableString.cutLeft(targetLineLength
+ - splitLinePrefix.length()) + "\n");
+ }
+ }
+
+ }
+
+ bufferedReader.close();
+ bufferedWriter.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();
+ }
+
+}
--- /dev/null
+/*
+ * 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.texttruncate;
+
+import java.io.File;
+import java.util.ArrayList;
+
+public class TextTruncatingOptions {
+
+ public ArrayList<String> inputPatterns = new ArrayList<String>();
+
+ public boolean recursive = false;
+
+ public File targetDirectory;
+
+}