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