Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.sed; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.regex.*; |
| 6 | |
Stuart McCulloch | 81d48de | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 7 | import aQute.lib.io.*; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 8 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 9 | public class Sed { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 10 | final File file; |
| 11 | final Replacer macro; |
| 12 | File output; |
| 13 | boolean backup = true; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 14 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 15 | final Map<Pattern,String> replacements = new LinkedHashMap<Pattern,String>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 16 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | public Sed(Replacer macro, File file) { |
| 18 | assert file.isFile(); |
| 19 | this.file = file; |
| 20 | this.macro = macro; |
| 21 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | public Sed(File file) { |
| 24 | assert file.isFile(); |
| 25 | this.file = file; |
| 26 | this.macro = null; |
| 27 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 28 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 29 | public void setOutput(File f) { |
| 30 | output = f; |
| 31 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | public void replace(String pattern, String replacement) { |
| 34 | replacements.put(Pattern.compile(pattern), replacement); |
| 35 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 36 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 37 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 52 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 53 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 67 | } finally { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | brdr.close(); |
| 69 | pw.close(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 70 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 71 | |
| 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 97 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 98 | public void setBackup(boolean b) { |
| 99 | this.backup = b; |
| 100 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 101 | } |