Latest bnd code
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1350613 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/libg/sed/Replacer.java b/bundleplugin/src/main/java/aQute/libg/sed/Replacer.java
index fa181f4..3c02aaf 100644
--- a/bundleplugin/src/main/java/aQute/libg/sed/Replacer.java
+++ b/bundleplugin/src/main/java/aQute/libg/sed/Replacer.java
@@ -1,5 +1,5 @@
package aQute.libg.sed;
public interface Replacer {
- String process(String line);
+ String process(String line);
}
diff --git a/bundleplugin/src/main/java/aQute/libg/sed/Sed.java b/bundleplugin/src/main/java/aQute/libg/sed/Sed.java
index 74f4756..4642c92 100644
--- a/bundleplugin/src/main/java/aQute/libg/sed/Sed.java
+++ b/bundleplugin/src/main/java/aQute/libg/sed/Sed.java
@@ -4,96 +4,98 @@
import java.util.*;
import java.util.regex.*;
+import aQute.lib.io.IO;
+
public class Sed {
- final File file;
- final Replacer macro;
- File output;
- boolean backup = true;
+ final File file;
+ final Replacer macro;
+ File output;
+ boolean backup = true;
- final Map<Pattern, String> replacements = new LinkedHashMap<Pattern, String>();
+ final Map<Pattern,String> replacements = new LinkedHashMap<Pattern,String>();
- public Sed(Replacer macro, File file) {
- assert file.isFile();
- this.file = file;
- this.macro = macro;
- }
-
- public Sed(File file) {
- assert file.isFile();
- this.file = file;
- this.macro = null;
- }
+ public Sed(Replacer macro, File file) {
+ assert file.isFile();
+ this.file = file;
+ this.macro = macro;
+ }
- public void setOutput(File f) {
- output = f;
- }
+ public Sed(File file) {
+ assert file.isFile();
+ this.file = file;
+ this.macro = null;
+ }
- public void replace(String pattern, String replacement) {
- replacements.put(Pattern.compile(pattern), replacement);
- }
+ public void setOutput(File f) {
+ output = f;
+ }
- public int doIt() throws IOException {
- int actions = 0;
- BufferedReader brdr = new BufferedReader(new InputStreamReader( new FileInputStream(file),"UTF-8"));
- File out;
- if (output != null)
- out = output;
- else
- out = new File(file.getAbsolutePath() + ".tmp");
- PrintWriter pw = new PrintWriter(new OutputStreamWriter( new FileOutputStream(out),"UTF-8"));
- try {
- String line;
- while ((line = brdr.readLine()) != null) {
- for (Pattern p : replacements.keySet()) {
- String replace = replacements.get(p);
- Matcher m = p.matcher(line);
+ public void replace(String pattern, String replacement) {
+ replacements.put(Pattern.compile(pattern), replacement);
+ }
- StringBuffer sb = new StringBuffer();
- while (m.find()) {
- String tmp = setReferences(m, replace);
- if ( macro != null)
- tmp = Matcher.quoteReplacement(macro.process(tmp));
- m.appendReplacement(sb, tmp);
- actions++;
- }
- m.appendTail(sb);
+ public int doIt() throws IOException {
+ int actions = 0;
+ BufferedReader brdr = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
+ File out;
+ if (output != null)
+ out = output;
+ else
+ out = new File(file.getAbsolutePath() + ".tmp");
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(out), "UTF-8"));
+ try {
+ String line;
+ while ((line = brdr.readLine()) != null) {
+ for (Pattern p : replacements.keySet()) {
+ String replace = replacements.get(p);
+ Matcher m = p.matcher(line);
- line = sb.toString();
- }
- pw.println(line);
- }
- pw.close();
- if (output == null) {
- if ( backup ) {
- File bak = new File(file.getAbsolutePath() + ".bak");
- file.renameTo(bak);
- }
- out.renameTo(file);
- }
+ StringBuffer sb = new StringBuffer();
+ while (m.find()) {
+ String tmp = setReferences(m, replace);
+ if (macro != null)
+ tmp = Matcher.quoteReplacement(macro.process(tmp));
+ m.appendReplacement(sb, tmp);
+ actions++;
+ }
+ m.appendTail(sb);
+
+ line = sb.toString();
+ }
+ pw.println(line);
+ }
} finally {
- brdr.close();
- pw.close();
+ brdr.close();
+ pw.close();
}
- return actions;
- }
+
+ if (output == null) {
+ if (backup) {
+ File bak = new File(file.getAbsolutePath() + ".bak");
+ IO.rename(file, bak);
+ }
+ IO.rename(out, file);
+ }
+
+ return actions;
+ }
+
+ private String setReferences(Matcher m, String replace) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < replace.length(); i++) {
+ char c = replace.charAt(i);
+ if (c == '$' && i < replace.length() - 1 && Character.isDigit(replace.charAt(i + 1))) {
+ int n = replace.charAt(i + 1) - '0';
+ if (n <= m.groupCount())
+ sb.append(m.group(n));
+ i++;
+ } else
+ sb.append(c);
+ }
+ return sb.toString();
+ }
- private String setReferences(Matcher m, String replace) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < replace.length(); i++) {
- char c = replace.charAt(i);
- if (c == '$' && i < replace.length() - 1
- && Character.isDigit(replace.charAt(i + 1))) {
- int n = replace.charAt(i + 1) - '0';
- if ( n <= m.groupCount() )
- sb.append(m.group(n));
- i++;
- } else
- sb.append(c);
- }
- return sb.toString();
- }
-
- public void setBackup(boolean b) {
- this.backup=b;
- }
+ public void setBackup(boolean b) {
+ this.backup = b;
+ }
}