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