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;
12 import eu.svjatoslav.commons.file.IOHelper;
13 import eu.svjatoslav.commons.string.WildCardMatcher;
14 import eu.svjatoslav.meviz.Module;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.util.Arrays;
21 public class Main implements Module {
23 private final byte[] bomHeader = new byte[]{(byte) 0xef, (byte) 0xbb, (byte) 0xbf};
25 private final CommandlineHandler commandlineHandler = new CommandlineHandler();
27 private BomStrippingOptions options;
29 private boolean contains(final byte[] header, final byte[] patternToSeek) {
31 for (int i = 0; i < patternToSeek.length; i++)
32 if (header[i] != patternToSeek[i])
38 private boolean fileContainsHeader(final File file)
41 final FileInputStream fileInputStream = new FileInputStream(file);
43 final byte[] currentFileHeader = new byte[bomHeader.length];
44 fileInputStream.read(currentFileHeader);
45 fileInputStream.close();
47 return contains(currentFileHeader, bomHeader);
50 private boolean fileMatchesInputPattern(final File file) {
51 final String fileName = file.getName().toLowerCase();
53 for (final String inputPattern : options.inputPatterns)
54 if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
61 public String getDescription() {
62 return "Remove byte order mark (bom) from UTF text files if they are present.";
66 public String getModuleCommand() {
70 private void processDirectory(final File directory) {
72 for (final File subFile : directory.listFiles())
73 if (subFile.isDirectory()) {
74 if (options.recursive)
75 processDirectory(subFile);
76 } else if (fileMatchesInputPattern(subFile))
79 } catch (final IOException exception) {
80 System.out.println("Error processing file: "
81 + subFile.getAbsolutePath());
82 System.out.println(" exception: "
83 + exception.getMessage());
88 private void processFile(final File file) throws IOException {
90 if (file.length() < bomHeader.length)
93 if (!fileContainsHeader(file))
96 System.out.println("Removing BOM from: " + file.getAbsolutePath());
97 stripFileFromHeader(file);
101 public void run(final String[] args) throws IOException {
103 options = commandlineHandler.parseCommandlineArguments(args);
105 if (options == null) {
106 showCommandlineHelp();
110 processDirectory(options.targetDirectory);
115 public void showCommandlineHelp() {
116 commandlineHandler.parser.showHelp();
119 private void stripFileFromHeader(final File file)
122 final byte[] fileContents = IOHelper.getFileContents(file);
124 // remove BOM header form file
125 final byte[] newFileContents = Arrays.copyOfRange(fileContents,
126 bomHeader.length, fileContents.length);
128 // overwrite file with new contents
129 IOHelper.saveToFile(file, newFileContents);