initial commit
[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         public static final int targetLineLength = 200;
27
28         public static final int maxSplittedLines = 3;
29
30         public static final String splitLinePrefix = "    ";
31
32         CommandlineHandler commandlineHandler = new CommandlineHandler();
33
34         TextSplittingOptions options;
35
36         public boolean fileMatchesInputPattern(final File file) {
37                 final String fileName = file.getName().toLowerCase();
38
39                 for (final String inputPattern : options.inputPatterns)
40                         if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
41                                 return true;
42
43                 return false;
44         }
45
46         @Override
47         public String getDescription() {
48                 return "Split text with long lines into multiple shorter lines.";
49         }
50
51         @Override
52         public String getModuleCommand() {
53                 return "splittext";
54         }
55
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));
62
63                 targetFilePath.append(".fixedLengh");
64
65                 // add file extension
66                 {
67                         final String fileExtension = FilePathParser
68                                         .getFileExtension(sourceFile);
69
70                         if (fileExtension.length() > 0)
71                                 targetFilePath.append("." + fileExtension);
72                 }
73                 return new File(targetFilePath.toString());
74         }
75
76         public void processDirectory(final File directory) {
77
78                 for (final File subFile : directory.listFiles())
79                         if (subFile.isDirectory()) {
80                                 if (options.recursive)
81                                         processDirectory(subFile);
82                         } else if (fileMatchesInputPattern(subFile))
83                                 try {
84                                         processFile(subFile);
85                                 } catch (final IOException exception) {
86                                         System.out.println("Error processing file: "
87                                                         + subFile.getAbsolutePath());
88                                         System.out.println("   exception: "
89                                                         + exception.getMessage());
90                                 }
91
92         }
93
94         public void processFile(final File file) throws IOException {
95                 final File targetFile = getTargetFile(file);
96
97                 final BufferedReader bufferedReader = new BufferedReader(
98                                 new FileReader(file));
99
100                 final BufferedWriter bufferedWriter = new BufferedWriter(
101                                 new FileWriter(targetFile));
102
103                 while (true) {
104                         final String readLine = bufferedReader.readLine();
105                         if (readLine == null)
106                                 break;
107
108                         if (readLine.length() <= targetLineLength)
109                                 bufferedWriter.write(readLine + "\n");
110                         else {
111
112                                 final CuttableString cuttableString = new CuttableString(
113                                                 readLine);
114
115                                 bufferedWriter.write(cuttableString.cutLeft(targetLineLength)
116                                                 + "\n");
117
118                                 int splittedLinesCount = 0;
119
120                                 while (!cuttableString.isEmpty()) {
121                                         splittedLinesCount++;
122                                         if (splittedLinesCount >= maxSplittedLines)
123                                                 break;
124
125                                         bufferedWriter.write(splitLinePrefix
126                                                         + cuttableString.cutLeft(targetLineLength
127                                                                         - splitLinePrefix.length()) + "\n");
128                                 }
129                         }
130
131                 }
132
133                 bufferedReader.close();
134                 bufferedWriter.close();
135         }
136
137         @Override
138         public void run(final String[] args) throws IOException {
139
140                 options = commandlineHandler.parseCommandlineArguments(args);
141
142                 if (options == null) {
143                         showCommandlineHelp();
144                         return;
145                 }
146
147                 processDirectory(options.targetDirectory);
148
149         }
150
151         @Override
152         public void showCommandlineHelp() {
153                 commandlineHandler.initParser().showHelp();
154         }
155
156 }