001package org.hl7.fhir.convertors.misc;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.IOException;
007import java.io.InputStream;
008import java.io.InputStreamReader;
009
010import org.hl7.fhir.utilities.TextFile;
011import org.hl7.fhir.utilities.Utilities;
012
013public class BOMRemover {
014
015  public static void main(String[] args) throws FileNotFoundException, IOException {
016    new BOMRemover().execute(new File(args[0]));
017
018  }
019
020  private void execute(File f) throws FileNotFoundException, IOException {
021    if (f.isDirectory()) {
022      for (File file : f.listFiles()) {
023        execute(file);
024      }
025    } else if (f.getName().endsWith(".java")) {
026      String src = fileToString(f);
027      String s = Utilities.stripBOM(src);
028      if (!s.equals(src)) {
029        System.out.println("Remove BOM from "+f.getAbsolutePath());
030        TextFile.stringToFile(s, f, false);
031      }
032    }
033  }
034
035
036  public static String fileToString(File f) throws FileNotFoundException, IOException {
037    return streamToString(new FileInputStream(f));
038  }
039  
040  public static String streamToString(InputStream input) throws IOException  {
041    InputStreamReader sr = new InputStreamReader(input, "UTF-8");    
042    StringBuilder b = new StringBuilder();
043    //while (sr.ready()) { Commented out by Claude Nanjo (1/14/2014) - sr.ready() always returns false - please remove if change does not impact other areas of codebase
044    int i = -1;
045    while((i = sr.read()) > -1) {
046      char c = (char) i;
047      b.append(c);
048    }
049    sr.close();
050    
051    return  b.toString(); 
052  }
053
054}