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.String2;
21 import eu.svjatoslav.commons.string.WildCardMatcher;
22 import eu.svjatoslav.meviz.Module;
24 public class Main implements Module {
26 CommandlineHandler commandlineHandler = new CommandlineHandler();
28 TextSplittingOptions options;
30 public boolean fileMatchesInputPattern(final File file) {
31 final String fileName = file.getName().toLowerCase();
33 for (final String inputPattern : options.fileInputPatterns)
34 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
41 public String getDescription() {
42 return "Split text with long lines into multiple shorter lines.";
46 public String getModuleCommand() {
50 private File getTargetFile(final File sourceFile) {
51 final StringBuffer targetFilePath = new StringBuffer();
52 targetFilePath.append(sourceFile.getParent());
53 targetFilePath.append("/");
54 targetFilePath.append(FilePathParser
55 .getFileNameWithoutExtension(sourceFile));
57 targetFilePath.append(".splitted");
61 final String fileExtension = FilePathParser
62 .getFileExtension(sourceFile);
64 if (fileExtension.length() > 0)
65 targetFilePath.append("." + fileExtension);
67 return new File(targetFilePath.toString());
70 public void processDirectory(final File directory) {
72 for (final File subFile : directory.listFiles())
73 if (subFile.isDirectory()) {
74 if (options.recursive)
75 processDirectory(subFile);
76 } else if (fileMatchesInputPattern(subFile))
79 } catch (final IOException exception) {
80 System.out.println("Error processing file: "
81 + subFile.getAbsolutePath());
82 System.out.println(" exception: "
83 + exception.getMessage());
88 public void processFile(final File file) throws IOException {
89 final File targetFile = getTargetFile(file);
91 final BufferedReader bufferedReader = new BufferedReader(
92 new FileReader(file));
94 final BufferedWriter bufferedWriter = new BufferedWriter(
95 new FileWriter(targetFile));
98 final String readLine = bufferedReader.readLine();
102 final String2 cuttableString = new String2(readLine);
104 while (!cuttableString.isEmpty()) {
105 for (final String pattern : options.textSplitPatterns)
106 if (WildCardMatcher.match(cuttableString.toString(),
108 bufferedWriter.write("\n");
110 final String character = cuttableString.cutLeft(1);
111 bufferedWriter.append(character);
114 bufferedWriter.write("\n");
117 bufferedReader.close();
118 bufferedWriter.close();
122 public void run(final String[] args) throws IOException {
124 options = commandlineHandler.parseCommandlineArguments(args);
126 if (options == null) {
127 showCommandlineHelp();
131 processDirectory(options.targetDirectory);
136 public void showCommandlineHelp() {
137 commandlineHandler.parser.showHelp();