5b0e3c905d471b9462e5871e43c065395bf78660
[meviz.git] / src / main / java / eu / svjatoslav / meviz / textsplitter / Main.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
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.
8  */
9
10 package eu.svjatoslav.meviz.textsplitter;
11
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;
16
17 import java.io.*;
18
19 public class Main implements Module {
20
21     CommandlineHandler commandlineHandler = new CommandlineHandler();
22
23     TextSplittingOptions options;
24
25     public boolean fileMatchesInputPattern(final File file) {
26         final String fileName = file.getName().toLowerCase();
27
28         for (final String inputPattern : options.fileInputPatterns)
29             if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
30                 return true;
31
32         return false;
33     }
34
35     @Override
36     public String getDescription() {
37         return "Split text with long lines into multiple shorter lines.";
38     }
39
40     @Override
41     public String getModuleCommand() {
42         return "splittext";
43     }
44
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));
51
52         targetFilePath.append(".splitted");
53
54         // add file extension
55         {
56             final String fileExtension = FilePathParser
57                     .getFileExtension(sourceFile);
58
59             if (fileExtension.length() > 0)
60                 targetFilePath.append("." + fileExtension);
61         }
62         return new File(targetFilePath.toString());
63     }
64
65     public void processDirectory(final File directory) {
66
67         for (final File subFile : directory.listFiles())
68             if (subFile.isDirectory()) {
69                 if (options.recursive)
70                     processDirectory(subFile);
71             } else if (fileMatchesInputPattern(subFile))
72                 try {
73                     processFile(subFile);
74                 } catch (final IOException exception) {
75                     System.out.println("Error processing file: "
76                             + subFile.getAbsolutePath());
77                     System.out.println("   exception: "
78                             + exception.getMessage());
79                 }
80
81     }
82
83     public void processFile(final File file) throws IOException {
84         final File targetFile = getTargetFile(file);
85
86         final BufferedReader bufferedReader = new BufferedReader(
87                 new FileReader(file));
88
89         final BufferedWriter bufferedWriter = new BufferedWriter(
90                 new FileWriter(targetFile));
91
92         while (true) {
93             final String readLine = bufferedReader.readLine();
94             if (readLine == null)
95                 break;
96
97             final String2 cuttableString = new String2(readLine);
98
99             while (!cuttableString.isEmpty()) {
100                 for (final String pattern : options.textSplitPatterns)
101                     if (WildCardMatcher.match(cuttableString.toString(),
102                             pattern))
103                         bufferedWriter.write("\n");
104
105                 final String character = cuttableString.cutLeft(1);
106                 bufferedWriter.append(character);
107             }
108
109             bufferedWriter.write("\n");
110         }
111
112         bufferedReader.close();
113         bufferedWriter.close();
114     }
115
116     @Override
117     public void run(final String[] args) throws IOException {
118
119         options = commandlineHandler.parseCommandlineArguments(args);
120
121         if (options == null) {
122             showCommandlineHelp();
123             return;
124         }
125
126         processDirectory(options.targetDirectory);
127
128     }
129
130     @Override
131     public void showCommandlineHelp() {
132         commandlineHandler.parser.showHelp();
133     }
134
135 }