Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/LineReader.java @ 480:f3fab7a20841
Refactor LineReader utility as stanalone class to facilitate reuse
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 17 Jul 2012 22:14:21 +0200 |
parents | |
children | e31e85cf4d4c |
comparison
equal
deleted
inserted
replaced
479:59b7c817bc4d | 480:f3fab7a20841 |
---|---|
1 /* | |
2 * Copyright (c) 2012 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.internal; | |
18 | |
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; | |
20 | |
21 import java.io.BufferedReader; | |
22 import java.io.File; | |
23 import java.io.FileReader; | |
24 import java.io.IOException; | |
25 import java.util.Collection; | |
26 | |
27 import org.tmatesoft.hg.repo.HgInvalidFileException; | |
28 import org.tmatesoft.hg.repo.ext.MqManager; | |
29 import org.tmatesoft.hg.util.LogFacility; | |
30 | |
31 /** | |
32 * Handy class to read line-based configuration files | |
33 * | |
34 * @author Artem Tikhomirov | |
35 * @author TMate Software Ltd. | |
36 */ | |
37 public final class LineReader { | |
38 | |
39 public interface LineConsumer<T> { | |
40 // boolean begin(File f, T paramObj) throws IOException; | |
41 boolean consume(String line, T paramObj) throws IOException; | |
42 // boolean end(File f, T paramObj) throws IOException; | |
43 } | |
44 | |
45 public static class SimpleLineCollector implements LineReader.LineConsumer<Collection<String>> { | |
46 | |
47 public boolean consume(String line, Collection<String> result) throws IOException { | |
48 result.add(line); | |
49 return true; | |
50 } | |
51 } | |
52 | |
53 private final File file; | |
54 private final LogFacility log; | |
55 private boolean trimLines = true; | |
56 private boolean skipEmpty = true; | |
57 private String ignoreThatStarts = null; | |
58 | |
59 public LineReader(File f, LogFacility logFacility) { | |
60 file = f; | |
61 log = logFacility; | |
62 } | |
63 | |
64 /** | |
65 * default: <code>true</code> | |
66 * <code>false</code> to return line as is | |
67 */ | |
68 public LineReader trimLines(boolean trim) { | |
69 trimLines = trim; | |
70 return this; | |
71 } | |
72 | |
73 /** | |
74 * default: <code>true</code> | |
75 * <code>false</code> to pass empty lines to consumer | |
76 */ | |
77 public LineReader skipEmpty(boolean skip) { | |
78 skipEmpty = skip; | |
79 return this; | |
80 } | |
81 | |
82 /** | |
83 * default: doesn't skip any line. | |
84 * set e.g. to "#" or "//" to skip lines that start with such prefix | |
85 */ | |
86 public LineReader ignoreLineComments(String lineStart) { | |
87 ignoreThatStarts = lineStart; | |
88 return this; | |
89 } | |
90 | |
91 public <T> void read(LineConsumer<T> consumer, T paramObj) throws HgInvalidFileException { | |
92 BufferedReader statusFileReader = null; | |
93 try { | |
94 // consumer.begin(file, paramObj); | |
95 statusFileReader = new BufferedReader(new FileReader(file)); | |
96 String line; | |
97 boolean ok = true; | |
98 while (ok && (line = statusFileReader.readLine()) != null) { | |
99 if (trimLines) { | |
100 line = line.trim(); | |
101 } | |
102 if (ignoreThatStarts != null && line.startsWith(ignoreThatStarts)) { | |
103 continue; | |
104 } | |
105 if (!skipEmpty || line.length() > 0) { | |
106 ok = consumer.consume(line, paramObj); | |
107 } | |
108 } | |
109 } catch (IOException ex) { | |
110 throw new HgInvalidFileException(ex.getMessage(), ex, file); | |
111 } finally { | |
112 try { | |
113 statusFileReader.close(); | |
114 } catch (IOException ex) { | |
115 log.dump(MqManager.class, Warn, ex, null); | |
116 } | |
117 // try { | |
118 // consumer.end(file, paramObj); | |
119 // } catch (IOException ex) { | |
120 // log.warn(MqManager.class, ex, null); | |
121 // } | |
122 } | |
123 } | |
124 } |