-/*
- * Meviz - Various tools collection to work with multimedia.
- * 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.
- */
-
-package eu.svjatoslav.meviz.texttruncate;
-
-import eu.svjatoslav.commons.file.FilePathParser;
-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 {
-
- private static final int targetLineLength = 200;
-
- private static final int maxSplittedLines = 3;
-
- private static final String splitLinePrefix = " ";
-
- private final CommandlineHandler commandlineHandler = new CommandlineHandler();
-
- private TextTruncatingOptions options;
-
- private 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 StringBuilder targetFilePath = new StringBuilder();
- 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());
- }
-
- private 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));
-
- 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 String2 cuttableString = new String2(readLine);
-
- bufferedWriter.write(cuttableString.trimLeft(targetLineLength)
- + "\n");
-
- int splittedLinesCount = 0;
-
- while (!cuttableString.isEmpty()) {
- splittedLinesCount++;
- if (splittedLinesCount >= maxSplittedLines)
- break;
-
- bufferedWriter.write(splitLinePrefix
- + cuttableString.trimLeft(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.parser.showHelp();
- }
-
-}