2 //#if defined(FOUNDATION10) || defined(J2SE13)
\r
5 *******************************************************************************
\r
6 * Copyright (C) 2002-2009, International Business Machines Corporation and *
\r
7 * others. All Rights Reserved. *
\r
8 *******************************************************************************
\r
10 package com.ibm.icu.dev.test.util;
\r
12 import java.io.BufferedReader;
\r
14 import java.io.IOException;
\r
15 import java.io.PrintWriter;
\r
16 import java.util.Locale;
\r
18 public class FileUtilities {
\r
19 public static void appendFile(String filename, String encoding, PrintWriter output) throws IOException {
\r
20 appendFile(filename, encoding, output, null);
\r
23 public static void appendFile(String filename, String encoding, PrintWriter output, String[] replacementList) throws IOException {
\r
24 BufferedReader br = BagFormatter.openReader("", filename, encoding);
\r
26 FileInputStream fis = new FileInputStream(filename);
\r
27 InputStreamReader isr = (encoding == UTF8_UNIX || encoding == UTF8_WINDOWS) ? new InputStreamReader(fis, "UTF8") : new InputStreamReader(fis);
\r
28 BufferedReader br = new BufferedReader(isr, 32*1024);
\r
30 appendBufferedReader(br, output, replacementList);
\r
33 public static void appendBufferedReader(BufferedReader br,
\r
34 PrintWriter output, String[] replacementList) throws IOException {
\r
36 String line = br.readLine();
\r
37 if (line == null) break;
\r
38 if (replacementList != null) {
\r
39 for (int i = 0; i < replacementList.length; i += 2) {
\r
40 line = replace(line, replacementList[i], replacementList[i+1]);
\r
43 output.println(line);
\r
49 * Replaces all occurances of piece with replacement, and returns new String
\r
51 public static String replace(String source, String piece, String replacement) {
\r
52 if (source == null || source.length() < piece.length()) return source;
\r
55 pos = source.indexOf(piece, pos);
\r
56 if (pos < 0) return source;
\r
57 source = source.substring(0,pos) + replacement + source.substring(pos + piece.length());
\r
58 pos += replacement.length();
\r
62 public static String replace(String source, String[][] replacements) {
\r
63 return replace(source, replacements, replacements.length);
\r
66 public static String replace(String source, String[][] replacements, int count) {
\r
67 for (int i = 0; i < count; ++i) {
\r
68 source = replace(source, replacements[i][0], replacements[i][1]);
\r
73 public static String replace(String source, String[][] replacements, boolean reverse) {
\r
74 if (!reverse) return replace(source, replacements);
\r
75 for (int i = 0; i < replacements.length; ++i) {
\r
76 source = replace(source, replacements[i][1], replacements[i][0]);
\r
81 public static String anchorize(String source) {
\r
82 String result = source.toLowerCase(Locale.ENGLISH).replaceAll("[^\\p{L}\\p{N}]+", "_");
\r
83 if (result.endsWith("_")) result = result.substring(0,result.length()-1);
\r
84 if (result.startsWith("_")) result = result.substring(1);
\r