1/* 2 ******************************************************************************* 3 * Copyright (C) 2002-2015, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ******************************************************************************* 6 */ 7package com.ibm.icu.dev.util; 8 9import java.io.BufferedReader; 10import java.io.IOException; 11import java.io.PrintWriter; 12import java.util.Locale; 13 14public class FileUtilities { 15 public static void appendFile(String filename, String encoding, PrintWriter output) throws IOException { 16 appendFile(filename, encoding, output, null); 17 } 18 19 public static void appendFile(String filename, String encoding, PrintWriter output, String[] replacementList) throws IOException { 20 BufferedReader br = BagFormatter.openReader("", filename, encoding); 21 /* 22 FileInputStream fis = new FileInputStream(filename); 23 InputStreamReader isr = (encoding == UTF8_UNIX || encoding == UTF8_WINDOWS) ? new InputStreamReader(fis, "UTF8") : new InputStreamReader(fis); 24 BufferedReader br = new BufferedReader(isr, 32*1024); 25 */ 26 try { 27 appendBufferedReader(br, output, replacementList); 28 } finally { 29 br.close(); 30 } 31 } 32 33 public static void appendBufferedReader(BufferedReader br, 34 PrintWriter output, String[] replacementList) throws IOException { 35 while (true) { 36 String line = br.readLine(); 37 if (line == null) break; 38 if (replacementList != null) { 39 for (int i = 0; i < replacementList.length; i += 2) { 40 line = replace(line, replacementList[i], replacementList[i+1]); 41 } 42 } 43 output.println(line); 44 } 45 br.close(); 46 } 47 48 /** 49 * Replaces all occurances of piece with replacement, and returns new String 50 */ 51 public static String replace(String source, String piece, String replacement) { 52 if (source == null || source.length() < piece.length()) return source; 53 int pos = 0; 54 while (true) { 55 pos = source.indexOf(piece, pos); 56 if (pos < 0) return source; 57 source = source.substring(0,pos) + replacement + source.substring(pos + piece.length()); 58 pos += replacement.length(); 59 } 60 } 61 62 public static String replace(String source, String[][] replacements) { 63 return replace(source, replacements, replacements.length); 64 } 65 66 public static String replace(String source, String[][] replacements, int count) { 67 for (int i = 0; i < count; ++i) { 68 source = replace(source, replacements[i][0], replacements[i][1]); 69 } 70 return source; 71 } 72 73 public static String replace(String source, String[][] replacements, boolean reverse) { 74 if (!reverse) return replace(source, replacements); 75 for (int i = 0; i < replacements.length; ++i) { 76 source = replace(source, replacements[i][1], replacements[i][0]); 77 } 78 return source; 79 } 80 81 public static String anchorize(String source) { 82 String result = source.toLowerCase(Locale.ENGLISH).replaceAll("[^\\p{L}\\p{N}]+", "_"); 83 if (result.endsWith("_")) result = result.substring(0,result.length()-1); 84 if (result.startsWith("_")) result = result.substring(1); 85 return result; 86 } 87} 88