blob: 0542f65180135c6a5575e54a3ef61a0766ca0f7f [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.sed;
2
3import java.io.*;
4import java.util.*;
5import java.util.regex.*;
6
Stuart McCulloch81d48de2012-06-29 19:23:09 +00007import aQute.lib.io.*;
Stuart McCulloch2286f232012-06-15 13:27:53 +00008
Stuart McCullochbb014372012-06-07 21:57:32 +00009public class Sed {
Stuart McCulloch2286f232012-06-15 13:27:53 +000010 final File file;
11 final Replacer macro;
12 File output;
13 boolean backup = true;
Stuart McCullochbb014372012-06-07 21:57:32 +000014
Stuart McCulloch2286f232012-06-15 13:27:53 +000015 final Map<Pattern,String> replacements = new LinkedHashMap<Pattern,String>();
Stuart McCullochbb014372012-06-07 21:57:32 +000016
Stuart McCulloch2286f232012-06-15 13:27:53 +000017 public Sed(Replacer macro, File file) {
18 assert file.isFile();
19 this.file = file;
20 this.macro = macro;
21 }
Stuart McCullochbb014372012-06-07 21:57:32 +000022
Stuart McCulloch2286f232012-06-15 13:27:53 +000023 public Sed(File file) {
24 assert file.isFile();
25 this.file = file;
26 this.macro = null;
27 }
Stuart McCullochbb014372012-06-07 21:57:32 +000028
Stuart McCulloch2286f232012-06-15 13:27:53 +000029 public void setOutput(File f) {
30 output = f;
31 }
Stuart McCullochbb014372012-06-07 21:57:32 +000032
Stuart McCulloch2286f232012-06-15 13:27:53 +000033 public void replace(String pattern, String replacement) {
34 replacements.put(Pattern.compile(pattern), replacement);
35 }
Stuart McCullochbb014372012-06-07 21:57:32 +000036
Stuart McCulloch2286f232012-06-15 13:27:53 +000037 public int doIt() throws IOException {
38 int actions = 0;
39 BufferedReader brdr = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
40 File out;
41 if (output != null)
42 out = output;
43 else
44 out = new File(file.getAbsolutePath() + ".tmp");
45 PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), "UTF-8"));
46 try {
47 String line;
48 while ((line = brdr.readLine()) != null) {
49 for (Pattern p : replacements.keySet()) {
50 String replace = replacements.get(p);
51 Matcher m = p.matcher(line);
Stuart McCullochbb014372012-06-07 21:57:32 +000052
Stuart McCulloch2286f232012-06-15 13:27:53 +000053 StringBuffer sb = new StringBuffer();
54 while (m.find()) {
55 String tmp = setReferences(m, replace);
56 if (macro != null)
57 tmp = Matcher.quoteReplacement(macro.process(tmp));
58 m.appendReplacement(sb, tmp);
59 actions++;
60 }
61 m.appendTail(sb);
62
63 line = sb.toString();
64 }
65 pw.println(line);
66 }
Stuart McCullochbb014372012-06-07 21:57:32 +000067 } finally {
Stuart McCulloch2286f232012-06-15 13:27:53 +000068 brdr.close();
69 pw.close();
Stuart McCullochbb014372012-06-07 21:57:32 +000070 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000071
72 if (output == null) {
73 if (backup) {
74 File bak = new File(file.getAbsolutePath() + ".bak");
75 IO.rename(file, bak);
76 }
77 IO.rename(out, file);
78 }
79
80 return actions;
81 }
82
83 private String setReferences(Matcher m, String replace) {
84 StringBuilder sb = new StringBuilder();
85 for (int i = 0; i < replace.length(); i++) {
86 char c = replace.charAt(i);
87 if (c == '$' && i < replace.length() - 1 && Character.isDigit(replace.charAt(i + 1))) {
88 int n = replace.charAt(i + 1) - '0';
89 if (n <= m.groupCount())
90 sb.append(m.group(n));
91 i++;
92 } else
93 sb.append(c);
94 }
95 return sb.toString();
96 }
Stuart McCullochbb014372012-06-07 21:57:32 +000097
Stuart McCulloch2286f232012-06-15 13:27:53 +000098 public void setBackup(boolean b) {
99 this.backup = b;
100 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000101}