2 *******************************************************************************
\r
3 * Copyright (C) 2002-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.util;
\r
9 import java.io.BufferedReader;
\r
10 import java.io.IOException;
\r
11 import java.io.PrintWriter;
\r
12 import java.util.Locale;
\r
14 public class FileUtilities {
\r
15 public static void appendFile(String filename, String encoding, PrintWriter output) throws IOException {
\r
16 appendFile(filename, encoding, output, null);
\r
19 public static void appendFile(String filename, String encoding, PrintWriter output, String[] replacementList) throws IOException {
\r
20 BufferedReader br = BagFormatter.openReader("", filename, encoding);
\r
22 FileInputStream fis = new FileInputStream(filename);
\r
23 InputStreamReader isr = (encoding == UTF8_UNIX || encoding == UTF8_WINDOWS) ? new InputStreamReader(fis, "UTF8") : new InputStreamReader(fis);
\r
24 BufferedReader br = new BufferedReader(isr, 32*1024);
\r
26 appendBufferedReader(br, output, replacementList);
\r
29 public static void appendBufferedReader(BufferedReader br,
\r
30 PrintWriter output, String[] replacementList) throws IOException {
\r
32 String line = br.readLine();
\r
33 if (line == null) break;
\r
34 if (replacementList != null) {
\r
35 for (int i = 0; i < replacementList.length; i += 2) {
\r
36 line = replace(line, replacementList[i], replacementList[i+1]);
\r
39 output.println(line);
\r
45 * Replaces all occurances of piece with replacement, and returns new String
\r
47 public static String replace(String source, String piece, String replacement) {
\r
48 if (source == null || source.length() < piece.length()) return source;
\r
51 pos = source.indexOf(piece, pos);
\r
52 if (pos < 0) return source;
\r
53 source = source.substring(0,pos) + replacement + source.substring(pos + piece.length());
\r
54 pos += replacement.length();
\r
58 public static String replace(String source, String[][] replacements) {
\r
59 return replace(source, replacements, replacements.length);
\r
62 public static String replace(String source, String[][] replacements, int count) {
\r
63 for (int i = 0; i < count; ++i) {
\r
64 source = replace(source, replacements[i][0], replacements[i][1]);
\r
69 public static String replace(String source, String[][] replacements, boolean reverse) {
\r
70 if (!reverse) return replace(source, replacements);
\r
71 for (int i = 0; i < replacements.length; ++i) {
\r
72 source = replace(source, replacements[i][1], replacements[i][0]);
\r
77 public static String anchorize(String source) {
\r
78 String result = source.toLowerCase(Locale.ENGLISH).replaceAll("[^\\p{L}\\p{N}]+", "_");
\r
79 if (result.endsWith("_")) result = result.substring(0,result.length()-1);
\r
80 if (result.startsWith("_")) result = result.substring(1);
\r