12 |
12 |
import java.io.PrintWriter;
|
13 |
13 |
import java.io.UnsupportedEncodingException;
|
14 |
14 |
import java.util.ArrayList;
|
|
15 |
import java.util.regex.Matcher;
|
|
16 |
import java.util.regex.Pattern;
|
15 |
17 |
|
16 |
18 |
import org.txm.utils.i18n.DetectBOM;
|
17 |
19 |
|
18 |
20 |
public class IOUtils {
|
19 |
21 |
public static final String UTF8 = "UTF-8";
|
20 |
22 |
|
|
23 |
public static ArrayList<String> findWithGroup(File file, String pattern) throws IOException {
|
|
24 |
return findWithGroup(file, UTF8, pattern, false);
|
|
25 |
}
|
|
26 |
|
|
27 |
public static ArrayList<String> findWithGroup(File file, String pattern, boolean normalizeSeparators) throws IOException {
|
|
28 |
return findWithGroup(file, UTF8, pattern, normalizeSeparators);
|
|
29 |
}
|
|
30 |
|
|
31 |
public static ArrayList<String> findWithGroup(File file, String encoding, String pattern) throws IOException {
|
|
32 |
return findWithGroup(file, encoding, pattern, false);
|
|
33 |
}
|
|
34 |
|
|
35 |
public static ArrayList<String> findWithGroup(File file, String encoding, String pattern, boolean normalizeSeparators) throws IOException {
|
|
36 |
ArrayList<String> matches = new ArrayList<String>();
|
|
37 |
String text = IOUtils.getText(file, encoding);
|
|
38 |
if (normalizeSeparators) text = text.replaceAll("\\s", " ");
|
|
39 |
|
|
40 |
Pattern test = Pattern.compile(pattern);
|
|
41 |
Matcher m = test.matcher(text);
|
|
42 |
while (m.find()) {
|
|
43 |
matches.add(m.group(1));
|
|
44 |
}
|
|
45 |
return matches;
|
|
46 |
}
|
|
47 |
|
21 |
48 |
public static BufferedReader getReader(File file) throws UnsupportedEncodingException, FileNotFoundException {
|
22 |
49 |
return getReader(file, UTF8);
|
23 |
50 |
}
|