1 package org.unicode.cldr.util; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 9 import com.ibm.icu.util.ICUUncheckedIOException; 10 11 public class FileProcessor { 12 private int lineCount; 13 protected boolean doHash = true; 14 handleStart()15 protected void handleStart() { 16 } 17 18 /** 19 * Return false to abort 20 * 21 * @param lineCount 22 * @param line 23 * @return 24 */ handleLine(int lineCount, String line)25 protected boolean handleLine(int lineCount, String line) { 26 return true; 27 } 28 handleEnd()29 protected void handleEnd() { 30 } 31 getLineCount()32 public int getLineCount() { 33 return lineCount; 34 } 35 handleComment(String line, int commentCharPosition)36 public void handleComment(String line, int commentCharPosition) { 37 } 38 process(Class<?> classLocation, String fileName)39 public FileProcessor process(Class<?> classLocation, String fileName) { 40 try { 41 BufferedReader in = FileReaders.openFile(classLocation, fileName); 42 return process(in, fileName); 43 } catch (Exception e) { 44 throw (RuntimeException) new IllegalArgumentException(lineCount + ":\t" + 0).initCause(e); 45 } 46 47 } 48 process(String fileName)49 public FileProcessor process(String fileName) { 50 try { 51 FileInputStream fileStream = new FileInputStream(fileName); 52 InputStreamReader reader = new InputStreamReader(fileStream, CldrUtility.UTF8); 53 BufferedReader bufferedReader = new BufferedReader(reader, 1024 * 64); 54 return process(bufferedReader, fileName); 55 } catch (Exception e) { 56 throw (RuntimeException) new IllegalArgumentException(lineCount + ":\t" + 0).initCause(e); 57 } 58 } 59 process(String directory, String fileName)60 public FileProcessor process(String directory, String fileName) { 61 try { 62 FileInputStream fileStream = new FileInputStream(directory + File.separator + fileName); 63 InputStreamReader reader = new InputStreamReader(fileStream, CldrUtility.UTF8); 64 BufferedReader bufferedReader = new BufferedReader(reader, 1024 * 64); 65 return process(bufferedReader, fileName); 66 } catch (Exception e) { 67 throw (RuntimeException) new IllegalArgumentException(lineCount + ":\t" + 0).initCause(e); 68 } 69 } 70 process(BufferedReader in, String fileName)71 public FileProcessor process(BufferedReader in, String fileName) { 72 handleStart(); 73 String line = null; 74 lineCount = 1; 75 try { 76 for (;; ++lineCount) { 77 line = in.readLine(); 78 if (line == null) { 79 break; 80 } 81 int comment = line.indexOf("#"); 82 if (comment == 0 || doHash && comment >= 0) { 83 handleComment(line, comment); 84 line = line.substring(0, comment); 85 } 86 if (line.startsWith("\uFEFF")) { 87 line = line.substring(1); 88 } 89 line = line.trim(); 90 if (line.length() == 0) { 91 continue; 92 } 93 if (!handleLine(lineCount, line)) { 94 break; 95 } 96 } 97 in.close(); 98 handleEnd(); 99 } catch (IOException e) { 100 throw new ICUUncheckedIOException(lineCount + ":\t" + line, e); 101 } 102 return this; 103 } 104 }