X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=meviz.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fmeviz%2Fbomremove%2FMain.java;h=24d7cddb1742fd7f035ce8fcee8beff501ea024d;hp=05251eeccacd4a60f3ca514a7f7aa7cfeff7b9f4;hb=cf6e4ace4972f24f40f88ea12fcf99c763e4e40a;hpb=d00818c445751ea60eb8ee30787708dcea46671a diff --git a/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java b/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java index 05251ee..24d7cdd 100755 --- a/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java @@ -1,7 +1,7 @@ /* * Meviz - Various tools collection to work with multimedia. - * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * + * Copyright (C) 2012 -- 2018, 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. @@ -9,125 +9,124 @@ package eu.svjatoslav.meviz.bomremove; +import eu.svjatoslav.commons.file.IOHelper; +import eu.svjatoslav.commons.string.WildCardMatcher; +import eu.svjatoslav.meviz.Module; + 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) 0xef, (byte) 0xbb, (byte) 0xbf }; + private final byte[] bomHeader = new byte[]{(byte) 0xef, (byte) 0xbb, (byte) 0xbf}; - CommandlineHandler commandlineHandler = new CommandlineHandler(); + private final CommandlineHandler commandlineHandler = new CommandlineHandler(); - BomStrippingOptions options; + private BomStrippingOptions options; - public boolean contains(final byte[] header, final byte[] patternToSeek) { + private boolean contains(final byte[] header, final byte[] patternToSeek) { - for (int i = 0; i < patternToSeek.length; i++) - if (header[i] != patternToSeek[i]) - return false; + for (int i = 0; i < patternToSeek.length; i++) + if (header[i] != patternToSeek[i]) + return false; - return true; - } + return true; + } - public boolean fileContainsHeader(final File file) - throws FileNotFoundException, IOException { + private boolean fileContainsHeader(final File file) + throws IOException { - final FileInputStream fileInputStream = new FileInputStream(file); + final FileInputStream fileInputStream = new FileInputStream(file); - final byte[] currentFileHeader = new byte[bomHeader.length]; - fileInputStream.read(currentFileHeader); - fileInputStream.close(); + final byte[] currentFileHeader = new byte[bomHeader.length]; + fileInputStream.read(currentFileHeader); + fileInputStream.close(); - return contains(currentFileHeader, bomHeader); - } + return contains(currentFileHeader, bomHeader); + } - public boolean fileMatchesInputPattern(final File file) { - final String fileName = file.getName().toLowerCase(); + private 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; + for (final String inputPattern : options.inputPatterns) + if (WildCardMatcher.match(fileName, inputPattern.toLowerCase())) + return true; - return false; - } + return false; + } - @Override - public String getDescription() { - return "Remove byte order mark (bom) from UTF text files if they are present."; - } + @Override + public String getDescription() { + return "Remove byte order mark (bom) from UTF text files if they are present."; + } - @Override - public String getModuleCommand() { - return "stripbom"; - } + @Override + public String getModuleCommand() { + return "stripbom"; + } - public void processDirectory(final File directory) { + private 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()); - } + 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 { + private void processFile(final File file) throws IOException { - if (file.length() < bomHeader.length) - return; + if (file.length() < bomHeader.length) + return; - if (!fileContainsHeader(file)) - return; + if (!fileContainsHeader(file)) + return; - System.out.println("Removing BOM from: " + file.getAbsolutePath()); - stripFileFromHeader(file); - } + System.out.println("Removing BOM from: " + file.getAbsolutePath()); + stripFileFromHeader(file); + } - @Override - public void run(final String[] args) throws IOException { + @Override + public void run(final String[] args) throws IOException { - options = commandlineHandler.parseCommandlineArguments(args); + options = commandlineHandler.parseCommandlineArguments(args); - if (options == null) { - showCommandlineHelp(); - return; - } + if (options == null) { + showCommandlineHelp(); + return; + } - processDirectory(options.targetDirectory); + processDirectory(options.targetDirectory); - } + } - @Override - public void showCommandlineHelp() { - commandlineHandler.initParser().showHelp(); - } + @Override + public void showCommandlineHelp() { + commandlineHandler.parser.showHelp(); + } - public void stripFileFromHeader(final File file) - throws FileNotFoundException, IOException { - // read entire file - final byte[] fileContents = IOHelper.getFileContents(file); + private void stripFileFromHeader(final File file) + throws 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); + // remove BOM header form file + final byte[] newFileContents = Arrays.copyOfRange(fileContents, + bomHeader.length, fileContents.length); - // overwrite file with new contents - IOHelper.saveToFile(file, newFileContents); - } + // overwrite file with new contents + IOHelper.saveToFile(file, newFileContents); + } }