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 | |
| 7 | public class Sed { |
| 8 | final File file; |
| 9 | final Replacer macro; |
| 10 | File output; |
| 11 | boolean backup = true; |
| 12 | |
| 13 | final Map<Pattern, String> replacements = new LinkedHashMap<Pattern, String>(); |
| 14 | |
| 15 | public Sed(Replacer macro, File file) { |
| 16 | assert file.isFile(); |
| 17 | this.file = file; |
| 18 | this.macro = macro; |
| 19 | } |
| 20 | |
| 21 | public Sed(File file) { |
| 22 | assert file.isFile(); |
| 23 | this.file = file; |
| 24 | this.macro = null; |
| 25 | } |
| 26 | |
| 27 | public void setOutput(File f) { |
| 28 | output = f; |
| 29 | } |
| 30 | |
| 31 | public void replace(String pattern, String replacement) { |
| 32 | replacements.put(Pattern.compile(pattern), replacement); |
| 33 | } |
| 34 | |
| 35 | public int doIt() throws IOException { |
| 36 | int actions = 0; |
| 37 | BufferedReader brdr = new BufferedReader(new InputStreamReader( new FileInputStream(file),"UTF-8")); |
| 38 | File out; |
| 39 | if (output != null) |
| 40 | out = output; |
| 41 | else |
| 42 | out = new File(file.getAbsolutePath() + ".tmp"); |
| 43 | PrintWriter pw = new PrintWriter(new OutputStreamWriter( new FileOutputStream(out),"UTF-8")); |
| 44 | try { |
| 45 | String line; |
| 46 | while ((line = brdr.readLine()) != null) { |
| 47 | for (Pattern p : replacements.keySet()) { |
| 48 | String replace = replacements.get(p); |
| 49 | Matcher m = p.matcher(line); |
| 50 | |
| 51 | StringBuffer sb = new StringBuffer(); |
| 52 | while (m.find()) { |
| 53 | String tmp = setReferences(m, replace); |
| 54 | if ( macro != null) |
| 55 | tmp = Matcher.quoteReplacement(macro.process(tmp)); |
| 56 | m.appendReplacement(sb, tmp); |
| 57 | actions++; |
| 58 | } |
| 59 | m.appendTail(sb); |
| 60 | |
| 61 | line = sb.toString(); |
| 62 | } |
| 63 | pw.println(line); |
| 64 | } |
| 65 | pw.close(); |
| 66 | if (output == null) { |
| 67 | if ( backup ) { |
| 68 | File bak = new File(file.getAbsolutePath() + ".bak"); |
| 69 | file.renameTo(bak); |
| 70 | } |
| 71 | out.renameTo(file); |
| 72 | } |
| 73 | } finally { |
| 74 | brdr.close(); |
| 75 | pw.close(); |
| 76 | } |
| 77 | return actions; |
| 78 | } |
| 79 | |
| 80 | private String setReferences(Matcher m, String replace) { |
| 81 | StringBuilder sb = new StringBuilder(); |
| 82 | for (int i = 0; i < replace.length(); i++) { |
| 83 | char c = replace.charAt(i); |
| 84 | if (c == '$' && i < replace.length() - 1 |
| 85 | && Character.isDigit(replace.charAt(i + 1))) { |
| 86 | int n = replace.charAt(i + 1) - '0'; |
| 87 | if ( n <= m.groupCount() ) |
| 88 | sb.append(m.group(n)); |
| 89 | i++; |
| 90 | } else |
| 91 | sb.append(c); |
| 92 | } |
| 93 | return sb.toString(); |
| 94 | } |
| 95 | |
| 96 | public void setBackup(boolean b) { |
| 97 | this.backup=b; |
| 98 | } |
| 99 | } |