KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > ReplaceLine


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.util;
6
7 import java.io.BufferedOutputStream JavaDoc;
8 import java.io.BufferedReader JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileNotFoundException JavaDoc;
12 import java.io.FileOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.regex.Matcher JavaDoc;
18 import java.util.regex.Pattern JavaDoc;
19
20 public final class ReplaceLine {
21
22   private ReplaceLine() {
23   // cannot instantiate
24
}
25
26   /**
27    * Replaces lines matching a token regular expression group.
28    *
29    * @return true if all tokens found matches
30    */

31   public static void parseFile(ReplaceLine.Token[] tokens, File JavaDoc file) throws FileNotFoundException JavaDoc, IOException JavaDoc {
32     Arrays.sort(tokens, new Comparator JavaDoc() {
33       public int compare(Object JavaDoc o1, Object JavaDoc o2) {
34         return new Integer JavaDoc(((Token) o1).lineNumber).compareTo(new Integer JavaDoc(((Token) o2).lineNumber));
35       }
36     });
37     int tokenIndex = 0, lineIndex = 0;
38     StringBuffer JavaDoc text = new StringBuffer JavaDoc();
39     BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(file)));
40     String JavaDoc line;
41     while ((line = reader.readLine()) != null) {
42       if (tokenIndex < tokens.length && ++lineIndex == tokens[tokenIndex].lineNumber) {
43         line = replaceToken(tokens[tokenIndex].replacePattern, line, tokens[tokenIndex].value);
44         tokenIndex++;
45       }
46       text.append(line + "\n");
47     }
48     reader.close();
49     BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
50     out.write(text.toString().getBytes());
51     out.flush();
52     out.close();
53   }
54
55   private static String JavaDoc replaceToken(String JavaDoc expression, String JavaDoc text, String JavaDoc value) {
56     Pattern JavaDoc pattern = Pattern.compile(expression);
57     Matcher JavaDoc matcher = pattern.matcher(text);
58     while (matcher.find()) {
59       return matcher.replaceAll(value);
60     }
61     return text;
62   }
63
64   public static class Token {
65     private final int lineNumber;
66     private final String JavaDoc replacePattern;
67     private final String JavaDoc value;
68
69     public Token(int lineNumber, String JavaDoc replacePattern, String JavaDoc value) {
70       this.lineNumber = lineNumber;
71       this.replacePattern = replacePattern;
72       this.value = value;
73     }
74   }
75 }
76
Popular Tags