]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/util/FileUtilities.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / util / FileUtilities.java
1 //##header J2SE15
2 //#if defined(FOUNDATION10) || defined(J2SE13)
3 //#else
4 /*
5  *******************************************************************************
6  * Copyright (C) 2002-2009, International Business Machines Corporation and    *
7  * others. All Rights Reserved.                                                *
8  *******************************************************************************
9  */
10 package com.ibm.icu.dev.test.util;
11
12 import java.io.BufferedReader;
13
14 import java.io.IOException;
15 import java.io.PrintWriter;
16 import java.util.Locale;
17
18 public class FileUtilities {
19     public static void appendFile(String filename, String encoding, PrintWriter output) throws IOException {
20         appendFile(filename, encoding, output, null);
21     }
22     
23     public static void appendFile(String filename, String encoding, PrintWriter output, String[] replacementList) throws IOException {
24         BufferedReader br = BagFormatter.openReader("", filename, encoding);
25         /*
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);
29         */
30         appendBufferedReader(br, output, replacementList);
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 //#endif