Upgraded to latest svjatoslav-commons.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / texttruncate / 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.texttruncate;
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.String2;
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         TextTruncatingOptions 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 "Truncate text with long lines into multiple shorter lines.";
49         }
50
51         @Override
52         public String getModuleCommand() {
53                 return "trunctext";
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(".truncated");
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 String2 cuttableString = new String2(readLine);
113
114                                 bufferedWriter.write(cuttableString.cutLeft(targetLineLength)
115                                                 + "\n");
116
117                                 int splittedLinesCount = 0;
118
119                                 while (!cuttableString.isEmpty()) {
120                                         splittedLinesCount++;
121                                         if (splittedLinesCount >= maxSplittedLines)
122                                                 break;
123
124                                         bufferedWriter.write(splitLinePrefix
125                                                         + cuttableString.cutLeft(targetLineLength
126                                                                         - splitLinePrefix.length()) + "\n");
127                                 }
128                         }
129
130                 }
131
132                 bufferedReader.close();
133                 bufferedWriter.close();
134         }
135
136         @Override
137         public void run(final String[] args) throws IOException {
138
139                 options = commandlineHandler.parseCommandlineArguments(args);
140
141                 if (options == null) {
142                         showCommandlineHelp();
143                         return;
144                 }
145
146                 processDirectory(options.targetDirectory);
147
148         }
149
150         @Override
151         public void showCommandlineHelp() {
152                 commandlineHandler.parser.showHelp();
153         }
154
155 }