import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Stream;
public class SimpleTagEditorSmarks1 {
// group 3 is , group 4 is
private final Pattern pat = Pattern.compile("(.+?)(()|(
)|(\\z))",
Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
private String replace(MatchResult mr) {
String suffix = mr.group(3) != null ? "{@code " //
: mr.group(4) != null ? "}" //
: ""; // EOF
return mr.group(1) + suffix;
}
private void editFile(Path in) {
System.out.println("processing " + in + "...");
Path out = Paths.get(in.toString() + ".replace");
try (Scanner sc = new Scanner(in, "UTF-8");
BufferedWriter bw = Files.newBufferedWriter(out, StandardCharsets.UTF_8);
PrintWriter pw = new PrintWriter(bw)) {
sc.findAll(pat)
.map(this::replace)
.forEachOrdered(pw::print);
} catch (IOException ioe) {
ioe.printStackTrace();
}
try {
Files.move(out, in, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private void run(String path) {
try (Stream paths = Files.walk(Paths.get(path))) {
paths.filter(p -> p.toString().endsWith(".java"))
.map(Path::toAbsolutePath)
.forEach(this::editFile);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("usage: java SimpleTagEditorSmarks1 ...");
} else {
(new SimpleTagEditorSmarks1()).run(args[0]);
}
}
}