2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.bomremove;
13 import java.io.FileInputStream;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.util.Arrays;
18 import eu.svjatoslav.commons.file.IOHelper;
19 import eu.svjatoslav.commons.string.WildCardMatcher;
20 import eu.svjatoslav.meviz.Module;
22 public class Main implements Module {
24 byte[] bomHeader = new byte[] { (byte) 0xef, (byte) 0xbb, (byte) 0xbf };
26 CommandlineHandler commandlineHandler = new CommandlineHandler();
28 BomStrippingOptions options;
30 public boolean contains(final byte[] header, final byte[] patternToSeek) {
32 for (int i = 0; i < patternToSeek.length; i++)
33 if (header[i] != patternToSeek[i])
39 public boolean fileContainsHeader(final File file)
40 throws FileNotFoundException, IOException {
42 final FileInputStream fileInputStream = new FileInputStream(file);
44 final byte[] currentFileHeader = new byte[bomHeader.length];
45 fileInputStream.read(currentFileHeader);
46 fileInputStream.close();
48 return contains(currentFileHeader, bomHeader);
51 public boolean fileMatchesInputPattern(final File file) {
52 final String fileName = file.getName().toLowerCase();
54 for (final String inputPattern : options.inputPatterns)
55 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
62 public String getDescription() {
63 return "Remove byte order mark (bom) from UTF text files of they are present.";
67 public String getModuleCommand() {
71 public void processDirectory(final File directory) {
73 for (final File subFile : directory.listFiles())
74 if (subFile.isDirectory()) {
75 if (options.recursive)
76 processDirectory(subFile);
77 } else if (fileMatchesInputPattern(subFile))
80 } catch (final IOException exception) {
81 System.out.println("Error processing file: "
82 + subFile.getAbsolutePath());
83 System.out.println(" exception: "
84 + exception.getMessage());
89 public void processFile(final File file) throws IOException {
91 if (file.length() < bomHeader.length)
94 if (!fileContainsHeader(file))
97 System.out.println("Removing BOM from: " + file.getAbsolutePath());
98 stripFileFromHeader(file);
102 public void run(final String[] args) throws IOException {
104 options = commandlineHandler.parseCommandlineArguments(args);
106 if (options == null) {
107 showCommandlineHelp();
111 processDirectory(options.targetDirectory);
116 public void showCommandlineHelp() {
117 commandlineHandler.initParser().showHelp();
120 public void stripFileFromHeader(final File file)
121 throws FileNotFoundException, IOException {
123 final byte[] fileContents = IOHelper.getFileContents(file);
125 // remove BOM header form file
126 final byte[] newFileContents = Arrays.copyOfRange(fileContents,
127 bomHeader.length, fileContents.length);
129 // overwrite file with new contents
130 IOHelper.saveToFile(file, newFileContents);