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