1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.glxdesktop.parser;
18
19 import java.io.BufferedWriter;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.nio.CharBuffer;
26 import java.nio.channels.FileChannel;
27 import java.nio.charset.Charset;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.oro.text.regex.MalformedPatternException;
32 import org.apache.oro.text.regex.MatchResult;
33 import org.apache.oro.text.regex.Pattern;
34 import org.apache.oro.text.regex.PatternCompiler;
35 import org.apache.oro.text.regex.PatternMatcher;
36 import org.apache.oro.text.regex.Perl5Compiler;
37 import org.apache.oro.text.regex.Perl5Matcher;
38 import org.apache.oro.text.regex.Perl5Substitution;
39 import org.apache.oro.text.regex.Util;
40
41 /***
42 * TODO update this class: it must throw a personal Exception
43 * and return more information trough these Exceptions.
44 * @author Alphonse Van Assche
45 *
46 */
47
48 public class ConfigFileTools {
49
50 private static final Log LOG = LogFactory.getLog(ConfigFileTools.class);
51
52 private FileInputStream fileInputStream;
53
54 private CharSequence sequence;
55
56 private String fileName;
57
58 private PatternCompiler compiler;
59
60 private PatternMatcher matcher;
61
62 private Pattern pattern = null;
63
64 private MatchResult matchResult;
65
66 public ConfigFileTools(String fileName) {
67 this.fileName = fileName;
68 this.sequence = openFile(fileName);
69 compiler = new Perl5Compiler();
70 matcher = new Perl5Matcher();
71 }
72
73 public void replaceValues(String regex, String substitute, int nbrOfSubs)
74 throws MalformedPatternException {
75 int interps = Perl5Substitution.INTERPOLATE_ALL;
76 String result;
77 pattern = compiler.compile(regex);
78 result = Util.substitute(matcher, pattern, new Perl5Substitution(
79 substitute, interps), sequence.toString(), nbrOfSubs);
80 saveBuffer(result.trim() + "\n");
81 }
82
83 public void replaceValues(String regex, String substitute) {
84 try {
85 replaceValues(regex, substitute, Util.SUBSTITUTE_ALL);
86 } catch (MalformedPatternException e) {
87 if (!LOG.isDebugEnabled()) {
88 LOG.error(e.getMessage());
89 } else {
90 LOG.error(e.getMessage(), e);
91 }
92 }
93 }
94
95 /***
96 * Remove given text in a file.
97 *
98 * @param regex
99 * @throws IOException
100 * @throws MalformedPatternException
101 */
102 public void removeTextInFile(String regex) throws IOException,
103 MalformedPatternException {
104 replaceValues(regex + "\n", "");
105 }
106
107 /***
108 * Append a given text at the end of a file.
109 *
110 * @param textToAppend
111 * @throws IOException
112 */
113 public void appendTextAtEndOfFile(String textToAppend) throws IOException {
114 String tmpFile = sequence.toString().trim() + "\n" + textToAppend
115 + "\n";
116 saveBuffer(tmpFile);
117 }
118
119 public void appendText(String regex, String textToAppend)
120 throws MalformedPatternException {
121 pattern = compiler.compile(regex);
122 matcher.contains(sequence.toString(), pattern);
123 matchResult = matcher.getMatch();
124 int beginOffset = matchResult.endOffset(0);
125 String text = sequence.toString();
126 String begin = text.substring(0, beginOffset).trim();
127 String end = sequence.toString().substring(beginOffset, text.length()).trim();
128 saveBuffer(begin + "\n" + textToAppend.trim() + "\n" + end);
129 }
130
131 /***
132 * Check if given text exist in file.
133 *
134 * @param regex
135 * @return
136 * @throws MalformedPatternException
137 */
138 public boolean checkTextInFile(String regex)
139 throws MalformedPatternException {
140
141 pattern = compiler.compile(regex);
142 return matcher.contains(sequence.toString().toCharArray(), pattern);
143 }
144
145 public String findValueByKey(String regex, String delimiter)
146 throws MalformedPatternException {
147 pattern = compiler.compile(regex + delimiter + ".*");
148 matcher.contains(sequence.toString().toCharArray(), pattern);
149 matchResult = matcher.getMatch();
150 if (matchResult != null) {
151 return matchResult.toString().split(delimiter)[1];
152 } else {
153 return null;
154 }
155 }
156
157 /***
158 * Find a {@Link String} with a given regexp, after
159 * the given delimiter to the end of the line.
160 * @param text source af search
161 * @param regex to find
162 * @param delimiter to
163 * @return a the String or <code>null</code> if the regexp is not found.
164 * @throws MalformedPatternException
165 */
166 public static String findValueByKey(String text, String regex,
167 String delimiter) throws MalformedPatternException {
168 Pattern pattern;
169 PatternCompiler compiler = new Perl5Compiler();
170 PatternMatcher matcher = new Perl5Matcher();
171 MatchResult matchResult;
172 pattern = compiler.compile(regex + delimiter + ".*");
173 matcher.contains(text, pattern);
174 matchResult = matcher.getMatch();
175 if (matchResult != null) {
176 return matchResult.toString().split(delimiter)[1];
177 } else {
178 return null;
179 }
180 }
181
182 /***
183 * Find Text between two regex, if the one of the two regex is no found
184 * this method return a empty string.
185 *
186 * @param text
187 * @param regex_begin
188 * @param regex_end
189 * @return the String between the two regular expressions.
190 */
191 public static String findTextInInterval(String text, String regexpBegin,
192 String regexpEnd) {
193 String ret = "";
194 boolean valid = true;
195 int begin = 0;
196 int end = 0;
197 Pattern pattern;
198 PatternCompiler compiler = new Perl5Compiler();
199 Perl5Matcher matcher = new Perl5Matcher();
200 matcher.setMultiline(true);
201 MatchResult matchResult;
202 try {
203 pattern = compiler.compile(regexpBegin,
204 Perl5Compiler.MULTILINE_MASK);
205 matcher.contains(text, pattern);
206 matchResult = matcher.getMatch();
207 if (matchResult == null) {
208 LOG.debug("First regex not found: " + regexpBegin);
209 valid = false;
210 } else {
211 begin = matchResult.endOffset(0);
212 }
213 pattern = compiler.compile(regexpEnd, Perl5Compiler.MULTILINE_MASK);
214 matcher.contains(text, pattern);
215 matchResult = matcher.getMatch();
216 if (matchResult == null) {
217 LOG.debug("Last regex not found: " + regexpEnd);
218 end = text.length();
219 } else {
220 end = matchResult.beginOffset(0);
221 }
222
223 } catch (MalformedPatternException e) {
224 if (!LOG.isDebugEnabled()) {
225 LOG.error(e.getMessage());
226 } else {
227 LOG.error(e.getMessage(), e);
228 }
229 }
230 if (valid) {
231 ret = text.substring(begin, end);
232 } else {
233 ret = "";
234 }
235 return ret;
236 }
237
238 public static String replaceValues(String text, String regex,
239 String substitute) {
240 int limite = Util.SUBSTITUTE_ALL;
241 int interps = Perl5Substitution.INTERPOLATE_ALL;
242 Pattern pattern;
243 PatternCompiler compiler = new Perl5Compiler();
244 PatternMatcher matcher = new Perl5Matcher();
245 String result;
246 try {
247 pattern = compiler.compile(regex);
248 result = Util.substitute(matcher, pattern, new Perl5Substitution(
249 substitute, interps), text, limite);
250 return result;
251 } catch (MalformedPatternException e) {
252 if (!LOG.isDebugEnabled()) {
253 LOG.error(e.getMessage());
254 } else {
255 LOG.error(e.getMessage(), e);
256 }
257 }
258
259 return new String();
260 }
261
262 /***
263 * Open config file
264 *
265 * @param fileName
266 * @return a CharSequence
267 * @throws IOException
268 */
269 public CharSequence openFile(String fileName) {
270 LOG.debug("openFile: " + fileName);
271 CharBuffer cbuf = null;
272 try {
273 fileInputStream = new FileInputStream(fileName);
274 FileChannel fc = fileInputStream.getChannel();
275 ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc
276 .size());
277
278 fc.close();
279
280 cbuf = Charset
281 .forName("UTF8").newDecoder()
282 .decode(bbuf);
283
284 } catch (FileNotFoundException e) {
285 if (!LOG.isDebugEnabled()) {
286 LOG.error(e.getMessage());
287 } else {
288 LOG.error(e.getMessage(), e);
289 }
290 } catch (IOException e) {
291 if (!LOG.isDebugEnabled()) {
292 LOG.error(e.getMessage());
293 } else {
294 LOG.error(e.getMessage(), e);
295 }
296 }
297 return cbuf;
298 }
299
300 /***
301 * Close config file
302 *
303 * @throws IOException
304 */
305 public void closeFile() {
306
307 try {
308 fileInputStream.close();
309 } catch (IOException e) {
310 if (!LOG.isDebugEnabled()) {
311 LOG.error(e.getMessage());
312 } else {
313 LOG.error(e.getMessage(), e);
314 }
315 }
316 }
317
318 /***
319 * Write file to disk.
320 */
321 public void save() {
322
323 try {
324 BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
325 writer.write(sequence.toString());
326 writer.close();
327 } catch (IOException e) {
328 if (!LOG.isDebugEnabled()) {
329 LOG.error(e.getMessage());
330 } else {
331 LOG.error(e.getMessage(), e);
332 }
333 }
334
335 }
336
337 /***
338 * Save the buffer
339 *
340 * @param content
341 * @throws IOException
342 */
343 private void saveBuffer(String content) {
344
345 sequence = CharBuffer.wrap(content);
346 }
347
348 }