2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.texttruncate;
12 import eu.svjatoslav.commons.file.FilePathParser;
13 import eu.svjatoslav.commons.string.String2;
14 import eu.svjatoslav.commons.string.WildCardMatcher;
15 import eu.svjatoslav.meviz.Module;
19 public class Main implements Module {
21 public static final int targetLineLength = 200;
23 public static final int maxSplittedLines = 3;
25 public static final String splitLinePrefix = " ";
27 CommandlineHandler commandlineHandler = new CommandlineHandler();
29 TextTruncatingOptions options;
31 public boolean fileMatchesInputPattern(final File file) {
32 final String fileName = file.getName().toLowerCase();
34 for (final String inputPattern : options.inputPatterns)
35 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
42 public String getDescription() {
43 return "Truncate text with long lines into multiple shorter lines.";
47 public String getModuleCommand() {
51 private File getTargetFile(final File sourceFile) {
52 final StringBuilder targetFilePath = new StringBuilder();
53 targetFilePath.append(sourceFile.getParent());
54 targetFilePath.append("/");
55 targetFilePath.append(FilePathParser
56 .getFileNameWithoutExtension(sourceFile));
58 targetFilePath.append(".truncated");
62 final String fileExtension = FilePathParser
63 .getFileExtension(sourceFile);
65 if (fileExtension.length() > 0)
66 targetFilePath.append("." + fileExtension);
68 return new File(targetFilePath.toString());
71 public void processDirectory(final File directory) {
73 for (final File subFile : directory.listFiles())
74 if (subFile.isDirectory()) {
75 if (options.recursive)
76 processDirectory(subFile);
77 } else if (fileMatchesInputPattern(subFile))
80 } catch (final IOException exception) {
81 System.out.println("Error processing file: "
82 + subFile.getAbsolutePath());
83 System.out.println(" exception: "
84 + exception.getMessage());
89 public void processFile(final File file) throws IOException {
90 final File targetFile = getTargetFile(file);
92 final BufferedReader bufferedReader = new BufferedReader(
93 new FileReader(file));
95 final BufferedWriter bufferedWriter = new BufferedWriter(
96 new FileWriter(targetFile));
99 final String readLine = bufferedReader.readLine();
100 if (readLine == null)
103 if (readLine.length() <= targetLineLength)
104 bufferedWriter.write(readLine + "\n");
107 final String2 cuttableString = new String2(readLine);
109 bufferedWriter.write(cuttableString.cutLeft(targetLineLength)
112 int splittedLinesCount = 0;
114 while (!cuttableString.isEmpty()) {
115 splittedLinesCount++;
116 if (splittedLinesCount >= maxSplittedLines)
119 bufferedWriter.write(splitLinePrefix
120 + cuttableString.cutLeft(targetLineLength
121 - splitLinePrefix.length()) + "\n");
127 bufferedReader.close();
128 bufferedWriter.close();
132 public void run(final String[] args) throws IOException {
134 options = commandlineHandler.parseCommandlineArguments(args);
136 if (options == null) {
137 showCommandlineHelp();
141 processDirectory(options.targetDirectory);
146 public void showCommandlineHelp() {
147 commandlineHandler.parser.showHelp();