Upgraded to latest svjatoslav-commons.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / bomremove / 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.bomremove;
11
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.util.Arrays;
17
18 import eu.svjatoslav.commons.file.IOHelper;
19 import eu.svjatoslav.commons.string.WildCardMatcher;
20 import eu.svjatoslav.meviz.Module;
21
22 public class Main implements Module {
23
24         byte[] bomHeader = new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };
25
26         CommandlineHandler commandlineHandler = new CommandlineHandler();
27
28         BomStrippingOptions options;
29
30         public boolean contains(final byte[] header, final byte[] patternToSeek) {
31
32                 for (int i = 0; i < patternToSeek.length; i++)
33                         if (header[i] != patternToSeek[i])
34                                 return false;
35
36                 return true;
37         }
38
39         public boolean fileContainsHeader(final File file)
40                         throws FileNotFoundException, IOException {
41
42                 final FileInputStream fileInputStream = new FileInputStream(file);
43
44                 final byte[] currentFileHeader = new byte[bomHeader.length];
45                 fileInputStream.read(currentFileHeader);
46                 fileInputStream.close();
47
48                 return contains(currentFileHeader, bomHeader);
49         }
50
51         public boolean fileMatchesInputPattern(final File file) {
52                 final String fileName = file.getName().toLowerCase();
53
54                 for (final String inputPattern : options.inputPatterns)
55                         if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
56                                 return true;
57
58                 return false;
59         }
60
61         @Override
62         public String getDescription() {
63                 return "Remove byte order mark (bom) from UTF text files if they are present.";
64         }
65
66         @Override
67         public String getModuleCommand() {
68                 return "stripbom";
69         }
70
71         public 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         public void processFile(final File file) throws IOException {
90
91                 if (file.length() < bomHeader.length)
92                         return;
93
94                 if (!fileContainsHeader(file))
95                         return;
96
97                 System.out.println("Removing BOM from: " + file.getAbsolutePath());
98                 stripFileFromHeader(file);
99         }
100
101         @Override
102         public void run(final String[] args) throws IOException {
103
104                 options = commandlineHandler.parseCommandlineArguments(args);
105
106                 if (options == null) {
107                         showCommandlineHelp();
108                         return;
109                 }
110
111                 processDirectory(options.targetDirectory);
112
113         }
114
115         @Override
116         public void showCommandlineHelp() {
117                 commandlineHandler.initParser().showHelp();
118         }
119
120         public void stripFileFromHeader(final File file)
121                         throws FileNotFoundException, IOException {
122                 // read entire file
123                 final byte[] fileContents = IOHelper.getFileContents(file);
124
125                 // remove BOM header form file
126                 final byte[] newFileContents = Arrays.copyOfRange(fileContents,
127                                 bomHeader.length, fileContents.length);
128
129                 // overwrite file with new contents
130                 IOHelper.saveToFile(file, newFileContents);
131         }
132
133 }