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 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 CommandlineHandler commandlineHandler = new CommandlineHandler();
23 TextSplittingOptions options;
25 public boolean fileMatchesInputPattern(final File file) {
26 final String fileName = file.getName().toLowerCase();
28 for (final String inputPattern : options.fileInputPatterns)
29 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
36 public String getDescription() {
37 return "Split text with long lines into multiple shorter lines.";
41 public String getModuleCommand() {
45 private File getTargetFile(final File sourceFile) {
46 final StringBuilder targetFilePath = new StringBuilder();
47 targetFilePath.append(sourceFile.getParent());
48 targetFilePath.append("/");
49 targetFilePath.append(FilePathParser
50 .getFileNameWithoutExtension(sourceFile));
52 targetFilePath.append(".splitted");
56 final String fileExtension = FilePathParser
57 .getFileExtension(sourceFile);
59 if (fileExtension.length() > 0)
60 targetFilePath.append("." + fileExtension);
62 return new File(targetFilePath.toString());
65 public void processDirectory(final File directory) {
67 for (final File subFile : directory.listFiles())
68 if (subFile.isDirectory()) {
69 if (options.recursive)
70 processDirectory(subFile);
71 } else if (fileMatchesInputPattern(subFile))
74 } catch (final IOException exception) {
75 System.out.println("Error processing file: "
76 + subFile.getAbsolutePath());
77 System.out.println(" exception: "
78 + exception.getMessage());
83 public void processFile(final File file) throws IOException {
84 final File targetFile = getTargetFile(file);
86 final BufferedReader bufferedReader = new BufferedReader(
87 new FileReader(file));
89 final BufferedWriter bufferedWriter = new BufferedWriter(
90 new FileWriter(targetFile));
93 final String readLine = bufferedReader.readLine();
97 final String2 cuttableString = new String2(readLine);
99 while (!cuttableString.isEmpty()) {
100 for (final String pattern : options.textSplitPatterns)
101 if (WildCardMatcher.match(cuttableString.toString(),
103 bufferedWriter.write("\n");
105 final String character = cuttableString.cutLeft(1);
106 bufferedWriter.append(character);
109 bufferedWriter.write("\n");
112 bufferedReader.close();
113 bufferedWriter.close();
117 public void run(final String[] args) throws IOException {
119 options = commandlineHandler.parseCommandlineArguments(args);
121 if (options == null) {
122 showCommandlineHelp();
126 processDirectory(options.targetDirectory);
131 public void showCommandlineHelp() {
132 commandlineHandler.parser.showHelp();