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 java.io.BufferedReader;
13 import java.io.BufferedWriter;
15 import java.io.FileReader;
16 import java.io.FileWriter;
17 import java.io.IOException;
19 import eu.svjatoslav.commons.file.FilePathParser;
20 import eu.svjatoslav.commons.string.String2;
21 import eu.svjatoslav.commons.string.WildCardMatcher;
22 import eu.svjatoslav.meviz.Module;
24 public class Main implements Module {
26 public static final int targetLineLength = 200;
28 public static final int maxSplittedLines = 3;
30 public static final String splitLinePrefix = " ";
32 CommandlineHandler commandlineHandler = new CommandlineHandler();
34 TextTruncatingOptions options;
36 public boolean fileMatchesInputPattern(final File file) {
37 final String fileName = file.getName().toLowerCase();
39 for (final String inputPattern : options.inputPatterns)
40 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
47 public String getDescription() {
48 return "Truncate text with long lines into multiple shorter lines.";
52 public String getModuleCommand() {
56 private File getTargetFile(final File sourceFile) {
57 final StringBuffer targetFilePath = new StringBuffer();
58 targetFilePath.append(sourceFile.getParent());
59 targetFilePath.append("/");
60 targetFilePath.append(FilePathParser
61 .getFileNameWithoutExtension(sourceFile));
63 targetFilePath.append(".truncated");
67 final String fileExtension = FilePathParser
68 .getFileExtension(sourceFile);
70 if (fileExtension.length() > 0)
71 targetFilePath.append("." + fileExtension);
73 return new File(targetFilePath.toString());
76 public void processDirectory(final File directory) {
78 for (final File subFile : directory.listFiles())
79 if (subFile.isDirectory()) {
80 if (options.recursive)
81 processDirectory(subFile);
82 } else if (fileMatchesInputPattern(subFile))
85 } catch (final IOException exception) {
86 System.out.println("Error processing file: "
87 + subFile.getAbsolutePath());
88 System.out.println(" exception: "
89 + exception.getMessage());
94 public void processFile(final File file) throws IOException {
95 final File targetFile = getTargetFile(file);
97 final BufferedReader bufferedReader = new BufferedReader(
98 new FileReader(file));
100 final BufferedWriter bufferedWriter = new BufferedWriter(
101 new FileWriter(targetFile));
104 final String readLine = bufferedReader.readLine();
105 if (readLine == null)
108 if (readLine.length() <= targetLineLength)
109 bufferedWriter.write(readLine + "\n");
112 final String2 cuttableString = new String2(readLine);
114 bufferedWriter.write(cuttableString.cutLeft(targetLineLength)
117 int splittedLinesCount = 0;
119 while (!cuttableString.isEmpty()) {
120 splittedLinesCount++;
121 if (splittedLinesCount >= maxSplittedLines)
124 bufferedWriter.write(splitLinePrefix
125 + cuttableString.cutLeft(targetLineLength
126 - splitLinePrefix.length()) + "\n");
132 bufferedReader.close();
133 bufferedWriter.close();
137 public void run(final String[] args) throws IOException {
139 options = commandlineHandler.parseCommandlineArguments(args);
141 if (options == null) {
142 showCommandlineHelp();
146 processDirectory(options.targetDirectory);
151 public void showCommandlineHelp() {
152 commandlineHandler.parser.showHelp();