From 92b09ba436ee986976a4430ebf5c8359916bc2a8 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Fri, 3 May 2013 14:14:13 +0300 Subject: [PATCH] possibility to split long text lines --- pom.xml | 2 +- src/main/java/eu/svjatoslav/meviz/Main.java | 1 + .../textsplitter/CommandlineHandler.java | 17 +- .../svjatoslav/meviz/textsplitter/Main.java | 38 ++--- .../textsplitter/TextSplittingOptions.java | 10 +- .../texttruncate/CommandlineHandler.java | 65 ++++++++ .../svjatoslav/meviz/texttruncate/Main.java | 156 ++++++++++++++++++ .../texttruncate/TextTruncatingOptions.java | 23 +++ 8 files changed, 274 insertions(+), 38 deletions(-) create mode 100755 src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java create mode 100755 src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java create mode 100755 src/main/java/eu/svjatoslav/meviz/texttruncate/TextTruncatingOptions.java diff --git a/pom.xml b/pom.xml index 6a00912..2da23c2 100755 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ eu.svjatoslav svjatoslavcommons - 1.0-SNAPSHOT + 1.1-SNAPSHOT diff --git a/src/main/java/eu/svjatoslav/meviz/Main.java b/src/main/java/eu/svjatoslav/meviz/Main.java index 8e2f4f1..f78bf81 100755 --- a/src/main/java/eu/svjatoslav/meviz/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/Main.java @@ -33,6 +33,7 @@ public class Main { 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()); } diff --git a/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java index 15be77c..5f7bc0e 100755 --- a/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java @@ -21,10 +21,14 @@ public class CommandlineHandler { 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"); @@ -33,7 +37,8 @@ public class CommandlineHandler { final Parser parser = new Parser(); parser.addParameter(recursiveParameter); - parser.addParameter(inputPatternParameter); + parser.addParameter(fileInputPatternsParameter); + parser.addParameter(splitPatternsParameter); parser.addParameter(workingDirectoryParameter); return parser; @@ -56,8 +61,12 @@ public class CommandlineHandler { 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; diff --git a/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java b/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java index 9a52d55..daaf307 100755 --- a/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java @@ -23,12 +23,6 @@ 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(); TextSplittingOptions options; @@ -36,7 +30,7 @@ public class Main implements Module { 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; @@ -60,7 +54,7 @@ public class Main implements Module { targetFilePath.append(FilePathParser .getFileNameWithoutExtension(sourceFile)); - targetFilePath.append(".fixedLengh"); + targetFilePath.append(".splitted"); // add file extension { @@ -105,29 +99,19 @@ public class Main implements Module { 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(); diff --git a/src/main/java/eu/svjatoslav/meviz/textsplitter/TextSplittingOptions.java b/src/main/java/eu/svjatoslav/meviz/textsplitter/TextSplittingOptions.java index fb0cb99..f6353e6 100755 --- a/src/main/java/eu/svjatoslav/meviz/textsplitter/TextSplittingOptions.java +++ b/src/main/java/eu/svjatoslav/meviz/textsplitter/TextSplittingOptions.java @@ -14,14 +14,12 @@ import java.util.ArrayList; public class TextSplittingOptions { - public ArrayList inputPatterns = new ArrayList(); + public ArrayList fileInputPatterns = new ArrayList(); - public boolean recursive = false; + public ArrayList textSplitPatterns = new ArrayList(); - public boolean testOnly = false; + public boolean recursive = false; - public String outputPattern; - - public File targetDirectory; + public File targetDirectory; } diff --git a/src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java new file mode 100755 index 0000000..d391bca --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java @@ -0,0 +1,65 @@ +/* + * 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; + } +} diff --git a/src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java b/src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java new file mode 100755 index 0000000..4106e20 --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java @@ -0,0 +1,156 @@ +/* + * 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(); + } + +} diff --git a/src/main/java/eu/svjatoslav/meviz/texttruncate/TextTruncatingOptions.java b/src/main/java/eu/svjatoslav/meviz/texttruncate/TextTruncatingOptions.java new file mode 100755 index 0000000..25d1ab5 --- /dev/null +++ b/src/main/java/eu/svjatoslav/meviz/texttruncate/TextTruncatingOptions.java @@ -0,0 +1,23 @@ +/* + * 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 inputPatterns = new ArrayList(); + + public boolean recursive = false; + + public File targetDirectory; + +} -- 2.20.1