X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fmeviz%2Ftexttruncate%2FMain.java;h=459497955ed56ae7ce9d7befd771fde37b235c75;hb=fd7c0e940fb4059155d07992b8c1902537cf193b;hp=4106e20791b731012b55fdec2b0cd5623684c864;hpb=92b09ba436ee986976a4430ebf5c8359916bc2a8;p=meviz.git diff --git a/src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java b/src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java index 4106e20..4594979 100755 --- a/src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/texttruncate/Main.java @@ -1,7 +1,7 @@ /* * Meviz - Various tools collection to work with multimedia. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2012 -- 2017, 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,142 @@ 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.String2; import eu.svjatoslav.commons.string.WildCardMatcher; import eu.svjatoslav.meviz.Module; +import java.io.*; + public class Main implements Module { - public static final int targetLineLength = 200; + private static final int targetLineLength = 200; + + private static final int maxSplittedLines = 3; + + private static final String splitLinePrefix = " "; - public static final int maxSplittedLines = 3; + private final CommandlineHandler commandlineHandler = new CommandlineHandler(); - public static final String splitLinePrefix = " "; + private TextTruncatingOptions options; - CommandlineHandler commandlineHandler = new CommandlineHandler(); + private boolean fileMatchesInputPattern(final File file) { + final String fileName = file.getName().toLowerCase(); - TextTruncatingOptions options; + for (final String inputPattern : options.inputPatterns) + if (WildCardMatcher.match(fileName, inputPattern.toLowerCase())) + return true; - public boolean fileMatchesInputPattern(final File file) { - final String fileName = file.getName().toLowerCase(); + return false; + } - for (final String inputPattern : options.inputPatterns) - if (WildCardMatcher.match(fileName, inputPattern.toLowerCase())) - return true; + @Override + public String getDescription() { + return "Truncate text with long lines into multiple shorter lines."; + } - return false; - } + @Override + public String getModuleCommand() { + return "trunctext"; + } - @Override - public String getDescription() { - return "Truncate text with long lines into multiple shorter lines."; - } + private File getTargetFile(final File sourceFile) { + final StringBuilder targetFilePath = new StringBuilder(); + targetFilePath.append(sourceFile.getParent()); + targetFilePath.append("/"); + targetFilePath.append(FilePathParser + .getFileNameWithoutExtension(sourceFile)); - @Override - public String getModuleCommand() { - return "trunctext"; - } + targetFilePath.append(".truncated"); - private File getTargetFile(final File sourceFile) { - final StringBuffer targetFilePath = new StringBuffer(); - targetFilePath.append(sourceFile.getParent()); - targetFilePath.append("/"); - targetFilePath.append(FilePathParser - .getFileNameWithoutExtension(sourceFile)); + // add file extension + { + final String fileExtension = FilePathParser + .getFileExtension(sourceFile); - targetFilePath.append(".truncated"); + if (fileExtension.length() > 0) + targetFilePath.append("." + fileExtension); + } + return new File(targetFilePath.toString()); + } - // add file extension - { - final String fileExtension = FilePathParser - .getFileExtension(sourceFile); + private void processDirectory(final File directory) { - if (fileExtension.length() > 0) - targetFilePath.append("." + fileExtension); - } - return new File(targetFilePath.toString()); - } + 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 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()); - } + private void processFile(final File file) throws IOException { + final File targetFile = getTargetFile(file); - } + final BufferedReader bufferedReader = new BufferedReader( + new FileReader(file)); - public void processFile(final File file) throws IOException { - final File targetFile = getTargetFile(file); + final BufferedWriter bufferedWriter = new BufferedWriter( + new FileWriter(targetFile)); - final BufferedReader bufferedReader = new BufferedReader( - new FileReader(file)); + while (true) { + final String readLine = bufferedReader.readLine(); + if (readLine == null) + break; - final BufferedWriter bufferedWriter = new BufferedWriter( - new FileWriter(targetFile)); + if (readLine.length() <= targetLineLength) + bufferedWriter.write(readLine + "\n"); + else { - while (true) { - final String readLine = bufferedReader.readLine(); - if (readLine == null) - break; + final String2 cuttableString = new String2(readLine); - if (readLine.length() <= targetLineLength) - bufferedWriter.write(readLine + "\n"); - else { + bufferedWriter.write(cuttableString.trimLeft(targetLineLength) + + "\n"); - final CuttableString cuttableString = new CuttableString( - readLine); + int splittedLinesCount = 0; - bufferedWriter.write(cuttableString.cutLeft(targetLineLength) - + "\n"); + while (!cuttableString.isEmpty()) { + splittedLinesCount++; + if (splittedLinesCount >= maxSplittedLines) + break; - int splittedLinesCount = 0; + bufferedWriter.write(splitLinePrefix + + cuttableString.trimLeft(targetLineLength + - splitLinePrefix.length()) + "\n"); + } + } - 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 { - bufferedReader.close(); - bufferedWriter.close(); - } + options = commandlineHandler.parseCommandlineArguments(args); - @Override - public void run(final String[] args) throws IOException { - - options = commandlineHandler.parseCommandlineArguments(args); + if (options == null) { + showCommandlineHelp(); + return; + } - if (options == null) { - showCommandlineHelp(); - return; - } - - processDirectory(options.targetDirectory); + processDirectory(options.targetDirectory); - } + } - @Override - public void showCommandlineHelp() { - commandlineHandler.initParser().showHelp(); - } + @Override + public void showCommandlineHelp() { + commandlineHandler.parser.showHelp(); + } }