Added module to strip byte order mark from UTF text files.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / bomremove / Main.java
diff --git a/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java b/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java
new file mode 100755 (executable)
index 0000000..a377f04
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * Meviz - Various tools collection to work with multimedia.
+ * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.meviz.bomremove;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Arrays;
+
+import eu.svjatoslav.commons.file.IOHelper;
+import eu.svjatoslav.commons.string.WildCardMatcher;
+import eu.svjatoslav.meviz.Module;
+
+public class Main implements Module {
+
+       byte[] bomHeader = new byte[] { (byte) 0xfe, (byte) 0xff };
+
+       CommandlineHandler commandlineHandler = new CommandlineHandler();
+
+       BomStrippingOptions options;
+
+       public boolean contains(final byte[] header, final byte[] patternToSeek) {
+
+               for (int i = 0; i < patternToSeek.length; i++)
+                       if (header[i] != patternToSeek[i])
+                               return false;
+
+               return true;
+       }
+
+       public boolean fileContainsHeader(final File file)
+                       throws FileNotFoundException, IOException {
+
+               final FileInputStream fileInputStream = new FileInputStream(file);
+
+               final byte[] currentFileHeader = new byte[2];
+               fileInputStream.read(currentFileHeader);
+               fileInputStream.close();
+
+               return contains(currentFileHeader, bomHeader);
+       }
+
+       public boolean fileMatchesInputPattern(final File file) {
+               final String fileName = file.getName().toLowerCase();
+
+               for (final String inputPattern : options.inputPatterns)
+                       if (WildCardMatcher.match(fileName, inputPattern.toLowerCase()))
+                               return true;
+
+               return false;
+       }
+
+       @Override
+       public String getDescription() {
+               return "Remove byte order mark (bom) from UTF text files of they are present.";
+       }
+
+       @Override
+       public String getModuleCommand() {
+               return "stripbom";
+       }
+
+       public void processDirectory(final File directory) {
+
+               for (final File subFile : directory.listFiles())
+                       if (subFile.isDirectory()) {
+                               if (options.recursive)
+                                       processDirectory(subFile);
+                       } else if (fileMatchesInputPattern(subFile))
+                               try {
+                                       processFile(subFile);
+                               } catch (final IOException exception) {
+                                       System.out.println("Error processing file: "
+                                                       + subFile.getAbsolutePath());
+                                       System.out.println("   exception: "
+                                                       + exception.getMessage());
+                               }
+
+       }
+
+       public void processFile(final File file) throws IOException {
+
+               if (file.length() < 2)
+                       return;
+
+               if (!fileContainsHeader(file))
+                       return;
+
+               stripFileFromHeader(file);
+       }
+
+       @Override
+       public void run(final String[] args) throws IOException {
+
+               options = commandlineHandler.parseCommandlineArguments(args);
+
+               if (options == null) {
+                       showCommandlineHelp();
+                       return;
+               }
+
+               processDirectory(options.targetDirectory);
+
+       }
+
+       @Override
+       public void showCommandlineHelp() {
+               commandlineHandler.initParser().showHelp();
+       }
+
+       public void stripFileFromHeader(final File file)
+                       throws FileNotFoundException, IOException {
+               // read entire file
+               final byte[] fileContents = IOHelper.getFileContents(file);
+
+               // remove BOM header form file
+               final byte[] newFileContents = Arrays.copyOfRange(fileContents,
+                               bomHeader.length, fileContents.length);
+
+               // overwrite file with new contents
+               IOHelper.saveToFile(file, newFileContents);
+       }
+
+}