X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fmeviz%2Ftextsplitter%2FMain.java;h=cad3b051125a43cb21849e05c87ad2385d3014af;hb=18c4552106d952d2655c52301cfeedb67cfd8b67;hp=9a52d55e54f943d11d20d2c3a1924da11f929fd4;hpb=50cb7085d553fdd82cd06806cd27b1675299f719;p=meviz.git diff --git a/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java b/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java index 9a52d55..cad3b05 100755 --- a/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/textsplitter/Main.java @@ -1,7 +1,7 @@ /* * 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. @@ -9,148 +9,127 @@ package eu.svjatoslav.meviz.textsplitter; -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.String2; 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; +import java.io.*; - public static final String splitLinePrefix = " "; - - CommandlineHandler commandlineHandler = new CommandlineHandler(); - - TextSplittingOptions options; - - public boolean fileMatchesInputPattern(final File file) { - final String fileName = file.getName().toLowerCase(); +public class Main implements Module { - for (final String inputPattern : options.inputPatterns) - if (WildCardMatcher.match(fileName, inputPattern.toLowerCase())) - return true; + private final CommandlineHandler commandlineHandler = new CommandlineHandler(); - return false; - } + private TextSplittingOptions options; - @Override - public String getDescription() { - return "Split text with long lines into multiple shorter lines."; - } + private boolean fileMatchesInputPattern(final File file) { + final String fileName = file.getName().toLowerCase(); - @Override - public String getModuleCommand() { - return "splittext"; - } + for (final String inputPattern : options.fileInputPatterns) + if (WildCardMatcher.match(fileName, inputPattern.toLowerCase())) + return true; - private File getTargetFile(final File sourceFile) { - final StringBuffer targetFilePath = new StringBuffer(); - targetFilePath.append(sourceFile.getParent()); - targetFilePath.append("/"); - targetFilePath.append(FilePathParser - .getFileNameWithoutExtension(sourceFile)); + return false; + } - targetFilePath.append(".fixedLengh"); + @Override + public String getDescription() { + return "Split text with long lines into multiple shorter lines."; + } - // add file extension - { - final String fileExtension = FilePathParser - .getFileExtension(sourceFile); + @Override + public String getModuleCommand() { + return "splittext"; + } - if (fileExtension.length() > 0) - targetFilePath.append("." + fileExtension); - } - return new File(targetFilePath.toString()); - } + private File getTargetFile(final File sourceFile) { + final StringBuilder targetFilePath = new StringBuilder(); + targetFilePath.append(sourceFile.getParent()); + targetFilePath.append("/"); + targetFilePath.append(FilePathParser + .getFileNameWithoutExtension(sourceFile)); - public void processDirectory(final File directory) { + targetFilePath.append(".splitted"); - 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()); - } + // add file extension + { + final String fileExtension = FilePathParser + .getFileExtension(sourceFile); - } + if (fileExtension.length() > 0) + targetFilePath.append("." + fileExtension); + } + return new File(targetFilePath.toString()); + } - public void processFile(final File file) throws IOException { - final File targetFile = getTargetFile(file); + private void processDirectory(final File directory) { - final BufferedReader bufferedReader = new BufferedReader( - new FileReader(file)); + 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()); + } - final BufferedWriter bufferedWriter = new BufferedWriter( - new FileWriter(targetFile)); + } - while (true) { - final String readLine = bufferedReader.readLine(); - if (readLine == null) - break; + private void processFile(final File file) throws IOException { + final File targetFile = getTargetFile(file); - if (readLine.length() <= targetLineLength) - bufferedWriter.write(readLine + "\n"); - else { + final BufferedReader bufferedReader = new BufferedReader( + new FileReader(file)); - final CuttableString cuttableString = new CuttableString( - readLine); + final BufferedWriter bufferedWriter = new BufferedWriter( + new FileWriter(targetFile)); - bufferedWriter.write(cuttableString.cutLeft(targetLineLength) - + "\n"); + while (true) { + final String readLine = bufferedReader.readLine(); + if (readLine == null) + break; - int splittedLinesCount = 0; + final String2 cuttableString = new String2(readLine); - while (!cuttableString.isEmpty()) { - splittedLinesCount++; - if (splittedLinesCount >= maxSplittedLines) - break; + while (!cuttableString.isEmpty()) { + for (final String pattern : options.textSplitPatterns) + if (WildCardMatcher.match(cuttableString.toString(), + pattern)) + bufferedWriter.write("\n"); - bufferedWriter.write(splitLinePrefix - + cuttableString.cutLeft(targetLineLength - - splitLinePrefix.length()) + "\n"); - } - } + final String character = cuttableString.trimLeft(1); + bufferedWriter.append(character); + } - } + bufferedWriter.write("\n"); + } - bufferedReader.close(); - bufferedWriter.close(); - } + bufferedReader.close(); + bufferedWriter.close(); + } - @Override - public void run(final String[] args) throws IOException { - - options = commandlineHandler.parseCommandlineArguments(args); + @Override + public void run(final String[] args) throws IOException { - if (options == null) { - showCommandlineHelp(); - return; - } - - processDirectory(options.targetDirectory); + options = commandlineHandler.parseCommandlineArguments(args); - } + if (options == null) { + showCommandlineHelp(); + return; + } + + processDirectory(options.targetDirectory); - @Override - public void showCommandlineHelp() { - commandlineHandler.initParser().showHelp(); - } + } + + @Override + public void showCommandlineHelp() { + commandlineHandler.parser.showHelp(); + } }