Added module to strip byte order mark from UTF text files.
[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) 0xfe, (byte) 0xff };
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[2];
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 of 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() < 2)
92                         return;
93
94                 if (!fileContainsHeader(file))
95                         return;
96
97                 stripFileFromHeader(file);
98         }
99
100         @Override
101         public void run(final String[] args) throws IOException {
102
103                 options = commandlineHandler.parseCommandlineArguments(args);
104
105                 if (options == null) {
106                         showCommandlineHelp();
107                         return;
108                 }
109
110                 processDirectory(options.targetDirectory);
111
112         }
113
114         @Override
115         public void showCommandlineHelp() {
116                 commandlineHandler.initParser().showHelp();
117         }
118
119         public void stripFileFromHeader(final File file)
120                         throws FileNotFoundException, IOException {
121                 // read entire file
122                 final byte[] fileContents = IOHelper.getFileContents(file);
123
124                 // remove BOM header form file
125                 final byte[] newFileContents = Arrays.copyOfRange(fileContents,
126                                 bomHeader.length, fileContents.length);
127
128                 // overwrite file with new contents
129                 IOHelper.saveToFile(file, newFileContents);
130         }
131
132 }