2 //#if defined(FOUNDATION10) || defined(J2SE13)
5 *******************************************************************************
6 * Copyright (C) 2002-2009, International Business Machines Corporation and *
7 * others. All Rights Reserved. *
8 *******************************************************************************
10 package com.ibm.icu.dev.test.util;
12 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.PrintWriter;
16 import java.util.Locale;
18 public class FileUtilities {
19 public static void appendFile(String filename, String encoding, PrintWriter output) throws IOException {
20 appendFile(filename, encoding, output, null);
23 public static void appendFile(String filename, String encoding, PrintWriter output, String[] replacementList) throws IOException {
24 BufferedReader br = BagFormatter.openReader("", filename, encoding);
26 FileInputStream fis = new FileInputStream(filename);
27 InputStreamReader isr = (encoding == UTF8_UNIX || encoding == UTF8_WINDOWS) ? new InputStreamReader(fis, "UTF8") : new InputStreamReader(fis);
28 BufferedReader br = new BufferedReader(isr, 32*1024);
30 appendBufferedReader(br, output, replacementList);
33 public static void appendBufferedReader(BufferedReader br,
34 PrintWriter output, String[] replacementList) throws IOException {
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]);
49 * Replaces all occurances of piece with replacement, and returns new String
51 public static String replace(String source, String piece, String replacement) {
52 if (source == null || source.length() < piece.length()) return source;
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();
62 public static String replace(String source, String[][] replacements) {
63 return replace(source, replacements, replacements.length);
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]);
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]);
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);