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.textsplitter;
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.CuttableString;
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 TextSplittingOptions 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 "Split 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(".fixedLengh");
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 CuttableString cuttableString = new CuttableString(
115 bufferedWriter.write(cuttableString.cutLeft(targetLineLength)
118 int splittedLinesCount = 0;
120 while (!cuttableString.isEmpty()) {
121 splittedLinesCount++;
122 if (splittedLinesCount >= maxSplittedLines)
125 bufferedWriter.write(splitLinePrefix
126 + cuttableString.cutLeft(targetLineLength
127 - splitLinePrefix.length()) + "\n");
133 bufferedReader.close();
134 bufferedWriter.close();
138 public void run(final String[] args) throws IOException {
140 options = commandlineHandler.parseCommandlineArguments(args);
142 if (options == null) {
143 showCommandlineHelp();
147 processDirectory(options.targetDirectory);
152 public void showCommandlineHelp() {
153 commandlineHandler.initParser().showHelp();